You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.3 KiB

4 years ago
  1. <template>
  2. <view class="mix-get-code" @click="getCode">
  3. <view v-if="loading" class="loading">
  4. <mix-icon-loading size="28rpx" color="#0083ff"></mix-icon-loading>
  5. </view>
  6. <text class="text" :class="{disabled: timeDown > 0}">
  7. {{ timeDown > 0 ? '重新获取 ' + timeDown + 's' : '获取验证码' }}
  8. </text>
  9. </view>
  10. </template>
  11. <script>
  12. /**
  13. * 手机验证码
  14. * @prop mobile 手机号
  15. * @prop templateCode 短信模版id
  16. */
  17. import {checkStr} from '@/common/js/util'
  18. export default {
  19. //获取手机验证码
  20. name: 'MixMobileCode',
  21. data() {
  22. return {
  23. loading: false,
  24. timeDown: ''
  25. }
  26. },
  27. props: {
  28. mobile: {
  29. type: String,
  30. default: ''
  31. },
  32. templateCode: {
  33. type: String,
  34. default: ''
  35. },
  36. action: {
  37. type: String,
  38. default: '用户注册' //设置支付密码
  39. }
  40. },
  41. methods: {
  42. //获取验证码
  43. async getCode(){
  44. if(this.timeDown > 0){
  45. return;
  46. }
  47. this.$util.throttle(()=>{
  48. const mobile = this.mobile || this.$store.state.userInfo.username;;
  49. if(!checkStr(mobile, 'mobile')){
  50. this.$util.msg('手机号码格式不正确');
  51. return;
  52. }
  53. this.loading = true;
  54. this.$request('smsCode', 'send', {
  55. mobile,
  56. action: this.action, //uni短信必填
  57. TemplateCode: this.templateCode, //阿里云必填
  58. }).then(response=>{
  59. this.$util.msg(response.msg);
  60. this.loading = false;
  61. if(response.status === 1){
  62. this.countDown(60);
  63. }
  64. }).catch(err=>{
  65. this.$util.msg('验证码发送失败');
  66. this.loading = false;
  67. console.log(err);
  68. })
  69. }, 2000)
  70. },
  71. //倒计时
  72. countDown(timer){
  73. timer --;
  74. this.timeDown = timer;
  75. if(timer > 0){
  76. setTimeout(()=>{
  77. this.countDown(timer);
  78. }, 1000)
  79. }
  80. },
  81. }
  82. }
  83. </script>
  84. <style scoped lang="scss">
  85. .mix-get-code{
  86. flex-shrink: 0;
  87. display: flex;
  88. justify-content: space-between;
  89. align-items: center;
  90. height: 36rpx;
  91. &:before{
  92. content: '';
  93. width: 0;
  94. height: 40;
  95. border-right: 1px solid #f0f0f0;
  96. }
  97. .loading{
  98. margin-right: 8rpx;
  99. }
  100. .text{
  101. line-height: 28rpx;
  102. font-size: 26rpx;
  103. color: #40a2ff;
  104. &.disabled{
  105. color: #ccc;
  106. }
  107. }
  108. }
  109. </style>