与bug描述一致
- 发布:2020-05-26 18:22
- 更新:2020-06-01 15:46
- 阅读:1789
产品分类: uniCloud/腾讯云
操作步骤:
预期结果:
获得用户信息
获得用户信息
实际结果:
拿不到用户信息
输出{ openId: '', appId: '', uid: '', customUserId: '' }
拿不到用户信息
输出{ openId: '', appId: '', uid: '', customUserId: '' }
bug描述:
按照文档写法,生成credentials.json并复制到云函数里面,贴代码
云函数token
const app = uniCloud.init({
spaceId: 'cloud-xxxxx',
credentials: require('./credentials.json')
});
const auth = app.auth();
let ticket = auth.createTicket( "123456789", {
refresh: 24 * 60 * 60 * 1000 //1天登录态, 默认为一小时
});
return ticket;
App.vue总入口
const auth = uniCloud.auth();
getToken({uid: id}).then(
res => {
auth.signInWithTicket(res).then(() => {
// 登录成功
console.log('客户端登录成功');//能正常执行,并且能获得auth.getUserInfo()
});
},
err => {
//登录信息失效
}
);
index.vue首页调用购物车接口,云函数为cart
const app = uniCloud.init({
spaceId: 'cloud-xxxxx',
credentials: require('./credentials.json')
});
const auth = app.auth();
let info = auth.getUserInfo();
console.log(info)
获得的用户信息为空
{ openId: '', appId: '', uid: '', customUserId: '' }
另外我统一写到公共函数 token里面测试,一样拿不到值
const app = uniCloud.init({
spaceId: 'cloud-market-xxx',
credentials: require('./credentials.json')
});
const auth = app.auth();
/**
* 获取登录用户信息
*/
const getUserInfo = function() {
let info = auth.getUserInfo();
console.log(info);//输出{ openId: '', appId: '', uid: '', customUserId: '' }
return info;
}
/**
* 生成票据
* @param {Object} id
*/
const createTicket = function(id){
return auth.createTicket(id + "", {
refresh: 24 * 60 * 60 * 1000 //1天登录态, 默认为一小时
});
}
module.exports = {
getUserInfo,
createTicket
}
"index.vue首页调用购物车接口"这个是什么时机调用的?在什么生命周期还是点击触发
-
重庆柔然科技 (作者)
首页的登录状态改变之后,会重新刷新页面,另外过一会儿点击底部菜单购物车也不能获得信息,其他接口都没有,你们有成功的完整例子吗?贴出来大家参考一下吧
2020-06-01 15:29
客户端代码
<template>
<view class="content">
<button type="default" @click="onClick">点我</button>
<button type="default" @click="check">check</button>
</view>
</template>
<script>
// 下个alpha版本就不需要手动初始化了
const client = uniCloud.init({
provider: 'tencent',
spaceId: 'your spaceId‘,
debugFunction: false,
})
const auth = client.auth()
export default {
data() {
return {
title: 'Hello'
}
},
onLoad() {
},
methods: {
onClick() {
client.callFunction({
name: 'login',
success(res) {
console.log(res);
auth.signInWithTicket(res.result.ticket).then((res) => {
console.log(res);
}).catch((err) => {
console.error(err)
})
},
fail(err) {
console.log('fail', err)
}
})
},
check() {
client.callFunction({
name: 'check',
success(res) {
console.log(res);
},
fail(err) {
console.log('fail', err)
}
})
}
}
}
</script>
<style>
</style>
云函数check
'use strict';
const auth = uniCloud.auth()
exports.main = async (event, context) => {
let userInfo = await auth.getUserInfo()
return {
userInfo
}
};
重庆柔然科技 (作者)
我请求数据返回的res,已经是ticket字符串了,signInWithTicket之后,用
auth.getUserInfo().then(res => {
console.log(res);
});
能获取到
{avatarUrl: ""
city: ""
country: ""
envName: "cloud-market-xxxx"
gender: "UNKNOWN"
loginType: "CUSTOM"
nickName: ""
openid: ""
province: ""
qqMiniOpenId: ""
requestId: "1f97bdf2879ce"
unionId: ""
uuid: "397dc7dd72dd4d4ba2366e6f14e75a8"}
2020-05-27 16:01
重庆柔然科技 (作者)
而且我看后面的数据请求,cookie都带 accessToken
2020-05-27 16:02
DCloud_uniCloud_WYQ
回复 重庆柔然科技: 参考这个链接 https://uniapp.dcloud.io/uniCloud/init , 配置debugFunction为false试试,下版优化这里
2020-05-28 10:19
3***@qq.com
回复 DCloud_uniCloud_WYQ: 我这边也遇到同样的问题,取不到自定义的customUserId
2020-05-31 09:43
重庆柔然科技 (作者)
回复 DCloud_uniCloud_WYQ: 测试了,没有用
2020-06-01 12:24