login.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const api_user = require("../../api/user.js");
  4. const utils_userCache = require("../../utils/userCache.js");
  5. if (!Array) {
  6. const _easycom_uni_easyinput2 = common_vendor.resolveComponent("uni-easyinput");
  7. _easycom_uni_easyinput2();
  8. }
  9. const _easycom_uni_easyinput = () => "../../uni_modules/uni-easyinput/components/uni-easyinput/uni-easyinput.js";
  10. if (!Math) {
  11. _easycom_uni_easyinput();
  12. }
  13. const _sfc_main = {
  14. __name: "login",
  15. setup(__props) {
  16. const phoneNumber = common_vendor.ref("");
  17. const verifyCode = common_vendor.ref("");
  18. const countdown = common_vendor.ref(0);
  19. const isLoading = common_vendor.ref(false);
  20. let timer = null;
  21. const showUserAgreementPopup = common_vendor.ref(false);
  22. const showPrivacyPolicyPopup = common_vendor.ref(false);
  23. const getVerifyCode = async () => {
  24. if (countdown.value > 0)
  25. return;
  26. if (!phoneNumber.value || phoneNumber.value.length !== 11) {
  27. common_vendor.index.showToast({
  28. title: "请输入正确的手机号",
  29. icon: "none"
  30. });
  31. return;
  32. }
  33. try {
  34. const res = await api_user.sendVerifyCode(phoneNumber.value);
  35. common_vendor.index.__f__("log", "at pages/login/login.vue:298", "验证码发送成功:", res);
  36. countdown.value = 60;
  37. timer = setInterval(() => {
  38. countdown.value--;
  39. if (countdown.value <= 0) {
  40. clearInterval(timer);
  41. }
  42. }, 1e3);
  43. common_vendor.index.showToast({
  44. title: "验证码已发送",
  45. icon: "success"
  46. });
  47. } catch (error) {
  48. common_vendor.index.__f__("error", "at pages/login/login.vue:314", "发送验证码失败:", error);
  49. }
  50. };
  51. const handleLogin = async () => {
  52. if (isLoading.value)
  53. return;
  54. if (!phoneNumber.value || phoneNumber.value.length !== 11) {
  55. common_vendor.index.showToast({
  56. title: "请输入正确的手机号",
  57. icon: "none"
  58. });
  59. return;
  60. }
  61. if (!verifyCode.value || verifyCode.value.length !== 6) {
  62. common_vendor.index.showToast({
  63. title: "请输入验证码",
  64. icon: "none"
  65. });
  66. return;
  67. }
  68. isLoading.value = true;
  69. try {
  70. const res = await api_user.loginByPhone(phoneNumber.value, verifyCode.value);
  71. common_vendor.index.__f__("log", "at pages/login/login.vue:342", "登录成功:", res);
  72. const { userInfo, token } = res.data || res;
  73. const saveSuccess = utils_userCache.saveUserInfo(userInfo, token);
  74. if (saveSuccess) {
  75. common_vendor.index.showToast({
  76. title: "登录成功",
  77. icon: "success"
  78. });
  79. setTimeout(() => {
  80. common_vendor.index.reLaunch({
  81. url: "/pages/index/index"
  82. });
  83. }, 1500);
  84. } else {
  85. common_vendor.index.showToast({
  86. title: "保存登录信息失败",
  87. icon: "none"
  88. });
  89. }
  90. } catch (error) {
  91. common_vendor.index.__f__("error", "at pages/login/login.vue:367", "登录失败:", error);
  92. } finally {
  93. isLoading.value = false;
  94. }
  95. };
  96. const wechatLogin = () => {
  97. common_vendor.index.getUserProfile({
  98. desc: "用于完善用户资料",
  99. success: (res) => {
  100. common_vendor.index.__f__("log", "at pages/login/login.vue:379", "微信授权成功:", res.userInfo);
  101. common_vendor.index.showToast({
  102. title: "微信授权成功",
  103. icon: "success"
  104. });
  105. },
  106. fail: (err) => {
  107. common_vendor.index.__f__("log", "at pages/login/login.vue:387", "微信授权失败:", err);
  108. }
  109. });
  110. };
  111. const showUserAgreement = () => {
  112. showUserAgreementPopup.value = true;
  113. };
  114. const closeUserAgreement = () => {
  115. showUserAgreementPopup.value = false;
  116. };
  117. const showPrivacyPolicy = () => {
  118. showPrivacyPolicyPopup.value = true;
  119. };
  120. const closePrivacyPolicy = () => {
  121. showPrivacyPolicyPopup.value = false;
  122. };
  123. common_vendor.onUnmounted(() => {
  124. if (timer) {
  125. clearInterval(timer);
  126. }
  127. });
  128. return (_ctx, _cache) => {
  129. return common_vendor.e({
  130. a: common_vendor.o(($event) => phoneNumber.value = $event, "e1"),
  131. b: common_vendor.p({
  132. placeholder: "请输入手机号",
  133. type: "number",
  134. maxlength: "11",
  135. clearable: false,
  136. modelValue: phoneNumber.value
  137. }),
  138. c: common_vendor.o(($event) => verifyCode.value = $event, "57"),
  139. d: common_vendor.p({
  140. placeholder: "验证码",
  141. type: "number",
  142. maxlength: "6",
  143. clearable: false,
  144. modelValue: verifyCode.value
  145. }),
  146. e: common_vendor.t(countdown.value > 0 ? `${countdown.value}s` : "获取验证码"),
  147. f: countdown.value > 0 ? 1 : "",
  148. g: common_vendor.o(getVerifyCode, "ad"),
  149. h: common_vendor.o(handleLogin, "76"),
  150. i: isLoading.value,
  151. j: common_vendor.o(showUserAgreement, "62"),
  152. k: common_vendor.o(showPrivacyPolicy, "24"),
  153. l: common_vendor.o(wechatLogin, "c2"),
  154. m: showUserAgreementPopup.value
  155. }, showUserAgreementPopup.value ? {
  156. n: common_vendor.o(closeUserAgreement, "07"),
  157. o: common_vendor.o(closeUserAgreement, "0f"),
  158. p: common_vendor.o(() => {
  159. }, "80"),
  160. q: common_vendor.o(closeUserAgreement, "07")
  161. } : {}, {
  162. r: showPrivacyPolicyPopup.value
  163. }, showPrivacyPolicyPopup.value ? {
  164. s: common_vendor.o(closePrivacyPolicy, "ba"),
  165. t: common_vendor.o(closePrivacyPolicy, "6e"),
  166. v: common_vendor.o(() => {
  167. }, "28"),
  168. w: common_vendor.o(closePrivacyPolicy, "72")
  169. } : {});
  170. };
  171. }
  172. };
  173. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-e4e4508d"]]);
  174. wx.createPage(MiniProgramPage);
  175. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/login/login.js.map