2025-08-10 02:08:03 +08:00
|
|
|
|
import envConfig from '../../env';
|
|
|
|
|
|
import Toast from 'tdesign-miniprogram/toast/index';
|
2025-08-09 17:41:14 +08:00
|
|
|
|
Component({
|
|
|
|
|
|
data: {
|
|
|
|
|
|
image: 'https://tdesign.gtimg.com/mobile/demos/avatar1.png',
|
|
|
|
|
|
},
|
|
|
|
|
|
methods: {
|
2025-08-10 02:08:03 +08:00
|
|
|
|
async getLoginCode() {
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
wx.login({
|
|
|
|
|
|
success: (res) => {
|
|
|
|
|
|
if (res.code) {
|
|
|
|
|
|
resolve(res.code); // 返回获取到的code
|
|
|
|
|
|
} else {
|
|
|
|
|
|
reject(new Error('获取登录凭证失败: ' + res.errMsg));
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
fail: (err) => {
|
|
|
|
|
|
reject(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
},
|
|
|
|
|
|
async wxPhone(e: { detail: any; }){
|
|
|
|
|
|
console.log("授权回调结果:", e);
|
2025-08-09 17:41:14 +08:00
|
|
|
|
if (e.detail.errMsg === "getPhoneNumber:ok") {
|
2025-08-10 02:08:03 +08:00
|
|
|
|
const { encryptedData, iv} = e.detail;
|
|
|
|
|
|
console.log("encryptedData:",encryptedData)
|
|
|
|
|
|
const code = await this.getLoginCode();
|
|
|
|
|
|
console.log("获得的code为:",code)
|
2025-08-09 17:41:14 +08:00
|
|
|
|
wx.request({
|
2025-08-10 02:08:03 +08:00
|
|
|
|
url: `${envConfig.apiBaseUrl}/user/login`,
|
2025-08-09 17:41:14 +08:00
|
|
|
|
method: "POST",
|
|
|
|
|
|
data: {
|
|
|
|
|
|
encryptedData,
|
|
|
|
|
|
iv,
|
|
|
|
|
|
code,
|
|
|
|
|
|
},
|
|
|
|
|
|
success(res) {
|
2025-08-10 02:08:03 +08:00
|
|
|
|
console.log(res)
|
|
|
|
|
|
if (res.data.code == "20001") {
|
|
|
|
|
|
Toast({
|
|
|
|
|
|
selector: '#t-toast',
|
|
|
|
|
|
message: '登录成功',
|
|
|
|
|
|
theme: 'success',
|
|
|
|
|
|
direction: 'column',
|
|
|
|
|
|
});
|
2025-08-09 17:41:14 +08:00
|
|
|
|
// 登录成功,存储用户信息并跳转首页
|
2025-08-10 02:08:03 +08:00
|
|
|
|
//wx.setStorageSync("userInfo", res.data.userInfo);
|
|
|
|
|
|
//wx.navigateTo({ url: "/pages/index/index" });
|
2025-08-09 17:41:14 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
wx.showToast({ title: "登录失败", icon: "none" });
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
fail() {
|
|
|
|
|
|
wx.showToast({ title: "网络错误", icon: "none" });
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 用户拒绝授权
|
|
|
|
|
|
wx.showToast({ title: "请允许授权以完成登录", icon: "none" });
|
|
|
|
|
|
}
|
2025-08-10 02:08:03 +08:00
|
|
|
|
},
|
|
|
|
|
|
onBack() {
|
|
|
|
|
|
// 获取当前页面栈
|
|
|
|
|
|
const pages = getCurrentPages();
|
|
|
|
|
|
|
|
|
|
|
|
// 页面栈长度大于1,说明有上一个页面
|
|
|
|
|
|
if (pages.length > 1) {
|
|
|
|
|
|
// 调用微信API返回上一页,delta: 1 表示返回一层
|
|
|
|
|
|
wx.navigateBack({
|
|
|
|
|
|
delta: 1
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 如果没有上一页(比如从首页直接进入登录页),可跳转到默认页面(如首页)
|
|
|
|
|
|
wx.switchTab({
|
|
|
|
|
|
url: '/pages/index/index' // 替换为你的首页路径
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-08-09 17:41:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
2025-08-10 02:08:03 +08:00
|
|
|
|
|
2025-08-09 17:41:14 +08:00
|
|
|
|
})
|