ValidationPhoneStepNew.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.$http.get(`/update/user/mobile/hasRegister`, {params: {mobile: this.valid.mobile}})
  70. .then(response => {
  71. if (response.data.content.hasRegister) {
  72. this.$toast({
  73. message: '该手机号已被注册',
  74. iconClass: 'el-icon-error'
  75. })
  76. } else {
  77. this.state.mobile = 'success'
  78. }
  79. })
  80. }
  81. }
  82. },
  83. // 验证正确的验证码
  84. validateCode () {
  85. if (!this.valid.token) {
  86. this.downToast('请先填写验证码')
  87. this.state.token = 'error'
  88. } else {
  89. if (!this.valid.mobile) {
  90. this.downToast('请先填写正确的手机号码')
  91. this.state.token = 'warning'
  92. } else {
  93. if (this.tokenCode) {
  94. let param = new FormData()
  95. param.append('mobile', this.valid.mobile)
  96. param.append('token', this.tokenCode)
  97. param.append('code', this.valid.token)
  98. let config = {
  99. headers: {'Content-Type': 'multipart/form-data'}
  100. }
  101. this.$http.post('/update/user/checkCode/mobile', param, config)
  102. .then(response => {
  103. if (response.data.success) {
  104. this.state.token = 'success'
  105. } else {
  106. this.$toast({
  107. message: response.data.errMsg,
  108. iconClass: 'el-icon-error'
  109. })
  110. this.state.token = 'error'
  111. }
  112. }).catch(() => {
  113. this.downToast('请检查网络是否正常或联系服务商')
  114. })
  115. } else {
  116. this.downToast('请点击先获取验证码信息')
  117. this.state.token = 'warning'
  118. }
  119. }
  120. }
  121. },
  122. // 获取验证码
  123. getCheckCode () {
  124. console.log('获取', this.tokenId)
  125. if (this.tokenTime > 0 && this.tokenTime < 60) {
  126. this.downToast('请稍后再点击,我在倒计时')
  127. } else {
  128. if (this.state.mobile === 'success') {
  129. this.$indicator.open('获取中...')
  130. let _this = this
  131. this.$http.get('/update/user/setMobile', {params: {mobile: this.valid.mobile, token: this.$route.query.token ? this.$route.query.token : this.tokenId}})
  132. .then(response => {
  133. console.log('de', response.data)
  134. this.$indicator.close()
  135. if (response.data) {
  136. this.tokenCode = response.data.content.token
  137. this.$toast({
  138. message: '验证码已经发送到您的手机,请注意查收',
  139. iconClass: 'el-icon-success'
  140. })
  141. this.tokenText = '已发送(' + this.tokenTime + 'S)'
  142. let setTime = setInterval(() => {
  143. _this.tokenTime--
  144. this.tokenText = '已发送(' + this.tokenTime + 'S)'
  145. if (this.tokenTime <= 0) {
  146. clearInterval(setTime)
  147. _this.tokenText = '获取验证码'
  148. _this.tokenTime = 60
  149. }
  150. }, 1000)
  151. }
  152. }).catch(() => {
  153. this.$indicator.close()
  154. this.downToast('请检查网络是否正常或联系服务商')
  155. })
  156. }
  157. }
  158. },
  159. sureAccount (type) {
  160. if (this.state.mobile !== 'success' ||
  161. this.state.token !== 'success') {
  162. this.downToast('请确认填写部分是否有误')
  163. } else {
  164. this.$indicator.open('验证过程中...')
  165. let param = new FormData()
  166. param.append('mobile', this.valid.mobile)
  167. param.append('code', this.valid.token)
  168. param.append('token', this.tokenCode)
  169. let config = {
  170. headers: {'Content-Type': 'multipart/form-data'}
  171. }
  172. this.$http.post('/update/user/setMobile', param, config)
  173. .then(response => {
  174. this.$indicator.close()
  175. if (response.data.success) {
  176. this.$emit('stepEvent', type)
  177. this.$emit('lastEvent', 'last')
  178. } else {
  179. this.downToast(response.data.errMsg)
  180. }
  181. }).catch(() => {
  182. this.$indicator.close()
  183. this.downToast('请检查网络是否正常或联系服务商')
  184. })
  185. }
  186. }
  187. }
  188. }
  189. </script>