phoneCode.vue 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <view class="phone-code">
  3. <view class="i-code" @tap="$tools.throttle(getCode)" :class="{disabled:isCountDown}">
  4. {{countDownOrGetCodeText}}
  5. </view>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. name: 'phoneCode',
  11. props: {
  12. mobile: {
  13. type: String,
  14. default: ''
  15. },
  16. event: {
  17. type: String,
  18. default: ''
  19. },
  20. },
  21. data() {
  22. return {
  23. isCountDown:false, //如倒计时,更改countDownOrGetCodeText
  24. countDownOrGetCodeText:"获取验证码",
  25. countTime:60,
  26. isOut:false,//去除定时
  27. timer:'',
  28. }
  29. },
  30. beforeDestroy() {
  31. this.isOut=true;
  32. // console.log('phoneCode 调一次')
  33. if(this.timer){
  34. clearTimeout(this.timer);
  35. this.timer=null;
  36. }
  37. },
  38. methods: {
  39. countDown(){
  40. if(this.isOut){
  41. return;
  42. }
  43. console.log('countDown定时一次')
  44. this.countDownOrGetCodeText="重新获取("+this.countTime+")";
  45. this.countTime--;
  46. if (this.countTime > 0) {
  47. this.timer=setTimeout(this.countDown, 1000);
  48. }else{
  49. this.countDownOrGetCodeText="获取验证码";
  50. this.isCountDown=false;
  51. this.countTime=60;
  52. }
  53. },
  54. async getCode(){
  55. // console.log('===',props.mobile)
  56. if(! this.$tools.match( this.mobile,'mobile')){
  57. return;
  58. }
  59. if(this.isCountDown==true){
  60. return;
  61. }
  62. this.isCountDown=true;
  63. try{
  64. await this.$request('user.sms',{mobile:this.mobile,event:this.event});
  65. }catch(err){
  66. // console.log('err---',err)
  67. this.$tools.msg(err.msg || '请求失败')
  68. this.isCountDown=false;
  69. this.countDownOrGetCodeText="获取验证码"
  70. this.countTime=60;
  71. }
  72. if(this.isCountDown){
  73. this.countDown();
  74. }
  75. },
  76. }
  77. }
  78. </script>
  79. <style lang="scss" scoped>
  80. .i-code{
  81. margin-left: auto;
  82. font-size: 26rpx;
  83. color: $base-color;
  84. &.disabled{
  85. color:#999;
  86. }
  87. }
  88. </style>