login.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const utils_api = require("../../utils/api.js");
  4. const _sfc_main = {
  5. data() {
  6. return {
  7. username: "",
  8. password: "",
  9. showPassword: false,
  10. isLoading: false
  11. };
  12. },
  13. onLoad(options) {
  14. if (options.username) {
  15. this.username = decodeURIComponent(options.username);
  16. common_vendor.index.showToast({
  17. title: "注册成功,请输入密码登录",
  18. icon: "success",
  19. duration: 2e3
  20. });
  21. }
  22. },
  23. computed: {
  24. canLogin() {
  25. return this.username.trim().length > 0 && this.password.length > 0 && !this.isLoading;
  26. }
  27. },
  28. methods: {
  29. togglePassword() {
  30. this.showPassword = !this.showPassword;
  31. },
  32. handleLogin() {
  33. if (!this.canLogin) {
  34. common_vendor.index.showToast({
  35. title: "请填写完整信息",
  36. icon: "none"
  37. });
  38. return;
  39. }
  40. this.isLoading = true;
  41. common_vendor.index.showLoading({
  42. title: "登录中...",
  43. mask: true
  44. });
  45. utils_api.login(this.username.trim(), this.password).then((res) => {
  46. common_vendor.index.hideLoading();
  47. this.isLoading = false;
  48. if (res.code === 200 && res.data) {
  49. try {
  50. const userInfo = res.data.user || {};
  51. common_vendor.index.setStorageSync("userInfo", {
  52. id: userInfo.id,
  53. username: userInfo.username,
  54. nickname: userInfo.nickname,
  55. avatar: userInfo.avatar,
  56. gender: userInfo.gender,
  57. birthday: userInfo.birthday,
  58. bio: userInfo.bio,
  59. phone: userInfo.phone,
  60. email: userInfo.email,
  61. isVip: userInfo.isVip,
  62. token: res.data.token,
  63. loginTime: (/* @__PURE__ */ new Date()).getTime()
  64. });
  65. common_vendor.index.setStorageSync("isLogin", true);
  66. } catch (e) {
  67. common_vendor.index.__f__("error", "at pages/login/login.vue:153", "保存用户信息失败", e);
  68. }
  69. common_vendor.index.showToast({
  70. title: "登录成功",
  71. icon: "success"
  72. });
  73. setTimeout(() => {
  74. common_vendor.index.switchTab({
  75. url: "/pages/index/index"
  76. });
  77. }, 1500);
  78. } else {
  79. common_vendor.index.showToast({
  80. title: res.message || "登录失败",
  81. icon: "none"
  82. });
  83. }
  84. }).catch((err) => {
  85. common_vendor.index.hideLoading();
  86. this.isLoading = false;
  87. common_vendor.index.__f__("error", "at pages/login/login.vue:177", "登录失败:", err);
  88. common_vendor.index.showToast({
  89. title: err.message || "登录失败,请检查网络连接",
  90. icon: "none",
  91. duration: 2e3
  92. });
  93. });
  94. },
  95. loginWithWeChat() {
  96. common_vendor.index.showToast({
  97. title: "微信登录",
  98. icon: "none"
  99. });
  100. },
  101. loginWithQQ() {
  102. common_vendor.index.showToast({
  103. title: "QQ登录",
  104. icon: "none"
  105. });
  106. },
  107. loginWithWeibo() {
  108. common_vendor.index.showToast({
  109. title: "微博登录",
  110. icon: "none"
  111. });
  112. },
  113. goToRegister() {
  114. common_vendor.index.navigateTo({
  115. url: "/pages/register/register"
  116. });
  117. }
  118. }
  119. };
  120. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  121. return {
  122. a: $data.username,
  123. b: common_vendor.o(($event) => $data.username = $event.detail.value),
  124. c: $data.showPassword ? "text" : "password",
  125. d: $data.password,
  126. e: common_vendor.o(($event) => $data.password = $event.detail.value),
  127. f: common_vendor.t($data.showPassword ? "隐藏" : "显示"),
  128. g: common_vendor.o((...args) => $options.togglePassword && $options.togglePassword(...args)),
  129. h: common_vendor.t($data.isLoading ? "登录中..." : "登录"),
  130. i: common_vendor.o((...args) => $options.handleLogin && $options.handleLogin(...args)),
  131. j: !$options.canLogin,
  132. k: common_vendor.o((...args) => $options.goToRegister && $options.goToRegister(...args)),
  133. l: common_vendor.o((...args) => $options.loginWithWeChat && $options.loginWithWeChat(...args)),
  134. m: common_vendor.o((...args) => $options.loginWithQQ && $options.loginWithQQ(...args)),
  135. n: common_vendor.o((...args) => $options.loginWithWeibo && $options.loginWithWeibo(...args))
  136. };
  137. }
  138. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-e4e4508d"]]);
  139. wx.createPage(MiniProgramPage);
  140. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/login/login.js.map