index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. isAgreed: false,
  8. showServiceAgreement: false,
  9. showPrivacyPolicy: false
  10. };
  11. },
  12. methods: {
  13. // 处理协议勾选
  14. handleAgreementChange(e) {
  15. this.isAgreed = e.detail.value.length > 0;
  16. },
  17. // 微信登录
  18. async handleWechatLogin() {
  19. if (!this.isAgreed) {
  20. common_vendor.index.showToast({ title: "请先同意用户协议和隐私政策", icon: "none" });
  21. return;
  22. }
  23. try {
  24. const loginResult = await new Promise((resolve, reject) => {
  25. common_vendor.index.login({
  26. provider: "weixin",
  27. success: (res) => resolve(res),
  28. fail: (err) => reject(err)
  29. });
  30. });
  31. if (!loginResult.code) {
  32. common_vendor.index.showToast({ title: "获取登录凭证失败", icon: "none" });
  33. return;
  34. }
  35. common_vendor.index.__f__("log", "at pages/login/index.vue:88", "获取到的code:", loginResult.code);
  36. const userInfoResult = await new Promise((resolve, reject) => {
  37. common_vendor.index.getUserInfo({
  38. provider: "weixin",
  39. withCredentials: true,
  40. success: (res) => resolve(res),
  41. fail: (err) => reject(err)
  42. });
  43. });
  44. if (!userInfoResult.userInfo) {
  45. common_vendor.index.showToast({ title: "获取用户信息失败", icon: "none" });
  46. return;
  47. }
  48. const loginRes = await common_vendor.index.request({
  49. url: "http://localhost:9527/api/wx-login",
  50. method: "POST",
  51. data: {
  52. code: loginResult.code,
  53. nickName: userInfoResult.userInfo.nickName,
  54. avatarUrl: userInfoResult.userInfo.avatarUrl,
  55. gender: userInfoResult.userInfo.gender.toString(),
  56. country: userInfoResult.userInfo.country,
  57. province: userInfoResult.userInfo.province,
  58. city: userInfoResult.userInfo.city,
  59. language: userInfoResult.userInfo.language,
  60. rawData: userInfoResult.rawData,
  61. signature: userInfoResult.signature,
  62. encryptedData: userInfoResult.encryptedData,
  63. iv: userInfoResult.iv
  64. },
  65. header: {
  66. "content-type": "application/json"
  67. }
  68. });
  69. if (loginRes.data.errcode === 40029) {
  70. common_vendor.index.__f__("error", "at pages/login/index.vue:130", "微信登录code无效,尝试重新获取");
  71. const newLoginResult = await new Promise((resolve, reject) => {
  72. common_vendor.index.login({
  73. provider: "weixin",
  74. success: (res) => resolve(res),
  75. fail: (err) => reject(err)
  76. });
  77. });
  78. if (!newLoginResult.code) {
  79. throw new Error("重新获取登录凭证失败");
  80. }
  81. const retryRes = await common_vendor.index.request({
  82. url: "http://localhost:9527/api/wx-login",
  83. method: "POST",
  84. data: {
  85. code: newLoginResult.code,
  86. nickName: userInfoResult.userInfo.nickName,
  87. avatarUrl: userInfoResult.userInfo.avatarUrl,
  88. gender: userInfoResult.userInfo.gender.toString(),
  89. country: userInfoResult.userInfo.country,
  90. province: userInfoResult.userInfo.province,
  91. city: userInfoResult.userInfo.city,
  92. language: userInfoResult.userInfo.language,
  93. rawData: userInfoResult.rawData,
  94. signature: userInfoResult.signature,
  95. encryptedData: userInfoResult.encryptedData,
  96. iv: userInfoResult.iv
  97. },
  98. header: {
  99. "content-type": "application/json"
  100. }
  101. });
  102. if (retryRes.statusCode === 200 && !retryRes.data.errcode) {
  103. common_vendor.index.setStorageSync("token", retryRes.data.data.token);
  104. common_vendor.index.setStorageSync("userInfo", retryRes.data.data.userInfo);
  105. common_vendor.index.switchTab({
  106. url: "/pages/discover/index"
  107. });
  108. } else {
  109. throw new Error(retryRes.data.errmsg || "登录失败");
  110. }
  111. } else if (loginRes.statusCode === 200 && !loginRes.data.errcode) {
  112. common_vendor.index.setStorageSync("token", loginRes.data.data.token);
  113. common_vendor.index.setStorageSync("userInfo", loginRes.data.data.userInfo);
  114. common_vendor.index.switchTab({
  115. url: "/pages/discover/index"
  116. });
  117. } else {
  118. throw new Error(loginRes.data.errmsg || "登录失败");
  119. }
  120. } catch (error) {
  121. common_vendor.index.__f__("error", "at pages/login/index.vue:192", "登录过程出错:", error);
  122. common_vendor.index.showToast({
  123. title: error.message || "登录失败,请重试",
  124. icon: "none"
  125. });
  126. }
  127. },
  128. // 跳转到验证码登录页面
  129. navigateToVerifyCodeLogin() {
  130. common_vendor.index.navigateTo({
  131. url: "/pages/login/verify-code"
  132. });
  133. },
  134. // 打开服务协议
  135. openServiceAgreement() {
  136. common_vendor.index.navigateTo({
  137. url: "/pages/agreement/service"
  138. });
  139. },
  140. // 打开隐私政策
  141. openPrivacyPolicy() {
  142. common_vendor.index.navigateTo({
  143. url: "/pages/agreement/privacy"
  144. });
  145. }
  146. }
  147. };
  148. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  149. return {
  150. a: common_assets._imports_0,
  151. b: common_vendor.o((...args) => $options.handleWechatLogin && $options.handleWechatLogin(...args)),
  152. c: common_vendor.o((...args) => $options.navigateToVerifyCodeLogin && $options.navigateToVerifyCodeLogin(...args)),
  153. d: $data.isAgreed,
  154. e: common_vendor.o((...args) => $options.openServiceAgreement && $options.openServiceAgreement(...args)),
  155. f: common_vendor.o((...args) => $options.openPrivacyPolicy && $options.openPrivacyPolicy(...args)),
  156. g: common_vendor.o((...args) => $options.handleAgreementChange && $options.handleAgreementChange(...args))
  157. };
  158. }
  159. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  160. wx.createPage(MiniProgramPage);
  161. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/login/index.js.map