verify-code.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const _sfc_main = {
  4. data() {
  5. return {
  6. loginType: "code",
  7. phone: "",
  8. verifyCode: "",
  9. account: "",
  10. password: "",
  11. showPassword: false,
  12. counting: false,
  13. countdown: 60,
  14. isAgreed: false,
  15. showServiceAgreement: false,
  16. showPrivacyPolicy: false
  17. };
  18. },
  19. computed: {
  20. isFormValid() {
  21. if (!this.isAgreed)
  22. return false;
  23. if (this.loginType === "code") {
  24. return this.phone.length === 11 && this.verifyCode.length === 6;
  25. } else {
  26. return this.account.length > 0 && this.password.length >= 6;
  27. }
  28. }
  29. },
  30. methods: {
  31. handleAgreementChange(e) {
  32. this.isAgreed = e.detail.value.length > 0;
  33. },
  34. async sendVerifyCode() {
  35. if (this.counting)
  36. return;
  37. if (this.phone.length !== 11) {
  38. common_vendor.index.showToast({ title: "请输入正确的手机号", icon: "none" });
  39. return;
  40. }
  41. try {
  42. const res = await common_vendor.index.request({
  43. url: "http://localhost:9527/api/getCode",
  44. method: "POST",
  45. data: {
  46. phone: this.phone
  47. },
  48. header: {
  49. "content-type": "application/json"
  50. }
  51. });
  52. common_vendor.index.__f__("log", "at pages/login/verify-code.vue:122", res.statusCode);
  53. if (res.statusCode === 200) {
  54. common_vendor.index.__f__("log", "at pages/login/verify-code.vue:124", "验证码接口返回:", res);
  55. common_vendor.index.showToast({
  56. title: "验证码已发送",
  57. icon: "success"
  58. });
  59. this.counting = true;
  60. this.countdown = 60;
  61. this.startCountdown();
  62. } else {
  63. throw new Error(res.data.msg || "获取验证码失败");
  64. }
  65. } catch (error) {
  66. common_vendor.index.__f__("error", "at pages/login/verify-code.vue:137", "获取验证码失败:", error);
  67. common_vendor.index.showToast({
  68. title: error.message || "获取验证码失败,请重试",
  69. icon: "none"
  70. });
  71. }
  72. },
  73. startCountdown() {
  74. const timer = setInterval(() => {
  75. if (this.countdown > 0) {
  76. this.countdown--;
  77. } else {
  78. this.counting = false;
  79. clearInterval(timer);
  80. }
  81. }, 1e3);
  82. },
  83. async handleLogin() {
  84. if (!this.isFormValid)
  85. return;
  86. if (!this.isAgreed) {
  87. common_vendor.index.showToast({ title: "请先同意服务协议和隐私政策", icon: "none" });
  88. return;
  89. }
  90. const res = await common_vendor.index.request({
  91. url: "http://localhost:9527/api/login",
  92. method: "POST",
  93. data: {
  94. phone: this.phone,
  95. password: this.password,
  96. code: this.verifyCode
  97. },
  98. header: {
  99. "content-type": "application/json"
  100. }
  101. });
  102. common_vendor.index.__f__("log", "at pages/login/verify-code.vue:173", res.data);
  103. if (res.data.code === 200) {
  104. common_vendor.index.setStorageSync("token", res.data.data.token);
  105. common_vendor.index.switchTab({
  106. url: "/pages/discover/index"
  107. // 替换为实际页面路径
  108. });
  109. } else {
  110. common_vendor.index.showToast({
  111. title: "登录失败",
  112. icon: "none"
  113. });
  114. }
  115. },
  116. closePopup() {
  117. this.showServiceAgreement = false;
  118. this.showPrivacyPolicy = false;
  119. },
  120. goToWechatLogin() {
  121. common_vendor.index.navigateTo({
  122. url: "/pages/login/index"
  123. });
  124. }
  125. }
  126. };
  127. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  128. return common_vendor.e({
  129. a: common_vendor.n($data.loginType === "code" ? "active" : ""),
  130. b: common_vendor.o(($event) => $data.loginType = "code"),
  131. c: common_vendor.n($data.loginType === "password" ? "active" : ""),
  132. d: common_vendor.o(($event) => $data.loginType = "password"),
  133. e: $data.loginType === "code"
  134. }, $data.loginType === "code" ? {
  135. f: $data.phone,
  136. g: common_vendor.o(($event) => $data.phone = $event.detail.value),
  137. h: $data.verifyCode,
  138. i: common_vendor.o(($event) => $data.verifyCode = $event.detail.value),
  139. j: common_vendor.t($data.counting ? `${$data.countdown}s后重发` : "获取验证码"),
  140. k: $data.counting ? 1 : "",
  141. l: common_vendor.o((...args) => $options.sendVerifyCode && $options.sendVerifyCode(...args))
  142. } : {
  143. m: $data.account,
  144. n: common_vendor.o(($event) => $data.account = $event.detail.value),
  145. o: $data.showPassword ? "text" : "password",
  146. p: $data.password,
  147. q: common_vendor.o(($event) => $data.password = $event.detail.value),
  148. r: common_vendor.n($data.showPassword ? "icon-eye-open" : "icon-eye-close"),
  149. s: common_vendor.o(($event) => $data.showPassword = !$data.showPassword)
  150. }, {
  151. t: common_vendor.o((...args) => $options.goToWechatLogin && $options.goToWechatLogin(...args)),
  152. v: !$options.isFormValid,
  153. w: common_vendor.o((...args) => $options.handleLogin && $options.handleLogin(...args)),
  154. x: $data.isAgreed,
  155. y: common_vendor.o(($event) => $data.showServiceAgreement = true),
  156. z: common_vendor.o(($event) => $data.showPrivacyPolicy = true),
  157. A: common_vendor.o((...args) => $options.handleAgreementChange && $options.handleAgreementChange(...args))
  158. });
  159. }
  160. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  161. wx.createPage(MiniProgramPage);
  162. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/login/verify-code.js.map