You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
canteen/uniCloud-aliyun/cloudfunctions/wxlogin/index.js

28 lines
792 B

'use strict';
const { error } = require('console');
const rp = require('request-promise');
exports.main = async (event, context) => {
//event为客户端上传的参数
let APPID = 'wxcfebd67cf993c6f3'
let SECRET = '26d79c22855a2fafff076793ca2427a5'
let JSCODE = event.code
let url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + APPID + '&secret=' + SECRET +
'&js_code=' + JSCODE + '&grant_type=authorization_code'
const res = await JSON.parse(await rp(url))
let db = uniCloud.database()
let token = res.session_key + '?' + res.openid
let temp = await db.collection('user-id').add({
_id: res.openid,
openid: res.openid,
session_key: res.session_key,
token: token,
header: null,
type: 1
}).catch(
error=>{}
)
//返回数据给客户端
return token
};