smsVerification.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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" @tap="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. } else {
  77. uni.showToast({
  78. title: res.message,
  79. icon: 'none'
  80. })
  81. }
  82. })
  83. }
  84. },
  85. deep: true
  86. }
  87. },
  88. methods:{
  89. pageInit () {
  90. this.value = ''
  91. this.sendCode = false
  92. this.showKeyboard = false
  93. this.retsetCode()
  94. // 图文验证码
  95. this.$refs.imageTxtPopup.open()
  96. },
  97. // 验证图片验证码
  98. captchaVerify(code, nowRandomCode){
  99. let obj = {}
  100. obj.phone = this.phone
  101. obj.random = nowRandomCode
  102. obj.code = code
  103. // 短信验证码
  104. sendVerifyCode(obj).then(res => {
  105. console.log(JSON.stringify(res))
  106. if(res.status == 200){ // 验证码输入正确
  107. this.randomCode = nowRandomCode // 图片验证码随机码
  108. // 关闭 图形验证码 弹框
  109. this.$refs.imageTxtPopup.close()
  110. // 开启倒计时
  111. this.getCodeFun()
  112. this.sendCode = true
  113. this.showKeyboard = true
  114. uni.showToast({icon: 'none',title: '验证码发送成功'})
  115. }else { // 验证码输入错误
  116. this.retsetCode()
  117. uni.showToast({icon: 'none',title: res.message})
  118. }
  119. })
  120. },
  121. // 重新触发获取图片验证码
  122. retsetCode(){
  123. const _this = this
  124. _this.verifyCode = ''
  125. _this.randomCode = ''
  126. // 切换验证码重新校验
  127. _this.changeImg = false
  128. _this.$nextTick(function(){
  129. _this.changeImg = true
  130. })
  131. },
  132. // 按键被点击(点击退格键不会触发此事件)
  133. valChange(val) {
  134. // 将每次按键的值拼接到value变量中,注意+=写法
  135. if(this.value.length < 6){
  136. this.value += val
  137. }
  138. },
  139. // 退格键被点击
  140. backspace() {
  141. // 删除value的最后一个字符
  142. if(this.value.length) this.value = this.value.substr(0, this.value.length - 1)
  143. },
  144. // 开始倒计时
  145. getCodeFun(){
  146. const TIME_COUNT = 60
  147. if (!this.timer) {
  148. this.count = TIME_COUNT
  149. this.timer = setInterval(() => {
  150. if (this.count > 0 && this.count <= TIME_COUNT) {
  151. this.count--
  152. this.nowVal = this.count + '秒后重新发送'
  153. this.isDisable = true
  154. } else {
  155. this.nowVal = '重新获取验证码'
  156. clearInterval(this.timer)
  157. this.timer = null
  158. this.isDisable = false
  159. }
  160. }, 1000)
  161. }
  162. },
  163. // 取消
  164. cancel(){
  165. uni.navigateBack({
  166. delta: 1
  167. })
  168. }
  169. }
  170. }
  171. </script>
  172. <style lang="less">
  173. .smsVerification-container{
  174. width: 100%;
  175. padding: 0 30rpx;
  176. box-sizing: border-box;
  177. background-color: #fff;
  178. .sms-title {
  179. margin: 50rpx 0 30rpx;
  180. font-size: 36rpx;
  181. font-weight: bold;
  182. }
  183. .sms-describe{
  184. display: block;
  185. color: #606266;
  186. margin: 20rpx 0 50rpx;
  187. }
  188. .sendcode-con{
  189. text-align: center;
  190. padding-top: 70rpx;
  191. .getcode-btn {
  192. display: block;
  193. font-size: 28rpx;
  194. color: #298bf2;
  195. }
  196. .grey-btn{
  197. color: #999;
  198. }
  199. }
  200. .btn-con{
  201. display: flex;
  202. justify-content: space-between;
  203. padding-top: 130rpx;
  204. .handle-btn{
  205. display: inline-block;
  206. width: 45%;
  207. margin: 0 auto;
  208. text-align: center;
  209. }
  210. }
  211. }
  212. </style>