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