useModal.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const sheep_store_index = require("../store/index.js");
  4. const sheep_helper_index = require("../helper/index.js");
  5. const sheep_helper_test = require("../helper/test.js");
  6. const sheep_api_member_auth = require("../api/member/auth.js");
  7. const sheep_index = require("../index.js");
  8. const sheep_api_member_signin = require("../api/member/signin.js");
  9. let time = 30;
  10. let timer = null;
  11. async function onSign() {
  12. console.log("onSign");
  13. const {
  14. code,
  15. data
  16. } = await sheep_api_member_signin.SignInApi.createSignInRecord();
  17. if (code === 0) {
  18. showSignUpModal(data);
  19. }
  20. if (timer) {
  21. clearInterval(timer);
  22. }
  23. common_vendor.index.setStorageSync("isRun", false);
  24. common_vendor.index.setStorageSync("isSign", true);
  25. }
  26. function resetSignStatusIfNeeded() {
  27. const today = (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
  28. const lastCheckDate = common_vendor.index.getStorageSync("lastCheckDate");
  29. if (today !== lastCheckDate) {
  30. common_vendor.index.setStorageSync("isSign", false);
  31. common_vendor.index.setStorageSync("lastCheckDate", today);
  32. }
  33. }
  34. function autoSign() {
  35. resetSignStatusIfNeeded();
  36. const isLogin = common_vendor.computed(() => sheep_index.sheep.$store("user").isLogin);
  37. if (isLogin.value) {
  38. common_vendor.index.setStorageSync("isRun", true);
  39. if (common_vendor.index.getStorageSync("isRun") && !common_vendor.index.getStorageSync("isSign")) {
  40. timer = setInterval(() => {
  41. time--;
  42. if (common_vendor.index.getStorageSync("isSign") || !isLogin.value) {
  43. cancelAutoSign();
  44. }
  45. if (time <= 0 && !common_vendor.index.getStorageSync("isSign")) {
  46. clearInterval(timer);
  47. onSign();
  48. }
  49. }, 1e3);
  50. }
  51. }
  52. }
  53. function cancelAutoSign() {
  54. if (timer) {
  55. clearInterval(timer);
  56. }
  57. time = 30;
  58. common_vendor.index.setStorageSync("isRun", false);
  59. }
  60. function showSignUpModal(obj) {
  61. sheep_store_index.$store("modal").$patch((state) => {
  62. state.signUp = true;
  63. state.signUpInfo = obj;
  64. });
  65. }
  66. function colseSignUpModal() {
  67. sheep_store_index.$store("modal").$patch((state) => {
  68. state.signUp = false;
  69. });
  70. }
  71. function showWalletModal(obj) {
  72. sheep_store_index.$store("modal").$patch((state) => {
  73. state.getWallet = true;
  74. state.getWalletInfo = obj;
  75. });
  76. }
  77. function colseWalletModal() {
  78. sheep_store_index.$store("modal").$patch((state) => {
  79. state.getWallet = false;
  80. });
  81. }
  82. function showAuthModal(type = "accountLogin", isActive = "accountLogin") {
  83. const modal = sheep_store_index.$store("modal");
  84. if (modal.auth !== "") {
  85. closeAuthModal();
  86. setTimeout(() => {
  87. modal.$patch((state) => {
  88. state.auth = type;
  89. state.isActive = isActive;
  90. });
  91. }, 100);
  92. } else {
  93. modal.$patch((state) => {
  94. state.auth = type;
  95. state.isActive = isActive;
  96. });
  97. }
  98. }
  99. function closeAuthModal() {
  100. sheep_store_index.$store("modal").$patch((state) => {
  101. state.auth = "";
  102. });
  103. }
  104. function showShareModal(spuId = 0) {
  105. sheep_store_index.$store("modal").$patch((state) => {
  106. state.share = true;
  107. state.shareInfo.spuId = spuId;
  108. });
  109. }
  110. function closeShareModal() {
  111. sheep_store_index.$store("modal").$patch((state) => {
  112. state.share = false;
  113. });
  114. }
  115. function showMenuTools() {
  116. sheep_store_index.$store("modal").$patch((state) => {
  117. state.menu = true;
  118. });
  119. }
  120. function closeMenuTools() {
  121. sheep_store_index.$store("modal").$patch((state) => {
  122. state.menu = false;
  123. });
  124. }
  125. function getSmsCode(event, mobile) {
  126. const modalStore = sheep_store_index.$store("modal");
  127. const lastSendTimer = modalStore.lastTimer[event];
  128. console.log(lastSendTimer);
  129. if (typeof lastSendTimer === "undefined") {
  130. sheep_helper_index.$helper.toast("短信发送事件错误");
  131. return;
  132. }
  133. const duration = common_vendor.dayjs().unix() - lastSendTimer;
  134. const canSend = duration >= 60;
  135. if (!canSend) {
  136. sheep_helper_index.$helper.toast("请稍后再试");
  137. return;
  138. }
  139. if (mobile && !sheep_helper_test.test.mobile(mobile)) {
  140. sheep_helper_index.$helper.toast("手机号码格式不正确");
  141. return;
  142. }
  143. let scene = -1;
  144. switch (event) {
  145. case "resetPassword":
  146. scene = 4;
  147. break;
  148. case "changePassword":
  149. scene = 3;
  150. break;
  151. case "changeMobileOld":
  152. scene = 2;
  153. break;
  154. case "changeMobileNew":
  155. scene = 5;
  156. break;
  157. case "smsLogin":
  158. scene = 1;
  159. break;
  160. }
  161. sheep_api_member_auth.AuthUtil.sendSmsCode(mobile, scene).then((res) => {
  162. if (res.code === 0) {
  163. modalStore.$patch((state) => {
  164. state.lastTimer[event] = common_vendor.dayjs().unix();
  165. });
  166. }
  167. });
  168. }
  169. function getSmsTimer(event, mobile = "") {
  170. const modalStore = sheep_store_index.$store("modal");
  171. const lastSendTimer = modalStore.lastTimer[event];
  172. if (typeof lastSendTimer === "undefined") {
  173. sheep_helper_index.$helper.toast("短信发送事件错误");
  174. return;
  175. }
  176. const duration = common_vendor.ref(common_vendor.dayjs().unix() - lastSendTimer - 60);
  177. const canSend = duration.value >= 0;
  178. if (canSend) {
  179. return "获取验证码";
  180. }
  181. if (!canSend) {
  182. setTimeout(() => {
  183. duration.value++;
  184. }, 1e3);
  185. return -duration.value.toString() + " 秒";
  186. }
  187. }
  188. function saveAdvHistory(adv) {
  189. const modal = sheep_store_index.$store("modal");
  190. modal.$patch((state) => {
  191. if (!state.advHistory.includes(adv.imgUrl)) {
  192. state.advHistory.push(adv.imgUrl);
  193. }
  194. });
  195. }
  196. exports.autoSign = autoSign;
  197. exports.cancelAutoSign = cancelAutoSign;
  198. exports.closeAuthModal = closeAuthModal;
  199. exports.closeMenuTools = closeMenuTools;
  200. exports.closeShareModal = closeShareModal;
  201. exports.colseSignUpModal = colseSignUpModal;
  202. exports.colseWalletModal = colseWalletModal;
  203. exports.getSmsCode = getSmsCode;
  204. exports.getSmsTimer = getSmsTimer;
  205. exports.resetSignStatusIfNeeded = resetSignStatusIfNeeded;
  206. exports.saveAdvHistory = saveAdvHistory;
  207. exports.showAuthModal = showAuthModal;
  208. exports.showMenuTools = showMenuTools;
  209. exports.showShareModal = showShareModal;
  210. exports.showWalletModal = showWalletModal;