index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const utils_assets = require("../../utils/assets.js");
  4. const api_system_agreement = require("../../api/system/agreement.js");
  5. const api_auth_index = require("../../api/auth/index.js");
  6. const _sfc_main = {
  7. data() {
  8. return {
  9. assets: utils_assets.assets,
  10. isAgreed: false,
  11. activeModal: "",
  12. avatarBase64: "",
  13. userName: "微信用户",
  14. currentProtocol: { title: "", content: "" },
  15. protocols: {
  16. user: { title: "", content: "" },
  17. privacy: { title: "", content: "" }
  18. },
  19. openId: "",
  20. unionId: "",
  21. phoneNumber: ""
  22. };
  23. },
  24. methods: {
  25. toggleAgreed() {
  26. this.isAgreed = !this.isAgreed;
  27. },
  28. startLoginFlow() {
  29. if (!this.isAgreed)
  30. this.activeModal = "confirm";
  31. else
  32. this.performLogin();
  33. },
  34. agreeAndClose() {
  35. this.isAgreed = true;
  36. this.activeModal = "";
  37. },
  38. async performLogin() {
  39. try {
  40. common_vendor.index.showLoading({ title: "登录中..." });
  41. const loginRes = await new Promise((resolve, reject) => {
  42. common_vendor.wx$1.login({
  43. success: resolve,
  44. fail: reject
  45. });
  46. });
  47. if (!loginRes.code) {
  48. common_vendor.index.hideLoading();
  49. common_vendor.index.showToast({ title: "获取登录凭证失败", icon: "none" });
  50. return;
  51. }
  52. const res = await api_auth_index.wechatLogin({ loginCode: loginRes.code });
  53. common_vendor.index.hideLoading();
  54. if (res.data && res.data.accessToken) {
  55. common_vendor.index.setStorageSync("token", res.data.accessToken);
  56. common_vendor.index.setStorageSync("isLogin", true);
  57. common_vendor.index.showToast({ title: "登录成功", icon: "success" });
  58. setTimeout(() => {
  59. common_vendor.index.reLaunch({ url: "/pages/order/index" });
  60. }, 1e3);
  61. } else if (res.data && res.data.openid) {
  62. this.openId = res.data.openid;
  63. this.unionId = res.data.unionid || "";
  64. this.activeModal = "profile";
  65. } else {
  66. common_vendor.index.showToast({ title: "登录失败", icon: "none" });
  67. }
  68. } catch (error) {
  69. common_vendor.index.hideLoading();
  70. common_vendor.index.__f__("error", "at pages/login/index.vue:196", "登录错误:", error);
  71. common_vendor.index.showToast({ title: "登录失败,请重试", icon: "none" });
  72. }
  73. },
  74. onChooseAvatar(e) {
  75. const tempPath = e.detail.avatarUrl;
  76. common_vendor.index.__f__("log", "at pages/login/index.vue:202", "[微信信息] 头像临时路径:", tempPath);
  77. const fs = common_vendor.index.getFileSystemManager();
  78. fs.readFile({
  79. filePath: tempPath,
  80. encoding: "base64",
  81. success: (res) => {
  82. common_vendor.index.__f__("log", "at pages/login/index.vue:208", "[微信信息] 头像 base64 长度:", res.data.length);
  83. this.avatarBase64 = "data:image/jpeg;base64," + res.data;
  84. common_vendor.index.__f__("log", "at pages/login/index.vue:210", "[微信信息] avatarBase64 已赋值,前缀:", this.avatarBase64.substring(0, 30));
  85. },
  86. fail: (err) => {
  87. common_vendor.index.__f__("error", "at pages/login/index.vue:213", "[微信信息] 读取头像 base64 失败:", err);
  88. }
  89. });
  90. },
  91. onNicknameBlur(e) {
  92. this.userName = e.detail.value;
  93. common_vendor.index.__f__("log", "at pages/login/index.vue:219", "[微信信息] 昵称(blur):", this.userName);
  94. },
  95. onNicknameChange(e) {
  96. this.userName = e.detail.value;
  97. common_vendor.index.__f__("log", "at pages/login/index.vue:223", "[微信信息] 昵称(input):", this.userName);
  98. },
  99. async goToPhoneAuth() {
  100. this.phoneNumber = "";
  101. this.activeModal = "phone";
  102. },
  103. async handleGetPhoneNumber(e) {
  104. if (e.detail.errMsg !== "getPhoneNumber:ok") {
  105. common_vendor.index.showToast({ title: "获取手机号失败,请重试", icon: "none" });
  106. return;
  107. }
  108. try {
  109. common_vendor.index.showLoading({ title: "获取手机号中..." });
  110. const phoneRes = await api_auth_index.getWechatPhone({
  111. phoneCode: e.detail.code,
  112. openId: this.openId
  113. });
  114. common_vendor.index.hideLoading();
  115. this.phoneNumber = phoneRes.data;
  116. common_vendor.index.showLoading({ title: "注册中..." });
  117. const registerRes = await api_auth_index.wechatRegister({
  118. openId: this.openId,
  119. unionId: this.unionId,
  120. phone: this.phoneNumber,
  121. nickname: this.userName,
  122. avatar: this.avatarBase64
  123. });
  124. common_vendor.index.hideLoading();
  125. this.performLogin();
  126. } catch (error) {
  127. common_vendor.index.hideLoading();
  128. common_vendor.index.__f__("error", "at pages/login/index.vue:262", "注册错误:", error);
  129. common_vendor.index.showToast({ title: "注册失败,请重试", icon: "none" });
  130. }
  131. },
  132. showProtocol(type) {
  133. this.currentProtocol = this.protocols[type];
  134. this.activeModal = "protocol";
  135. },
  136. closeAllModals() {
  137. this.activeModal = "";
  138. }
  139. },
  140. async mounted() {
  141. try {
  142. const [userRes, privacyRes] = await Promise.all([
  143. api_system_agreement.getAgreement(1),
  144. api_system_agreement.getAgreement(2)
  145. ]);
  146. this.protocols.user = { title: userRes.data.title, content: userRes.data.content };
  147. this.protocols.privacy = { title: privacyRes.data.title, content: privacyRes.data.content };
  148. } catch (e) {
  149. common_vendor.index.__f__("error", "at pages/login/index.vue:281", "[协议] 加载失败", e);
  150. }
  151. }
  152. };
  153. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  154. return common_vendor.e({
  155. a: $data.assets.logo,
  156. b: $data.assets.wechat,
  157. c: common_vendor.o((...args) => $options.startLoginFlow && $options.startLoginFlow(...args), "ec"),
  158. d: $data.isAgreed,
  159. e: common_vendor.o(($event) => $options.showProtocol("user"), "a3"),
  160. f: common_vendor.o(($event) => $options.showProtocol("privacy"), "86"),
  161. g: common_vendor.o((...args) => $options.toggleAgreed && $options.toggleAgreed(...args), "56"),
  162. h: $data.activeModal
  163. }, $data.activeModal ? {
  164. i: common_vendor.o((...args) => $options.closeAllModals && $options.closeAllModals(...args), "a3")
  165. } : {}, {
  166. j: $data.activeModal === "confirm"
  167. }, $data.activeModal === "confirm" ? {
  168. k: common_vendor.o(($event) => $data.activeModal = "", "e1"),
  169. l: common_vendor.o((...args) => $options.agreeAndClose && $options.agreeAndClose(...args), "1f")
  170. } : {}, {
  171. m: $data.activeModal === "profile"
  172. }, $data.activeModal === "profile" ? {
  173. n: common_vendor.o(($event) => $data.activeModal = "", "c9"),
  174. o: common_vendor.o(($event) => $data.activeModal = "phone", "14"),
  175. p: $data.avatarBase64 || "https://img.icons8.com/color/144/user.png",
  176. q: common_vendor.o((...args) => $options.onChooseAvatar && $options.onChooseAvatar(...args), "14"),
  177. r: $data.userName,
  178. s: common_vendor.o((...args) => $options.onNicknameBlur && $options.onNicknameBlur(...args), "60"),
  179. t: common_vendor.o((...args) => $options.onNicknameChange && $options.onNicknameChange(...args), "df"),
  180. v: common_vendor.o((...args) => $options.goToPhoneAuth && $options.goToPhoneAuth(...args), "65")
  181. } : {}, {
  182. w: $data.activeModal === "phone"
  183. }, $data.activeModal === "phone" ? {
  184. x: $data.assets.logo,
  185. y: common_vendor.t($data.phoneNumber || "获取中..."),
  186. z: common_vendor.o(($event) => $data.activeModal = "", "8c"),
  187. A: common_vendor.o((...args) => $options.handleGetPhoneNumber && $options.handleGetPhoneNumber(...args), "f8")
  188. } : {}, {
  189. B: $data.activeModal === "protocol"
  190. }, $data.activeModal === "protocol" ? {
  191. C: common_vendor.t($data.currentProtocol.title),
  192. D: common_vendor.o(($event) => $data.activeModal = "", "d4"),
  193. E: $data.currentProtocol.content,
  194. F: common_vendor.o(($event) => $data.activeModal = "", "7e")
  195. } : {});
  196. }
  197. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-d08ef7d4"]]);
  198. wx.createPage(MiniProgramPage);
  199. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/login/index.js.map