npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

auth-mini-sdk

v2.0.5

Published

统一认证登录sdk

Readme

统一认证第三方小程序接入文档

安装 auth-mini-sdk 包。

接入 @Galileo/auth-mini-sdk gitlab 私有库包需要依赖 node,请确保已具备 node 环境(建议>=12.0.0)。

接着查看您的项目是否具有 package.json 文件,如果有则按照如下方式安装公司 gitlab 私有库统一认证子包 @Galileo/auth-mini-sdk

安装方式

安装 gitlab 私有库统一认证包(执行以下命令请尽量使用 Git Bash 命令工具)

第一步:
npm config set -- '//git.yottacloud.cn/api/v4/projects/1153/packages/npm/:_authToken' "key"
key: 请找项目管理员获取
第二步:
echo @Galileo:registry=https://git.yottacloud.cn/api/v4/projects/1153/packages/npm/ >> .npmrc
第三步:
npm i @Galileo/auth-mini-sdk

使用方式

首先需要将 auth-mini-sdk 做为分包添加进您的小程序。为此我们提供了打包文件,您需要按照以下步骤执行导入 auth-mini-sdk 包。

第一步,添加一个脚本命令指向我们提供的打包方法,代码如下,脚本名称可以根据项目情况进行调整。

"scripts": {
  "build": "node node_modules/@Galileo/auth-mini-sdk/build.js"
}

接下来按照如下命令执行以后,auth-mini-sdk 将添加到您的小程序根目录,app.json 也将同步添加此分包配置,这样您的项目中就拥有 auth-mini-sdk 这个分包了。

# 使用 yarn 运行
yarn build

# 使用 npm 运行
npm run build

完成上述步骤后,需要在开发者工具中构建 npm(开发者工具左上角-工具-构建 npm)。

支持的功能和方法

  • goLogin 方法用于跳转到统一认证登录页
  • getToken 方法用于同步获取 token
  • loginOut 用于退出登录
  • goPersonalCenter 跳转个人中心
  • fastLogin 用于无感快速登录(一般不需要使用此方法)

代码示例

1. 初始化 SDK => app.ts/app.js

// 接下来必须在您的主包app.js或者app.ts中全局引入并【执行】下面代码,以达到初始化统一认证分包内部 【测试/生产】 环境进行调试开发
// baseUrl: 后续统一认证后端服务会统一私有化部署到各个接入方系统,所以该域名会根据接入方发生变化。

import * as authMiniSdk from "auth-mini-sdk";
// 联调初期默认可使用统一认证域名环境
authMiniSdk.init({
  baseUrl: "http://authory.tke-test.yottacloud.cn", // 认证系统的测试/生产域名。
  authOptions: {
    appid: appid, // 统一认证后台应用 appid
    redirect_url: redirect_uri, // 统一认证后台应用配置的回调 url
    state: "", // 自定义参数
    response_type: response_type, // response_type: 可以是 "id_token token" 或 "code"
    /**
     * 当response_type为'code'时,业务方需要实现一个异步函数来将code换成业务token,并将其赋值给code2tokenCallback变量。
     * 在这个回调函数中,需要接受两个参数:第一个参数是统一认证提供的code,第二个参数是一个回调函数。
     * 回调函数的格式如下:
     *
     * @callback requestTokenCallback
     * @param {Error|null} err - 错误对象或null
     * @param {Object} tokenObject - 包含access_token和expires_in的对象
     * @param {string} tokenObject.access_token - 用户的access_token
     * @param {number} tokenObject.expires_in - 该token还有多少[秒]过期
     *
     * @function code2tokenCallback
     * @param {string} code - 统一认证提供的code
     * @param {requestTokenCallback} callback - 回调函数
     */
    code2tokenCallback: function (code, callback) {
      setTimeout(() => {
        // 这是一个模拟业务方拿到授权code换取业务方token的过程
        // mock token
        let token = code + "sdsdsdsdsd";
        callback(null, {
          access_token: "bar " + token,
          expires_in: 24 * 3600, // 该token一天后过期
        });
      }, 1000);
    },
    scope: ["openid", "profile", "email", "phone", "oauth2_info"],
  },
  loginCallbackAsyncFunction: () => {
    // 非必填
    // 登陆后处理异步函数,从登录页回跳到业务页面之前回调用此函数,调用成功之后才会回调
  },
});

2. 具体的功能调用 => page.js

import * as authMiniSdk from "auth-mini-sdk";

/**
 * @typedef {Object} ResObject
 * @property {string} access_token - 用户隐式流访问令牌
 * @property {boolean} is_expire - 是否过期
 * @property {number} expire_time - 毫秒
 * @property {string} id_token - id_token
 */

/**
 * 跳转到登录页面。
 * @returns {Promise<ResObject>} 一个解析为包含用户访问令牌和其他信息的对象的Promise。
 */
authMiniSdk.goLogin().then((res) => {
  console.log(res);
});

/**
 * 获取用户的令牌。
 * @returns {ResObject} 包含用户访问令牌和其他信息的对象。
 */
const res = authMiniSdk.getToken();

/**
 * 注销用户。
 */
authMiniSdk.loginOut();

/**
 * 跳转到个人中心页面。
 */
authMiniSdk.goPersonalCenter();

// 当用户有缓存但令牌已过期
// 表明用户已绑定手机,曾经登录过且未退出登录
// 在业务层面,可以尝试快速登录
if (authMiniSdk.getToken().access_token && authMiniSdk.getToken().is_expire) {
  /**
   * 尝试快速登录。
   * @returns {Promise<ResObject>} 一个解析为包含用户访问令牌和其他信息的对象的Promise。
   */
  authMiniSdk
    .fastLogin()
    .then((res) => {
      console.log(res);
      wx.showToast({
        title: "无感登陆成功",
      });
      this.setData({
        resdata: res,
      });
    })
    .catch((e) => {
      wx.showToast({
        icon: "error",
        title: e,
      });
    });
}
  • 如果是主包分包的架构,推荐将上述功能挂载到小程序主包 getApp 上

注意事项

  • 关于response_type请与业务方沟通好,是使用code还是隐式流
  • 在小程序中使用 npm 依赖需要构建 npm(开发者工具左上角-工具-构建 npm)。
  • 请确保没有与统一认证分包名称冲突的其他分包。
  • ts/js 模板小程序请确保本地 project.private.config.json 或者 project.config.json 中有正确的 miniprogramRoot 值,否者打包程序会在根目录下构建小程序 app.json 中的统一认证分包目录。