login.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. loginType: "phone",
  8. // 'phone' or 'account'
  9. phone: "",
  10. code: "",
  11. username: "",
  12. password: "",
  13. agreed: false,
  14. showRegisterPopup: false,
  15. regPhone: "",
  16. regCode: "",
  17. regUsername: "",
  18. regPassword: "",
  19. regCodeTimer: 0,
  20. // Countdown timer for registration code
  21. isSendingRegCode: false,
  22. // Flag to prevent multiple requests
  23. loginCodeTimer: 0,
  24. // Countdown timer for login code
  25. isSendingLoginCode: false
  26. // Flag to prevent multiple requests
  27. };
  28. },
  29. methods: {
  30. // 处理微信登录
  31. async oneClickLogin() {
  32. common_vendor.index.__f__("log", "at pages/login/login.vue:92", "WeChat login button clicked");
  33. common_vendor.index.getUserProfile({
  34. desc: "用于完善用户资料",
  35. lang: "zh_CN",
  36. success: (userRes) => {
  37. common_vendor.index.__f__("log", "at pages/login/login.vue:98", userRes);
  38. common_vendor.index.showLoading({ title: "登录中...", mask: true });
  39. common_vendor.index.login({
  40. provider: "weixin",
  41. success: (wx_res) => {
  42. common_vendor.index.__f__("log", "at pages/login/login.vue:105", "uni.getUserProfile success:", userRes);
  43. common_vendor.index.__f__("log", "at pages/login/login.vue:106", "uni.login success, got code:", wx_res.code);
  44. common_vendor.index.request({
  45. url: "http://localhost:3333/WeChart/login",
  46. method: "POST",
  47. data: {
  48. code: wx_res.code,
  49. weChatLoginDto: userRes.userInfo
  50. },
  51. header: { "content-type": "application/json" },
  52. success: (res) => {
  53. var _a;
  54. common_vendor.index.hideLoading();
  55. if (res.statusCode === 200 && res.data) {
  56. common_vendor.index.__f__("log", "at pages/login/login.vue:119", "Backend login success:", res.data);
  57. common_vendor.index.showToast({ title: "登录成功", icon: "success" });
  58. setTimeout(() => {
  59. common_vendor.index.switchTab({ url: "/pages/home/index" });
  60. }, 1500);
  61. } else {
  62. common_vendor.index.__f__("error", "at pages/login/login.vue:125", "Backend login failed:", res);
  63. common_vendor.index.showToast({ title: ((_a = res.data) == null ? void 0 : _a.message) || "登录失败", icon: "none" });
  64. }
  65. },
  66. fail: (err) => {
  67. common_vendor.index.hideLoading();
  68. common_vendor.index.__f__("error", "at pages/login/login.vue:131", "Backend request failed:", err);
  69. common_vendor.index.showToast({ title: "微信登录失败", icon: "none" });
  70. }
  71. });
  72. },
  73. fail: (err) => {
  74. common_vendor.index.hideLoading();
  75. common_vendor.index.__f__("error", "at pages/login/login.vue:138", "uni.login failed:", err);
  76. common_vendor.index.showToast({ title: "微信登录失败", icon: "none" });
  77. }
  78. });
  79. },
  80. fail: (err) => {
  81. common_vendor.index.__f__("error", "at pages/login/login.vue:144", "uni.getUserProfile failed:", err);
  82. common_vendor.index.showToast({ title: "获取用户信息失败", icon: "none" });
  83. }
  84. });
  85. },
  86. async sendCode() {
  87. var _a;
  88. if (this.isSendingLoginCode || this.loginCodeTimer > 0) {
  89. return;
  90. }
  91. if (!this.phone) {
  92. common_vendor.index.showToast({ title: "请输入手机号", icon: "none" });
  93. return;
  94. }
  95. this.isSendingLoginCode = true;
  96. common_vendor.index.showLoading({ title: "发送中...", mask: true });
  97. try {
  98. const res = await common_vendor.index.request({
  99. url: "http://localhost:3333/user/code",
  100. method: "POST",
  101. data: {
  102. phone: this.phone
  103. },
  104. header: { "content-type": "application/json" }
  105. });
  106. common_vendor.index.hideLoading();
  107. this.isSendingLoginCode = false;
  108. if (res.statusCode === 200 && res.data) {
  109. common_vendor.index.__f__("log", "at pages/login/login.vue:175", "Send login code success:", res.data);
  110. common_vendor.index.showToast({ title: "验证码已发送", icon: "success" });
  111. this.loginCodeTimer = 60;
  112. const timerInterval = setInterval(() => {
  113. this.loginCodeTimer--;
  114. if (this.loginCodeTimer <= 0) {
  115. clearInterval(timerInterval);
  116. this.loginCodeTimer = 0;
  117. }
  118. }, 1e3);
  119. } else {
  120. common_vendor.index.__f__("error", "at pages/login/login.vue:189", "Send login code failed:", res);
  121. common_vendor.index.showToast({ title: ((_a = res.data) == null ? void 0 : _a.message) || "发送失败", icon: "none" });
  122. }
  123. } catch (err) {
  124. common_vendor.index.hideLoading();
  125. this.isSendingLoginCode = false;
  126. common_vendor.index.__f__("error", "at pages/login/login.vue:195", "Send login code request failed:", err);
  127. common_vendor.index.showToast({ title: "发送失败", icon: "none" });
  128. }
  129. },
  130. async doLogin() {
  131. var _a;
  132. if (!this.agreed) {
  133. common_vendor.index.showToast({ title: "请先同意协议", icon: "none" });
  134. return;
  135. }
  136. if (!this.phone || !this.code) {
  137. common_vendor.index.showToast({ title: "请填写手机号和验证码", icon: "none" });
  138. return;
  139. }
  140. common_vendor.index.showLoading({ title: "登录中...", mask: true });
  141. try {
  142. const res = await common_vendor.index.request({
  143. url: "http://localhost:3333/user/login",
  144. method: "POST",
  145. data: {
  146. phone: this.phone,
  147. code: this.code
  148. },
  149. header: { "content-type": "application/json" }
  150. });
  151. common_vendor.index.hideLoading();
  152. if (res.statusCode === 200 && res.data) {
  153. common_vendor.index.__f__("log", "at pages/login/login.vue:225", "Login success:", res.data);
  154. common_vendor.index.showToast({ title: "登录成功", icon: "success" });
  155. setTimeout(() => {
  156. common_vendor.index.switchTab({ url: "/pages/home/index" });
  157. }, 1500);
  158. } else {
  159. common_vendor.index.__f__("error", "at pages/login/login.vue:232", "Login failed:", res.data);
  160. common_vendor.index.showToast({ title: ((_a = res.data) == null ? void 0 : _a.message) || "登录失败", icon: "error" });
  161. }
  162. } catch (err) {
  163. common_vendor.index.hideLoading();
  164. common_vendor.index.__f__("error", "at pages/login/login.vue:237", "Login request failed:", err);
  165. common_vendor.index.showToast({ title: "登录失败", icon: "none" });
  166. }
  167. },
  168. toRegister() {
  169. this.showRegisterPopup = true;
  170. },
  171. toForgot() {
  172. common_vendor.index.showToast({ title: "跳转找回密码", icon: "none" });
  173. },
  174. openAgreement() {
  175. common_vendor.index.navigateTo({ url: "/pages/agreement/agreement" });
  176. },
  177. openPrivacy() {
  178. common_vendor.index.navigateTo({ url: "/pages/privacy/privacy" });
  179. },
  180. qqLogin() {
  181. common_vendor.index.showToast({ title: "QQ登录", icon: "none" });
  182. },
  183. async sendRegCode() {
  184. var _a;
  185. if (this.isSendingRegCode || this.regCodeTimer > 0) {
  186. return;
  187. }
  188. if (!this.regPhone) {
  189. common_vendor.index.showToast({ title: "请输入手机号", icon: "none" });
  190. return;
  191. }
  192. this.isSendingRegCode = true;
  193. common_vendor.index.showLoading({ title: "发送中...", mask: true });
  194. try {
  195. const res = await common_vendor.index.request({
  196. url: "http://localhost:3333/user/code",
  197. method: "POST",
  198. data: {
  199. phone: this.regPhone
  200. // 发送手机号参数
  201. },
  202. header: { "content-type": "application/json" }
  203. });
  204. common_vendor.index.hideLoading();
  205. this.isSendingRegCode = false;
  206. if (res.statusCode === 200 && res.data) {
  207. common_vendor.index.__f__("log", "at pages/login/login.vue:288", "Send registration code success:", res.data);
  208. common_vendor.index.showToast({ title: "验证码已发送", icon: "success" });
  209. this.regCodeTimer = 60;
  210. const timerInterval = setInterval(() => {
  211. this.regCodeTimer--;
  212. if (this.regCodeTimer <= 0) {
  213. clearInterval(timerInterval);
  214. this.regCodeTimer = 0;
  215. }
  216. }, 1e3);
  217. } else {
  218. common_vendor.index.__f__("error", "at pages/login/login.vue:302", "Send registration code failed:", res);
  219. common_vendor.index.showToast({ title: ((_a = res.data) == null ? void 0 : _a.message) || "发送失败", icon: "none" });
  220. }
  221. } catch (err) {
  222. common_vendor.index.hideLoading();
  223. this.isSendingRegCode = false;
  224. common_vendor.index.__f__("error", "at pages/login/login.vue:309", "Send registration code request failed:", err);
  225. common_vendor.index.showToast({ title: "发送失败", icon: "none" });
  226. }
  227. },
  228. async doRegister() {
  229. var _a;
  230. if (!this.regPhone || !this.regCode || !this.regUsername || !this.regPassword) {
  231. common_vendor.index.showToast({ title: "请填写所有注册信息", icon: "none" });
  232. return;
  233. }
  234. common_vendor.index.showLoading({ title: "注册中...", mask: true });
  235. try {
  236. const res = await common_vendor.index.request({
  237. url: "http://localhost:3333/user/register",
  238. method: "POST",
  239. data: {
  240. phone: this.regPhone,
  241. code: this.regCode,
  242. username: this.regUsername,
  243. password: this.regPassword
  244. },
  245. header: { "content-type": "application/json" }
  246. });
  247. common_vendor.index.hideLoading();
  248. if (res.statusCode === 200 && res.data) {
  249. common_vendor.index.__f__("log", "at pages/login/login.vue:337", "Registration success:", res.data);
  250. common_vendor.index.showToast({ title: "注册成功", icon: "success" });
  251. this.regPhone = "";
  252. this.regCode = "";
  253. this.regUsername = "";
  254. this.regPassword = "";
  255. setTimeout(() => {
  256. this.showRegisterPopup = false;
  257. }, 1500);
  258. } else {
  259. common_vendor.index.__f__("error", "at pages/login/login.vue:349", "Registration failed:", res);
  260. common_vendor.index.showToast({ title: ((_a = res.data) == null ? void 0 : _a.message) || "注册失败", icon: "none" });
  261. }
  262. } catch (err) {
  263. common_vendor.index.hideLoading();
  264. common_vendor.index.__f__("error", "at pages/login/login.vue:354", "Registration request failed:", err);
  265. common_vendor.index.showToast({ title: "注册失败", icon: "none" });
  266. }
  267. }
  268. }
  269. };
  270. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  271. return common_vendor.e({
  272. a: common_vendor.n($data.loginType === "phone" ? "active" : ""),
  273. b: common_vendor.o(($event) => $data.loginType = "phone"),
  274. c: common_vendor.n($data.loginType === "account" ? "active" : ""),
  275. d: common_vendor.o(($event) => $data.loginType = "account"),
  276. e: $data.loginType === "phone"
  277. }, $data.loginType === "phone" ? {
  278. f: $data.phone,
  279. g: common_vendor.o(($event) => $data.phone = $event.detail.value),
  280. h: $data.code,
  281. i: common_vendor.o(($event) => $data.code = $event.detail.value),
  282. j: common_vendor.t($data.loginCodeTimer > 0 ? $data.loginCodeTimer + "s" : "获取验证码"),
  283. k: common_vendor.o((...args) => $options.sendCode && $options.sendCode(...args)),
  284. l: $data.loginCodeTimer > 0 || $data.isSendingLoginCode
  285. } : {
  286. m: $data.username,
  287. n: common_vendor.o(($event) => $data.username = $event.detail.value),
  288. o: $data.password,
  289. p: common_vendor.o(($event) => $data.password = $event.detail.value),
  290. q: common_vendor.o((...args) => $options.toRegister && $options.toRegister(...args)),
  291. r: common_vendor.o((...args) => $options.toForgot && $options.toForgot(...args))
  292. }, {
  293. s: !$data.agreed,
  294. t: common_vendor.o((...args) => $options.doLogin && $options.doLogin(...args)),
  295. v: $data.agreed,
  296. w: common_vendor.o(($event) => $data.agreed = !$data.agreed),
  297. x: common_vendor.o((...args) => $options.openAgreement && $options.openAgreement(...args)),
  298. y: common_vendor.o((...args) => $options.openPrivacy && $options.openPrivacy(...args)),
  299. z: common_assets._imports_0$1,
  300. A: common_vendor.o((...args) => $options.oneClickLogin && $options.oneClickLogin(...args)),
  301. B: $data.showRegisterPopup
  302. }, $data.showRegisterPopup ? {
  303. C: $data.regPhone,
  304. D: common_vendor.o(($event) => $data.regPhone = $event.detail.value),
  305. E: $data.regCode,
  306. F: common_vendor.o(($event) => $data.regCode = $event.detail.value),
  307. G: common_vendor.t($data.regCodeTimer > 0 ? $data.regCodeTimer + "s" : "获取验证码"),
  308. H: common_vendor.o((...args) => $options.sendRegCode && $options.sendRegCode(...args)),
  309. I: $data.regCodeTimer > 0 || $data.isSendingRegCode,
  310. J: $data.regUsername,
  311. K: common_vendor.o(($event) => $data.regUsername = $event.detail.value),
  312. L: $data.regPassword,
  313. M: common_vendor.o(($event) => $data.regPassword = $event.detail.value),
  314. N: common_vendor.o((...args) => $options.doRegister && $options.doRegister(...args)),
  315. O: common_assets._imports_1,
  316. P: common_vendor.o(($event) => $data.showRegisterPopup = false)
  317. } : {});
  318. }
  319. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  320. wx.createPage(MiniProgramPage);
  321. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/login/login.js.map