my.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const common_assets = require("../../common/assets.js");
  4. const _sfc_main = {
  5. data() {
  6. return {
  7. isLogin: false,
  8. userInfo: {
  9. username: "",
  10. userId: "",
  11. avatar: ""
  12. },
  13. menuItems: [
  14. { title: "个人资料", icon: "icon-user", action: "profile" },
  15. // { title: '我的订单', icon: 'icon-order', action: 'orders' },
  16. // { title: '我的收藏', icon: 'icon-star', action: 'favorites' },
  17. { title: "设置", icon: "icon-settings", action: "settings" },
  18. { title: "帮助中心", icon: "icon-help", action: "help" }
  19. ]
  20. };
  21. },
  22. onShow() {
  23. this.checkLoginStatus();
  24. },
  25. methods: {
  26. // 检查登录状态
  27. checkLoginStatus() {
  28. try {
  29. const token = common_vendor.index.getStorageSync("token");
  30. const userInfo = common_vendor.index.getStorageSync("userInfo");
  31. if (token && userInfo) {
  32. this.isLogin = true;
  33. this.userInfo = JSON.parse(userInfo);
  34. } else {
  35. this.isLogin = false;
  36. this.userInfo = {
  37. username: "",
  38. userId: "",
  39. avatar: ""
  40. };
  41. }
  42. } catch (e) {
  43. console.error("获取登录状态失败", e);
  44. this.isLogin = false;
  45. }
  46. },
  47. // 前往登录页
  48. goLogin() {
  49. common_vendor.index.showActionSheet({
  50. itemList: ["账号密码登录", "微信登录"],
  51. success: (res) => {
  52. if (res.tapIndex === 0) {
  53. common_vendor.index.navigateTo({
  54. url: "/pages/login/login"
  55. });
  56. } else if (res.tapIndex === 1) {
  57. this.wxLogin();
  58. }
  59. }
  60. });
  61. },
  62. // 微信登录
  63. wxLogin() {
  64. common_vendor.index.showLoading({
  65. title: "登录中..."
  66. });
  67. common_vendor.index.login({
  68. provider: "weixin",
  69. success: (loginRes) => {
  70. this.wxLoginRequest(loginRes.code);
  71. this.getUserProfileNew();
  72. },
  73. fail: (err) => {
  74. common_vendor.index.hideLoading();
  75. common_vendor.index.showToast({
  76. title: "微信登录失败",
  77. icon: "none"
  78. });
  79. console.error("微信登录失败", err);
  80. }
  81. });
  82. },
  83. // 使用新的方式获取用户头像和昵称
  84. getUserProfileNew() {
  85. common_vendor.wx$1.getUserInfo({
  86. desc: "用于完善用户资料",
  87. success: (res) => {
  88. console.log("获取用户信息成功", res);
  89. },
  90. fail: (err) => {
  91. console.error("获取用户信息失败", err);
  92. }
  93. });
  94. },
  95. // 头像选择回调
  96. onChooseAvatar(e) {
  97. const { avatarUrl } = e.detail;
  98. console.log("用户选择的头像:", avatarUrl);
  99. },
  100. // 发送微信登录请求到服务器
  101. wxLoginRequest(code, userInfo = {}) {
  102. common_vendor.index.request({
  103. url: "https://your-api-domain.com/api/wx/login",
  104. method: "POST",
  105. data: {
  106. code
  107. // 不再依赖 getUserProfile 获取的信息
  108. // 如果有用户选择的头像,可以在这里传递
  109. },
  110. success: (res) => {
  111. common_vendor.index.hideLoading();
  112. if (res.data.code === 0) {
  113. const userData = {
  114. username: res.data.data.username || "微信用户",
  115. userId: res.data.data.userId,
  116. avatar: res.data.data.avatar || "/static/avatar.png"
  117. };
  118. try {
  119. common_vendor.index.setStorageSync("token", res.data.data.token);
  120. common_vendor.index.setStorageSync("userInfo", JSON.stringify(userData));
  121. this.isLogin = true;
  122. this.userInfo = userData;
  123. common_vendor.index.showToast({
  124. title: "登录成功",
  125. icon: "success"
  126. });
  127. } catch (e) {
  128. console.error("保存登录信息失败", e);
  129. }
  130. } else {
  131. common_vendor.index.showToast({
  132. title: res.data.msg || "登录失败",
  133. icon: "none"
  134. });
  135. }
  136. },
  137. fail: (err) => {
  138. common_vendor.index.hideLoading();
  139. common_vendor.index.showToast({
  140. title: "网络请求失败",
  141. icon: "none"
  142. });
  143. console.error("微信登录请求失败", err);
  144. }
  145. });
  146. },
  147. handleMenuClick(item) {
  148. const needLogin = ["profile", "orders", "favorites"];
  149. if (needLogin.includes(item.action) && !this.isLogin) {
  150. common_vendor.index.showToast({
  151. title: "请先登录",
  152. icon: "none"
  153. });
  154. setTimeout(() => {
  155. this.goLogin();
  156. }, 1500);
  157. return;
  158. }
  159. switch (item.action) {
  160. case "profile":
  161. common_vendor.index.navigateTo({ url: "/pages/profile/profile" });
  162. break;
  163. case "orders":
  164. common_vendor.index.navigateTo({ url: "/pages/orders/orders" });
  165. break;
  166. case "favorites":
  167. common_vendor.index.navigateTo({ url: "/pages/favorites/favorites" });
  168. break;
  169. case "settings":
  170. common_vendor.index.navigateTo({ url: "/pages/settings/settings" });
  171. break;
  172. case "help":
  173. common_vendor.index.navigateTo({ url: "/pages/help/help" });
  174. break;
  175. default:
  176. common_vendor.index.showToast({
  177. title: `点击了${item.title}`,
  178. icon: "none"
  179. });
  180. }
  181. },
  182. handleLogout() {
  183. common_vendor.index.showModal({
  184. title: "提示",
  185. content: "确定要退出登录吗?",
  186. success: (res) => {
  187. if (res.confirm) {
  188. try {
  189. common_vendor.index.removeStorageSync("token");
  190. common_vendor.index.removeStorageSync("userInfo");
  191. this.isLogin = false;
  192. this.userInfo = {
  193. username: "",
  194. userId: "",
  195. avatar: ""
  196. };
  197. common_vendor.index.showToast({
  198. title: "已退出登录",
  199. icon: "success"
  200. });
  201. } catch (e) {
  202. console.error("退出登录失败", e);
  203. common_vendor.index.showToast({
  204. title: "退出登录失败",
  205. icon: "none"
  206. });
  207. }
  208. }
  209. }
  210. });
  211. }
  212. }
  213. };
  214. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  215. return common_vendor.e({
  216. a: $data.isLogin
  217. }, $data.isLogin ? {
  218. b: $data.userInfo.avatar || "/static/avatar.png",
  219. c: common_vendor.t($data.userInfo.username),
  220. d: common_vendor.t($data.userInfo.userId)
  221. } : {
  222. e: common_assets._imports_0,
  223. f: common_vendor.o((...args) => $options.goLogin && $options.goLogin(...args))
  224. }, {
  225. g: common_vendor.f($data.menuItems, (item, index, i0) => {
  226. return {
  227. a: common_vendor.n(item.icon),
  228. b: common_vendor.t(item.title),
  229. c: index,
  230. d: common_vendor.o(($event) => $options.handleMenuClick(item), index)
  231. };
  232. }),
  233. h: $data.isLogin
  234. }, $data.isLogin ? {
  235. i: common_vendor.o((...args) => $options.handleLogout && $options.handleLogout(...args))
  236. } : {});
  237. }
  238. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  239. wx.createPage(MiniProgramPage);