调用来源: h5
function h5GzhLogin() {
if (isWechat()) {
let appid = "wx433ba628e7cd762d" //为测试号id
let code = getUrlParam("code") //是否存在code
let local = window.location.href
if (code == null || code === "") {
window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_base&state=1#wechat_redirect`
}
if (!uni.getStorageSync('token')) {
uniCloud.callFunction({
name: 'login',
data: {
code
},
}).then((res) => {
console.log(res);
uni.hideLoading()
if (res.result.status !== 0) {
return Promise.reject(new Error(res.result.msg))
}
uni.setStorageSync('token', res.result.token)
uni.showModal({
content: '注册成功,token已存储',
showCancel: false
})
}).catch((err) => {
console.log(err);
uni.hideLoading()
uni.showModal({
content: '注册失败,' + err.message,
showCancel: false
})
})
}
}
}
云函数:
'use strict'
const crypto = require('crypto')
const jwt = require('jwt-simple')
const {
loginConfig,
tokenExp
} = require('../../utils/constants.js')
const db = uniCloud.database()
exports.main = async (event, context) => {
console.log(event)
console.log(context)
if(!event || !event.code) return
let data = {
appid: loginConfig.AppId,
secret: loginConfig.AppSecret,
js_code: event.code,
grant_type: 'authorization_code'
}
console.log(data)
console.log('收到登录请求!')
const res = await uniCloud.httpclient.request('https://api.weixin.qq.com/sns/jscode2session', {
method: 'GET',
data,
dataType: 'json'
})
云函数运行日志:
[weads] 10:23:19.083 [阿里云:weads-dev]云函数login上传完成,开始运行...
[weads] 10:23:30.276 [阿里云:weads-dev]运行状态:成功
[weads] 10:23:30.277 [阿里云:weads-dev]返回结果:{}
[weads] 10:23:30.277 [阿里云:weads-dev]运行日志:
Event {} --------------------------------------------------------------(Event不管怎么样都是空的,前端确定是有数据传过去的)
{ body: {},
rawBody: <Buffer 7b 7d>,
args: {},
headers:
{ accept: 'application/json',
authorization: 'FC STS.NUTPF5RoarJySKWXURLgdXof6:rfpV78MwNS1Pt+hBRWZAcbxkfwnZ+pY1pR5Yj3Faz5o=',
'content-length': '2',
'content-md5': 'OTk5MTRiOTMyYmQzN2E1MGI5ODNjNWU3YzkwYWU5M2I=',
'content-type': 'application/json',
date: 'Mon, 16 Mar 2020 02:23:23 GMT',
host: '1470483271556125.cn-zhangjiakou-internal.fc.aliyuncs.com',
'user-agent': 'Node.js(v10.19.0) OS(linux/x64) SDK(@alicloud/fc2@v2.2.0)',
'x-basement-sdk-type': 'server',
'x-basement-space-id': '65ad40fb-3a45-484f-8b9c-a2b665270529',
'x-forwarded-proto': 'http' },
user: { id: undefined },
requestId: '497084F4-35FF-4946-BB00-C049CF9C1D97',
logger:
BusinessLogger {
options:
{ startTime: 2020-03-16T02:23:25.404Z,
spaceId: '65ad40fb-3a45-484f-8b9c-a2b665270529',
functionName: 'login',
runtimeId: '0.5.4',
functionId: 'fun-5e6e33ee1c9d44185c17ed1f',
functionEntry: undefined,
deploymentId: 'dep-5e6ee3191c9d44185c180dc6',
requestId: '497084F4-35FF-4946-BB00-C049CF9C1D97' } },
httpclient:
HttpClient2 {
domain: null,
不知道哪里出错了,求助
luochuan (作者)
code是传了过去,云函数的event却怎么也收不到
2020-03-16 14:48