123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- "use strict";
- const common_vendor = require("../../common/vendor.js");
- const common_assets = require("../../common/assets.js");
- const _sfc_main = {
- data() {
- return {
- isLogin: false,
- userInfo: {
- username: "",
- userId: "",
- avatar: ""
- },
- menuItems: [
- { title: "个人资料", icon: "icon-user", action: "profile" },
- // { title: '我的订单', icon: 'icon-order', action: 'orders' },
- // { title: '我的收藏', icon: 'icon-star', action: 'favorites' },
- { title: "设置", icon: "icon-settings", action: "settings" },
- { title: "帮助中心", icon: "icon-help", action: "help" }
- ]
- };
- },
- onShow() {
- this.checkLoginStatus();
- },
- methods: {
- // 检查登录状态
- checkLoginStatus() {
- try {
- const token = common_vendor.index.getStorageSync("token");
- const userInfo = common_vendor.index.getStorageSync("userInfo");
- if (token && userInfo) {
- this.isLogin = true;
- this.userInfo = JSON.parse(userInfo);
- } else {
- this.isLogin = false;
- this.userInfo = {
- username: "",
- userId: "",
- avatar: ""
- };
- }
- } catch (e) {
- console.error("获取登录状态失败", e);
- this.isLogin = false;
- }
- },
- // 前往登录页
- goLogin() {
- common_vendor.index.showActionSheet({
- itemList: ["账号密码登录", "微信登录"],
- success: (res) => {
- if (res.tapIndex === 0) {
- common_vendor.index.navigateTo({
- url: "/pages/login/login"
- });
- } else if (res.tapIndex === 1) {
- this.wxLogin();
- }
- }
- });
- },
- // 微信登录
- wxLogin() {
- common_vendor.index.showLoading({
- title: "登录中..."
- });
- common_vendor.index.login({
- provider: "weixin",
- success: (loginRes) => {
- this.wxLoginRequest(loginRes.code);
- this.getUserProfileNew();
- },
- fail: (err) => {
- common_vendor.index.hideLoading();
- common_vendor.index.showToast({
- title: "微信登录失败",
- icon: "none"
- });
- console.error("微信登录失败", err);
- }
- });
- },
- // 使用新的方式获取用户头像和昵称
- getUserProfileNew() {
- common_vendor.wx$1.getUserInfo({
- desc: "用于完善用户资料",
- success: (res) => {
- console.log("获取用户信息成功", res);
- },
- fail: (err) => {
- console.error("获取用户信息失败", err);
- }
- });
- },
- // 头像选择回调
- onChooseAvatar(e) {
- const { avatarUrl } = e.detail;
- console.log("用户选择的头像:", avatarUrl);
- },
- // 发送微信登录请求到服务器
- wxLoginRequest(code, userInfo = {}) {
- common_vendor.index.request({
- url: "https://your-api-domain.com/api/wx/login",
- method: "POST",
- data: {
- code
- // 不再依赖 getUserProfile 获取的信息
- // 如果有用户选择的头像,可以在这里传递
- },
- success: (res) => {
- common_vendor.index.hideLoading();
- if (res.data.code === 0) {
- const userData = {
- username: res.data.data.username || "微信用户",
- userId: res.data.data.userId,
- avatar: res.data.data.avatar || "/static/avatar.png"
- };
- try {
- common_vendor.index.setStorageSync("token", res.data.data.token);
- common_vendor.index.setStorageSync("userInfo", JSON.stringify(userData));
- this.isLogin = true;
- this.userInfo = userData;
- common_vendor.index.showToast({
- title: "登录成功",
- icon: "success"
- });
- } catch (e) {
- console.error("保存登录信息失败", e);
- }
- } else {
- common_vendor.index.showToast({
- title: res.data.msg || "登录失败",
- icon: "none"
- });
- }
- },
- fail: (err) => {
- common_vendor.index.hideLoading();
- common_vendor.index.showToast({
- title: "网络请求失败",
- icon: "none"
- });
- console.error("微信登录请求失败", err);
- }
- });
- },
- handleMenuClick(item) {
- const needLogin = ["profile", "orders", "favorites"];
- if (needLogin.includes(item.action) && !this.isLogin) {
- common_vendor.index.showToast({
- title: "请先登录",
- icon: "none"
- });
- setTimeout(() => {
- this.goLogin();
- }, 1500);
- return;
- }
- switch (item.action) {
- case "profile":
- common_vendor.index.navigateTo({ url: "/pages/profile/profile" });
- break;
- case "orders":
- common_vendor.index.navigateTo({ url: "/pages/orders/orders" });
- break;
- case "favorites":
- common_vendor.index.navigateTo({ url: "/pages/favorites/favorites" });
- break;
- case "settings":
- common_vendor.index.navigateTo({ url: "/pages/settings/settings" });
- break;
- case "help":
- common_vendor.index.navigateTo({ url: "/pages/help/help" });
- break;
- default:
- common_vendor.index.showToast({
- title: `点击了${item.title}`,
- icon: "none"
- });
- }
- },
- handleLogout() {
- common_vendor.index.showModal({
- title: "提示",
- content: "确定要退出登录吗?",
- success: (res) => {
- if (res.confirm) {
- try {
- common_vendor.index.removeStorageSync("token");
- common_vendor.index.removeStorageSync("userInfo");
- this.isLogin = false;
- this.userInfo = {
- username: "",
- userId: "",
- avatar: ""
- };
- common_vendor.index.showToast({
- title: "已退出登录",
- icon: "success"
- });
- } catch (e) {
- console.error("退出登录失败", e);
- common_vendor.index.showToast({
- title: "退出登录失败",
- icon: "none"
- });
- }
- }
- }
- });
- }
- }
- };
- function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
- return common_vendor.e({
- a: $data.isLogin
- }, $data.isLogin ? {
- b: $data.userInfo.avatar || "/static/avatar.png",
- c: common_vendor.t($data.userInfo.username),
- d: common_vendor.t($data.userInfo.userId)
- } : {
- e: common_assets._imports_0,
- f: common_vendor.o((...args) => $options.goLogin && $options.goLogin(...args))
- }, {
- g: common_vendor.f($data.menuItems, (item, index, i0) => {
- return {
- a: common_vendor.n(item.icon),
- b: common_vendor.t(item.title),
- c: index,
- d: common_vendor.o(($event) => $options.handleMenuClick(item), index)
- };
- }),
- h: $data.isLogin
- }, $data.isLogin ? {
- i: common_vendor.o((...args) => $options.handleLogout && $options.handleLogout(...args))
- } : {});
- }
- const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
- wx.createPage(MiniProgramPage);
|