index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const _sfc_main = {
  4. data() {
  5. return {
  6. loading: false,
  7. userInfo: null,
  8. rawData: null
  9. };
  10. },
  11. mounted() {
  12. this.initWechat();
  13. },
  14. methods: {
  15. initWechat() {
  16. common_vendor.index.__f__("log", "at pages/login/index.vue:39", "跳过依赖 WeixinJSBridge 的初始化");
  17. },
  18. handleGetUserProfile() {
  19. common_vendor.wx$1.showLoading({
  20. title: "获取信息中...",
  21. // 缩短提示文字
  22. mask: true
  23. });
  24. common_vendor.wx$1.getUserProfile({
  25. desc: "用于完善会员资料",
  26. success: (res) => {
  27. this.userInfo = res.userInfo;
  28. this.rawData = res.rawData;
  29. common_vendor.index.__f__("log", "at pages/login/index.vue:51", "获取到的 rawData:", this.rawData);
  30. common_vendor.wx$1.hideLoading();
  31. },
  32. fail: (err) => {
  33. common_vendor.index.__f__("error", "at pages/login/index.vue:55", "获取用户信息失败:", err);
  34. common_vendor.wx$1.showToast({ title: "请授权后再登录", icon: "none" });
  35. common_vendor.wx$1.hideLoading();
  36. }
  37. });
  38. },
  39. // 处理获取手机号事件
  40. handleGetPhoneNumber: async function(e) {
  41. if (e.detail.errMsg === "getPhoneNumber:ok") {
  42. if (!this.rawData) {
  43. common_vendor.index.__f__("error", "at pages/login/index.vue:65", "请先授权获取用户信息");
  44. common_vendor.wx$1.showToast({ title: "请先授权获取用户信息", icon: "none" });
  45. return;
  46. }
  47. try {
  48. common_vendor.wx$1.showLoading({
  49. title: "登录中...",
  50. // 缩短提示文字
  51. mask: true
  52. });
  53. this.loading = true;
  54. const encryptedData = e.detail.encryptedData;
  55. const iv = e.detail.iv;
  56. const { code } = await new Promise((resolve, reject) => {
  57. common_vendor.wx$1.login({
  58. success: (res) => resolve(res),
  59. fail: (err) => reject(err)
  60. });
  61. });
  62. const response = await new Promise((resolve, reject) => {
  63. common_vendor.wx$1.request({
  64. url: "http://localhost:8080/api/auth/wechat/login",
  65. method: "POST",
  66. data: {
  67. code,
  68. encryptedData,
  69. iv,
  70. rawData: this.rawData
  71. // 使用获取到的 rawData
  72. },
  73. success: (res) => resolve(res),
  74. fail: (err) => {
  75. if (err.errMsg.includes("request:fail url not in domain list")) {
  76. common_vendor.wx$1.showToast({
  77. title: "请求域名未配置,请联系管理员",
  78. icon: "none",
  79. duration: 3e3
  80. });
  81. }
  82. common_vendor.index.__f__("error", "at pages/login/index.vue:104", "网络请求失败:", err);
  83. reject(err);
  84. }
  85. });
  86. });
  87. common_vendor.index.__f__("log", "at pages/login/index.vue:110", "后端返回的完整数据:", response.data);
  88. if (response.data) {
  89. if (response.data.wxNickname && response.data.wxAvatarUrl) {
  90. common_vendor.wx$1.setStorageSync("userToken", response.data.token);
  91. common_vendor.wx$1.setStorageSync("userInfo", {
  92. userId: response.data.userId,
  93. wxNickname: response.data.wxNickname,
  94. wxAvatarUrl: response.data.wxAvatarUrl
  95. });
  96. common_vendor.wx$1.navigateTo({
  97. url: "/pages/home/index"
  98. });
  99. } else {
  100. common_vendor.index.__f__("error", "at pages/login/index.vue:124", "后端未返回微信昵称或头像信息:", response.data);
  101. common_vendor.wx$1.showToast({ title: "获取用户信息失败,请重试", icon: "none" });
  102. }
  103. } else {
  104. common_vendor.index.__f__("error", "at pages/login/index.vue:128", "登录响应数据为空:", response);
  105. common_vendor.wx$1.showToast({ title: "登录失败,请重试", icon: "none" });
  106. }
  107. } catch (error) {
  108. common_vendor.index.__f__("error", "at pages/login/index.vue:132", "登录过程出错:", error);
  109. common_vendor.wx$1.showToast({ title: "登录失败,请重试", icon: "none" });
  110. } finally {
  111. this.loading = false;
  112. common_vendor.wx$1.hideLoading();
  113. }
  114. } else {
  115. common_vendor.index.__f__("error", "at pages/login/index.vue:139", "用户拒绝授权", e.detail.errMsg);
  116. common_vendor.wx$1.showToast({ title: "请授权后再登录", icon: "none" });
  117. }
  118. }
  119. }
  120. };
  121. if (!Array) {
  122. const _component_uni_spinner = common_vendor.resolveComponent("uni-spinner");
  123. _component_uni_spinner();
  124. }
  125. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  126. return common_vendor.e({
  127. a: common_vendor.o((...args) => $options.handleGetPhoneNumber && $options.handleGetPhoneNumber(...args)),
  128. b: common_vendor.o((...args) => $options.handleGetUserProfile && $options.handleGetUserProfile(...args)),
  129. c: $data.loading
  130. }, $data.loading ? {
  131. d: common_vendor.p({
  132. type: "circular",
  133. color: "#07c160",
  134. size: "40"
  135. })
  136. } : {});
  137. }
  138. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-d08ef7d4"]]);
  139. wx.createPage(MiniProgramPage);
  140. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/login/index.js.map