smsVerification.vue 5.8 KB

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