2025-11-24 22:37:11 +08:00

137 lines
7.0 KiB
JavaScript

"use strict";
const common_vendor = require("../../common/vendor.js");
const common_assets = require("../../common/assets.js");
const api_appUser = require("../../api/appUser.js");
const _sfc_main = {
__name: "index",
setup(__props) {
const loading = common_vendor.ref(false);
const error = common_vendor.ref("");
const needBindPhone = common_vendor.ref(false);
common_vendor.computed(() => !!common_vendor.index.getStorageSync("token"));
function toUserAgreement() {
common_vendor.index.navigateTo({ url: "/pages/agreement/user" });
}
function toPurchaseAgreement() {
common_vendor.index.navigateTo({ url: "/pages/agreement/purchase" });
}
function onGetPhoneNumber(e) {
const phoneCode = e.detail.code;
common_vendor.index.__f__("log", "at pages/login/index.vue:37", "login_flow start getPhoneNumber, codeExists:", !!phoneCode);
if (!phoneCode) {
common_vendor.index.showToast({ title: "未授权手机号", icon: "none" });
common_vendor.index.__f__("error", "at pages/login/index.vue:40", "login_flow error: missing phoneCode");
return;
}
loading.value = true;
error.value = "";
common_vendor.index.login({
provider: "weixin",
success: async (res) => {
try {
const loginCode = res.code;
common_vendor.index.__f__("log", "at pages/login/index.vue:50", "login_flow uni.login success, loginCode exists:", !!loginCode);
const data = await api_appUser.wechatLogin(loginCode);
common_vendor.index.__f__("log", "at pages/login/index.vue:52", "login_flow wechatLogin response user_id:", data && data.user_id);
const token = data && data.token;
const user_id = data && data.user_id;
const avatar = data && data.avatar;
const nickname = data && data.nickname;
const invite_code = data && data.invite_code;
common_vendor.index.setStorageSync("user_info", data || {});
if (token) {
common_vendor.index.setStorageSync("token", token);
common_vendor.index.__f__("log", "at pages/login/index.vue:61", "login_flow token stored");
}
if (user_id) {
common_vendor.index.setStorageSync("user_id", user_id);
common_vendor.index.__f__("log", "at pages/login/index.vue:65", "login_flow user_id stored:", user_id);
}
if (avatar) {
common_vendor.index.setStorageSync("avatar", avatar);
}
if (nickname) {
common_vendor.index.setStorageSync("nickname", nickname);
}
if (invite_code) {
common_vendor.index.setStorageSync("invite_code", invite_code);
}
common_vendor.index.__f__("log", "at pages/login/index.vue:76", "login_flow bindPhone start");
try {
await new Promise((r) => setTimeout(r, 600));
const bindRes = await api_appUser.bindPhone(user_id, phoneCode, { "X-Suppress-Auth-Modal": true });
const phoneNumber = bindRes && (bindRes.phone || bindRes.phone_number || bindRes.mobile) || "";
if (phoneNumber)
common_vendor.index.setStorageSync("phone_number", phoneNumber);
} catch (bindErr) {
if (bindErr && bindErr.statusCode === 401) {
common_vendor.index.__f__("warn", "at pages/login/index.vue:85", "login_flow bindPhone 401, try re-login and retry");
const relogin = await new Promise((resolve, reject) => {
common_vendor.index.login({ provider: "weixin", success: resolve, fail: reject });
});
const data2 = await api_appUser.wechatLogin(relogin.code);
const token2 = data2 && data2.token;
const user2 = data2 && data2.user_id;
if (token2)
common_vendor.index.setStorageSync("token", token2);
if (user2)
common_vendor.index.setStorageSync("user_id", user2);
await new Promise((r) => setTimeout(r, 600));
const bindRes2 = await api_appUser.bindPhone(user2 || user_id, phoneCode, { "X-Suppress-Auth-Modal": true });
const phoneNumber2 = bindRes2 && (bindRes2.phone || bindRes2.phone_number || bindRes2.mobile) || "";
if (phoneNumber2)
common_vendor.index.setStorageSync("phone_number", phoneNumber2);
} else {
throw bindErr;
}
}
common_vendor.index.setStorageSync("phone_bound", true);
common_vendor.index.__f__("log", "at pages/login/index.vue:105", "login_flow bindPhone success, phone_bound stored");
try {
const stats = await api_appUser.getUserStats(user_id);
common_vendor.index.__f__("log", "at pages/login/index.vue:108", "login_flow getUserStats success");
const balance = await api_appUser.getPointsBalance(user_id);
common_vendor.index.__f__("log", "at pages/login/index.vue:110", "login_flow getPointsBalance success");
common_vendor.index.setStorageSync("user_stats", stats);
const b = balance && balance.balance !== void 0 ? balance.balance : balance;
common_vendor.index.setStorageSync("points_balance", b);
} catch (e2) {
common_vendor.index.__f__("error", "at pages/login/index.vue:115", "login_flow fetch stats/points error:", e2 && (e2.message || e2.errMsg));
}
common_vendor.index.showToast({ title: "登录并绑定成功", icon: "success" });
common_vendor.index.__f__("log", "at pages/login/index.vue:118", "login_flow navigate to index");
common_vendor.index.reLaunch({ url: "/pages/index/index" });
} catch (err) {
common_vendor.index.__f__("error", "at pages/login/index.vue:121", "login_flow error:", err && (err.message || err.errMsg), "status:", err && err.statusCode);
error.value = err.message || "登录或绑定失败";
} finally {
loading.value = false;
}
},
fail: (e2) => {
common_vendor.index.__f__("error", "at pages/login/index.vue:128", "login_flow uni.login fail:", e2 && e2.errMsg);
error.value = "微信登录失败";
loading.value = false;
}
});
}
return (_ctx, _cache) => {
return common_vendor.e({
a: common_assets._imports_0,
b: loading.value,
c: common_vendor.o(onGetPhoneNumber),
d: common_vendor.o(toUserAgreement),
e: common_vendor.o(toPurchaseAgreement),
f: needBindPhone.value
}, needBindPhone.value ? {} : {}, {
g: error.value
}, error.value ? {
h: common_vendor.t(error.value)
} : {});
};
}
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-d08ef7d4"]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/login/index.js.map