uni-send-sms-code.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <view class="short-code-btn" hover-class="hover" @click="start">
  3. <text class="inner-text" :class="reverseNumber==0?'inner-text-active':''">{{innerText}}</text>
  4. </view>
  5. </template>
  6. <script>
  7. function debounce(func, wait) {
  8. let timer;
  9. wait = wait || 500;
  10. return function() {
  11. let context = this;
  12. let args = arguments;
  13. if (timer) clearTimeout(timer);
  14. let callNow = !timer;
  15. timer = setTimeout(() => {
  16. timer = null;
  17. }, wait)
  18. if (callNow) func.apply(context, args);
  19. }
  20. }
  21. export default {
  22. name: "uni-send-sms-code",
  23. props: {
  24. /**
  25. * 倒计时时长 s
  26. */
  27. count: {
  28. type: [String, Number],
  29. default: 60
  30. },
  31. /**
  32. * 手机号码
  33. */
  34. phone: {
  35. type: [String, Number],
  36. default: ''
  37. },
  38. /*
  39. 验证码类型,用于防止不同功能的验证码混用,目前支持的类型login登录、register注册、bind绑定手机、unbind解绑手机
  40. */
  41. codeType:{
  42. type: String,
  43. default(){
  44. return 'login'
  45. }
  46. }
  47. },
  48. data() {
  49. return {
  50. reverseNumber: 0,
  51. reverseTimer: null
  52. };
  53. },
  54. computed: {
  55. innerText() {
  56. if (this.reverseNumber == 0) return '获取验证码';
  57. return '重新发送('+this.reverseNumber+'s)';
  58. }
  59. },
  60. created() {
  61. this.initClick();
  62. },
  63. methods: {
  64. initClick() {
  65. this.start = debounce(() => {
  66. if (this.reverseNumber != 0) return;
  67. this.sendMsg();
  68. })
  69. },
  70. sendMsg() {
  71. let reg_phone = /^1\d{10}$/;
  72. if(!reg_phone.test(this.phone))return uni.showToast({
  73. title: '手机号格式错误',
  74. icon: 'none'
  75. });
  76. uniCloud.callFunction({
  77. name:'uni-id-cf',
  78. data:{
  79. action:'sendSmsCode',
  80. params:{
  81. "mobile": this.phone,
  82. "type": this.codeType
  83. },
  84. },
  85. success: ({result}) => {
  86. console.log(result);
  87. if(result.code===0){
  88. uni.showToast({
  89. title: "短信验证码发送成功",
  90. icon: 'none'
  91. });
  92. this.reverseNumber = Number(this.count);
  93. this.getCode();
  94. this.$emit('getCode');
  95. }else{
  96. uni.showModal({
  97. content: result.msg,
  98. showCancel: false
  99. });
  100. }
  101. }
  102. })
  103. },
  104. getCode() {
  105. if (this.reverseNumber == 0) {
  106. clearTimeout(this.reverseTimer);
  107. this.reverseTimer = null;
  108. return;
  109. }
  110. this.reverseNumber--;
  111. this.reverseTimer = setTimeout(() => {
  112. this.getCode();
  113. }, 1000)
  114. }
  115. }
  116. }
  117. </script>
  118. <style scoped>
  119. .short-code-btn {
  120. width: 200rpx;
  121. height: 85rpx;
  122. /* #ifndef APP-NVUE */
  123. display: flex;
  124. /* #endif */
  125. justify-content: center;
  126. align-items: center;
  127. }
  128. .inner-text {
  129. font-size: 28rpx;
  130. color: #AAAAAA;
  131. }
  132. .inner-text-active {
  133. color: #007aff;
  134. }
  135. </style>