login.js 6.2 KB

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