index.js 5.4 KB

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