index.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <template>
  2. <app-view class="g-form register">
  3. <template #header>
  4. <app-navbar title="个人用户注册" />
  5. </template>
  6. <Form ref="formRef" class="g-form__container register__form" @submit="formSubmit">
  7. <CellGroup inset>
  8. <Field v-model="formData.mobilephone" type="tel" name="mobilephone" label="手机号码" placeholder="必填"
  9. autocomplete="off" :rules="formRules.mobilephone" />
  10. <Field v-model="formData.vcode" type="digit" name="vcode" label="短信验证码" placeholder="必填" autocomplete="off"
  11. :rules="formRules.vcode">
  12. <template #button>
  13. <Button size="small" type="primary" :disabled="isCountdown" @click="sendVerifyCode">
  14. <span v-if="isCountdown">重新发送({{ currentTime.seconds }})</span>
  15. <span v-else>获取验证码</span>
  16. </Button>
  17. </template>
  18. </Field>
  19. <Field v-model="formData.loginpwd" name="loginpwd" type="password" label="登录密码" placeholder="必填"
  20. autocomplete="off" :rules="formRules.loginpwd" />
  21. <Field v-model="formData.refernum" name="refernum" label="推荐码" placeholder="必填" autocomplete="off"
  22. :rules="formRules.refernum">
  23. <!-- <template #button>
  24. <app-qrcode-scan @success="onScanSuccess">
  25. <Button size="small" type="primary">扫码</Button>
  26. </app-qrcode-scan>
  27. </template> -->
  28. </Field>
  29. </CellGroup>
  30. <CellGroup inset>
  31. <Cell>
  32. <template #title>
  33. <div class="agreement">
  34. <Checkbox shape="square" icon-size=".32rem" v-model="checked">我已阅读并同意</Checkbox>
  35. <span @click="routerTo('rules-zcxy')" style="color:#E92020">《用户注册协议》</span>
  36. <span @click="routerTo('rules-yhkhfxgzs')" style="color:#E92020">《风险告知书》</span>
  37. </div>
  38. </template>
  39. </Cell>
  40. </CellGroup>
  41. </Form>
  42. <template #footer>
  43. <div class="g-form__footer">
  44. <Button type="primary" @click="formRef?.submit" round block>免费注册</Button>
  45. </div>
  46. <app-reward :show="showReward" :value="redEnvelope" title="注册成功!" @click="router.back()" />
  47. </template>
  48. </app-view>
  49. </template>
  50. <script lang="ts" setup>
  51. import { reactive, ref, computed } from 'vue'
  52. import { CellGroup, Cell, Button, Field, Form, FormInstance, Checkbox, Toast, FieldRule } from 'vant'
  53. import { useCountDown } from '@vant/use'
  54. import { fullloading, dialog } from '@/utils/vant'
  55. import { validateRules } from '@/constants/regex'
  56. import { useNavigation } from '@/hooks/navigation'
  57. import { userRegister, sendRegisterVerifyCode, queryMyRegisterMoney } from '@/services/api/common'
  58. import cryptojs from 'crypto-js'
  59. import AppReward from '@mobile/components/modules/reward/index.vue'
  60. // import AppQrcodeScan from '@mobile/components/base/qrcode-scan/index.vue'
  61. const { router, routerTo } = useNavigation()
  62. const formRef = ref<FormInstance>()
  63. const checked = ref(false) // 是否同意注册条款
  64. const isCountdown = ref(false) // 是否正在倒计时
  65. const showReward = ref(false) // 显示红包
  66. const redEnvelope = ref(0) // 红包金额
  67. // 倒计时函数
  68. const countdown = useCountDown({
  69. time: 30 * 1000,
  70. onFinish: () => {
  71. countdown.reset()
  72. isCountdown.value = false
  73. }
  74. })
  75. // 倒计时剩余时间
  76. const currentTime = computed(() => countdown.current.value)
  77. // 表单数据
  78. const formData = reactive<Model.RegisterReq>({
  79. mobilephone: '',
  80. loginpwd: '',
  81. vcode: '',
  82. refernum: '',
  83. isaudit: 1,
  84. userinfotype: 1,
  85. usertype: 5,
  86. openmode: 5,
  87. lsitAgreementID: []
  88. })
  89. // 表单验证规则
  90. const formRules: { [key in keyof Model.RegisterReq]?: FieldRule[] } = {
  91. mobilephone: [{
  92. required: true,
  93. message: '请输入手机号码',
  94. validator: (val) => {
  95. if (validateRules.phone.validate(val)) {
  96. return true
  97. }
  98. return validateRules.phone.message
  99. }
  100. }],
  101. loginpwd: [{
  102. required: true,
  103. message: '请输入登录密码',
  104. validator: (val) => {
  105. if (validateRules.password.validate(val)) {
  106. return true
  107. }
  108. return validateRules.password.message
  109. }
  110. }],
  111. vcode: [{
  112. required: true,
  113. message: '请输入短信验证码',
  114. }],
  115. refernum: [{
  116. required: true,
  117. message: '请输入推荐码',
  118. }],
  119. }
  120. // 扫码成功
  121. // const onScanSuccess = (text: string) => {
  122. // formData.refernum = text
  123. // }
  124. // 发送手机验证码
  125. const sendVerifyCode = () => {
  126. formRef.value?.validate('mobilephone').then(() => {
  127. sendRegisterVerifyCode({
  128. data: formData.mobilephone,
  129. success: () => {
  130. isCountdown.value = true
  131. countdown.start()
  132. },
  133. fail: () => {
  134. Toast.fail('发送失败')
  135. }
  136. })
  137. })
  138. }
  139. // 获取注册红包
  140. const getRegisterMoney = (accountid: number) => {
  141. const toggleDialog = () => {
  142. dialog('您的账号已成功注册。').then(() => {
  143. router.back()
  144. })
  145. }
  146. return queryMyRegisterMoney({
  147. data: {
  148. accountid
  149. },
  150. success: (res) => {
  151. // 查询成功显示获得红包
  152. if (res.data.length) {
  153. redEnvelope.value = res.data[0].amount
  154. showReward.value = true
  155. } else {
  156. toggleDialog()
  157. }
  158. },
  159. fail: () => {
  160. // 查询失败提示注册成功后返回登录页面
  161. toggleDialog()
  162. }
  163. })
  164. }
  165. // 表单提交
  166. const formSubmit = () => {
  167. if (checked.value) {
  168. fullloading((hideLoading) => {
  169. const { Utf8, Base64 } = cryptojs.enc
  170. // 密码需进行两次base64加密
  171. const passwordData = Base64.stringify(Utf8.parse(formData.loginpwd))
  172. const loginpwd = Base64.stringify(Utf8.parse(passwordData))
  173. userRegister({
  174. data: {
  175. ...formData,
  176. loginpwd
  177. },
  178. success: (res) => {
  179. if (res.code === 0) {
  180. // 等待3秒后查询注册红包
  181. setTimeout(() => {
  182. getRegisterMoney(res.taaccount[0].accountid).finally(() => {
  183. hideLoading()
  184. })
  185. }, 3000)
  186. } else {
  187. Toast.fail(res.message)
  188. }
  189. },
  190. fail: (err) => {
  191. Toast.fail(err)
  192. }
  193. })
  194. }, '正在注册...')
  195. } else {
  196. Toast('请先同意注册条款')
  197. }
  198. }
  199. </script>
  200. <style lang="less" scoped>
  201. @import './index.less';
  202. </style>