uniCloud云函数——微信小程序登录凭证校验[code2Session](即:使用 code 换取 openid 和 session_key 等信息)解决方案
生活随笔
收集整理的這篇文章主要介紹了
uniCloud云函数——微信小程序登录凭证校验[code2Session](即:使用 code 换取 openid 和 session_key 等信息)解决方案
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
接口文檔
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
解決方案
'use strict'; exports.main = async (event, context) => {//event為客戶端上傳的參數(shù)console.log('event : ', event)let result = {}if("mp-weixin"==context.PLATFORM){result = (await uniCloud.httpclient.request("https://api.weixin.qq.com/sns/jscode2session",{method: "GET",data: {'appid': "xxx",'secret': "xxx",'js_code': event.code,'grant_type': "authorization_code"},dataType: 'json' // 指定返回值為json格式,自動進行parse})).datareturn {code:0,data: result}}//返回數(shù)據(jù)給客戶端return {code:-1,data: result} };調(diào)用示例
uniCloud.callFunction({name: 'code2Session',data: {code} }).then((res) => {if (res.result.code === 0) {let openid = res.result.data.openid;uni.setStorageSync('openid', openid)} }).catch((e) => {console.error(e) })參考文章
總結(jié)
以上是生活随笔為你收集整理的uniCloud云函数——微信小程序登录凭证校验[code2Session](即:使用 code 换取 openid 和 session_key 等信息)解决方案的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: uni-app——Vue3简单整合uVi
- 下一篇: 《虚拟化和云计算》实验报告——MININ