code.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { ref } from 'vue'
  2. import { sendSmsCode } from '@/api/common'
  3. import { getEnterpriseEmailCode } from '@/api/enterprise'
  4. import { modalStore } from '@/store/modal'
  5. import dayjs from 'dayjs';
  6. import test from './test'
  7. const modal = modalStore()
  8. // 发送验证码
  9. export const getSmsCode = (event, mobile) => {
  10. const lastSendTimer = modal.lastTimer[event];
  11. if (typeof lastSendTimer === 'undefined') {
  12. uni.showToast({
  13. icon: 'error',
  14. title: '短信发送事件错误'
  15. })
  16. return;
  17. }
  18. const duration = dayjs().unix() - lastSendTimer;
  19. const canSend = duration >= 60;
  20. if (!canSend) {
  21. uni.showToast({
  22. icon: 'none',
  23. title: '请稍后再试'
  24. })
  25. return;
  26. }
  27. // 只有 mobile 非空时才校验。因为部分场景(修改密码),不需要输入手机
  28. if (mobile && !test.mobile(mobile)) {
  29. uni.showToast({
  30. icon: 'none',
  31. title: '手机号码格式不正确'
  32. })
  33. return;
  34. }
  35. // 发送验证码 + 更新上次发送验证码时间
  36. let scene = -1;
  37. switch (event) {
  38. case 'resetPassword':
  39. scene = 4;
  40. break;
  41. case 'changePassword':
  42. scene = 3;
  43. break;
  44. case 'changeMobile':
  45. scene = 2;
  46. break;
  47. case 'smsLogin':
  48. scene = 30; // 对接门墩短信登录
  49. case 'smsRegister':
  50. scene = 30; // 对接门墩短信登录
  51. break;
  52. }
  53. sendSmsCode({ phone: mobile, scene }).then((res) => {
  54. if (res.code === 0) {
  55. uni.showToast({
  56. title: '发送成功,请注意查收',
  57. icon: 'none',
  58. duration: 2000
  59. })
  60. modal.$patch((state) => {
  61. state.lastTimer[event] = dayjs().unix();
  62. })
  63. }
  64. })
  65. }
  66. // 发送邮箱验证码
  67. export const getEmailCode = (event, email) => {
  68. const lastSendTimer = modal.lastTimer[event];
  69. if (typeof lastSendTimer === 'undefined') {
  70. uni.showToast({
  71. icon: 'error',
  72. title: '短信发送事件错误'
  73. })
  74. return;
  75. }
  76. const duration = dayjs().unix() - lastSendTimer;
  77. const canSend = duration >= 60;
  78. if (!canSend) {
  79. uni.showToast({
  80. icon: 'none',
  81. title: '请稍后再试'
  82. })
  83. return;
  84. }
  85. if (email && !test.email(email)) {
  86. uni.showToast({
  87. icon: 'none',
  88. title: '企业邮箱格式不正确'
  89. })
  90. return;
  91. }
  92. getEnterpriseEmailCode(email).then((res) => {
  93. if (res.code === 0) {
  94. uni.showToast({
  95. title: '发送成功,请注意查收',
  96. icon: 'none',
  97. duration: 2000
  98. })
  99. modal.$patch((state) => {
  100. state.lastTimer[event] = dayjs().unix();
  101. })
  102. }
  103. })
  104. }
  105. // 验证码倒计时
  106. export const getSmsTimer = (event) => {
  107. const lastSendTimer = modal.lastTimer[event]
  108. if (typeof lastSendTimer === 'undefined') {
  109. uni.showToast({
  110. icon: 'none',
  111. title: '短信发送事件错误'
  112. })
  113. return
  114. }
  115. const duration = ref(dayjs().unix() - lastSendTimer - 60)
  116. const canSend = duration.value >= 0
  117. if (canSend) {
  118. return '获取验证码'
  119. }
  120. if (!canSend) {
  121. setTimeout(() => {
  122. duration.value++;
  123. }, 1000);
  124. return -duration.value.toString() + ' 秒'
  125. }
  126. }