form-club-register.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <template>
  2. <div class="club-register">
  3. <el-form :model="formData" :rules="rules" ref="form" label-width="0">
  4. <el-form-item prop="mobile">
  5. <el-input
  6. type="text"
  7. v-model="formData.mobile"
  8. placeholder="手机号"
  9. @blur="onMobileBlur"
  10. maxlength="11"
  11. show-word-limit
  12. @input="handleMobileInput"
  13. ></el-input>
  14. </el-form-item>
  15. <el-form-item prop="verifyCode">
  16. <div class="verifyCode flex justify-between">
  17. <el-input
  18. v-model="formData.verifyCode"
  19. placeholder="验证码"
  20. maxlength="6"
  21. show-word-limit
  22. @input="handleVerifyCodeInput"
  23. ></el-input>
  24. <div class="send ml-8" @click="onSend">{{ sendCodeBtnText }}</div>
  25. </div>
  26. </el-form-item>
  27. <el-form-item prop="password">
  28. <el-input
  29. type="password"
  30. v-model="formData.password"
  31. placeholder="密码"
  32. maxlength="12"
  33. show-word-limit
  34. show-password
  35. ></el-input>
  36. </el-form-item>
  37. <el-form-item prop="confirmPwd">
  38. <el-input
  39. type="password"
  40. v-model="formData.confirmPwd"
  41. placeholder="再次输入密码"
  42. maxlength="12"
  43. show-word-limit
  44. show-password
  45. ></el-input>
  46. </el-form-item>
  47. </el-form>
  48. <SimpleDialog
  49. v-model="active"
  50. @confirm="onConfirm"
  51. @cancel="onCancel"
  52. confirmText="去登录"
  53. description="抱歉,该手机号已注册,您可以登录后再来进行正品授权申请!"
  54. :center="true"
  55. />
  56. </div>
  57. </template>
  58. <script>
  59. import { mapGetters } from 'vuex'
  60. import { isMobile } from '@/utils/validator'
  61. export default {
  62. data() {
  63. const confirmPwdValide = (rule, value, callback) => {
  64. if (this.formData.password !== value) {
  65. callback(new Error('两次输入的密码不一致'))
  66. } else {
  67. callback()
  68. }
  69. }
  70. const mobileValidate = (rule, value, callback) => {
  71. if (!isMobile(value)) {
  72. callback(new Error('手机号格式不正确'))
  73. } else {
  74. callback()
  75. }
  76. }
  77. return {
  78. formData: {
  79. mobile: '',
  80. verifyCode: '',
  81. password: '',
  82. confirmPwd: '',
  83. },
  84. registerStatus: true, // 能否注册
  85. sendStatus: 0,
  86. active: false,
  87. rules: {
  88. mobile: [
  89. { required: true, message: '手机号不能为空', trigger: ['blur'] },
  90. { validator: mobileValidate, trigger: ['blur'] },
  91. ],
  92. verifyCode: [
  93. { required: true, message: '验证码不能为空', trigger: ['blur'] },
  94. ],
  95. password: [
  96. { required: true, message: '密码不能为空', trigger: ['blur'] },
  97. { min: 8, max: 12, message: '请输入8-12位密码', trigger: ['blur'] },
  98. ],
  99. confirmPwd: [
  100. { required: true, message: '请再次输入密码', trigger: ['blur'] },
  101. { validator: confirmPwdValide, trigger: ['blur'] },
  102. ],
  103. },
  104. }
  105. },
  106. computed: {
  107. ...mapGetters(['authUserId', 'routePrefix', 'accessToken']),
  108. sendCodeBtnText() {
  109. return this.sendStatus === 0
  110. ? '获取验证码'
  111. : `再次发送${this.sendStatus}s`
  112. },
  113. },
  114. methods: {
  115. async onSend() {
  116. if (!this.registerStatus) return (this.active = true)
  117. if (this.sendStatus > 0) return
  118. // 验证手机号是否合法
  119. if (!isMobile(this.formData.mobile)) {
  120. this.$toast('请输入正确的手机号')
  121. return
  122. }
  123. try {
  124. // 发送验证码
  125. await this.$http.api.sendVerifyCode({
  126. mobile: this.formData.mobile,
  127. authUserId: this.authUserId,
  128. type: 1,
  129. })
  130. this.$toast('验证码已发送')
  131. // 开启倒计时
  132. this.countdown()
  133. } catch (error) {
  134. console.log(error)
  135. }
  136. },
  137. // 输入框输入时
  138. handleMobileInput() {
  139. this.formData.mobile = this.formData.mobile.replace(/\D/gi, '')
  140. },
  141. // 输入框输入时
  142. handleVerifyCodeInput() {
  143. this.formData.verifyCode = this.formData.verifyCode.replace(/\D/gi, '')
  144. },
  145. countdown() {
  146. this.sendStatus = 30
  147. this.timer = setInterval(() => {
  148. if (this.sendStatus === 0) {
  149. clearInterval(this.timer)
  150. return
  151. }
  152. this.sendStatus--
  153. }, 1000)
  154. },
  155. onConfirm() {
  156. this.$router.push(`${this.routePrefix}`)
  157. },
  158. onCancel() {
  159. this.active = false
  160. },
  161. onMobileBlur() {
  162. if (isMobile(this.formData.mobile)) {
  163. this.checkouMobileBindClub()
  164. }
  165. },
  166. // 判断用户手机号是否绑定机构
  167. async checkouMobileBindClub() {
  168. try {
  169. const res = await this.$http.api.fetchClubAuthInfo({
  170. authUserId: this.authUserId,
  171. mobile: this.formData.mobile,
  172. })
  173. if (res.data.clubUser) {
  174. this.active = true
  175. this.registerStatus = false
  176. } else {
  177. this.registerStatus = true
  178. }
  179. } catch (error) {
  180. console.log(error)
  181. }
  182. },
  183. genetageFormData() {
  184. return {
  185. mobile: this.formData.mobile,
  186. verifyCode: this.formData.verifyCode,
  187. password: this.formData.password,
  188. }
  189. },
  190. // 表单验证
  191. validate() {
  192. this.$emit('step', this.genetageFormData())
  193. return this.$refs.form.validate()
  194. },
  195. },
  196. }
  197. </script>
  198. <style lang="scss" scoped>
  199. .verifyCode {
  200. .send {
  201. cursor: pointer;
  202. white-space: nowrap;
  203. @include themify($themes) {
  204. color: themed('color');
  205. }
  206. }
  207. }
  208. </style>