smsVerification.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <view class="smsVerification-container">
  3. <view v-if="sendCode" class="">
  4. <view class="sms-title">输入短信验证码</view>
  5. <text class="sms-describe">验证码已发送至{{userPhone}},请在下方输入框内输入6位数字验证码</text>
  6. <view class="sms-item" @click="showKeyboard=!showKeyboard">
  7. <u-message-input mode="bottomLine" :maxlength="6" v-model="value" :disabled-keyboard="true"></u-message-input>
  8. </view>
  9. <view class="sendcode-con">
  10. <text :class="['getcode-btn', isDisable ? 'grey-btn' : '']" @click="isDisable ? null : pageInit()">{{nowVal}}</text>
  11. </view>
  12. </view>
  13. <!-- 数字键盘 -->
  14. <u-keyboard ref="uKeyboard" :dot-enabled="false" :mask="false" :tooltip="false" v-model="showKeyboard" @change="valChange" @backspace="backspace"></u-keyboard>
  15. <!-- 图文验证码 -->
  16. <uni-popup :maskClick="false" ref="imageTxtPopup" type="center">
  17. <popup-con title="图文验证码" :popBtn="popBtn" :changeImg="changeImg" @captchaImg="captchaVerify"></popup-con>
  18. </uni-popup>
  19. </view>
  20. </template>
  21. <script>
  22. import uniPopup from '@/components/uni-popup/uni-popup.vue'
  23. import popupCon from '@/components/uni-popup/popup-con.vue'
  24. import { sendVerifyCode } from '@/api/user.js'
  25. import { checkCode } from '@/api/order.js'
  26. export default {
  27. components: {
  28. uniPopup,
  29. popupCon
  30. },
  31. data() {
  32. return {
  33. nowVal: '获取验证码', // 按钮显示内容
  34. count: '', // 当前秒数
  35. timer: null, // 倒计时
  36. isDisable: false, // 是否禁止获取验证码
  37. value: '', //输入的内容
  38. phone: '', // 用户手机号
  39. showKeyboard: false, // 是否打开键盘
  40. popBtn: [], // name为操作按钮名称,color为操作按钮颜色值
  41. changeImg: false, // 是否重新更换图形验证码
  42. verifyCode: '',
  43. randomCode: '', // 图片验证码随机码
  44. sendCode: false // 短信是否发送成功
  45. }
  46. },
  47. onLoad() {
  48. this.phone = this.$store.state.vuex_userData.mobile
  49. console.log(this.$store.state.vuex_userData,'his.$store.state.vuex_OrderAddress')
  50. //因为此时实例没有挂载完成,需要延迟操作
  51. setTimeout(() => {
  52. this.pageInit()
  53. }, 50)
  54. },
  55. computed: {
  56. userPhone () {
  57. return this.phone.substr(0,3) + "****" + this.phone.substr(7)
  58. }
  59. },
  60. watch: {
  61. // 监听输入框输入长度
  62. value: {
  63. handler(newVal) {
  64. // console.log(newVal.length,'NNNNNNNNNNNN')
  65. if(newVal.length==6) {
  66. let params = {
  67. mobile: this.phone,
  68. verifiCode: newVal
  69. }
  70. checkCode(params).then(res=>{
  71. if(res.status==200) {
  72. // 跳转到设置密码
  73. uni.redirectTo({
  74. url: `/pages/userCenter/userInfo/paymentPwd?code=${this.value}&mobile=${this.phone}`
  75. })
  76. }
  77. })
  78. }
  79. },
  80. deep: true
  81. }
  82. },
  83. methods:{
  84. pageInit () {
  85. this.value = ''
  86. this.sendCode = false
  87. this.showKeyboard = false
  88. this.retsetCode()
  89. // 图文验证码
  90. this.$refs.imageTxtPopup.open()
  91. },
  92. // 验证图片验证码
  93. captchaVerify(code, nowRandomCode){
  94. let obj = {}
  95. obj.phone = this.phone
  96. obj.random = nowRandomCode
  97. obj.code = code
  98. // 短信验证码
  99. sendVerifyCode(obj).then(res => {
  100. console.log(JSON.stringify(res))
  101. if(res.status == 200){ // 验证码输入正确
  102. this.randomCode = nowRandomCode // 图片验证码随机码
  103. // 关闭 图形验证码 弹框
  104. this.$refs.imageTxtPopup.close()
  105. // 开启倒计时
  106. this.getCodeFun()
  107. this.sendCode = true
  108. this.showKeyboard = true
  109. uni.showToast({icon: 'none',title: '验证码发送成功'})
  110. }else { // 验证码输入错误
  111. this.retsetCode()
  112. uni.showToast({icon: 'none',title: res.message})
  113. }
  114. })
  115. },
  116. // 重新触发获取图片验证码
  117. retsetCode(){
  118. const _this = this
  119. _this.verifyCode = ''
  120. _this.randomCode = ''
  121. // 切换验证码重新校验
  122. _this.changeImg = false
  123. _this.$nextTick(function(){
  124. _this.changeImg = true
  125. })
  126. },
  127. // 按键被点击(点击退格键不会触发此事件)
  128. valChange(val) {
  129. // 将每次按键的值拼接到value变量中,注意+=写法
  130. if(this.value.length < 6){
  131. this.value += val
  132. }
  133. },
  134. // 退格键被点击
  135. backspace() {
  136. // 删除value的最后一个字符
  137. if(this.value.length) this.value = this.value.substr(0, this.value.length - 1)
  138. },
  139. // 开始倒计时
  140. getCodeFun(){
  141. const TIME_COUNT = 60
  142. if (!this.timer) {
  143. this.count = TIME_COUNT
  144. this.timer = setInterval(() => {
  145. if (this.count > 0 && this.count <= TIME_COUNT) {
  146. this.count--
  147. this.nowVal = this.count + '秒后重新发送'
  148. this.isDisable = true
  149. } else {
  150. this.nowVal = '重新获取验证码'
  151. clearInterval(this.timer)
  152. this.timer = null
  153. this.isDisable = false
  154. }
  155. }, 1000)
  156. }
  157. },
  158. // 取消
  159. cancel(){
  160. uni.navigateBack({
  161. delta: 1
  162. })
  163. }
  164. }
  165. }
  166. </script>
  167. <style lang="less">
  168. .smsVerification-container{
  169. width: 100%;
  170. padding: 0 30rpx;
  171. box-sizing: border-box;
  172. background-color: #fff;
  173. .sms-title {
  174. margin: 50rpx 0 30rpx;
  175. font-size: 36rpx;
  176. font-weight: bold;
  177. }
  178. .sms-describe{
  179. display: block;
  180. color: #606266;
  181. margin: 20rpx 0 50rpx;
  182. }
  183. .sendcode-con{
  184. text-align: center;
  185. padding-top: 70rpx;
  186. .getcode-btn {
  187. display: block;
  188. font-size: 28rpx;
  189. color: #298bf2;
  190. }
  191. .grey-btn{
  192. color: #999;
  193. }
  194. }
  195. .btn-con{
  196. display: flex;
  197. justify-content: space-between;
  198. padding-top: 130rpx;
  199. .handle-btn{
  200. display: inline-block;
  201. width: 45%;
  202. margin: 0 auto;
  203. text-align: center;
  204. }
  205. }
  206. }
  207. </style>