login.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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:8081/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:8081/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, _b;
  132. if (!this.agreed) {
  133. common_vendor.index.showToast({ title: "请先同意协议", icon: "none" });
  134. return;
  135. }
  136. if (this.loginType === "phone") {
  137. if (!this.phone || !this.code) {
  138. common_vendor.index.showToast({ title: "请填写手机号和验证码", icon: "none" });
  139. return;
  140. }
  141. common_vendor.index.showLoading({ title: "登录中...", mask: true });
  142. try {
  143. const res = await common_vendor.index.request({
  144. url: "http://localhost:8081/user/login",
  145. method: "POST",
  146. data: {
  147. phone: this.phone,
  148. code: this.code
  149. },
  150. header: { "content-type": "application/json" }
  151. });
  152. common_vendor.index.hideLoading();
  153. if (res.statusCode === 200 && 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.showToast({ title: ((_a = res.data) == null ? void 0 : _a.message) || "登录失败", icon: "error" });
  160. }
  161. } catch (err) {
  162. common_vendor.index.hideLoading();
  163. common_vendor.index.showToast({ title: "登录失败", icon: "none" });
  164. }
  165. } else if (this.loginType === "account") {
  166. if (!this.username || !this.password) {
  167. common_vendor.index.showToast({ title: "请填写账号和密码", icon: "none" });
  168. return;
  169. }
  170. common_vendor.index.showLoading({ title: "登录中...", mask: true });
  171. try {
  172. const res = await common_vendor.index.request({
  173. url: "http://localhost:8081/user/UserPassLogin",
  174. method: "POST",
  175. data: {
  176. username: this.username,
  177. password: this.password
  178. },
  179. header: { "content-type": "application/json" }
  180. });
  181. common_vendor.index.hideLoading();
  182. if (res.statusCode === 200 && res.data && res.data.code === 200) {
  183. common_vendor.index.showToast({ title: "登录成功", icon: "success" });
  184. setTimeout(() => {
  185. common_vendor.index.switchTab({ url: "/pages/home/index" });
  186. }, 1500);
  187. } else {
  188. common_vendor.index.showToast({ title: ((_b = res.data) == null ? void 0 : _b.msg) || "登录失败", icon: "error" });
  189. }
  190. } catch (err) {
  191. common_vendor.index.hideLoading();
  192. common_vendor.index.showToast({ title: "登录失败", icon: "none" });
  193. }
  194. }
  195. },
  196. toRegister() {
  197. this.showRegisterPopup = true;
  198. },
  199. toForgot() {
  200. common_vendor.index.showToast({ title: "跳转找回密码", icon: "none" });
  201. },
  202. openAgreement() {
  203. common_vendor.index.navigateTo({ url: "/pages/agreement/agreement" });
  204. },
  205. openPrivacy() {
  206. common_vendor.index.navigateTo({ url: "/pages/privacy/privacy" });
  207. },
  208. qqLogin() {
  209. common_vendor.index.showToast({ title: "QQ登录", icon: "none" });
  210. },
  211. async sendRegCode() {
  212. var _a;
  213. if (this.isSendingRegCode || this.regCodeTimer > 0) {
  214. return;
  215. }
  216. if (!this.regPhone) {
  217. common_vendor.index.showToast({ title: "请输入手机号", icon: "none" });
  218. return;
  219. }
  220. this.isSendingRegCode = true;
  221. common_vendor.index.showLoading({ title: "发送中...", mask: true });
  222. try {
  223. const res = await common_vendor.index.request({
  224. url: "http://localhost:8081/user/code",
  225. method: "POST",
  226. data: {
  227. phone: this.regPhone
  228. // 发送手机号参数
  229. },
  230. header: { "content-type": "application/json" }
  231. });
  232. common_vendor.index.hideLoading();
  233. this.isSendingRegCode = false;
  234. if (res.statusCode === 200 && res.data) {
  235. common_vendor.index.__f__("log", "at pages/login/login.vue:314", "Send registration code success:", res.data);
  236. common_vendor.index.showToast({ title: "验证码已发送", icon: "success" });
  237. this.regCodeTimer = 60;
  238. const timerInterval = setInterval(() => {
  239. this.regCodeTimer--;
  240. if (this.regCodeTimer <= 0) {
  241. clearInterval(timerInterval);
  242. this.regCodeTimer = 0;
  243. }
  244. }, 1e3);
  245. } else {
  246. common_vendor.index.__f__("error", "at pages/login/login.vue:328", "Send registration code failed:", res);
  247. common_vendor.index.showToast({ title: ((_a = res.data) == null ? void 0 : _a.message) || "发送失败", icon: "none" });
  248. }
  249. } catch (err) {
  250. common_vendor.index.hideLoading();
  251. this.isSendingRegCode = false;
  252. common_vendor.index.__f__("error", "at pages/login/login.vue:335", "Send registration code request failed:", err);
  253. common_vendor.index.showToast({ title: "发送失败", icon: "none" });
  254. }
  255. },
  256. async doRegister() {
  257. var _a;
  258. if (!this.regPhone || !this.regCode || !this.regUsername || !this.regPassword) {
  259. common_vendor.index.showToast({ title: "请填写所有注册信息", icon: "none" });
  260. return;
  261. }
  262. common_vendor.index.showLoading({ title: "注册中...", mask: true });
  263. try {
  264. const res = await common_vendor.index.request({
  265. url: "http://localhost:8081/user/register",
  266. method: "POST",
  267. data: {
  268. phone: this.regPhone,
  269. code: this.regCode,
  270. username: this.regUsername,
  271. password: this.regPassword
  272. },
  273. header: { "content-type": "application/json" }
  274. });
  275. common_vendor.index.hideLoading();
  276. if (res.statusCode === 200 && res.data) {
  277. common_vendor.index.__f__("log", "at pages/login/login.vue:363", "Registration success:", res.data);
  278. common_vendor.index.showToast({ title: "注册成功", icon: "success" });
  279. this.regPhone = "";
  280. this.regCode = "";
  281. this.regUsername = "";
  282. this.regPassword = "";
  283. setTimeout(() => {
  284. this.showRegisterPopup = false;
  285. }, 1500);
  286. } else {
  287. common_vendor.index.__f__("error", "at pages/login/login.vue:375", "Registration failed:", res);
  288. common_vendor.index.showToast({ title: ((_a = res.data) == null ? void 0 : _a.message) || "注册失败", icon: "none" });
  289. }
  290. } catch (err) {
  291. common_vendor.index.hideLoading();
  292. common_vendor.index.__f__("error", "at pages/login/login.vue:380", "Registration request failed:", err);
  293. common_vendor.index.showToast({ title: "注册失败", icon: "none" });
  294. }
  295. }
  296. }
  297. };
  298. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  299. return common_vendor.e({
  300. a: common_vendor.n($data.loginType === "phone" ? "active" : ""),
  301. b: common_vendor.o(($event) => $data.loginType = "phone"),
  302. c: common_vendor.n($data.loginType === "account" ? "active" : ""),
  303. d: common_vendor.o(($event) => $data.loginType = "account"),
  304. e: $data.loginType === "phone"
  305. }, $data.loginType === "phone" ? {
  306. f: $data.phone,
  307. g: common_vendor.o(($event) => $data.phone = $event.detail.value),
  308. h: $data.code,
  309. i: common_vendor.o(($event) => $data.code = $event.detail.value),
  310. j: common_vendor.t($data.loginCodeTimer > 0 ? $data.loginCodeTimer + "s" : "获取验证码"),
  311. k: common_vendor.o((...args) => $options.sendCode && $options.sendCode(...args)),
  312. l: $data.loginCodeTimer > 0 || $data.isSendingLoginCode
  313. } : {
  314. m: $data.username,
  315. n: common_vendor.o(($event) => $data.username = $event.detail.value),
  316. o: $data.password,
  317. p: common_vendor.o(($event) => $data.password = $event.detail.value),
  318. q: common_vendor.o((...args) => $options.toRegister && $options.toRegister(...args)),
  319. r: common_vendor.o((...args) => $options.toForgot && $options.toForgot(...args))
  320. }, {
  321. s: !$data.agreed,
  322. t: common_vendor.o((...args) => $options.doLogin && $options.doLogin(...args)),
  323. v: $data.agreed,
  324. w: common_vendor.o(($event) => $data.agreed = !$data.agreed),
  325. x: common_vendor.o((...args) => $options.openAgreement && $options.openAgreement(...args)),
  326. y: common_vendor.o((...args) => $options.openPrivacy && $options.openPrivacy(...args)),
  327. z: common_assets._imports_0,
  328. A: common_vendor.o((...args) => $options.oneClickLogin && $options.oneClickLogin(...args)),
  329. B: $data.showRegisterPopup
  330. }, $data.showRegisterPopup ? {
  331. C: $data.regPhone,
  332. D: common_vendor.o(($event) => $data.regPhone = $event.detail.value),
  333. E: $data.regCode,
  334. F: common_vendor.o(($event) => $data.regCode = $event.detail.value),
  335. G: common_vendor.t($data.regCodeTimer > 0 ? $data.regCodeTimer + "s" : "获取验证码"),
  336. H: common_vendor.o((...args) => $options.sendRegCode && $options.sendRegCode(...args)),
  337. I: $data.regCodeTimer > 0 || $data.isSendingRegCode,
  338. J: $data.regUsername,
  339. K: common_vendor.o(($event) => $data.regUsername = $event.detail.value),
  340. L: $data.regPassword,
  341. M: common_vendor.o(($event) => $data.regPassword = $event.detail.value),
  342. N: common_vendor.o((...args) => $options.doRegister && $options.doRegister(...args)),
  343. O: common_assets._imports_1,
  344. P: common_vendor.o(($event) => $data.showRegisterPopup = false)
  345. } : {});
  346. }
  347. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  348. wx.createPage(MiniProgramPage);
  349. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/login/login.js.map