index.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const common_assets = require("../../common/assets.js");
  4. const _sfc_main = {
  5. __name: "index",
  6. setup(__props) {
  7. const agreed = common_vendor.ref(false);
  8. const showLoginPopup = common_vendor.ref(false);
  9. const showRegisterPopup = common_vendor.ref(false);
  10. const loginTab = common_vendor.ref("phone");
  11. const loginPhone = common_vendor.ref("");
  12. const loginCode = common_vendor.ref("");
  13. const loginAccount = common_vendor.ref("");
  14. const loginPassword = common_vendor.ref("");
  15. const regPhone = common_vendor.ref("");
  16. const regCode = common_vendor.ref("");
  17. const regAccount = common_vendor.ref("");
  18. const regPassword = common_vendor.ref("");
  19. const loginCodeCountdown = common_vendor.ref(0);
  20. const regCodeCountdown = common_vendor.ref(0);
  21. let loginCodeTimer = null;
  22. let regCodeTimer = null;
  23. function openProtocol(type) {
  24. const urls = {
  25. service: "/pages/protocol/service",
  26. privacy: "/pages/protocol/privacy"
  27. };
  28. common_vendor.index.navigateTo({ url: urls[type] });
  29. }
  30. function socialLogin(type) {
  31. if (!agreed.value) {
  32. common_vendor.index.showToast({
  33. title: "请先同意服务协议和隐私政策",
  34. icon: "none"
  35. });
  36. return;
  37. }
  38. if (type === "wechat") {
  39. common_vendor.index.login({
  40. provider: "weixin",
  41. success: async (loginRes) => {
  42. common_vendor.index.__f__("log", "at pages/login/index.vue:148", "uni.login result:", loginRes);
  43. if (loginRes.code) {
  44. try {
  45. const userInfoRes = await common_vendor.index.getUserProfile({
  46. desc: "用于完善用户资料"
  47. }).catch((err) => {
  48. common_vendor.index.__f__("error", "at pages/login/index.vue:154", "getUserProfile error:", err);
  49. common_vendor.index.showToast({
  50. title: "已取消授权",
  51. icon: "none"
  52. });
  53. throw err;
  54. });
  55. common_vendor.index.__f__("log", "at pages/login/index.vue:162", "userInfoRes:", userInfoRes);
  56. if (!userInfoRes || !userInfoRes.userInfo) {
  57. common_vendor.index.showToast({
  58. title: "未获取到用户信息",
  59. icon: "none"
  60. });
  61. return;
  62. }
  63. const wxLoginDto = {
  64. avatarUrl: userInfoRes.userInfo.avatarUrl,
  65. gender: userInfoRes.userInfo.gender,
  66. nickName: userInfoRes.userInfo.nickName
  67. };
  68. const requestData = {
  69. code: loginRes.code,
  70. wxLoginDto
  71. };
  72. if (userInfoRes.encryptedData)
  73. requestData.encryptedData = userInfoRes.encryptedData;
  74. if (userInfoRes.iv)
  75. requestData.iv = userInfoRes.iv;
  76. common_vendor.index.__f__("log", "at pages/login/index.vue:186", "requestData:", requestData);
  77. common_vendor.index.request({
  78. url: "http://localhost:9500/wechat/login",
  79. method: "POST",
  80. data: requestData,
  81. success: (res) => {
  82. common_vendor.index.__f__("log", "at pages/login/index.vue:193", "后端返回:", res);
  83. if (res.statusCode === 200 && res.data.code === 200) {
  84. if (res.data.token) {
  85. common_vendor.index.setStorageSync("token", res.data.token);
  86. }
  87. common_vendor.index.setStorageSync("isLoggedIn", true);
  88. common_vendor.index.setStorageSync("userInfo", wxLoginDto);
  89. common_vendor.index.showToast({
  90. title: "登录成功",
  91. icon: "success",
  92. duration: 1500
  93. });
  94. common_vendor.index.reLaunch({
  95. url: "/pages/index/index"
  96. });
  97. } else {
  98. common_vendor.index.showToast({
  99. title: res.data.msg || "登录失败,请重试",
  100. icon: "none"
  101. });
  102. }
  103. },
  104. fail: (err) => {
  105. common_vendor.index.__f__("error", "at pages/login/index.vue:218", "uni.request error:", err);
  106. common_vendor.index.showToast({
  107. title: "网络错误,请稍后重试",
  108. icon: "none"
  109. });
  110. }
  111. });
  112. } catch (err) {
  113. common_vendor.index.__f__("error", "at pages/login/index.vue:227", "catch error:", err);
  114. }
  115. } else {
  116. common_vendor.index.showToast({
  117. title: "微信登录失败",
  118. icon: "none"
  119. });
  120. }
  121. },
  122. fail: (err) => {
  123. common_vendor.index.__f__("error", "at pages/login/index.vue:237", "uni.login error:", err);
  124. common_vendor.index.showToast({
  125. title: "微信登录失败,请重试",
  126. icon: "none"
  127. });
  128. }
  129. });
  130. }
  131. }
  132. function openRegister() {
  133. showLoginPopup.value = false;
  134. showRegisterPopup.value = true;
  135. }
  136. function getLoginCode() {
  137. if (loginCodeCountdown.value > 0)
  138. return;
  139. if (!/^1[3-9]\d{9}$/.test(loginPhone.value)) {
  140. common_vendor.index.showToast({ title: "请输入正确手机号", icon: "none" });
  141. return;
  142. }
  143. loginCodeCountdown.value = 60;
  144. loginCodeTimer = setInterval(() => {
  145. if (loginCodeCountdown.value > 0) {
  146. loginCodeCountdown.value--;
  147. } else {
  148. clearInterval(loginCodeTimer);
  149. loginCodeTimer = null;
  150. }
  151. }, 1e3);
  152. common_vendor.index.__f__("log", "at pages/login/index.vue:268", "开始获取验证码...");
  153. common_vendor.index.request({
  154. url: "http://localhost:9500/user/code",
  155. method: "POST",
  156. data: { phone: loginPhone.value },
  157. success(res) {
  158. common_vendor.index.__f__("log", "at pages/login/index.vue:274", "验证码响应:", res.data);
  159. if (res.statusCode === 200) {
  160. common_vendor.index.showToast({ title: "验证码已发送", icon: "none" });
  161. } else {
  162. common_vendor.index.showToast({ title: res.data.msg || "发送失败", icon: "none" });
  163. }
  164. },
  165. fail(err) {
  166. common_vendor.index.__f__("error", "at pages/login/index.vue:282", "获取验证码失败:", err);
  167. common_vendor.index.showToast({ title: "网络错误", icon: "none" });
  168. }
  169. });
  170. }
  171. function getRegCode() {
  172. if (regCodeCountdown.value > 0)
  173. return;
  174. if (!/^1[3-9]\d{9}$/.test(regPhone.value)) {
  175. common_vendor.index.showToast({ title: "请输入正确手机号", icon: "none" });
  176. return;
  177. }
  178. regCodeCountdown.value = 60;
  179. regCodeTimer = setInterval(() => {
  180. if (regCodeCountdown.value > 0) {
  181. regCodeCountdown.value--;
  182. } else {
  183. clearInterval(regCodeTimer);
  184. regCodeTimer = null;
  185. }
  186. }, 1e3);
  187. common_vendor.index.__f__("log", "at pages/login/index.vue:305", "开始获取注册验证码...");
  188. common_vendor.index.request({
  189. url: "http://localhost:9500/user/code",
  190. method: "POST",
  191. data: { phone: regPhone.value },
  192. success(res) {
  193. common_vendor.index.__f__("log", "at pages/login/index.vue:311", "验证码响应:", res.data);
  194. if (res.statusCode === 200) {
  195. common_vendor.index.showToast({ title: "验证码已发送", icon: "none" });
  196. } else {
  197. common_vendor.index.showToast({ title: res.data.msg || "发送失败", icon: "none" });
  198. }
  199. },
  200. fail(err) {
  201. common_vendor.index.__f__("error", "at pages/login/index.vue:319", "获取验证码失败:", err);
  202. common_vendor.index.showToast({ title: "网络错误", icon: "none" });
  203. }
  204. });
  205. }
  206. function handlePopupLogin() {
  207. if (loginTab.value === "phone") {
  208. if (!/^1[3-9]\d{9}$/.test(loginPhone.value)) {
  209. common_vendor.index.showToast({ title: "请输入正确手机号", icon: "none" });
  210. return;
  211. }
  212. if (!loginCode.value) {
  213. common_vendor.index.showToast({ title: "请输入验证码", icon: "none" });
  214. return;
  215. }
  216. common_vendor.index.__f__("log", "at pages/login/index.vue:335", "开始登录请求...");
  217. common_vendor.index.request({
  218. url: "http://localhost:9500/user/login",
  219. method: "POST",
  220. data: {
  221. phone: loginPhone.value,
  222. code: loginCode.value
  223. },
  224. success(res) {
  225. common_vendor.index.__f__("log", "at pages/login/index.vue:344", "登录响应数据:", res.data);
  226. common_vendor.index.__f__("log", "at pages/login/index.vue:345", "响应状态码:", res.statusCode);
  227. if (res.statusCode === 200) {
  228. common_vendor.index.setStorageSync("userPhone", loginPhone.value);
  229. common_vendor.index.setStorageSync("isLoggedIn", true);
  230. showLoginPopup.value = false;
  231. common_vendor.index.showToast({
  232. title: "登录成功",
  233. icon: "success",
  234. duration: 1500
  235. });
  236. common_vendor.index.__f__("log", "at pages/login/index.vue:364", "开始跳转到首页...");
  237. common_vendor.index.reLaunch({
  238. url: "/pages/index/index",
  239. success: () => {
  240. common_vendor.index.__f__("log", "at pages/login/index.vue:368", "跳转成功");
  241. },
  242. fail: (err) => {
  243. common_vendor.index.__f__("error", "at pages/login/index.vue:371", "reLaunch 跳转失败:", err);
  244. common_vendor.index.__f__("log", "at pages/login/index.vue:373", "尝试使用 switchTab 跳转...");
  245. common_vendor.index.switchTab({
  246. url: "/pages/index/index",
  247. success: () => {
  248. common_vendor.index.__f__("log", "at pages/login/index.vue:377", "switchTab 跳转成功");
  249. },
  250. fail: (err2) => {
  251. common_vendor.index.__f__("error", "at pages/login/index.vue:380", "switchTab 跳转失败:", err2);
  252. common_vendor.index.showToast({
  253. title: "页面跳转失败,请重试",
  254. icon: "none"
  255. });
  256. }
  257. });
  258. }
  259. });
  260. } else {
  261. common_vendor.index.__f__("log", "at pages/login/index.vue:390", "登录失败,状态码:", res.statusCode);
  262. common_vendor.index.showToast({
  263. title: res.data.msg || "登录失败,请重试",
  264. icon: "none"
  265. });
  266. }
  267. },
  268. fail(err) {
  269. common_vendor.index.__f__("error", "at pages/login/index.vue:398", "登录请求失败:", err);
  270. common_vendor.index.showToast({ title: "网络错误", icon: "none" });
  271. }
  272. });
  273. } else {
  274. if (!loginAccount.value) {
  275. common_vendor.index.showToast({ title: "请输入账号", icon: "none" });
  276. return;
  277. }
  278. if (!loginPassword.value) {
  279. common_vendor.index.showToast({ title: "请输入密码", icon: "none" });
  280. return;
  281. }
  282. common_vendor.index.__f__("log", "at pages/login/index.vue:412", "开始登录请求...");
  283. common_vendor.index.request({
  284. url: "http://localhost:9500/user/loginup",
  285. method: "POST",
  286. data: {
  287. username: loginAccount.value,
  288. password: loginPassword.value
  289. },
  290. success(res) {
  291. common_vendor.index.__f__("log", "at pages/login/index.vue:421", "登录响应数据:", res.data);
  292. common_vendor.index.__f__("log", "at pages/login/index.vue:422", "响应状态码:", res.statusCode);
  293. if (res.statusCode === 200 && res.data.code === 200) {
  294. common_vendor.index.setStorageSync("userPhone", loginAccount.value);
  295. common_vendor.index.setStorageSync("isLoggedIn", true);
  296. showLoginPopup.value = false;
  297. common_vendor.index.showToast({
  298. title: "登录成功",
  299. icon: "success",
  300. duration: 1500
  301. });
  302. common_vendor.index.__f__("log", "at pages/login/index.vue:441", "开始跳转到首页...");
  303. common_vendor.index.reLaunch({
  304. url: "/pages/index/index",
  305. success: () => {
  306. common_vendor.index.__f__("log", "at pages/login/index.vue:445", "跳转成功");
  307. },
  308. fail: (err) => {
  309. common_vendor.index.__f__("error", "at pages/login/index.vue:448", "reLaunch 跳转失败:", err);
  310. common_vendor.index.__f__("log", "at pages/login/index.vue:450", "尝试使用 switchTab 跳转...");
  311. common_vendor.index.switchTab({
  312. url: "/pages/index/index",
  313. success: () => {
  314. common_vendor.index.__f__("log", "at pages/login/index.vue:454", "switchTab 跳转成功");
  315. },
  316. fail: (err2) => {
  317. common_vendor.index.__f__("error", "at pages/login/index.vue:457", "switchTab 跳转失败:", err2);
  318. common_vendor.index.showToast({
  319. title: "页面跳转失败,请重试",
  320. icon: "none"
  321. });
  322. }
  323. });
  324. }
  325. });
  326. } else {
  327. common_vendor.index.__f__("log", "at pages/login/index.vue:467", "登录失败,状态码:", res.statusCode);
  328. common_vendor.index.__f__("log", "at pages/login/index.vue:468", "登录失败,错误信息:", res.data.msg);
  329. common_vendor.index.showToast({
  330. title: res.data.msg || "账号或密码错误",
  331. icon: "none",
  332. duration: 2e3
  333. });
  334. }
  335. },
  336. fail(err) {
  337. common_vendor.index.__f__("error", "at pages/login/index.vue:477", "登录请求失败:", err);
  338. common_vendor.index.showToast({
  339. title: "网络错误,请稍后重试",
  340. icon: "none",
  341. duration: 2e3
  342. });
  343. }
  344. });
  345. }
  346. }
  347. function handleRegister() {
  348. if (!/^1[3-9]\d{9}$/.test(regPhone.value)) {
  349. common_vendor.index.showToast({ title: "请输入正确手机号", icon: "none" });
  350. return;
  351. }
  352. if (!regCode.value) {
  353. common_vendor.index.showToast({ title: "请输入验证码", icon: "none" });
  354. return;
  355. }
  356. if (!regAccount.value) {
  357. common_vendor.index.showToast({ title: "请输入账号", icon: "none" });
  358. return;
  359. }
  360. if (!regPassword.value) {
  361. common_vendor.index.showToast({ title: "请输入密码", icon: "none" });
  362. return;
  363. }
  364. common_vendor.index.__f__("log", "at pages/login/index.vue:505", "开始注册请求...");
  365. common_vendor.index.request({
  366. url: "http://localhost:9500/user/register",
  367. method: "POST",
  368. data: {
  369. phone: regPhone.value,
  370. code: regCode.value,
  371. username: regAccount.value,
  372. password: regPassword.value
  373. },
  374. success(res) {
  375. common_vendor.index.__f__("log", "at pages/login/index.vue:516", "注册响应数据:", res.data);
  376. common_vendor.index.__f__("log", "at pages/login/index.vue:517", "响应状态码:", res.statusCode);
  377. if (res.statusCode === 200) {
  378. common_vendor.index.showToast({
  379. title: "注册成功",
  380. icon: "success",
  381. duration: 1500
  382. });
  383. showRegisterPopup.value = false;
  384. setTimeout(() => {
  385. showLoginPopup.value = true;
  386. loginTab.value = "phone";
  387. loginPhone.value = regPhone.value;
  388. }, 1500);
  389. } else {
  390. common_vendor.index.__f__("log", "at pages/login/index.vue:538", "注册失败,状态码:", res.statusCode);
  391. common_vendor.index.showToast({
  392. title: res.data.msg || "注册失败,请重试",
  393. icon: "none"
  394. });
  395. }
  396. },
  397. fail(err) {
  398. common_vendor.index.__f__("error", "at pages/login/index.vue:546", "注册请求失败:", err);
  399. common_vendor.index.showToast({ title: "网络错误", icon: "none" });
  400. }
  401. });
  402. }
  403. common_vendor.onUnmounted(() => {
  404. if (loginCodeTimer)
  405. clearInterval(loginCodeTimer);
  406. if (regCodeTimer)
  407. clearInterval(regCodeTimer);
  408. });
  409. return (_ctx, _cache) => {
  410. return common_vendor.e({
  411. a: common_assets._imports_1,
  412. b: common_assets._imports_1$1,
  413. c: !agreed.value,
  414. d: common_vendor.o(($event) => socialLogin("wechat")),
  415. e: agreed.value,
  416. f: common_vendor.o(($event) => agreed.value = !agreed.value),
  417. g: common_vendor.o(($event) => openProtocol("service")),
  418. h: common_vendor.o(($event) => openProtocol("privacy")),
  419. i: common_assets._imports_2,
  420. j: common_vendor.o(($event) => showLoginPopup.value = true),
  421. k: common_assets._imports_3,
  422. l: showLoginPopup.value
  423. }, showLoginPopup.value ? common_vendor.e({
  424. m: common_vendor.o(($event) => showLoginPopup.value = false),
  425. n: loginTab.value === "phone" ? 1 : "",
  426. o: common_vendor.o(($event) => loginTab.value = "phone"),
  427. p: loginTab.value === "account" ? 1 : "",
  428. q: common_vendor.o(($event) => loginTab.value = "account"),
  429. r: loginTab.value === "phone"
  430. }, loginTab.value === "phone" ? {
  431. s: loginPhone.value,
  432. t: common_vendor.o(($event) => loginPhone.value = $event.detail.value),
  433. v: loginCode.value,
  434. w: common_vendor.o(($event) => loginCode.value = $event.detail.value),
  435. x: common_vendor.t(loginCodeCountdown.value > 0 ? loginCodeCountdown.value + "s" : "获取验证码"),
  436. y: loginCodeCountdown.value > 0,
  437. z: common_vendor.o(getLoginCode)
  438. } : {
  439. A: loginAccount.value,
  440. B: common_vendor.o(($event) => loginAccount.value = $event.detail.value),
  441. C: loginPassword.value,
  442. D: common_vendor.o(($event) => loginPassword.value = $event.detail.value)
  443. }, {
  444. E: common_vendor.o(handlePopupLogin),
  445. F: common_vendor.o(openRegister)
  446. }) : {}, {
  447. G: showRegisterPopup.value
  448. }, showRegisterPopup.value ? {
  449. H: common_vendor.o(($event) => showRegisterPopup.value = false),
  450. I: regPhone.value,
  451. J: common_vendor.o(($event) => regPhone.value = $event.detail.value),
  452. K: regCode.value,
  453. L: common_vendor.o(($event) => regCode.value = $event.detail.value),
  454. M: common_vendor.t(regCodeCountdown.value > 0 ? regCodeCountdown.value + "s" : "获取验证码"),
  455. N: common_vendor.o(getRegCode),
  456. O: regAccount.value,
  457. P: common_vendor.o(($event) => regAccount.value = $event.detail.value),
  458. Q: regPassword.value,
  459. R: common_vendor.o(($event) => regPassword.value = $event.detail.value),
  460. S: common_vendor.o(handleRegister)
  461. } : {});
  462. };
  463. }
  464. };
  465. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-d08ef7d4"]]);
  466. wx.createPage(MiniProgramPage);
  467. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/login/index.js.map