const uniID = require('uni-id-common')
// const db = uniCloud.database()
async function myCheckToken(clientInfo,token){
// 创建uni-id实例,其上方法同uniID
const tokenRes = await uniID.createInstance({
clientInfo
}).checkToken(token)
return tokenRes
}
module.exports = {
_before: function () { // 通用预处理器
const httpInfo = this.getHttpInfo()
const clientInfo = this.getClientInfo()
const {token,body} = httpInfo.headers
if(body){
this.params = JSON.parse(body)
}
// let uid
if(token) {
// 创建uni-id实例,其上方法同uniID
this.tokenRes = myCheckToken(clientInfo,token)
// if(this.tokenRes.errCode === 0) {
// this.uid = this.tokenRes.uid
// // this.tokenRes = tokenRes
// }else{
// throw{
// // errCode: tokenRes.errCode,
// errCode: 4004,
// errMsg: 'token无效,请先登录'
// }
// }
}else{
throw{
errCode: 404,
errMsg: '请先登录'
}
}
},
test: function() {
const uid = this.tokenRes.uid
return this.tokenRes
// return {
// errvvv: 000,
// errMsg: this.tokenRes.uid
// }
},
_after(error, result) {
if(error) {
return error
}
return result
}
}
用点号取this.tokenRes对象里的属性均为空值,如下取值方式:
this.tokenRes.uid
this.tokenRes.errCode
但却可以把对象this.tokenRes整体返回给客户端,客户端接手到的结果如下:
data:
code: 0
errCode: 0
exp: 1711028570
iat: 1703252570
permission: []
role: []
uid: "65855260fe975f47a5d321df"
proto: Object
errMsg: "request:ok"
请问该怎么取到his.tokenRes对象里面的属性值啊????
0 个回复