全新微信小程序源码|小程序游戏源码|小游戏源码2000套

  Authing小程序SDK(authing-wxapp-sdk)适合在微信小程序环境下使用,基于authing-js-sdk (打开新窗口),对微信小程序环境进行了适配。您可以使用AuthenticationClient中的所有方法authing-js-sdk  (打开新窗口),如获取和修改用户信息、添加用户自定义字段等。同时专门用于小程序环境通过微信授权获取用户手机号、微信授权登录、登录带有微信授权的手机号码等。
  源码:y.wxlbyx.icu
  ●认证中配置小程序登录
  ●安装
  ○安装 npm 包
  ○小程序开发者工具中构建npm
  ●初始化
  ●如何使用
  ●API参考
  ○通过代码登录
  ○通过电话登录
  ○获取电话
  ○更新头像
  ●最佳实践
  ●错误处理
  ●得到帮助
  认证中配置小程序登录
  为了在小程序中使用Authing小程序SDK,需要在微信开放平台申请小程序 (打开新窗口)同时在【Authing Console】(https://console.authing.cn/console/userpool)填写小程序的配置。
  配置小程序登录
  #安装
  从小程序基础库2.2.1及以上版本、开发者工具1.02.1808300及以上版本开始,小程序支持使用npm安装第三方包。详情请参见:【npm支持| 微信开放文档]
  安装 npm 包
  使用 npm:
  npm install authing-wxapp-sdk
  使用纱线:
  yarn add authing-wxapp-sdk
  小程序开发者工具中构建npm
  点击开发者工具中的菜单栏:Tools --> Build npm:


  检查使用 npm 模块选项:


  初始化
  AuthenticationClient需要传入用户池ID( userPoolId):
  您可以了解如何获取UserPoolId (打开新窗口)这里。如果您对用户池的概念不熟悉,可以点击这里了解Authing系统的核心概念 (打开新窗口)。
  const {AuthenticationClient} = require("authing-wxapp-sdk")
  const authing = new AuthenticationClient({
  userPoolId: "YOUR_USERPOOL_ID",
  })
  完整参数列表如下:
  ●userPoolId:用户池ID。
  ●accessToken:使用用户的token初始化SDK。
  ●timeout:请求超时时间,单位毫秒,默认10000(10秒)。
  ●onError:错误处理功能,您可以使用它来全局捕获Authing客户端请求的所有异常。该函数定义为:
  (code: number, message: string, data: any) => void
  ●host:认证服务器地址。如果您使用的是公有云版本,请忽略该参数。如果您使用的是私有化部署版本,则该参数为必填项。
  指示
  用户登录后,SDK会将用户的信息token写入微信的Storage中,后续请求会自动携带该信息进行token访问。
  const {code} = await wx.login()
  // No user authorization required
  const user = await authing.loginByCode(code); // Successful login, write token to WeChat Storage
  // You can do this after logging in
  await authing.updateProfile(
  nickname:'Bob'
  )
  后续用户再次打开小程序,如果小程序的Storage中保存有用户的token,则访问authing的请求会自动携带该token。
  // The request can be successful because the user is logged in.
  await authing.updateProfile(
  nickname:'Mick'
  )
  API参考
  您可以使用AuthenticationClient中的所有方法authing-js-sdk  (打开新窗口),调用方法和authing- js-sdk是完全一样的。
  #通过代码登录
  使用微信授权方式登录。
  - 如果用户首次登录小程序,且未使用与小程序绑定的微信应用以相同主体登录,则会创建一个新账户。- 如果用户首次登录小程序,但用户已使用同主题小程序绑定的微信应用登录,则会返回对应的微信账号。
  #参数
  ●code:调用wx.login()code得到的调用 (打开新窗口),无需用户授权。必需的。
  ●options: 选修的。
  ●options.iv:微信按钮组件 (打开新窗口)与iv iv加密数据iv加密数据open-type` 。首次使用需要用户手动授权。选修的。getUserInfo.andmust be passed at the same time, Authing Server will try to encrypt user data fromand
  ●options.encryptedData:微信按钮组件 (打开新窗口)作为加密数据iv加密数据iv加密数据open-type` 。首次使用需要用户手动授权。选修的。getUserInfo.andmust be passed at the same time, Authing Server will try to encrypt user data fromand
  ●options.rawData:微信按钮组件 (打开新窗口)随着rawData iv cryptoData rawData` 被传递,Authing Server 将直接使用该数据作为用户的配置文件open-type。首次使用需要用户手动授权。选修的。getUserInfo. Choose either+. If
  #例子
  1、静默授权
  首次注册的用户个人资料中的昵称和头像将为空,因为尚未获取用户的头像和昵称。
  const {code} = await wx.login()
  const data = await authing.loginByCode(code)
  用户手动授权获取昵称头像
  仅第一次需要授权。授权后,用户可以使用wx.getUserInfo直接获取头像昵称。
  -首次请求用户手册授权
  <button open-type="getUserInfo" bindgetuserinfo="getUserInfo"> Get avatar nickname </button>
  getUserInfo: async function (e) {
  const {code} = await wx.login()
  const {rawData} = e.detail
  const user = await authing.loginByCode(code, {rawData })
  // Or pass iv encryptedData
  // const {iv, encryptedData} = e.detail
  // const user = await authing.loginByCode(code, {iv, encryptedData })
  console.log(user)
  }
  -以后可以通过自动获取wx.getUserInfo
  const {rawData} = await wx.getUserInfo()
  const user = await authing.loginByCode(code, {rawData })
  // Or pass iv encryptedData
  // const {iv, encryptedData} = e.detail
  // const user = await authing.loginByCode(code, {iv, encryptedData })
  通过电话登录
  通过微信手机号授权登录。每次调用都需要用户手动授权。
  - 手机号码首次注册,会绑定微信账号(不存在则创建)。-如果该电话号码之前已注册过,则返回该电话号码对应的账号,并将该电话号码与当前微信账号绑定。
  #参数
  ●code:调用wx.login()code得到的调用 (打开新窗口),无需用户授权。必需的。
  ●iv:微信按钮组件 (打开新窗口)截至点击事件返回open-type。必需的。getPhoneNumberiv
  ●encryptedData:微信按钮组件 (打开新窗口)与open-type作为getPhoneNumber. encryptedData由点击事件返回。必需的。
  例子
  <button open-type="getPhoneNumber" bindgetphonenumber="getPhone"> Get phone number </button>
  getPhone: async function(e) {
  const {code} = await wx.login()
  const {iv, encryptedData} = e.detail
  const data = await authing.loginByPhone(code, iv, encryptedData)
  console.log(data)
  }
  获取电话
  获取当前用户的手机号码(该手机号码不会用于注册或绑定账户)
  参数
  ●code:调用wx.login()code得到的调用 (打开新窗口),无需用户授权。必需的。
  ●iv:微信按钮组件 (打开新窗口)截至点击事件返回open-type。必需的。getPhoneNumberiv
  ●encryptedData:微信按钮组件 (打开新窗口)与open-type作为getPhoneNumber. encryptedData由点击事件返回。必需的。
  例子
  <button open-type="getPhoneNumber" bindgetphonenumber="getPhone"> Get phone number </button>
  getPhone: async function(e) {
  const {code} = await wx.login()
  const {iv, encryptedData} = e.detail
  const data = await authing.getPhone(code, iv, encryptedData)
  console.log(data)
  }
  返回数据示例:
  {
  "countryCode": "86",
  "phoneNumber": "176xxxx6754",
  "purePhoneNumber": "176xxxx6754",
  "openid": "o1p9H4wAgb9uTqpxG5Z1g0pIr3FE",
  "unionid": "o0pqE6Fbr5M-exSu_PeL_sjwN44U"
  }
  #更新头像
  更新用户头像,该方法会自动调用wx.chooseImage获取图片并上传到Authing的cdn。只需要一行代码即可调用。
  例子
  const {photo} = await authing.updateAvatar()
  console.log(photo)
  最佳实践


  建议用户loginByCode在首次使用小程序时,获取小程序账号对应的Authing账号。如果账号之前绑定过手机号码,则无需再次请求用户对该手机号码进行授权。如果账号未绑定手机号码,则调用该loginByPhone方法请求用户对手机号码进行授权。
  用户登录后,authing-wxapp-sdk会将其写入token到Storage小程序中,可以调用authing.checkLoginStatus()判断用户的token是否有效,当token无效时重新启动登录。
  错误处理
  您可以使用try catch错误处理:
  try {
  const user = await authing.loginByEmail('[email protected]','passw0rd')
  } catch (error) {
  console.log(error.code); // 2004
  console.log(error.message); // user does not exist
  }
  完整的错误代码请参见此文档 (打开新窗口)。
  还可以指定onError统一捕获所有Authing请求异常,比如使用微信组件等wx.showModal显示错误提示。
  const authing = new AuthenticationClient({
  userPoolId,
  onError: (code, message) => {
  wx.showModal({
  content: message,
  showCancel: false
  })
  }
  })

猜你喜欢

转载自blog.csdn.net/m0_75279972/article/details/133136912