export const SECRET = "SECRET "
export const APPID = "APPID "
export const GET_OPENID_URL = (code: string) => {
return `https://api.weixin.qq.com/sns/jscode2session?appid=${APPID}&secret=${SECRET}&js_code=${code}&grant_type=authorization_code`
}
import { GET_OPENID_URL } from "../contanst/index";
export const login = () => {
return new Promise(async (resolve, reject) => {
const code = await wx.login();
if (code && code.code) {
const url = GET_OPENID_URL(code.code)
await wx.request({
url,
method: "GET",
success: res => {
resolve(res.data);
},
fail: error => {
console.log(error, 'error');
wx.showToast({
title: "获取openid失败",
icon: "error"
})
reject("获取openid失败")
}
})
} else {
wx.showToast({
title: "获取openid失败",
icon: "error"
})
reject("获取openid失败")
}
})
}
使用
const user_key = wx.getStorageSync(USER_KEY);
if (user_key) {
const obj = JSON.parse(user_key);
if (obj.session_key && obj.openid) {
this.globalData.user_open_info = {
session_key: obj.session_key,
openid: obj.openid
}
console.log(this.globalData.user_open_info, 'this.globalData.user_open_info');
}
} else {
const loginRes = await login();
if (loginRes) {
wx.setStorage({
key: USER_KEY,
data: JSON.stringify(loginRes),
})
}
}
文章版权声明:除非注明,否则均为
譬如朝露博客原创文章,转载或复制请以超链接形式并注明出处。
发表评论
侧栏公告
寄语
譬如朝露博客是一个分享前端知识的网站,联系方式11523518。
热评文章
标签列表
热门文章
友情链接