SmsBox.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <view>
  3. <uni-forms :modelValue="postForm" ref="postForm" :rules="rules" :label-width="100">
  4. <uni-forms-item name="mobile" label="手机号码" required>
  5. <uni-easyinput type="text" v-model="postForm.mobile" placeholder="手机号码/用户名" />
  6. </uni-forms-item>
  7. <uni-forms-item name="captchaValue" label="图形验证" required>
  8. <view class="ca-box">
  9. <uni-easyinput class="input" type="text" v-model="postForm.captchaValue"
  10. placeholder="图形验证码" />
  11. <image class="img" @click="refreshCaptcha" :src="captchaUrl">
  12. </view>
  13. </uni-forms-item>
  14. <uni-forms-item name="smsCode" label="短信验证" required>
  15. <view class="ca-box">
  16. <uni-easyinput class="input" type="number" v-model="postForm.smsCode" placeholder="短信验证码" />
  17. <button class="btn" type="warn" @click="sendSms" :disabled="left > 0">
  18. {{left ==0 ? '发送': `${ left }秒` }}
  19. </button>
  20. </view>
  21. </uni-forms-item>
  22. </uni-forms>
  23. </view>
  24. </template>
  25. <script>
  26. import { send } from '@/api/ability/sms'
  27. import { isMobile } from '@/common/validate'
  28. export default {
  29. name: 'SmsBox',
  30. props: {
  31. value: Object,
  32. type: Number
  33. },
  34. data() {
  35. return {
  36. captchaUrl: '',
  37. rules: {
  38. mobile: {
  39. rules: [{
  40. required: true,
  41. errorMessage: '手机号码不能为空!'
  42. },
  43. {
  44. validateFunction:function (rule, value, data, callback) {
  45. if(!isMobile(value)){
  46. callback("手机号码格式错误!");
  47. }
  48. }
  49. }
  50. ]
  51. },
  52. captchaValue: {
  53. rules: [{
  54. required: true,
  55. errorMessage: '图形验证码不能为空!'
  56. }]
  57. },
  58. smsCode: {
  59. rules: []
  60. }
  61. },
  62. // 数据
  63. postForm: {
  64. },
  65. // 值
  66. timer: '',
  67. left: ''
  68. }
  69. },
  70. watch: {
  71. // 传递参数
  72. postForm: {
  73. handler(val) {
  74. // 发送验证码
  75. this.$emit('input', val)
  76. },
  77. deep: true
  78. },
  79. // 检测查询变化
  80. value: {
  81. handler(val) {
  82. this.postForm = val
  83. },
  84. deep: true
  85. }
  86. },
  87. created() {
  88. this.postForm = this.value
  89. this.refreshCaptcha()
  90. },
  91. methods: {
  92. // 表单验证
  93. async validate(){
  94. // 增加校验
  95. this.rules.smsCode.rules = [{
  96. required: true,
  97. errorMessage: '请输入6位数字验证码!'
  98. }]
  99. // 返回结果
  100. return await this.$refs.postForm.validate().then(()=>{
  101. return true
  102. }).catch(()=>{
  103. return false
  104. })
  105. },
  106. // 刷新编码
  107. refreshCaptcha() {
  108. this.postForm.captchaKey = new Date().getTime()
  109. this.captchaUrl = `${this.$urls.api}/api/common/captcha/gen?key=${this.postForm.captchaKey}`
  110. },
  111. // 发送短信
  112. sendSms() {
  113. // 计时未停止,不允许发送
  114. if(this.left > 0){
  115. return;
  116. }
  117. if(this.timer){
  118. clearInterval(this.timer)
  119. }
  120. // 移除校验
  121. this.rules.smsCode.rules = []
  122. // 发送类型
  123. this.postForm.smsType = this.type
  124. // 表单验证
  125. this.$refs.postForm.validate().then((res) => {
  126. send(this.postForm).then(() => {
  127. this.left = 60
  128. this.timer = setInterval(this.calc, 1000)
  129. this.dialogVisible = false
  130. uni.showToast({
  131. title: '短信发送成功,请查收手机短信!',
  132. icon:'none',
  133. duration: 2000
  134. })
  135. // 发送验证码
  136. this.$emit('input', this.postForm)
  137. })
  138. }).catch(()=>{
  139. })
  140. },
  141. calc() {
  142. this.left -= 1
  143. if (this.left === 0) {
  144. clearInterval(this.timer)
  145. }
  146. }
  147. }
  148. }
  149. </script>