index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. isAgreed: false,
  8. showServiceAgreement: false,
  9. showPrivacyPolicy: false
  10. };
  11. },
  12. methods: {
  13. // 处理协议勾选
  14. handleAgreementChange(e) {
  15. this.isAgreed = e.detail.value.length > 0;
  16. },
  17. // 微信登录
  18. handleWechatLogin() {
  19. if (!this.isAgreed) {
  20. common_vendor.index.showToast({ title: "请先同意用户协议和隐私政策", icon: "none" });
  21. return;
  22. }
  23. common_vendor.wx$1.getUserProfile({
  24. desc: "用于登录和完善个人信息",
  25. // 清晰的授权用途说明
  26. success: async (userRes) => {
  27. const { encryptedData, iv } = userRes;
  28. common_vendor.index.__f__("log", "at pages/login/index.vue:76", userRes);
  29. const loginResult = await new Promise((resolve, reject) => {
  30. common_vendor.wx$1.login({ success: resolve, fail: reject });
  31. });
  32. if (!loginResult.code) {
  33. common_vendor.index.showToast({ title: "登录凭证获取失败", icon: "none" });
  34. return;
  35. }
  36. const code = loginResult.code;
  37. common_vendor.index.__f__("log", "at pages/login/index.vue:87", code);
  38. const avatar_url = userRes.userInfo.avatarUrl;
  39. const nickname = userRes.userInfo.nickName;
  40. try {
  41. const loginRes = await common_vendor.index.request({
  42. url: "http://localhost:9527/api/wx-login",
  43. method: "POST",
  44. data: { code, encryptedData, iv, avatar_url, nickname },
  45. header: { "content-type": "application/json" }
  46. });
  47. common_vendor.index.__f__("log", "at pages/login/index.vue:98", loginRes);
  48. if (loginRes.statusCode !== 200 || loginRes.data.errcode) {
  49. throw new Error(loginRes.data.errmsg || "登录失败");
  50. }
  51. common_vendor.index.setStorageSync("uid", loginRes.data.data.id);
  52. common_vendor.index.setStorageSync("token", loginRes.data.data.token);
  53. common_vendor.index.setStorageSync("userInfo", loginRes.data.data.userInfo);
  54. common_vendor.index.switchTab({ url: "/pages/discover/index" });
  55. } catch (error) {
  56. common_vendor.index.__f__("error", "at pages/login/index.vue:111", "登录接口调用失败:", error);
  57. common_vendor.index.showToast({ title: "登录失败,请重试", icon: "none" });
  58. }
  59. },
  60. fail: (err) => {
  61. common_vendor.index.__f__("error", "at pages/login/index.vue:116", "用户授权失败:", err);
  62. if (err.errCode === 10004) {
  63. common_vendor.index.showToast({ title: "请授权后再登录", icon: "none" });
  64. }
  65. }
  66. });
  67. },
  68. // 跳转到验证码登录页面
  69. navigateToVerifyCodeLogin() {
  70. common_vendor.index.navigateTo({
  71. url: "/pages/login/verify-code"
  72. });
  73. },
  74. // 打开服务协议
  75. openServiceAgreement() {
  76. common_vendor.index.navigateTo({
  77. url: "/pages/agreement/service"
  78. });
  79. },
  80. // 打开隐私政策
  81. openPrivacyPolicy() {
  82. common_vendor.index.navigateTo({
  83. url: "/pages/agreement/privacy"
  84. });
  85. }
  86. }
  87. };
  88. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  89. return {
  90. a: common_assets._imports_0,
  91. b: common_vendor.o((...args) => $options.handleWechatLogin && $options.handleWechatLogin(...args)),
  92. c: common_vendor.o((...args) => $options.navigateToVerifyCodeLogin && $options.navigateToVerifyCodeLogin(...args)),
  93. d: $data.isAgreed,
  94. e: common_vendor.o((...args) => $options.openServiceAgreement && $options.openServiceAgreement(...args)),
  95. f: common_vendor.o((...args) => $options.openPrivacyPolicy && $options.openPrivacyPolicy(...args)),
  96. g: common_vendor.o((...args) => $options.handleAgreementChange && $options.handleAgreementChange(...args))
  97. };
  98. }
  99. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  100. wx.createPage(MiniProgramPage);
  101. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/login/index.js.map