login.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. showForgotPopup: false,
  28. forgotPhone: "",
  29. forgotCode: "",
  30. newPassword: "",
  31. confirmPassword: "",
  32. forgotCodeTimer: 0,
  33. isSendingForgotCode: false
  34. };
  35. },
  36. methods: {
  37. // 处理微信登录
  38. async oneClickLogin() {
  39. common_vendor.index.__f__("log", "at pages/login/login.vue:123", "WeChat login button clicked");
  40. common_vendor.index.getUserProfile({
  41. desc: "用于完善用户资料",
  42. lang: "zh_CN",
  43. success: (userRes) => {
  44. common_vendor.index.__f__("log", "at pages/login/login.vue:129", userRes);
  45. common_vendor.index.showLoading({ title: "登录中...", mask: true });
  46. common_vendor.index.login({
  47. provider: "weixin",
  48. success: (wx_res) => {
  49. common_vendor.index.__f__("log", "at pages/login/login.vue:136", "uni.getUserProfile success:", userRes);
  50. common_vendor.index.__f__("log", "at pages/login/login.vue:137", "uni.login success, got code:", wx_res.code);
  51. common_vendor.index.request({
  52. url: "http://localhost:3333/WeChart/login",
  53. method: "POST",
  54. data: {
  55. code: wx_res.code,
  56. weChatLoginDto: userRes.userInfo
  57. },
  58. header: { "content-type": "application/json" },
  59. success: (res) => {
  60. var _a;
  61. common_vendor.index.hideLoading();
  62. if (res.statusCode === 200 && res.data) {
  63. common_vendor.index.__f__("log", "at pages/login/login.vue:150", "Backend login success:", res.data);
  64. common_vendor.index.showToast({ title: "登录成功", icon: "success" });
  65. setTimeout(() => {
  66. common_vendor.index.switchTab({ url: "/pages/home/index" });
  67. }, 1500);
  68. } else {
  69. common_vendor.index.__f__("error", "at pages/login/login.vue:156", "Backend login failed:", res);
  70. common_vendor.index.showToast({ title: ((_a = res.data) == null ? void 0 : _a.message) || "登录失败", icon: "none" });
  71. }
  72. },
  73. fail: (err) => {
  74. common_vendor.index.hideLoading();
  75. common_vendor.index.__f__("error", "at pages/login/login.vue:162", "Backend request failed:", err);
  76. common_vendor.index.showToast({ title: "微信登录失败", icon: "none" });
  77. }
  78. });
  79. },
  80. fail: (err) => {
  81. common_vendor.index.hideLoading();
  82. common_vendor.index.__f__("error", "at pages/login/login.vue:169", "uni.login failed:", err);
  83. common_vendor.index.showToast({ title: "微信登录失败", icon: "none" });
  84. }
  85. });
  86. },
  87. fail: (err) => {
  88. common_vendor.index.__f__("error", "at pages/login/login.vue:175", "uni.getUserProfile failed:", err);
  89. common_vendor.index.showToast({ title: "获取用户信息失败", icon: "none" });
  90. }
  91. });
  92. },
  93. async sendCode() {
  94. var _a;
  95. if (this.isSendingLoginCode || this.loginCodeTimer > 0) {
  96. return;
  97. }
  98. if (!this.phone) {
  99. common_vendor.index.showToast({ title: "请输入手机号", icon: "none" });
  100. return;
  101. }
  102. this.isSendingLoginCode = true;
  103. common_vendor.index.showLoading({ title: "发送中...", mask: true });
  104. try {
  105. const res = await common_vendor.index.request({
  106. url: "http://localhost:3333/user/code",
  107. method: "POST",
  108. data: {
  109. phone: this.phone
  110. },
  111. header: { "content-type": "application/json" }
  112. });
  113. common_vendor.index.hideLoading();
  114. this.isSendingLoginCode = false;
  115. if (res.statusCode === 200 && res.data) {
  116. common_vendor.index.__f__("log", "at pages/login/login.vue:206", "Send login code success:", res.data);
  117. common_vendor.index.showToast({ title: "验证码已发送", icon: "success" });
  118. this.loginCodeTimer = 60;
  119. const timerInterval = setInterval(() => {
  120. this.loginCodeTimer--;
  121. if (this.loginCodeTimer <= 0) {
  122. clearInterval(timerInterval);
  123. this.loginCodeTimer = 0;
  124. }
  125. }, 1e3);
  126. } else {
  127. common_vendor.index.__f__("error", "at pages/login/login.vue:220", "Send login code failed:", res);
  128. common_vendor.index.showToast({ title: ((_a = res.data) == null ? void 0 : _a.message) || "发送失败", icon: "none" });
  129. }
  130. } catch (err) {
  131. common_vendor.index.hideLoading();
  132. this.isSendingLoginCode = false;
  133. common_vendor.index.__f__("error", "at pages/login/login.vue:226", "Send login code request failed:", err);
  134. common_vendor.index.showToast({ title: "发送失败", icon: "none" });
  135. }
  136. },
  137. async doLogin() {
  138. var _a, _b;
  139. if (!this.agreed) {
  140. common_vendor.index.showToast({ title: "请先同意协议", icon: "none" });
  141. return;
  142. }
  143. if (this.loginType === "phone") {
  144. if (!this.phone || !this.code) {
  145. common_vendor.index.showToast({ title: "请填写手机号和验证码", icon: "none" });
  146. return;
  147. }
  148. common_vendor.index.showLoading({ title: "登录中...", mask: true });
  149. try {
  150. const res = await common_vendor.index.request({
  151. url: "http://localhost:3333/user/login",
  152. method: "POST",
  153. data: {
  154. phone: this.phone,
  155. code: this.code
  156. },
  157. header: { "content-type": "application/json" }
  158. });
  159. common_vendor.index.hideLoading();
  160. if (res.statusCode === 200 && res.data) {
  161. common_vendor.index.showToast({ title: "登录成功", icon: "success" });
  162. setTimeout(() => {
  163. common_vendor.index.switchTab({ url: "/pages/home/index" });
  164. }, 1500);
  165. } else {
  166. common_vendor.index.showToast({ title: ((_a = res.data) == null ? void 0 : _a.message) || "登录失败", icon: "error" });
  167. }
  168. } catch (err) {
  169. common_vendor.index.hideLoading();
  170. common_vendor.index.showToast({ title: "登录失败", icon: "none" });
  171. }
  172. } else if (this.loginType === "account") {
  173. if (!this.username || !this.password) {
  174. common_vendor.index.showToast({ title: "请填写账号和密码", icon: "none" });
  175. return;
  176. }
  177. common_vendor.index.showLoading({ title: "登录中...", mask: true });
  178. try {
  179. const res = await common_vendor.index.request({
  180. url: "http://localhost:3333/user/UserPassLogin",
  181. method: "POST",
  182. data: {
  183. username: this.username,
  184. password: this.password
  185. },
  186. header: { "content-type": "application/json" }
  187. });
  188. common_vendor.index.hideLoading();
  189. if (res.statusCode === 200 && res.data && res.data.code === 200) {
  190. common_vendor.index.showToast({ title: "登录成功", icon: "success" });
  191. setTimeout(() => {
  192. common_vendor.index.switchTab({ url: "/pages/home/index" });
  193. }, 1500);
  194. } else {
  195. common_vendor.index.showToast({ title: ((_b = res.data) == null ? void 0 : _b.msg) || "登录失败", icon: "error" });
  196. }
  197. } catch (err) {
  198. common_vendor.index.hideLoading();
  199. common_vendor.index.showToast({ title: "登录失败", icon: "none" });
  200. }
  201. }
  202. },
  203. toRegister() {
  204. this.showRegisterPopup = true;
  205. },
  206. toForgot() {
  207. this.showForgotPopup = true;
  208. },
  209. openAgreement() {
  210. common_vendor.index.navigateTo({ url: "/pages/agreement/agreement" });
  211. },
  212. openPrivacy() {
  213. common_vendor.index.navigateTo({ url: "/pages/privacy/privacy" });
  214. },
  215. qqLogin() {
  216. common_vendor.index.showToast({ title: "QQ登录", icon: "none" });
  217. },
  218. async sendRegCode() {
  219. var _a;
  220. if (this.isSendingRegCode || this.regCodeTimer > 0) {
  221. return;
  222. }
  223. if (!this.regPhone) {
  224. common_vendor.index.showToast({ title: "请输入手机号", icon: "none" });
  225. return;
  226. }
  227. this.isSendingRegCode = true;
  228. common_vendor.index.showLoading({ title: "发送中...", mask: true });
  229. try {
  230. const res = await common_vendor.index.request({
  231. url: "http://localhost:3333/user/code",
  232. method: "POST",
  233. data: {
  234. phone: this.regPhone
  235. // 发送手机号参数
  236. },
  237. header: { "content-type": "application/json" }
  238. });
  239. common_vendor.index.hideLoading();
  240. this.isSendingRegCode = false;
  241. if (res.statusCode === 200 && res.data) {
  242. common_vendor.index.__f__("log", "at pages/login/login.vue:345", "Send registration code success:", res.data);
  243. common_vendor.index.showToast({ title: "验证码已发送", icon: "success" });
  244. this.regCodeTimer = 60;
  245. const timerInterval = setInterval(() => {
  246. this.regCodeTimer--;
  247. if (this.regCodeTimer <= 0) {
  248. clearInterval(timerInterval);
  249. this.regCodeTimer = 0;
  250. }
  251. }, 1e3);
  252. } else {
  253. common_vendor.index.__f__("error", "at pages/login/login.vue:359", "Send registration code failed:", res);
  254. common_vendor.index.showToast({ title: ((_a = res.data) == null ? void 0 : _a.message) || "发送失败", icon: "none" });
  255. }
  256. } catch (err) {
  257. common_vendor.index.hideLoading();
  258. this.isSendingRegCode = false;
  259. common_vendor.index.__f__("error", "at pages/login/login.vue:366", "Send registration code request failed:", err);
  260. common_vendor.index.showToast({ title: "发送失败", icon: "none" });
  261. }
  262. },
  263. async doRegister() {
  264. var _a;
  265. if (!this.regPhone || !this.regCode || !this.regUsername || !this.regPassword) {
  266. common_vendor.index.showToast({ title: "请填写所有注册信息", icon: "none" });
  267. return;
  268. }
  269. common_vendor.index.showLoading({ title: "注册中...", mask: true });
  270. try {
  271. const res = await common_vendor.index.request({
  272. url: "http://localhost:3333/user/register",
  273. method: "POST",
  274. data: {
  275. phone: this.regPhone,
  276. code: this.regCode,
  277. username: this.regUsername,
  278. password: this.regPassword
  279. },
  280. header: { "content-type": "application/json" }
  281. });
  282. common_vendor.index.hideLoading();
  283. if (res.statusCode === 200 && res.data) {
  284. common_vendor.index.__f__("log", "at pages/login/login.vue:394", "Registration success:", res.data);
  285. common_vendor.index.showToast({ title: "注册成功", icon: "success" });
  286. this.regPhone = "";
  287. this.regCode = "";
  288. this.regUsername = "";
  289. this.regPassword = "";
  290. setTimeout(() => {
  291. this.showRegisterPopup = false;
  292. }, 1500);
  293. } else {
  294. common_vendor.index.__f__("error", "at pages/login/login.vue:406", "Registration failed:", res);
  295. common_vendor.index.showToast({ title: ((_a = res.data) == null ? void 0 : _a.message) || "注册失败", icon: "none" });
  296. }
  297. } catch (err) {
  298. common_vendor.index.hideLoading();
  299. common_vendor.index.__f__("error", "at pages/login/login.vue:411", "Registration request failed:", err);
  300. common_vendor.index.showToast({ title: "注册失败", icon: "none" });
  301. }
  302. },
  303. async sendForgotCode() {
  304. var _a;
  305. if (this.isSendingForgotCode || this.forgotCodeTimer > 0) {
  306. return;
  307. }
  308. if (!this.forgotPhone) {
  309. common_vendor.index.showToast({ title: "请输入手机号", icon: "none" });
  310. return;
  311. }
  312. this.isSendingForgotCode = true;
  313. common_vendor.index.showLoading({ title: "发送中...", mask: true });
  314. try {
  315. const res = await common_vendor.index.request({
  316. url: "http://localhost:3333/user/code",
  317. method: "POST",
  318. data: {
  319. phone: this.forgotPhone
  320. },
  321. header: { "content-type": "application/json" }
  322. });
  323. common_vendor.index.hideLoading();
  324. this.isSendingForgotCode = false;
  325. if (res.statusCode === 200 && res.data) {
  326. common_vendor.index.__f__("log", "at pages/login/login.vue:441", "Send forgot code success:", res.data);
  327. common_vendor.index.showToast({ title: "验证码已发送", icon: "success" });
  328. this.forgotCodeTimer = 60;
  329. const timerInterval = setInterval(() => {
  330. this.forgotCodeTimer--;
  331. if (this.forgotCodeTimer <= 0) {
  332. clearInterval(timerInterval);
  333. this.forgotCodeTimer = 0;
  334. }
  335. }, 1e3);
  336. } else {
  337. common_vendor.index.__f__("error", "at pages/login/login.vue:453", "Send forgot code failed:", res);
  338. common_vendor.index.showToast({ title: ((_a = res.data) == null ? void 0 : _a.message) || "发送失败", icon: "none" });
  339. }
  340. } catch (err) {
  341. common_vendor.index.hideLoading();
  342. this.isSendingForgotCode = false;
  343. common_vendor.index.__f__("error", "at pages/login/login.vue:459", "Send forgot code request failed:", err);
  344. common_vendor.index.showToast({ title: "发送失败", icon: "none" });
  345. }
  346. },
  347. async doResetPassword() {
  348. var _a;
  349. if (!this.forgotPhone || !this.forgotCode || !this.newPassword || !this.confirmPassword) {
  350. common_vendor.index.showToast({ title: "请填写所有信息", icon: "none" });
  351. return;
  352. }
  353. if (this.newPassword !== this.confirmPassword) {
  354. common_vendor.index.showToast({ title: "两次密码输入不一致", icon: "none" });
  355. return;
  356. }
  357. common_vendor.index.showLoading({ title: "修改中...", mask: true });
  358. try {
  359. const res = await common_vendor.index.request({
  360. url: "http://localhost:3333/user/ForgetPass",
  361. method: "POST",
  362. data: {
  363. phone: this.forgotPhone,
  364. code: this.forgotCode,
  365. newPassword: this.newPassword
  366. },
  367. header: { "content-type": "application/json" }
  368. });
  369. common_vendor.index.hideLoading();
  370. if (res.statusCode === 200 && res.data) {
  371. common_vendor.index.__f__("log", "at pages/login/login.vue:491", "Reset password success:", res.data);
  372. common_vendor.index.showToast({ title: "密码修改成功", icon: "success" });
  373. this.forgotPhone = "";
  374. this.forgotCode = "";
  375. this.newPassword = "";
  376. this.confirmPassword = "";
  377. setTimeout(() => {
  378. this.showForgotPopup = false;
  379. this.loginType = "account";
  380. }, 1500);
  381. } else {
  382. common_vendor.index.__f__("error", "at pages/login/login.vue:507", "Reset password failed:", res);
  383. common_vendor.index.showToast({ title: ((_a = res.data) == null ? void 0 : _a.message) || "修改失败", icon: "none" });
  384. }
  385. } catch (err) {
  386. common_vendor.index.hideLoading();
  387. common_vendor.index.__f__("error", "at pages/login/login.vue:512", "Reset password request failed:", err);
  388. common_vendor.index.showToast({ title: "修改失败", icon: "none" });
  389. }
  390. }
  391. }
  392. };
  393. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  394. return common_vendor.e({
  395. a: common_vendor.n($data.loginType === "phone" ? "active" : ""),
  396. b: common_vendor.o(($event) => $data.loginType = "phone"),
  397. c: common_vendor.n($data.loginType === "account" ? "active" : ""),
  398. d: common_vendor.o(($event) => $data.loginType = "account"),
  399. e: $data.loginType === "phone"
  400. }, $data.loginType === "phone" ? {
  401. f: $data.phone,
  402. g: common_vendor.o(($event) => $data.phone = $event.detail.value),
  403. h: $data.code,
  404. i: common_vendor.o(($event) => $data.code = $event.detail.value),
  405. j: common_vendor.t($data.loginCodeTimer > 0 ? $data.loginCodeTimer + "s" : "获取验证码"),
  406. k: common_vendor.o((...args) => $options.sendCode && $options.sendCode(...args)),
  407. l: $data.loginCodeTimer > 0 || $data.isSendingLoginCode
  408. } : {
  409. m: $data.username,
  410. n: common_vendor.o(($event) => $data.username = $event.detail.value),
  411. o: $data.password,
  412. p: common_vendor.o(($event) => $data.password = $event.detail.value),
  413. q: common_vendor.o((...args) => $options.toRegister && $options.toRegister(...args)),
  414. r: common_vendor.o((...args) => $options.toForgot && $options.toForgot(...args))
  415. }, {
  416. s: !$data.agreed,
  417. t: common_vendor.o((...args) => $options.doLogin && $options.doLogin(...args)),
  418. v: $data.agreed,
  419. w: common_vendor.o(($event) => $data.agreed = !$data.agreed),
  420. x: common_vendor.o((...args) => $options.openAgreement && $options.openAgreement(...args)),
  421. y: common_vendor.o((...args) => $options.openPrivacy && $options.openPrivacy(...args)),
  422. z: common_assets._imports_0$1,
  423. A: common_vendor.o((...args) => $options.oneClickLogin && $options.oneClickLogin(...args)),
  424. B: $data.showRegisterPopup
  425. }, $data.showRegisterPopup ? {
  426. C: $data.regPhone,
  427. D: common_vendor.o(($event) => $data.regPhone = $event.detail.value),
  428. E: $data.regCode,
  429. F: common_vendor.o(($event) => $data.regCode = $event.detail.value),
  430. G: common_vendor.t($data.regCodeTimer > 0 ? $data.regCodeTimer + "s" : "获取验证码"),
  431. H: common_vendor.o((...args) => $options.sendRegCode && $options.sendRegCode(...args)),
  432. I: $data.regCodeTimer > 0 || $data.isSendingRegCode,
  433. J: $data.regUsername,
  434. K: common_vendor.o(($event) => $data.regUsername = $event.detail.value),
  435. L: $data.regPassword,
  436. M: common_vendor.o(($event) => $data.regPassword = $event.detail.value),
  437. N: common_vendor.o((...args) => $options.doRegister && $options.doRegister(...args)),
  438. O: common_assets._imports_1,
  439. P: common_vendor.o(($event) => $data.showRegisterPopup = false)
  440. } : {}, {
  441. Q: $data.showForgotPopup
  442. }, $data.showForgotPopup ? {
  443. R: $data.forgotPhone,
  444. S: common_vendor.o(($event) => $data.forgotPhone = $event.detail.value),
  445. T: $data.forgotCode,
  446. U: common_vendor.o(($event) => $data.forgotCode = $event.detail.value),
  447. V: common_vendor.t($data.forgotCodeTimer > 0 ? $data.forgotCodeTimer + "s" : "获取验证码"),
  448. W: common_vendor.o((...args) => $options.sendForgotCode && $options.sendForgotCode(...args)),
  449. X: $data.forgotCodeTimer > 0 || $data.isSendingForgotCode,
  450. Y: $data.newPassword,
  451. Z: common_vendor.o(($event) => $data.newPassword = $event.detail.value),
  452. aa: $data.confirmPassword,
  453. ab: common_vendor.o(($event) => $data.confirmPassword = $event.detail.value),
  454. ac: common_vendor.o((...args) => $options.doResetPassword && $options.doResetPassword(...args)),
  455. ad: common_assets._imports_1,
  456. ae: common_vendor.o(($event) => $data.showForgotPopup = false)
  457. } : {});
  458. }
  459. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  460. wx.createPage(MiniProgramPage);
  461. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/login/login.js.map