209 lines
7.0 KiB
JavaScript
209 lines
7.0 KiB
JavaScript
"use strict";
|
||
const common_vendor = require("../../common/vendor.js");
|
||
const modules_api_AppServer = require("../../modules/api/AppServer.js");
|
||
const utils_auth = require("../../utils/auth.js");
|
||
const modules_Config = require("../../modules/Config.js");
|
||
const _sfc_main = {
|
||
data() {
|
||
return {
|
||
isLoading: false,
|
||
agreeToTerms: false,
|
||
statusBarHeight: 0,
|
||
appLogo: ""
|
||
};
|
||
},
|
||
onLoad() {
|
||
const systemInfo = common_vendor.index.getSystemInfoSync();
|
||
this.statusBarHeight = systemInfo.statusBarHeight || 0;
|
||
this.loadConfig();
|
||
},
|
||
methods: {
|
||
back() {
|
||
common_vendor.index.navigateBack({
|
||
delta: 1
|
||
});
|
||
},
|
||
// Handle WeChat login
|
||
async handleWechatLogin() {
|
||
if (this.isLoading) {
|
||
return;
|
||
}
|
||
if (!this.agreeToTerms) {
|
||
common_vendor.index.showToast({
|
||
title: this.$t("login.mustAgreeToTerms"),
|
||
icon: "none"
|
||
});
|
||
return;
|
||
}
|
||
this.isLoading = true;
|
||
try {
|
||
const loginRes = await this.getWechatLoginCode();
|
||
console.log("微信登录 code:", loginRes);
|
||
if (!loginRes || !loginRes.code) {
|
||
common_vendor.index.showToast({
|
||
title: this.$t("login.wechatLoginFailed"),
|
||
icon: "none"
|
||
});
|
||
this.isLoading = false;
|
||
return;
|
||
}
|
||
let invitationCode = "";
|
||
const app = getApp();
|
||
if (app && app.globalData && app.globalData.inviteCode) {
|
||
invitationCode = app.globalData.inviteCode;
|
||
} else {
|
||
invitationCode = common_vendor.index.getStorageSync("inviteCode") || "";
|
||
}
|
||
if (invitationCode) {
|
||
console.log("使用邀请码登录:", invitationCode);
|
||
}
|
||
const appserver = new modules_api_AppServer.AppServer();
|
||
const data = await appserver.WechatLogin(loginRes.code, invitationCode);
|
||
console.log("登录接口返回:", data);
|
||
if (!data) {
|
||
console.error("登录接口无响应");
|
||
common_vendor.index.showToast({
|
||
title: this.$t("login.loginFailed"),
|
||
icon: "none"
|
||
});
|
||
this.isLoading = false;
|
||
return;
|
||
}
|
||
if (data.code !== 0) {
|
||
console.error("登录失败,code:", data.code, "message:", data.message);
|
||
common_vendor.index.showToast({
|
||
title: data.message || this.$t("login.loginFailed"),
|
||
icon: "none"
|
||
});
|
||
this.isLoading = false;
|
||
return;
|
||
}
|
||
if (!data.data || !data.data.token) {
|
||
console.error("Token 数据缺失:", data.data);
|
||
common_vendor.index.showToast({
|
||
title: this.$t("login.loginFailed"),
|
||
icon: "none"
|
||
});
|
||
this.isLoading = false;
|
||
return;
|
||
}
|
||
console.log("登录成功,保存认证信息");
|
||
const token = "Bearer " + data.data.token;
|
||
utils_auth.saveAuthData(token, data.data.refreshToken, data.data.user);
|
||
if (invitationCode) {
|
||
const app2 = getApp();
|
||
if (app2 && app2.globalData) {
|
||
app2.globalData.inviteCode = "";
|
||
}
|
||
common_vendor.index.removeStorageSync("inviteCode");
|
||
console.log("邀请码已清除");
|
||
}
|
||
common_vendor.index.showToast({
|
||
title: this.$t("login.loginSuccess") || "登录成功",
|
||
icon: "success",
|
||
duration: 1500
|
||
});
|
||
setTimeout(() => {
|
||
this.redirectToHome();
|
||
}, 500);
|
||
} catch (error) {
|
||
console.error("登录异常:", error);
|
||
console.error("错误堆栈:", error.stack);
|
||
common_vendor.index.showToast({
|
||
title: this.$t("login.loginError") + ": " + (error.message || ""),
|
||
icon: "none",
|
||
duration: 3e3
|
||
});
|
||
} finally {
|
||
this.isLoading = false;
|
||
}
|
||
},
|
||
// 获取微信登录code(Promise封装)
|
||
getWechatLoginCode() {
|
||
return new Promise((resolve, reject) => {
|
||
common_vendor.index.login({
|
||
provider: "weixin",
|
||
success: (res) => {
|
||
resolve(res);
|
||
},
|
||
fail: (err) => {
|
||
console.error("uni.login 失败:", err);
|
||
reject(err);
|
||
}
|
||
});
|
||
});
|
||
},
|
||
// 登录成功后跳转到首页
|
||
redirectToHome() {
|
||
const app = getApp();
|
||
if (app.globalData) {
|
||
app.globalData.shouldRefresh = true;
|
||
app.globalData.loginTime = Date.now();
|
||
}
|
||
common_vendor.index.switchTab({
|
||
url: "/pages/index/index",
|
||
success: () => {
|
||
console.log("登录成功,已跳转到首页");
|
||
},
|
||
fail: (err) => {
|
||
console.error("跳转首页失败:", err);
|
||
common_vendor.index.reLaunch({
|
||
url: "/pages/index/index"
|
||
});
|
||
}
|
||
});
|
||
},
|
||
// Toggle agreement checkbox
|
||
toggleAgreement() {
|
||
this.agreeToTerms = !this.agreeToTerms;
|
||
},
|
||
// Go to user agreement page
|
||
goToUserAgreement() {
|
||
common_vendor.index.navigateTo({
|
||
url: "/pages/me/user-agreement-page"
|
||
});
|
||
},
|
||
// Go to privacy policy page
|
||
goToPrivacyPolicy() {
|
||
common_vendor.index.navigateTo({
|
||
url: "/pages/me/privacy-policy-page"
|
||
});
|
||
},
|
||
// 加载配置
|
||
async loadConfig() {
|
||
try {
|
||
const config = await modules_Config.Config.getPublicConfig();
|
||
if (config.app_logo) {
|
||
this.appLogo = modules_Config.Config.getImageUrl(config.app_logo);
|
||
}
|
||
} catch (error) {
|
||
console.error("加载配置失败:", error);
|
||
}
|
||
}
|
||
}
|
||
};
|
||
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
|
||
return common_vendor.e({
|
||
a: common_vendor.o((...args) => $options.back && $options.back(...args)),
|
||
b: common_vendor.t(_ctx.$t("login.title")),
|
||
c: common_vendor.o((...args) => _ctx.markAllRead && _ctx.markAllRead(...args)),
|
||
d: $data.appLogo
|
||
}, $data.appLogo ? {
|
||
e: $data.appLogo
|
||
} : {}, {
|
||
f: common_vendor.t(_ctx.$t("login.oneClickLogin")),
|
||
g: common_vendor.o((...args) => $options.handleWechatLogin && $options.handleWechatLogin(...args)),
|
||
h: $data.isLoading,
|
||
i: $data.agreeToTerms ? "/static/ic_checked.png" : "/static/ic_check.png",
|
||
j: common_vendor.t(_ctx.$t("login.agreeToTerms")),
|
||
k: common_vendor.t(_ctx.$t("login.userAgreement")),
|
||
l: common_vendor.o((...args) => $options.goToUserAgreement && $options.goToUserAgreement(...args)),
|
||
m: common_vendor.t(_ctx.$t("login.and")),
|
||
n: common_vendor.t(_ctx.$t("login.privacyPolicy")),
|
||
o: common_vendor.o((...args) => $options.goToPrivacyPolicy && $options.goToPrivacyPolicy(...args)),
|
||
p: common_vendor.o((...args) => $options.toggleAgreement && $options.toggleAgreement(...args))
|
||
});
|
||
}
|
||
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-27b7efa5"], ["__file", "F:/gitCode/uniapp/appointment_system/miniprogram/src/pages/login/login-page.vue"]]);
|
||
wx.createPage(MiniProgramPage);
|