Files
PartsInquiry/frontend/unpackage/dist/dev/mp-weixin/pages/my/index.js
2025-09-29 21:38:32 +08:00

333 lines
12 KiB
JavaScript

"use strict";
const common_vendor = require("../../common/vendor.js");
const common_http = require("../../common/http.js");
const common_config = require("../../common/config.js");
const common_assets = require("../../common/assets.js");
function normalizeAvatar(url) {
if (!url)
return "/static/icons/icons8-mitt-24.png";
const s = String(url);
if (/^https?:\/\//i.test(s))
return s;
if (!common_config.API_BASE_URL)
return s;
if (s.startsWith("/"))
return `${common_config.API_BASE_URL}${s}`;
return `${common_config.API_BASE_URL}/${s}`;
}
const _sfc_main = {
data() {
return {
avatarUrl: "/static/icons/icons8-mitt-24.png",
shopName: "未登录",
mobile: "",
pendingJsCode: "",
logging: false,
vipIsVip: false,
vipStart: "",
vipEnd: ""
};
},
onShow() {
this.fetchProfile();
this.loadVip();
try {
if (common_vendor.index.getStorageSync("TOKEN")) {
this.$forceUpdate && this.$forceUpdate();
}
} catch (e) {
}
},
computed: {
isLoggedIn() {
try {
return !!common_vendor.index.getStorageSync("TOKEN");
} catch (e) {
return false;
}
},
avatarDisplay() {
return normalizeAvatar(this.avatarUrl);
},
emailDisplay() {
if (!this.isLoggedIn)
return "";
const e = String(common_vendor.index.getStorageSync("USER_EMAIL") || "");
if (!e)
return "未绑定邮箱";
const at = e.indexOf("@");
if (at > 1) {
const name = e.slice(0, at);
const domain = e.slice(at);
return (name.length <= 2 ? name[0] + "*" : name.slice(0, 2) + "***") + domain;
}
return e;
},
vipStartDisplay() {
return this.formatDisplay(this.vipStart);
},
vipEndDisplay() {
return this.formatDisplay(this.vipEnd);
}
},
methods: {
// 登录相关方法已移除
async fetchProfile() {
const hasToken = (() => {
try {
return !!common_vendor.index.getStorageSync("TOKEN");
} catch (e) {
return false;
}
})();
if (!hasToken) {
this.shopName = "未登录";
this.avatarUrl = "/static/icons/icons8-mitt-24.png";
this.mobile = "";
return;
}
try {
const profile = await common_http.get("/api/user/me");
const latestAvatar = (profile == null ? void 0 : profile.avatarUrl) || "";
if (latestAvatar) {
const bust = `${latestAvatar}${latestAvatar.includes("?") ? "&" : "?"}t=${Date.now()}`;
this.avatarUrl = bust;
try {
common_vendor.index.setStorageSync("USER_AVATAR_RAW", latestAvatar);
common_vendor.index.setStorageSync("USER_AVATAR", latestAvatar);
} catch (_) {
}
} else {
const cached = common_vendor.index.getStorageSync("USER_AVATAR") || "";
this.avatarUrl = cached || "/static/icons/icons8-mitt-24.png";
}
const storeName = (profile == null ? void 0 : profile.name) || common_vendor.index.getStorageSync("SHOP_NAME") || "未命名店铺";
this.shopName = storeName;
const phone = (profile == null ? void 0 : profile.phone) || common_vendor.index.getStorageSync("USER_MOBILE") || "";
this.mobile = phone;
} catch (e) {
try {
const storeName = common_vendor.index.getStorageSync("SHOP_NAME") || "";
const avatar = common_vendor.index.getStorageSync("USER_AVATAR") || "";
const phone = common_vendor.index.getStorageSync("USER_MOBILE") || "";
if (storeName)
this.shopName = storeName;
if (avatar)
this.avatarUrl = avatar;
this.mobile = phone;
} catch (_) {
}
}
},
async loadVip() {
try {
const hasToken = (() => {
try {
return !!common_vendor.index.getStorageSync("TOKEN");
} catch (e) {
return false;
}
})();
if (!hasToken) {
this.vipIsVip = false;
this.vipStart = "";
this.vipEnd = "";
return;
}
const data = await common_http.get("/api/vip/status");
const active = !!(data == null ? void 0 : data.isVip);
this.vipIsVip = active;
this.vipEnd = (data == null ? void 0 : data.expireAt) || "";
let computedStart = "";
const exp = this.vipEnd;
if (exp) {
const m = String(exp).match(/^(\d{4})-(\d{2})-(\d{2})(?:[ T](\d{2}):(\d{2})(?::(\d{2}))?)?/);
if (m) {
const y = Number(m[1]);
const mo = Number(m[2]) - 1;
const da = Number(m[3]);
const hh = Number(m[4] || "0");
const mm = Number(m[5] || "0");
const ss = Number(m[6] || "0");
const startDate = new Date(y, mo - 1, da, hh, mm, ss);
const y2 = startDate.getFullYear();
const m2 = (startDate.getMonth() + 1).toString().padStart(2, "0");
const d2 = startDate.getDate().toString().padStart(2, "0");
const h2 = startDate.getHours().toString().padStart(2, "0");
const i2 = startDate.getMinutes().toString().padStart(2, "0");
computedStart = `${y2}-${m2}-${d2} ${h2}:${i2}`;
}
}
this.vipStart = computedStart;
try {
common_vendor.index.setStorageSync("USER_VIP_IS_VIP", String(active));
common_vendor.index.setStorageSync("USER_VIP_END", this.vipEnd);
if (this.vipStart)
common_vendor.index.setStorageSync("USER_VIP_START", this.vipStart);
else
common_vendor.index.removeStorageSync("USER_VIP_START");
} catch (_) {
}
} catch (e) {
try {
const isVip = String(common_vendor.index.getStorageSync("USER_VIP_IS_VIP") || "false").toLowerCase() === "true";
this.vipIsVip = isVip;
this.vipStart = common_vendor.index.getStorageSync("USER_VIP_START") || "";
this.vipEnd = common_vendor.index.getStorageSync("USER_VIP_END") || "";
} catch (_) {
}
}
},
formatDisplay(value) {
if (!value)
return "-";
const s = String(value);
const m = s.match(/^(\d{4}-\d{2}-\d{2})/);
if (m)
return m[1];
const d = new Date(s);
if (!isNaN(d.getTime())) {
const y = d.getFullYear();
const mo = String(d.getMonth() + 1).padStart(2, "0");
const da = String(d.getDate()).padStart(2, "0");
return `${y}-${mo}-${da}`;
}
return s;
},
startLogin() {
if (this.logging)
return;
this.logging = true;
const tryOnce = async () => ({});
common_vendor.index.login({ provider: "weixin", success: async (res) => {
this.pendingJsCode = res.code || "";
if (!this.pendingJsCode) {
this.logging = false;
return common_vendor.index.showToast({ title: "获取登录code失败", icon: "none" });
}
try {
await tryOnce();
} catch (e) {
const msg = e && e.message || "";
if (msg.includes("40163") || msg.toLowerCase().includes("been used")) {
common_vendor.index.login({ provider: "weixin", success: async (r2) => {
const fresh = r2.code || "";
if (!fresh) {
this.logging = false;
return;
}
try {
await tryOnce();
} finally {
this.logging = false;
}
} });
return;
}
} finally {
this.logging = false;
}
}, fail: () => {
this.logging = false;
common_vendor.index.showToast({ title: "微信登录失败", icon: "none" });
} });
},
goLogin() {
common_vendor.index.navigateTo({ url: "/pages/auth/login" });
},
onGetPhoneNumber(e) {
if (this.logging)
return;
this.logging = true;
common_vendor.index.login({ provider: "weixin", success: (res) => {
const jsCode = res.code || "";
if (!jsCode) {
this.logging = false;
return common_vendor.index.showToast({ title: "获取登录code失败", icon: "none" });
}
Promise.resolve().finally(() => {
this.logging = false;
});
}, fail: () => {
this.logging = false;
common_vendor.index.showToast({ title: "微信登录失败", icon: "none" });
} });
},
goSmsLogin() {
common_vendor.index.navigateTo({ url: "/pages/my/sms-login" });
},
onAvatarError() {
this.avatarUrl = "/static/icons/icons8-mitt-24.png";
},
goVip() {
common_vendor.index.navigateTo({ url: "/pages/my/vip" });
},
goMyOrders() {
common_vendor.index.navigateTo({ url: "/pages/my/orders" });
},
editProfile() {
common_vendor.index.navigateTo({ url: "/pages/my/security" });
},
goAbout() {
common_vendor.index.navigateTo({ url: "/pages/my/about" });
},
logout() {
try {
common_vendor.index.removeStorageSync("TOKEN");
common_vendor.index.removeStorageSync("LOGINED");
common_vendor.index.removeStorageSync("LOGIN_PHONE");
common_vendor.index.removeStorageSync("DEFAULT_USER_ID");
common_vendor.index.setStorageSync("ENABLE_DEFAULT_USER", "false");
common_vendor.index.removeStorageSync("USER_AVATAR");
common_vendor.index.removeStorageSync("USER_AVATAR_RAW");
common_vendor.index.removeStorageSync("USER_NAME");
common_vendor.index.removeStorageSync("USER_MOBILE");
common_vendor.index.removeStorageSync("USER_EMAIL");
common_vendor.index.removeStorageSync("SHOP_NAME");
common_vendor.index.removeStorageSync("USER_VIP_IS_VIP");
common_vendor.index.removeStorageSync("USER_VIP_START");
common_vendor.index.removeStorageSync("USER_VIP_END");
common_vendor.index.showToast({ title: "已清理本地信息", icon: "none" });
setTimeout(() => {
common_vendor.index.reLaunch({ url: "/pages/index/index" });
}, 300);
} catch (e) {
common_vendor.index.reLaunch({ url: "/pages/index/index" });
}
}
}
};
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
return common_vendor.e({
a: $options.isLoggedIn
}, $options.isLoggedIn ? {
b: $options.avatarDisplay,
c: common_vendor.o((...args) => $options.onAvatarError && $options.onAvatarError(...args)),
d: common_vendor.t($data.shopName),
e: common_vendor.t($options.emailDisplay)
} : {
f: common_assets._imports_0$1,
g: common_vendor.o((...args) => $options.goLogin && $options.goLogin(...args))
}, {
h: $options.isLoggedIn
}, $options.isLoggedIn ? {
i: common_vendor.t($data.vipIsVip ? "VIP" : "非VIP"),
j: common_vendor.t($options.vipStartDisplay),
k: common_vendor.t($options.vipEndDisplay),
l: $data.vipIsVip ? 1 : ""
} : {}, {
m: $data.vipIsVip
}, $data.vipIsVip ? {} : {}, {
n: common_vendor.o((...args) => $options.goVip && $options.goVip(...args)),
o: common_vendor.o((...args) => $options.goMyOrders && $options.goMyOrders(...args)),
p: common_vendor.o((...args) => $options.editProfile && $options.editProfile(...args)),
q: common_vendor.o((...args) => $options.goAbout && $options.goAbout(...args)),
r: $options.isLoggedIn
}, $options.isLoggedIn ? {
s: common_vendor.o((...args) => $options.logout && $options.logout(...args))
} : {});
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/my/index.js.map