index.js 8.4 KB

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