ValidationPhoneStepNew.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <div class="f-main">
  3. <div class="content-top">
  4. <p>验证手机</p>
  5. </div>
  6. <div class="f-form">
  7. <div class="page-part">
  8. <mt-field placeholder="新手机号码"
  9. v-model="valid.mobile"
  10. :state="state.mobile"
  11. type="tel"
  12. @blur.native.capture="validateMobile"
  13. ></mt-field>
  14. </div>
  15. <div class="page-part">
  16. <mt-field auto-complete="off"
  17. placeholder="短信验证码"
  18. v-model="valid.token"
  19. :state="state.token"
  20. @blur.native.capture="validateCode">
  21. <span class="token" @click="getCheckCode" v-text="tokenText" v-if="state.mobile === 'success'">获取验证码</span>
  22. <span class="token-no" v-text="tokenText" v-if="state.mobile !== 'success'">获取验证码</span>
  23. </mt-field>
  24. </div>
  25. <div class="page-part">
  26. <mt-button size="large" type="primary" @click="sureAccount('last')">提 交</mt-button>
  27. </div>
  28. </div>
  29. </div>
  30. </template>
  31. <script>
  32. export default {
  33. name: 'step-one',
  34. props: ['tokenId'],
  35. data () {
  36. return {
  37. state: {
  38. mobile: 'error',
  39. token: 'error'
  40. },
  41. valid: {
  42. mobile: '',
  43. token: ''
  44. },
  45. tokenCode: '',
  46. tokenTime: 60,
  47. tokenText: '获取验证码'
  48. }
  49. },
  50. methods: {
  51. // 弹窗处理
  52. downToast (type) {
  53. this.$toast({
  54. message: type,
  55. iconClass: 'el-icon-warning'
  56. })
  57. },
  58. // 验证手机号
  59. validateMobile () {
  60. let reg = /^1[0-9]{10}$/
  61. if (!this.valid.mobile) {
  62. this.downToast('请先填写手机号')
  63. this.state.mobile = 'error'
  64. } else {
  65. if (!reg.test(this.valid.mobile)) {
  66. this.downToast('请填写正确的手机号')
  67. this.state.mobile = 'warning'
  68. } else {
  69. this.state.mobile = 'success'
  70. }
  71. }
  72. },
  73. // 验证正确的验证码
  74. validateCode () {
  75. if (!this.valid.token) {
  76. this.downToast('请先填写验证码')
  77. this.state.token = 'error'
  78. } else {
  79. if (!this.valid.mobile) {
  80. this.downToast('请先填写正确的手机号码')
  81. this.state.token = 'warning'
  82. } else {
  83. if (this.tokenCode) {
  84. let param = new FormData()
  85. param.append('mobile', this.valid.mobile)
  86. param.append('token', this.tokenCode)
  87. param.append('code', this.valid.token)
  88. let config = {
  89. headers: {'Content-Type': 'multipart/form-data'}
  90. }
  91. this.$http.post('/update/user/checkCode/mobile', param, config)
  92. .then(response => {
  93. if (response.data.success) {
  94. this.state.token = 'success'
  95. } else {
  96. this.$toast({
  97. message: response.data.errMsg,
  98. iconClass: 'el-icon-error'
  99. })
  100. this.state.token = 'error'
  101. }
  102. }).catch((err) => {
  103. this.downToast(err.errMsg)
  104. })
  105. } else {
  106. this.downToast('请点击先获取验证码信息')
  107. this.state.token = 'warning'
  108. }
  109. }
  110. }
  111. },
  112. // 获取验证码
  113. getCheckCode () {
  114. if (this.tokenTime > 0 && this.tokenTime < 60) {
  115. this.downToast('请稍后再点击,我在倒计时')
  116. } else {
  117. if (this.state.mobile === 'success') {
  118. this.$indicator.open('获取中...')
  119. let _this = this
  120. this.$http.get('/update/user/setMobile', {params: {mobile: this.valid.mobile, token: this.$route.query.token ? this.$route.query.token : this.tokenId}})
  121. .then(response => {
  122. this.$indicator.close()
  123. if (response.data) {
  124. this.tokenCode = response.data.content.token
  125. this.$toast({
  126. message: '验证码已经发送到您的手机,请注意查收',
  127. iconClass: 'el-icon-success'
  128. })
  129. this.tokenText = '已发送(' + this.tokenTime + 'S)'
  130. let setTime = setInterval(() => {
  131. _this.tokenTime--
  132. this.tokenText = '已发送(' + this.tokenTime + 'S)'
  133. if (this.tokenTime <= 0) {
  134. clearInterval(setTime)
  135. _this.tokenText = '获取验证码'
  136. _this.tokenTime = 60
  137. }
  138. }, 1000)
  139. }
  140. }).catch((err) => {
  141. this.$indicator.close()
  142. this.downToast(err.errMsg)
  143. })
  144. }
  145. }
  146. },
  147. sureAccount (type) {
  148. if (this.state.mobile !== 'success' ||
  149. this.state.token !== 'success') {
  150. this.downToast('请确认填写部分是否有误')
  151. } else {
  152. this.$indicator.open('验证过程中...')
  153. let param = new FormData()
  154. param.append('mobile', this.valid.mobile)
  155. param.append('code', this.valid.token)
  156. param.append('token', this.tokenCode)
  157. let config = {
  158. headers: {'Content-Type': 'multipart/form-data'}
  159. }
  160. this.$http.post('/update/user/setMobile', param, config)
  161. .then(response => {
  162. this.$indicator.close()
  163. if (response.data.success) {
  164. this.$emit('stepEvent', type)
  165. this.$emit('lastEvent', 'last')
  166. } else {
  167. this.downToast(response.data.errMsg)
  168. }
  169. }).catch((err) => {
  170. this.$indicator.close()
  171. this.downToast(err.errMsg)
  172. })
  173. }
  174. }
  175. }
  176. }
  177. </script>