smsCode.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. const mixinSmsCode = {
  2. data() {
  3. return {
  4. smsCode: {
  5. countDownTime: 60,
  6. isGetting: false,
  7. getted: false,
  8. timer: null
  9. }
  10. }
  11. },
  12. methods: {
  13. // 获取验证码方法
  14. getCode(PHONE_KEY, callBack) {
  15. if (!this[PHONE_KEY]) {
  16. uni.showToast({
  17. icon: 'none',
  18. title: '请输入手机号'
  19. });
  20. return;
  21. }
  22. uni.showLoading({
  23. title: '正在获取验证码'
  24. });
  25. callBack()
  26. },
  27. // 验证码请求后续
  28. getSmsCodeOver() {
  29. uni.hideLoading();
  30. this.smsCode.isGetting = true;
  31. this.smsCode.getted = true;
  32. this.smsCode.timer = setInterval(() => {
  33. if (this.smsCode.countDownTime > 0 && this.smsCode.countDownTime <= 60) {
  34. this.smsCode.countDownTime--;
  35. } else {
  36. this.resetCountDown(false);
  37. }
  38. }, 1000);
  39. },
  40. // 倒计时初始化
  41. resetCountDown(isInit) {
  42. clearInterval(this.smsCode.timer);
  43. this.smsCode = {
  44. countDownTime: 60,
  45. isGetting: false,
  46. getted: isInit ? false : true,
  47. timer: null
  48. };
  49. this.$forceUpdate();
  50. },
  51. }
  52. }
  53. export default mixinSmsCode