login.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const utils_api = require("../../utils/api.js");
  4. const utils_auth = require("../../utils/auth.js");
  5. const _sfc_main = {
  6. data() {
  7. return {
  8. phone: "",
  9. code: "",
  10. countdown: 0,
  11. agreed: false,
  12. canUseWxLogin: true,
  13. // 是否支持微信登录
  14. timer: null
  15. };
  16. },
  17. onLoad() {
  18. this.canUseWxLogin = true;
  19. },
  20. onUnload() {
  21. if (this.timer) {
  22. clearInterval(this.timer);
  23. }
  24. },
  25. methods: {
  26. /**
  27. * 处理微信一键登录
  28. * @param {object} e - 事件对象,包含加密的手机号信息
  29. */
  30. async handleWxLogin(e) {
  31. if (!this.agreed) {
  32. common_vendor.index.showToast({
  33. title: "请先同意用户协议和隐私政策",
  34. icon: "none"
  35. });
  36. return;
  37. }
  38. if (e.detail.errMsg === "getPhoneNumber:ok") {
  39. try {
  40. common_vendor.index.showLoading({ title: "登录中..." });
  41. const loginRes = await common_vendor.index.login();
  42. const result = await utils_api.wxLogin(loginRes.code);
  43. utils_auth.setToken(result.data.token);
  44. utils_auth.setUserInfo(result.data.userInfo);
  45. common_vendor.index.hideLoading();
  46. common_vendor.index.showToast({
  47. title: "登录成功",
  48. icon: "success"
  49. });
  50. setTimeout(() => {
  51. this.navigateBack();
  52. }, 1500);
  53. } catch (error) {
  54. common_vendor.index.hideLoading();
  55. common_vendor.index.showToast({
  56. title: error.message || "登录失败",
  57. icon: "none"
  58. });
  59. }
  60. } else {
  61. common_vendor.index.showToast({
  62. title: "获取手机号失败",
  63. icon: "none"
  64. });
  65. }
  66. },
  67. /**
  68. * 发送验证码
  69. */
  70. async sendCode() {
  71. if (!this.phone) {
  72. common_vendor.index.showToast({
  73. title: "请输入手机号",
  74. icon: "none"
  75. });
  76. return;
  77. }
  78. if (!/^1[3-9]\d{9}$/.test(this.phone)) {
  79. common_vendor.index.showToast({
  80. title: "请输入正确的手机号",
  81. icon: "none"
  82. });
  83. return;
  84. }
  85. if (!this.agreed) {
  86. common_vendor.index.showToast({
  87. title: "请先同意用户协议和隐私政策",
  88. icon: "none"
  89. });
  90. return;
  91. }
  92. try {
  93. common_vendor.index.showLoading({ title: "发送中..." });
  94. await utils_api.sendSmsCode(this.phone);
  95. common_vendor.index.hideLoading();
  96. common_vendor.index.showToast({
  97. title: "验证码已发送",
  98. icon: "success"
  99. });
  100. this.countdown = 60;
  101. this.timer = setInterval(() => {
  102. this.countdown--;
  103. if (this.countdown <= 0) {
  104. clearInterval(this.timer);
  105. }
  106. }, 1e3);
  107. } catch (error) {
  108. common_vendor.index.hideLoading();
  109. common_vendor.index.showToast({
  110. title: error.message || "发送失败",
  111. icon: "none"
  112. });
  113. }
  114. },
  115. /**
  116. * 处理手机号登录
  117. */
  118. async handlePhoneLogin() {
  119. if (!this.phone) {
  120. common_vendor.index.showToast({
  121. title: "请输入手机号",
  122. icon: "none"
  123. });
  124. return;
  125. }
  126. if (!/^1[3-9]\d{9}$/.test(this.phone)) {
  127. common_vendor.index.showToast({
  128. title: "请输入正确的手机号",
  129. icon: "none"
  130. });
  131. return;
  132. }
  133. if (!this.code) {
  134. common_vendor.index.showToast({
  135. title: "请输入验证码",
  136. icon: "none"
  137. });
  138. return;
  139. }
  140. if (!this.agreed) {
  141. common_vendor.index.showToast({
  142. title: "请先同意用户协议和隐私政策",
  143. icon: "none"
  144. });
  145. return;
  146. }
  147. try {
  148. common_vendor.index.showLoading({ title: "登录中..." });
  149. console.log("准备登录,phone:", this.phone, "code:", this.code);
  150. const result = await utils_api.phoneLogin(this.phone, this.code);
  151. console.log("登录成功,result:", result);
  152. utils_auth.setToken(result.data.token);
  153. utils_auth.setUserInfo(result.data.userInfo);
  154. utils_auth.setUserInfo(result.data.userInfo);
  155. common_vendor.index.hideLoading();
  156. common_vendor.index.showToast({
  157. title: "登录成功",
  158. icon: "success"
  159. });
  160. setTimeout(() => {
  161. this.navigateBack();
  162. }, 1500);
  163. } catch (error) {
  164. common_vendor.index.hideLoading();
  165. common_vendor.index.showToast({
  166. title: error.message || "登录失败",
  167. icon: "none"
  168. });
  169. }
  170. },
  171. /**
  172. * 处理协议勾选变化
  173. */
  174. handleAgreeChange(e) {
  175. this.agreed = e.detail.value.length > 0;
  176. },
  177. /**
  178. * 返回上一页或首页
  179. */
  180. navigateBack() {
  181. const pages = getCurrentPages();
  182. if (pages.length > 1) {
  183. common_vendor.index.navigateBack();
  184. const app = getApp();
  185. if (app.globalData.loginCallback) {
  186. app.globalData.loginCallback();
  187. app.globalData.loginCallback = null;
  188. }
  189. } else {
  190. common_vendor.index.switchTab({
  191. url: "/pages/index/index"
  192. });
  193. }
  194. }
  195. }
  196. };
  197. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  198. return common_vendor.e({
  199. a: $data.canUseWxLogin
  200. }, $data.canUseWxLogin ? {
  201. b: common_vendor.o((...args) => $options.handleWxLogin && $options.handleWxLogin(...args))
  202. } : {}, {
  203. c: $data.phone,
  204. d: common_vendor.o(($event) => $data.phone = $event.detail.value),
  205. e: $data.code,
  206. f: common_vendor.o(($event) => $data.code = $event.detail.value),
  207. g: common_vendor.t($data.countdown > 0 ? `${$data.countdown}秒后重试` : "获取验证码"),
  208. h: common_vendor.o((...args) => $options.sendCode && $options.sendCode(...args)),
  209. i: $data.countdown > 0,
  210. j: common_vendor.o((...args) => $options.handlePhoneLogin && $options.handlePhoneLogin(...args)),
  211. k: $data.agreed,
  212. l: common_vendor.o((...args) => $options.handleAgreeChange && $options.handleAgreeChange(...args))
  213. });
  214. }
  215. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-cdfe2409"], ["__file", "D:/program/gupiao-wx/src/pages/login/login.vue"]]);
  216. wx.createPage(MiniProgramPage);