login.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const api_user = require("../../api/user.js");
  4. const _sfc_main = {
  5. data() {
  6. return {
  7. isAgreed: false,
  8. wxLoginCode: "",
  9. tenant_id: 1
  10. // 默认租户ID
  11. };
  12. },
  13. onLoad() {
  14. this.getTenantId();
  15. },
  16. methods: {
  17. // 获取本地存储的tenant_id,如果没有则使用默认值1
  18. getTenantId() {
  19. const tenantId = common_vendor.index.getStorageSync("tenant_id");
  20. if (tenantId) {
  21. this.tenant_id = tenantId;
  22. console.log("从本地存储获取tenant_id:", tenantId);
  23. return tenantId;
  24. } else {
  25. console.log("使用默认tenant_id:", this.tenant_id);
  26. return this.tenant_id;
  27. }
  28. },
  29. toggleAgreement() {
  30. this.isAgreed = !this.isAgreed;
  31. },
  32. handleLogin() {
  33. if (!this.isAgreed) {
  34. common_vendor.index.showToast({
  35. title: "请先阅读并同意相关协议",
  36. icon: "none"
  37. });
  38. return;
  39. }
  40. common_vendor.index.showModal({
  41. title: "微信登录",
  42. content: "是否使用微信账号登录?",
  43. confirmText: "确认登录",
  44. cancelText: "取消",
  45. success: (res) => {
  46. if (res.confirm) {
  47. this.getWxLoginCode();
  48. } else {
  49. console.log("用户取消了登录");
  50. common_vendor.index.showToast({
  51. title: "已取消登录",
  52. icon: "none"
  53. });
  54. }
  55. }
  56. });
  57. },
  58. getWxLoginCode() {
  59. common_vendor.index.showLoading({ title: "登录中..." });
  60. common_vendor.index.login({
  61. provider: "weixin",
  62. success: (loginRes) => {
  63. console.log("获取微信登录code成功:", loginRes.code);
  64. this.wxLoginCode = loginRes.code;
  65. common_vendor.index.hideLoading();
  66. this.getUserProfile();
  67. },
  68. fail: (err) => {
  69. common_vendor.index.hideLoading();
  70. console.error("获取微信登录code失败:", err);
  71. common_vendor.index.showToast({
  72. title: "微信登录失败",
  73. icon: "none"
  74. });
  75. }
  76. });
  77. },
  78. getUserProfile() {
  79. common_vendor.wx$1.getUserProfile({
  80. desc: "用于完善用户资料",
  81. success: (profileRes) => {
  82. console.log("获取用户信息成功:", profileRes);
  83. const loginParams = {
  84. code: this.wxLoginCode,
  85. userInfo: profileRes.userInfo,
  86. tenant_id: this.getTenantId() || 1,
  87. signature: profileRes.signature,
  88. rawData: profileRes.rawData,
  89. encryptedData: profileRes.encryptedData,
  90. iv: profileRes.iv
  91. };
  92. this.wxLoginRequest(loginParams);
  93. },
  94. fail: (err) => {
  95. console.error("获取用户信息失败:", err);
  96. this.wxLoginRequest({
  97. code: this.wxLoginCode,
  98. userInfo: {
  99. nickName: "微信用户",
  100. avatarUrl: "",
  101. gender: 0,
  102. province: "",
  103. city: "",
  104. country: "",
  105. tenant_id: this.getTenantId() || 1
  106. },
  107. tenant_id: this.getTenantId() || 1
  108. });
  109. }
  110. });
  111. },
  112. async wxLoginRequest(loginParams) {
  113. try {
  114. common_vendor.index.showLoading({ title: "登录中..." });
  115. console.log("发送登录请求,参数:", loginParams);
  116. const data = await api_user.wxLogin(loginParams);
  117. console.log("登录成功,返回数据:", data);
  118. const userData = this.buildUserData(data, loginParams);
  119. this.saveLoginState(data, userData);
  120. } catch (error) {
  121. this.handleLoginError(error, loginParams);
  122. }
  123. },
  124. buildUserData(data, loginParams) {
  125. console.log("构建用户数据,服务器返回:", data);
  126. let userData = {
  127. username: data.username || data.nickName || (loginParams.userInfo ? loginParams.userInfo.nickName : "微信用户"),
  128. name: data.name || data.username || data.nickName || (loginParams.userInfo ? loginParams.userInfo.nickName : ""),
  129. id: data.id || data.userId || data.user_id || data.openid || "",
  130. userId: data.userId || data.user_id || data.id || data.openid || "",
  131. avatar: data.avatar || data.avatarUrl || (loginParams.userInfo ? loginParams.userInfo.avatarUrl : "/static/avatar.png"),
  132. phone: data.phone || data.mobile || "",
  133. gender: data.gender || (loginParams.userInfo ? loginParams.userInfo.gender : 0),
  134. openid: data.openid || "",
  135. unionid: data.unionid || "",
  136. session_key: data.session_key || "",
  137. is_new_user: data.is_new_user || false,
  138. loginTime: (/* @__PURE__ */ new Date()).getTime()
  139. /* tenant_id: 1 */
  140. };
  141. console.log("构建的用户数据:", userData);
  142. return userData;
  143. },
  144. saveLoginState(data, userData) {
  145. const token = data.token || data.session_key || "";
  146. if (token) {
  147. common_vendor.index.setStorageSync("token", token);
  148. console.log("Token已保存:", token);
  149. }
  150. if (userData.avatar) {
  151. console.log("用户头像已保存:", userData.avatar);
  152. } else if (data.avatarUrl) {
  153. userData.avatar = data.avatarUrl;
  154. }
  155. common_vendor.index.setStorageSync("userInfo", JSON.stringify(userData));
  156. console.log("基本用户信息已保存");
  157. if (userData.id) {
  158. this.fetchUserDetail(userData.id, userData, userData.openid);
  159. } else {
  160. this.completeLogin();
  161. }
  162. },
  163. // 获取用户详细信息
  164. async fetchUserDetail(userId, basicUserData, openid) {
  165. try {
  166. console.log("获取用户详细信息,用户ID:", userId);
  167. const userDetail = await api_user.getUserInfo(userId, openid);
  168. console.log("获取用户详细信息成功:", userDetail);
  169. if (userDetail) {
  170. const updatedUserInfo = {
  171. ...basicUserData,
  172. // 更新基本信息
  173. name: userDetail.name || basicUserData.name,
  174. phone: userDetail.phone || userDetail.mobile || basicUserData.phone,
  175. gender: userDetail.gender || basicUserData.gender,
  176. // 添加详细信息
  177. id_card: userDetail.id_card || "",
  178. emergency_contact: userDetail.emergency_contact || "",
  179. emergency_phone: userDetail.emergency_phone || "",
  180. relation: userDetail.relation || "",
  181. // 其他可能的字段
  182. ...userDetail
  183. };
  184. common_vendor.index.setStorageSync("userInfo", JSON.stringify(updatedUserInfo));
  185. console.log("用户详细信息已更新并保存");
  186. }
  187. } catch (error) {
  188. console.error("获取用户详细信息失败:", error);
  189. } finally {
  190. this.completeLogin();
  191. }
  192. },
  193. // 完成登录流程
  194. completeLogin() {
  195. common_vendor.index.hideLoading();
  196. common_vendor.index.showToast({
  197. title: "登录成功",
  198. icon: "success",
  199. success: () => {
  200. setTimeout(() => {
  201. common_vendor.index.switchTab({
  202. url: "/pages/index/index"
  203. });
  204. }, 1500);
  205. }
  206. });
  207. },
  208. handleLoginError(error, loginParams) {
  209. common_vendor.index.hideLoading();
  210. if (error.message && (error.message.includes("code无效") || error.message.includes("已过期") || error.message.includes("已被使用") || error.status === 999)) {
  211. console.error("微信登录code无效,重新获取:", error);
  212. common_vendor.index.showToast({
  213. title: "code已过期,请重试",
  214. icon: "none",
  215. duration: 2e3
  216. });
  217. setTimeout(() => {
  218. this.getWxLoginCode();
  219. }, 1e3);
  220. return;
  221. }
  222. console.error("微信登录失败:", error);
  223. common_vendor.index.showToast({
  224. title: error.message || "登录失败",
  225. icon: "none"
  226. });
  227. },
  228. navigateToPrivacy() {
  229. common_vendor.index.navigateTo({
  230. url: "/pages/privacy/privacy"
  231. });
  232. },
  233. navigateToAgreement() {
  234. common_vendor.index.navigateTo({
  235. url: "/pages/agreement/agreement"
  236. });
  237. }
  238. }
  239. };
  240. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  241. return {
  242. a: common_vendor.o((...args) => $options.handleLogin && $options.handleLogin(...args)),
  243. b: $data.isAgreed,
  244. c: common_vendor.o((...args) => $options.toggleAgreement && $options.toggleAgreement(...args)),
  245. d: common_vendor.o((...args) => $options.navigateToAgreement && $options.navigateToAgreement(...args))
  246. };
  247. }
  248. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  249. wx.createPage(MiniProgramPage);