5 分钟即可接入,微信小程序 SDK 全面升级

Authing 小程序 SDK 5.0 (opens new window)于 2022 年 9 月 7 日发布,五分钟即可通过微信接入小程序。相较于原版 SDK,Authing 小程序 SDK 5.0 首要有以下几个优势:

  • 用法更简略,NPM 包名更明晰
  • 支撑的接口更多、结构更多,掩盖更多开发者需求
  • 支撑的类型提示更多,操作更快捷
  • 运转更流畅

据微信 2021 年财报披露,微信小程序 DAU 已经突破 5 亿,微信生态从业者超过 3000 万。阿拉丁研究院基于小程序数据库和揭露材料测算,2022 年 H1 期间,微信、支付宝、抖音、快手等多个渠道小程序数量累计超过 750 万,日活超过 7.8 亿。

5 分钟即可接入,微信小程序 SDK 全面升级

那么,在这庞大用户量的背面,是微信本身的强交际属性为小程序的点击与共享带来了天然优势。

就小程序本身而言,首要对企业有以下三个优势:第一,即需即用,布置方便。关于企业来说,开发一个 APP 需求数月时刻研制、测验、上线,而小程序的上线时刻最快只需求几天,节省了 80% 研制周期与本钱。

第二,用户体验好,登录即用。企业在卖出产品时,展示页的登录速度决定了用户流失率。而运用小程序,无需下载、装置 APP,也无需占用用户手机内存,在微信里即可一键授权登录,登录后即可实现产品浏览、下单、支付,方便快捷。

第三,天然流量优势,获客本钱低。依靠着微信 12 亿多的活跃用户,为小程序带来天然流量(据腾讯发布的 2021 年财报,微信月活跃账户数 12.68 亿)。此外,微信为小程序供给了约 50 个进口,包含首要进口、小程序本身进口、搜索栏进口、公众号进口、小程序码进口、微信场景进口和其他进口,这些进口促进了小程序交际变现,用户点击即可跳转。

小程序这一切优势的背面,是一个看似“小玩意”的功用在支撑——微信授权小程序登录。举例来说,假设你想用微信直接登录小程序,小程序向微信发出登录恳求,系统会跳转到微信登录页面,点击授权并登录,用户就能够登录了。

“微信授权小程序登录”看似——点击、授权,即可登录,但这背面是杂乱的开发流程。

5 分钟即可接入,微信小程序 SDK 全面升级

微信官方开发时序图

Authing 小程序 SDK 5.0 (opens new window)于 2022 年 9 月 7 日发布,五分钟即可通过微信接入小程序,如果您正在运用之前的版本 authing-wxapp-sdk (opens new window),可参阅:

docs.authing.cn/v2/referenc…

相较于原版 SDK,Authing 小程序 SDK 5.0 首要有以下几个优势:

  • 用法更简略,NPM 包名更明晰:用法更简略,按需导入 NPM 包
  • 支撑的接口更多、结构更多,掩盖更多开发者需求: a. 支撑的接口更多:集成并增强 Authing 最新 V3 版认证 API,掩盖核心认证、授权类功用 b. 支撑的小程序结构更多:支撑微信原生小程序、Taro 和 uniapp 结构
  • 支撑的类型提示更多,操作更快捷:完整的 TS 类型提示
  • 运转更流畅:包体积更小,不到 5 分钟即可完成接入

晋级版 SDK 为开发者供给了更安稳、更快捷的开发环境,建议您赶快晋级。

功用对比:

5 分钟即可接入,微信小程序 SDK 全面升级

微信小程序 SDK 集成流程

STEP 1:创立运用

  1. 运用 Authing 创立一个运用:
  • 进入控制台
  • 打开左边运用菜单,点击自建运用菜单
  • 点击右上角创立自建运用按钮
  • 填写运用称号和认证地址、挑选标准 Web 运用
  • 点击创立

5 分钟即可接入,微信小程序 SDK 全面升级

  1. 以下身份验证方法挑选 none

5 分钟即可接入,微信小程序 SDK 全面升级

  1. 保存当时装备

STEP 2:创立社会化身份源

在微信公众渠道后台的开发 -> 开发办理 -> 开发设置页面获取小程序 ID 和小程序密钥。

在 Authing 控制台身份源办理 -> 社会化身份源 -> 创立社会化身份源 -> 微信 -> 小程序创立一个微信社会化身份源,并填写以下信息:

  • 仅有标识:这是此衔接的仅有标识,设置之后不能修正。
  • 小程序称号
  • 小程序 ID
  • 小程序密钥
  • 挑选运用此身份源的运用
  • 点击保存

STEP 3:装置 SDK

微信原生小程序


npminstall--save@authing/miniapp-wx

Taro


npminstall--save@authing/miniapp-taro

uniapp


npminstall--save@authing/miniapp-uniapp

STEP 4:初始化 SDK

微信原生小程序

import{Authing}from'@authing/miniapp-wx'
//以下两种暗码加密方法能够按需运用,选其一即可
//rsa加密
import{encryptFunction}from'@authing/miniapp-jsencrypt'
//sm2加密
import{encryptFunction}from'@authing/miniapp-sm2encrypt'
constauthing=newAuthing({
appId:'630b549efa97ba795338e2cd',
host:'http://localhost:3000',
userPoolId:'630b549d5a697473a2d7fa20',
//非必传,暗码默认将以明文传输
encryptFunction
})

Taro


import{Authing}from'@authing/miniapp-taro'
//以下两种暗码加密方法能够按需运用,选其一即可
//rsa加密
import{encryptFunction}from'@authing/miniapp-jsencrypt'
//sm2加密
import{encryptFunction}from'@authing/miniapp-sm2encrypt'
constauthing=newAuthing({
appId:'630b549efa97ba795338e2cd',
host:'http://localhost:3000',
userPoolId:'630b549d5a697473a2d7fa20',
//非必传,暗码默认将以明文传输
encryptFunction
})

uniapp

import{Authing}from'@authing/miniapp-uniapp'
//以下两种暗码加密方法能够按需运用,选其一即可
//rsa加密
import{encryptFunction}from'@authing/miniapp-jsencrypt'
//sm2加密
import{encryptFunction}from'@authing/miniapp-sm2encrypt'
constauthing=newAuthing({
appId:'630b549efa97ba795338e2cd',
host:'http://localhost:3000',
userPoolId:'630b549d5a697473a2d7fa20',
//非必传,暗码默认将以明文传输
encryptFunction
})

STEP 5:运用 SDK

微信授权 code 登录

authing.loginByCode

5 分钟即可接入,微信小程序 SDK 全面升级

WechatMiniProgramCodePayload

5 分钟即可接入,微信小程序 SDK 全面升级

出参

参阅:LoginState

示例代码

微信原生小程序


//index.js
Page({
asyncloginByCode(){
const{encryptedData,iv}=awaitwx.getUserProfile({
desc:'getUserProfile'
})
constres=awaitauthing.loginByCode({
connection:'wechat_mini_program_code',
extIdpConnidentifier:'authing-zhaoyiming-miniprogram',
wechatMiniProgramCodePayload:{
encryptedData,
iv
},
options:{
scope:'openidprofileoffline_access'
}
})
console.log('authing.loginByCoderes:',res)
}
})

Taro


exportdefaultclassIndexextendsComponent{
render(){
return(
this.loginByCode()}>loginByCode
)
}
asyncloginByCode(){
const{encryptedData,iv}=awaitTaro.getUserProfile({
desc:'getUserProfile'
})
constres=awaitauthing.loginByCode({
connection:'wechat_mini_program_code',
extIdpConnidentifier:'authing-zhaoyiming-miniprogram',
wechatMiniProgramCodePayload:{
encryptedData,
iv
},
options:{
scope:'openidprofileoffline_access'
}
})
console.log('authing.loginByCoderes:',res)
}
}

uniapp


exportdefault{
methods:{
asyncloginByCode(){
const[,{encryptedData,iv}]=awaituni.getUserProfile({
desc:'getUserProfile'
})
constres=awaitauthing.loginByCode({
connection:'wechat_mini_program_code',
extIdpConnidentifier:'authing-zhaoyiming-miniprogram',
wechatMiniProgramCodePayload:{
encryptedData,
iv
},
options:{
scope:'openidprofileoffline_access'
}
})
console.log('authing.loginByCoderes:',res)
}
}
}

账号暗码登录

authing.loginByPassword

入参

5 分钟即可接入,微信小程序 SDK 全面升级

PasswordPayload

5 分钟即可接入,微信小程序 SDK 全面升级

出参

参阅:LoginState

示例代码

微信原生小程序


//index.js
Page({
asyncloginByPassword(){
constres=awaitauthing.loginByPassword({
connection:'PASSWORD',
passwordPayload:{
password:'123',
username:'test'
},
options:{
passwordEncryptType:'sm2',
scope:'offline_accessopenidprofile'
}
})
console.log('authing.loginByPasswordres:',res)
}
})

Taro


exportdefaultclassIndexextendsComponent{
render(){
return(
this.loginByPassword()}>loginByPassword
)
}
asyncloginByPassword(){
constres=awaitauthing.loginByPassword({
connection:'PASSWORD',
passwordPayload:{
password:'123',
username:'test'
},
options:{
passwordEncryptType:'sm2',
scope:'offline_accessopenidprofile'
}
})
console.log('authing.loginByPasswordres:',res)
}
}

uniapp


exportdefault{
methods:{
asyncloginByPassword(){
constres=awaitauthing.loginByPassword({
connection:'PASSWORD',
passwordPayload:{
password:'123',
username:'test'
},
options:{
passwordEncryptType:'rsa',
scope:'offline_accessopenidprofile'
}
})
console.log('authing.loginByPasswordres:',res)
}
}
}

发送短信验证码

authing.sendSms

入参

5 分钟即可接入,微信小程序 SDK 全面升级

出参

5 分钟即可接入,微信小程序 SDK 全面升级

示例代码

微信原生小程序


//index.js
Page({
asyncsendSms(){
//指定channel为CHANNEL_LOGIN,发送登录所用的验证码
constres=awaitauthing.sendSms({
phoneNumber:'13100000000',
phoneCountryCode:'+86',
channel:'CHANNEL_LOGIN'
})
console.log('authing.sendSmsres:',res)
}
})

Taro


exportdefaultclassIndexextendsComponent{
render(){
return(
this.sendSms()}>sendSms
)
}
asyncsendSms(){
//指定channel为CHANNEL_LOGIN,发送登录所用的验证码
constres=awaitauthing.sendSms({
phoneNumber:'13100000000',
phoneCountryCode:'+86',
channel:'CHANNEL_LOGIN'
})
console.log('authing.sendSmsres:',res)
}
}

uniapp


exportdefault{
methods:{
asyncsendSms(){
//指定channel为CHANNEL_LOGIN,发送登录所用的验证码
constres=awaitauthing.sendSms({
phoneNumber:'13100000000',
phoneCountryCode:'+86',
channel:'CHANNEL_LOGIN'
})
console.log('authing.sendSmsres:',res)
},
}
}

验证码登录

authing.loginByPassCode

入参

5 分钟即可接入,微信小程序 SDK 全面升级

PassCodePayload

5 分钟即可接入,微信小程序 SDK 全面升级

出参

参阅:LoginState

示例代码

微信原生小程序


//index.js
Page({
asyncloginByPassCode(){
constres=awaitauthing.loginByPassCode({
connection:'PASSCODE',
passCodePayload:{
//手机收到的短信验证码
passCode:'5671',
phone:'13100000000',
phoneCountryCode:'+86'
},
options:{
scope:'openidprofileoffline_access'
}
})
console.log('authing.loginByPassCode:',res)
}
})

Taro


exportdefaultclassIndexextendsComponent{
render(){
return(
this.loginByPassCode()}>loginByPassCode
)
}
asyncloginByPassCode(){
constres=awaitauthing.loginByPassCode({
connection:'PASSCODE',
passCodePayload:{
//手机收到的短信验证码
passCode:'9973',
phone:'13100000000',
phoneCountryCode:'+86'
}
})
console.log('authing.loginByPassCode:',res)
}
}

uniapp

exportdefault{
methods:{
asyncloginByPassCode(){
constres=awaitauthing.loginByPassCode({
connection:'PASSCODE',
passCodePayload:{
//手机收到的短信验证码
passCode:'9973',
phone:'13100000000',
phoneCountryCode:'+86'
}
})
console.log('authing.loginByPassCode:',res)
}
}
}

改写 Token

authing.refreshToken

说明

改写 Token 需求用到登录接口回来的 refresh_token 字段,运用登录相关方法时需传入参数 scope,并包含 offline_access,具体参阅:WxLoginOptions 和 NormalLoginOptions

入参

出参

参阅:LoginState

示例代码

微信原生小程序


//index.js
Page({
asyncrefreshToken(){
constres=awaitauthing.refreshToken()
console.log('authing.refreshTokenres:',res)
}
})

Taro


exportdefaultclassIndexextendsComponent{
render(){
return(
this.refreshToken()}>refreshToken
)
}
asyncrefreshToken(){
constres=awaitauthing.refreshToken()
console.log('authing.refreshTokenres:',res)
}
}

uniapp


exportdefault{
methods:{
asyncrefreshToken(){
constres=awaitauthing.refreshToken()
console.log('authing.refreshTokenres:',res)
}
}
}

获取用户手机号

authing.getPhone

入参

5 分钟即可接入,微信小程序 SDK 全面升级

出参

5 分钟即可接入,微信小程序 SDK 全面升级

Watermark

5 分钟即可接入,微信小程序 SDK 全面升级

示例代码

微信原生小程序

//index.js
Page({
/**
*需求在真机上测验,微信开发者工具不会回来code
*@param{*}e
*/
asyncgetPhone(e){
const{code}=e.detail
constres=awaitauthing.getPhone({
extIdpConnidentifier:'authing-zhaoyiming-miniprogram',
code
})
console.log('authing.getPhoneres:',res)
}
})

Taro

exportdefaultclassIndexextendsComponent{
render(){
return(
this.getPhone(e)}>getPhone
)
}
/**
*需求在真机上测验,微信开发者工具不会回来code
*@param{*}e
*/
asyncgetPhone(e){
const{code}=e.detail
constres=awaitauthing.getPhone({
extIdpConnidentifier:'authing-zhaoyiming-miniprogram',
code
})
console.log('authing.getPhoneres:',res)
}
}

uniapp

exportdefault{
methods:{
/**
*需求在真机上测验,微信开发者工具不会回来code
*@param{*}e
*/
asyncgetPhone(e){
const{code}=e.detail
constres=awaitauthing.getPhone({
extIdpConnidentifier:'authing-zhaoyiming-miniprogram',
code
})
console.log('authing.getPhoneres:',res)
}
}
}

修正暗码

authing.updatePassword

入参

5 分钟即可接入,微信小程序 SDK 全面升级

出参

Promise<boolean>

示例代码

微信原生小程序


//index.js
Page({
asyncupdatePassword(){
constres=awaitauthing.updatePassword({
newPassword:'123',
oldPassword:'123',
passwordEncryptType:'none'
})
console.log('authing.updatePasswordres:',res)
},
})

Taro

exportdefaultclassIndexextendsComponent{
render(){
return(
this.updatePassword()}>updatePassword
)
}
asyncupdatePassword(){
constres=awaitauthing.updatePassword({
newPassword:'123',
oldPassword:'123',
passwordEncryptType:'none'
})
console.log('authing.updatePasswordres:',res)
}
}

uniapp

exportdefault{
methods:{
asyncupdatePassword(){
constres=awaitauthing.updatePassword({
newPassword:'123',
oldPassword:'123',
passwordEncryptType:'none'
})
console.log('authing.updatePasswordres:',res)
},
}
}

获取用户信息

authing.getUserInfo

入参

出参

参阅:LoginState

示例代码

微信原生小程序

//index.js
Page({
asyncgetUserInfo(){
constres=awaitauthing.getUserInfo()
console.log('authing.getUserInfores:',res)
}
})

Taro

exportdefaultclassIndexextendsComponent{
render(){
return(
this.getUserInfo()}>getUserInfo
)
}
asyncgetUserInfo(){
constres=awaitauthing.getUserInfo()
console.log('authing.getUserInfores:',res)
}
}

uniapp

exportdefault{
methods:{
asyncgetUserInfo(){
constres=awaitauthing.getUserInfo()
console.log('authing.getUserInfores:',res)
}
}
}

修正头像

authing.updateAvatar

入参

出参

5 分钟即可接入,微信小程序 SDK 全面升级

Data

5 分钟即可接入,微信小程序 SDK 全面升级

示例代码

微信原生小程序

//index.js
Page({
asyncupdateAvatar(){
constres=awaitauthing.updateAvatar()
console.log('authing.updateAvatarres:',res)
},
})

Taro

exportdefaultclassIndexextendsComponent{
render(){
return(
this.updateAvatar()}>updateAvatar
)
}
asyncupdateAvatar(){
constres=awaitauthing.updateAvatar()
console.log('authing.updateAvatarres:',res)
}
}

uniapp

exportdefault{
methods:{
asyncupdateAvatar(){
constres=awaitauthing.updateAvatar()
console.log('authing.updateAvatarres:',res)
}
}
}

修正用户信息

authing.updateUserInfo

入参

参阅:UserInfo

出参

参阅:UserInfo

示例代码

微信原生小程序

//index.js
Page({
asyncupdateUserInfo(){
constres=awaitauthing.updateUserInfo({
address:'Helloworld'
})
console.log('authing.updateUserInfores:',res)
}
})

Taro


exportdefaultclassIndexextendsComponent{
render(){
return(
this.updateUserInfo()}>updateUserInfo
)
}
asyncupdateUserInfo(){
constres=awaitauthing.updateUserInfo({
address:'Helloworld'
})
console.log('authing.updateUserInfores:',res)
}
}

uniapp

exportdefault{
methods:{
asyncupdateUserInfo(){
constres=awaitauthing.updateUserInfo({
address:'Helloworld'
})
console.log('authing.updateUserInfores:',res)
}
}
}

退出登录

authing.logout

入参

出参

Promise<boolean>

示例代码

微信原生小程序

//index.js
Page({
asynclogout(){
constres=awaitauthing.logout()
console.log('authing.logoutres:',res)
}
})

Taro

exportdefaultclassIndexextendsComponent{
render(){
return(
this.logout()}>logout
)
}
asynclogout(){
constres=awaitauthing.logout()
console.log('authing.logoutres:',res)
}
}

uniapp

exportdefault{
methods:{
asynclogout(){
constres=awaitauthing.logout()
console.log('authing.logoutres:',res)
}
}
}

微信网页端 SDK 集成流程请您参阅:

docs.authing.cn/v2/referenc…

未来规划

Authing 产品负责人佟野表明:我们会一向秉承着开发者友好的初心,继续迭代微信小程序 SDK,将根据用户需求拓展更多功用,比如 MFA 登录相关,并将跨渠道支撑其他主流小程序渠道及结构,继续优化包体积,协助企业及开发者更快捷地登录小程序与网页,继续为业务赋能。

关于 Authing

Authing 身份云是国内仅有以开发者为中心的全场景 IDaaS 服务商,以身份及服务的云核算视角,基于多租户云原生架构,集成了所有主流身份认证协议,遵从不同国家和职业的合规性要求,在所有 SaaS 软件和数亿用户中树立高安全、高性能、高生产力的统一身份认证渠道,支撑所有企业和开发者快捷灵活接入,满意各类场景化需求。

Authing 共有 1000+ API,支撑5000 TPS开发量级,供给主流语言 SDK,包含 JavaScript,Java,C#,Go ,Python,PHP,Ruby 等,掩盖 Web 端及 Android 和 iOS 移动端。具有数十万开发者的社区生态,合计为 4000 万终端用户供给身份云服务,每月有超过 5000 万次的 API 调用次数。

目前,Authing 身份云已协助 20,000+ 家企业和开发者构建标准化的用户身份系统,感谢可口可乐、元气森林、招商银行、中国石油、三星集团、CSDN 等客户挑选并施行 Authing 解决方案。

点击此处了解更多职业身份办理

「解决方案」以及「最佳实践事例」