上传代码
This commit is contained in:
172
uniapp04/unpackage/dist/dev/mp-alipay/pages/my/my.js
vendored
Normal file
172
uniapp04/unpackage/dist/dev/mp-alipay/pages/my/my.js
vendored
Normal file
@ -0,0 +1,172 @@
|
||||
"use strict";
|
||||
const common_vendor = require("../../common/vendor.js");
|
||||
const API_api = require("../../API/api.js");
|
||||
if (!Math) {
|
||||
common_vendor.unref(orderDateils)();
|
||||
}
|
||||
const orderDateils = () => "../orderDetaiols.js";
|
||||
const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent({
|
||||
__name: "my",
|
||||
setup(__props) {
|
||||
const getUserInfo = () => {
|
||||
my.getOpenUserInfo({
|
||||
fail: (err) => {
|
||||
console.error("【授权失败】错误详情:", JSON.stringify(err, null, 2));
|
||||
},
|
||||
success: (res) => {
|
||||
try {
|
||||
console.log("【原始响应数据】:", res);
|
||||
const response = typeof res.response === "string" ? JSON.parse(res.response) : res.response;
|
||||
if (response == null ? void 0 : response.response) {
|
||||
const userInfo = response.response;
|
||||
console.log("【用户信息解析成功】", userInfo);
|
||||
user.value.userName = userInfo.nickName || "普通用户";
|
||||
user.value.userAvatar = userInfo.avatar || "https://src.pcsoft.com.cn/d/file/soft/wlgj/wlgx/2017-04-27/20fccb4806f44d7bfafc2360b8934768.jpg";
|
||||
console.table({
|
||||
nickName: userInfo.nickName,
|
||||
avatar: userInfo.avatar,
|
||||
gender: userInfo.gender === "m" ? "男" : "女",
|
||||
city: userInfo.city,
|
||||
province: userInfo.province
|
||||
});
|
||||
if (true) {
|
||||
console.log("【完整用户信息】:", JSON.stringify(userInfo, null, 2));
|
||||
}
|
||||
} else {
|
||||
console.warn("【响应格式异常】", response);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("【解析失败】", e);
|
||||
console.log("【原始响应内容】:", res.response);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
common_vendor.onMounted(() => {
|
||||
getUserInfo();
|
||||
});
|
||||
common_vendor.onShow(() => {
|
||||
console.log("=== 我的页面显示,开始检查登录状态 ===");
|
||||
const userInfo = common_vendor.index.getStorageSync("userInfo");
|
||||
const cookie = common_vendor.index.getStorageSync("cookie");
|
||||
const sessionId = common_vendor.index.getStorageSync("sessionId");
|
||||
const isLoggedIn = common_vendor.index.getStorageSync("isLoggedIn");
|
||||
console.log("本地存储检查:");
|
||||
console.log("userInfo:", userInfo);
|
||||
console.log("cookie:", cookie);
|
||||
console.log("sessionId:", sessionId);
|
||||
console.log("isLoggedIn:", isLoggedIn);
|
||||
if (!cookie && !sessionId) {
|
||||
console.log("缺少认证信息,用户可能未登录");
|
||||
}
|
||||
getLoginUser();
|
||||
});
|
||||
const user = common_vendor.ref({
|
||||
userAvatar: "",
|
||||
userName: "",
|
||||
message: ""
|
||||
});
|
||||
const loginButtonText = common_vendor.ref(user.value.message === "ok" ? "退出登录" : "去登录");
|
||||
const getLoginUser = () => {
|
||||
const cookie = common_vendor.index.getStorageSync("cookie");
|
||||
const sessionId = common_vendor.index.getStorageSync("sessionId");
|
||||
console.log("发送请求前检查认证信息:");
|
||||
console.log("cookie:", cookie);
|
||||
console.log("sessionId:", sessionId);
|
||||
common_vendor.index.request({
|
||||
url: API_api.apiImageUrl + "/api/user/current",
|
||||
method: "GET",
|
||||
header: {
|
||||
"cookie": cookie || "",
|
||||
// 修正这里
|
||||
"sessionId": sessionId || ""
|
||||
// 添加sessionId作为备选
|
||||
},
|
||||
data: {},
|
||||
success: (res) => {
|
||||
console.log("获取用户信息响应:", res.data);
|
||||
user.value.message = res.data.message;
|
||||
if (user.value.message === "ok") {
|
||||
user.value.userName = res.data.data.username || user.value.userName;
|
||||
user.value.userAvatar = res.data.data.avatarUrl || user.value.userAvatar;
|
||||
loginButtonText.value = "退出登录";
|
||||
if (!user.value.userName || !user.value.userAvatar) {
|
||||
getUserInfo();
|
||||
}
|
||||
} else {
|
||||
console.log("用户未登录,原因:", res.data.message);
|
||||
loginButtonText.value = "去登录";
|
||||
}
|
||||
},
|
||||
fail(err) {
|
||||
console.log("获取用户信息失败:", err);
|
||||
}
|
||||
});
|
||||
};
|
||||
const handleLoginLogout = () => {
|
||||
if (user.value.message === "ok") {
|
||||
logout();
|
||||
} else {
|
||||
goToLogin();
|
||||
}
|
||||
};
|
||||
const goToLogin = () => {
|
||||
common_vendor.index.navigateTo({
|
||||
url: "/pages/login/login"
|
||||
});
|
||||
};
|
||||
const logout = () => {
|
||||
common_vendor.index.request({
|
||||
url: API_api.apiImageUrl + "/api/user/logout",
|
||||
method: "POST",
|
||||
header: {
|
||||
"cookie": common_vendor.index.getStorageSync("cookie") || ""
|
||||
},
|
||||
success(res) {
|
||||
console.log("退出登录响应:", res);
|
||||
common_vendor.index.removeStorageSync("identify");
|
||||
common_vendor.index.removeStorageSync("cookie");
|
||||
common_vendor.index.removeStorageSync("userInfo");
|
||||
common_vendor.index.removeStorageSync("isLoggedIn");
|
||||
common_vendor.index.removeStorageSync("sessionId");
|
||||
common_vendor.index.removeStorageSync("notPay");
|
||||
common_vendor.index.removeStorageSync("cartItems");
|
||||
user.value.message = "未登录";
|
||||
user.value.userName = "";
|
||||
user.value.userAvatar = "";
|
||||
loginButtonText.value = "去登录";
|
||||
common_vendor.index.reLaunch({
|
||||
url: "/pages/login/login"
|
||||
});
|
||||
common_vendor.index.showToast({
|
||||
title: "退出成功",
|
||||
duration: 2e3
|
||||
});
|
||||
},
|
||||
fail(err) {
|
||||
console.log("退出登录失败:", err);
|
||||
common_vendor.index.removeStorageSync("identify");
|
||||
common_vendor.index.removeStorageSync("cookie");
|
||||
common_vendor.index.removeStorageSync("userInfo");
|
||||
common_vendor.index.removeStorageSync("isLoggedIn");
|
||||
common_vendor.index.removeStorageSync("sessionId");
|
||||
user.value.message = "未登录";
|
||||
loginButtonText.value = "去登录";
|
||||
}
|
||||
});
|
||||
};
|
||||
return (_ctx, _cache) => {
|
||||
return common_vendor.e({
|
||||
a: user.value.message === "ok"
|
||||
}, user.value.message === "ok" ? {
|
||||
b: user.value.userAvatar || "https://默认头像.jpg",
|
||||
c: common_vendor.t(user.value.userName || "普通用户"),
|
||||
d: common_vendor.o(getUserInfo)
|
||||
} : {}, {
|
||||
e: common_vendor.t(loginButtonText.value),
|
||||
f: common_vendor.o(handleLoginLogout)
|
||||
});
|
||||
};
|
||||
}
|
||||
});
|
||||
my.createPage(_sfc_main);
|
Reference in New Issue
Block a user