完成用户登录和全局token
This commit is contained in:
@@ -23,12 +23,13 @@ Component({
|
||||
},
|
||||
async wxPhone(e: { detail: any; }){
|
||||
console.log("授权回调结果:", e);
|
||||
|
||||
if (e.detail.errMsg === "getPhoneNumber:ok") {
|
||||
const { encryptedData, iv} = e.detail;
|
||||
console.log("encryptedData:",encryptedData)
|
||||
const code = await this.getLoginCode();
|
||||
console.log("获得的code为:",code)
|
||||
wx.request({
|
||||
wx.request<WechatMiniprogram.LoginUserMsg>({
|
||||
url: `${envConfig.apiBaseUrl}/user/login`,
|
||||
method: "POST",
|
||||
data: {
|
||||
@@ -45,11 +46,60 @@ Component({
|
||||
theme: 'success',
|
||||
direction: 'column',
|
||||
});
|
||||
// 登录成功,存储用户信息并跳转首页
|
||||
//wx.setStorageSync("userInfo", res.data.userInfo);
|
||||
//wx.navigateTo({ url: "/pages/index/index" });
|
||||
} else {
|
||||
wx.showToast({ title: "登录失败", icon: "none" });
|
||||
const app = getApp()
|
||||
app.setToken(res.data.token)
|
||||
app.setUserInfo({
|
||||
uid: res.data.userinfo.uid,
|
||||
username: res.data.userinfo.username,
|
||||
telephone: res.data.userinfo.telephone,
|
||||
gender: 2,
|
||||
})
|
||||
|
||||
setTimeout(() => {
|
||||
// 3.1 刷新user组件
|
||||
const pages = getCurrentPages();
|
||||
let homePage = null;
|
||||
pages.forEach(page => {
|
||||
if (page.route === 'pages/index/index') {
|
||||
homePage = page;
|
||||
}
|
||||
});
|
||||
|
||||
if (homePage) {
|
||||
const userComponent = homePage.selectComponent('#userComp');
|
||||
if (userComponent) {
|
||||
userComponent.refreshUserStatus();
|
||||
}
|
||||
} else {
|
||||
// 首页不在栈中,跳转首页
|
||||
wx.redirectTo({
|
||||
url: '/pages/home/home'
|
||||
});
|
||||
// 注意:如果跳转后不需要执行下面的返回逻辑,可在此处加 return
|
||||
// return;
|
||||
}
|
||||
|
||||
// 3.2 页面跳转/返回
|
||||
if (pages.length > 1) {
|
||||
// 返回上一页
|
||||
wx.navigateBack({
|
||||
delta: 1
|
||||
});
|
||||
} else {
|
||||
// 跳转到首页(tabBar页面用switchTab)
|
||||
wx.switchTab({
|
||||
url: '/pages/index/index'
|
||||
});
|
||||
}
|
||||
|
||||
}, 1500); // 延迟时间:1500毫秒(1.5秒),可根据需要调整
|
||||
}else{
|
||||
Toast({
|
||||
selector: '#t-toast',
|
||||
message: '登录失败',
|
||||
theme: 'error',
|
||||
direction: 'column',
|
||||
});
|
||||
}
|
||||
},
|
||||
fail() {
|
||||
@@ -61,6 +111,30 @@ Component({
|
||||
wx.showToast({ title: "请允许授权以完成登录", icon: "none" });
|
||||
}
|
||||
},
|
||||
testSend(){
|
||||
const token = wx.getStorageSync("token");
|
||||
wx.request({
|
||||
url: `${envConfig.apiBaseUrl}/user/test`,
|
||||
method: "POST",
|
||||
data: {
|
||||
|
||||
},
|
||||
header:{
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
,
|
||||
success(res){
|
||||
console.log(res)
|
||||
Toast({
|
||||
selector: '#t-toast',
|
||||
message: '测试成功',
|
||||
theme: 'success',
|
||||
direction: 'column',
|
||||
});
|
||||
}
|
||||
})
|
||||
},
|
||||
onBack() {
|
||||
// 获取当前页面栈
|
||||
const pages = getCurrentPages();
|
||||
|
||||
Reference in New Issue
Block a user