login.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const common_assets = require("../../common/assets.js");
  4. const api_auth = require("../../api/auth.js");
  5. const _sfc_main = {
  6. __name: "login",
  7. setup(__props) {
  8. const isAgree = common_vendor.ref(false);
  9. const showAgreementModal = common_vendor.ref(false);
  10. const showDocModal = common_vendor.ref(false);
  11. const docTitle = common_vendor.ref("");
  12. const docContent = common_vendor.ref("");
  13. const loginCode = common_vendor.ref("");
  14. const getLoginCode = () => {
  15. common_vendor.index.login({
  16. provider: "weixin",
  17. success: (res) => {
  18. loginCode.value = res.code;
  19. }
  20. });
  21. };
  22. common_vendor.onMounted(() => {
  23. const token = common_vendor.index.getStorageSync("token");
  24. const userInfo = common_vendor.index.getStorageSync("userInfo");
  25. if (token && userInfo && userInfo.studentId) {
  26. if (userInfo.isNewUser && !userInfo.name) {
  27. common_vendor.index.redirectTo({ url: "/pages/profile/profile" });
  28. } else {
  29. common_vendor.index.switchTab({ url: "/pages/jobs/jobs" });
  30. }
  31. return;
  32. }
  33. getLoginCode();
  34. });
  35. const openDocModal = async (type) => {
  36. try {
  37. const res = await api_auth.getAgreement(type);
  38. if (res.code === 200) {
  39. docTitle.value = res.data.title;
  40. docContent.value = res.data.content;
  41. showDocModal.value = true;
  42. return;
  43. }
  44. } catch (err) {
  45. common_vendor.index.__f__("error", "at pages/login/login.vue:153", "获取协议失败", err);
  46. common_vendor.index.showToast({
  47. title: "获取协议失败,请稍后重试",
  48. icon: "none"
  49. });
  50. }
  51. };
  52. const closeDocModal = () => {
  53. showDocModal.value = false;
  54. };
  55. const onAgreeChange = (e) => {
  56. isAgree.value = e.detail.value.length > 0;
  57. };
  58. const closeAgreementModal = () => {
  59. showAgreementModal.value = false;
  60. };
  61. const handleDirectLogin = async (e) => {
  62. if (e.detail.errMsg !== "getPhoneNumber:ok") {
  63. common_vendor.index.showToast({
  64. title: "授权失败",
  65. icon: "none"
  66. });
  67. return;
  68. }
  69. await onAllowLogin(e.detail.code);
  70. };
  71. const handleDirectLoginFromModal = async (e) => {
  72. isAgree.value = true;
  73. showAgreementModal.value = false;
  74. if (e.detail.errMsg !== "getPhoneNumber:ok") {
  75. common_vendor.index.showToast({
  76. title: "授权失败",
  77. icon: "none"
  78. });
  79. return;
  80. }
  81. await onAllowLogin(e.detail.code);
  82. };
  83. const onAllowLogin = async (phoneCode) => {
  84. common_vendor.index.showLoading({ title: "登录中..." });
  85. try {
  86. const res = await api_auth.wechatLogin({
  87. code: loginCode.value,
  88. phoneCode
  89. });
  90. common_vendor.index.hideLoading();
  91. if (res.code === 200) {
  92. common_vendor.index.setStorageSync("token", res.data.token);
  93. common_vendor.index.setStorageSync("userInfo", res.data);
  94. common_vendor.index.showToast({
  95. title: "登录成功",
  96. icon: "success"
  97. });
  98. setTimeout(() => {
  99. if (res.data.isNewUser) {
  100. common_vendor.index.navigateTo({ url: "/pages/profile/profile" });
  101. } else {
  102. common_vendor.index.switchTab({ url: "/pages/jobs/jobs" });
  103. }
  104. }, 1e3);
  105. }
  106. } catch (err) {
  107. common_vendor.index.__f__("error", "at pages/login/login.vue:234", "登录异常", err);
  108. common_vendor.index.hideLoading();
  109. getLoginCode();
  110. }
  111. };
  112. return (_ctx, _cache) => {
  113. return common_vendor.e({
  114. a: isAgree.value
  115. }, isAgree.value ? {
  116. b: common_vendor.o(handleDirectLogin)
  117. } : {
  118. c: common_vendor.o(($event) => showAgreementModal.value = true)
  119. }, {
  120. d: isAgree.value,
  121. e: common_vendor.o(($event) => openDocModal("service")),
  122. f: common_vendor.o(($event) => openDocModal("privacy")),
  123. g: common_vendor.o(onAgreeChange),
  124. h: common_assets._imports_0,
  125. i: common_assets._imports_1,
  126. j: common_assets._imports_2,
  127. k: common_assets._imports_3,
  128. l: common_vendor.o(closeAgreementModal),
  129. m: common_vendor.o(($event) => openDocModal("service")),
  130. n: common_vendor.o(($event) => openDocModal("privacy")),
  131. o: common_vendor.o(closeAgreementModal),
  132. p: common_vendor.o(handleDirectLoginFromModal),
  133. q: showAgreementModal.value ? 1 : "",
  134. r: common_vendor.o(closeDocModal),
  135. s: common_vendor.t(docTitle.value),
  136. t: docContent.value,
  137. v: common_vendor.o(closeDocModal),
  138. w: showDocModal.value ? 1 : ""
  139. });
  140. };
  141. }
  142. };
  143. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-e4e4508d"]]);
  144. wx.createPage(MiniProgramPage);
  145. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/login/login.js.map