register.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <div class="w-full h-full bg-page flex items-center justify-center">
  3. <div class="flex bg-white">
  4. <div class="bg-white w-[380px] p-[30px] h-[686px]" v-if="active">
  5. <div class="flex items-end my-[30px]">
  6. <div class="mr-[20px] text-base cursor-pointer leading-none" :class="{ 'font-bold': type == item.type }" v-for="item in registerType" @click="type = item.type">{{item.title }}</div>
  7. </div>
  8. <el-form :model="formData" ref="formRef" :rules="formRules" :validate-on-rule-change="false">
  9. <div v-show="type == 'username'">
  10. <el-form-item prop="username">
  11. <el-input v-model="formData.username" :placeholder="t('usernamePlaceholder')" clearable :inline-message="true" :readonly="real_name_input" @click="real_name_input = false" @blur="real_name_input = true">
  12. </el-input>
  13. </el-form-item>
  14. <el-form-item prop="password">
  15. <el-input v-model="formData.password" :placeholder="t('passwordPlaceholder')" type="password" clearable :show-password="true" :readonly="password_input" @click="password_input = false" @blur="password_input = true">
  16. </el-input>
  17. </el-form-item>
  18. <el-form-item prop="confirm_password">
  19. <el-input v-model="formData.confirm_password" :placeholder="t('confirmPasswordPlaceholder')" type="password" clearable :show-password="true" :readonly="confirm_password_input" @click="confirm_password_input = false" @blur="confirm_password_input = true">
  20. </el-input>
  21. </el-form-item>
  22. </div>
  23. <div v-show="type == 'mobile' || configStore.login.is_bind_mobile">
  24. <el-form-item prop="mobile">
  25. <el-input v-model="formData.mobile" :placeholder="t('mobilePlaceholder')" clearable>
  26. </el-input>
  27. </el-form-item>
  28. <el-form-item prop="mobile_code">
  29. <el-input v-model="formData.mobile_code" :placeholder="t('codePlaceholder')">
  30. <template #suffix>
  31. <sms-code :mobile="formData.mobile" type="login" v-model="formData.mobile_key" @click="sendSmsCode" ref="smsCodeRef"></sms-code>
  32. </template>
  33. </el-input>
  34. </el-form-item>
  35. </div>
  36. <div v-show="type == 'username'">
  37. <el-form-item prop="captcha_code">
  38. <el-input v-model="formData.captcha_code" :placeholder="t('captchaPlaceholder')">
  39. <template #suffix>
  40. <div class="py-0 leading-none">
  41. <el-image :src="captcha.image.value" class="h-[30px] cursor-pointer" @click="captcha.refresh()"></el-image>
  42. </div>
  43. </template>
  44. </el-input>
  45. </el-form-item>
  46. </div>
  47. <div class="flex justify-end leading-none">
  48. <NuxtLink to="/auth/login">
  49. <el-button type="primary" link>{{ t('haveAccount') }},{{ t('toLogin') }}</el-button>
  50. </NuxtLink>
  51. </div>
  52. <el-form-item>
  53. <el-button type="primary" class="mt-[20px] w-full" size="large" @click="handleRegister" :loading="loading">{{ loading ? t('registering') : t('register') }}</el-button>
  54. </el-form-item>
  55. <div class="text-xs py-[50rpx] flex justify-center w-full" v-if="configStore.login.agreement_show">
  56. {{ t('registerAgreeTips') }}
  57. <NuxtLink to="/auth/agreement?key=service">
  58. <span class="text-primary">{{ t('userAgreement') }}</span>
  59. </NuxtLink>
  60. {{ t('and') }}
  61. <NuxtLink to="/auth/agreement?key=privacy">
  62. <span class="text-primary">{{ t('privacyAgreement') }}</span>
  63. </NuxtLink>
  64. </div>
  65. <div class="mt-[20px] flex justify-center" v-if="show">
  66. <span class="iconfont icon-weixin1 text-[#1AAD19] !text-[24px]" @click="active = !active"></span>
  67. </div>
  68. </el-form>
  69. </div>
  70. <div class="flex flex-col items-center w-[380px] py-[100px] h-[556px]" v-else>
  71. <div class="title font-bold text-xl">打开手机微信</div>
  72. <div class="tips text-sm mt-[5px]">点击右上角打开扫一扫</div>
  73. <div class="qrcode mt-[30px] border leading-none">
  74. <el-image :src="img('https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQHU7zwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAySlJSbU1Sb0hiMlQxOEcwSGhBY1AAAgTSfStkAwRYAgAA')" class="w-[120px]"></el-image>
  75. </div>
  76. <div class="mt-[60px] text-base cursor-pointer leading-none" @click="active = !active">账号注册</div>
  77. </div>
  78. </div>
  79. </div>
  80. </template>
  81. <script lang="ts" setup>
  82. import { ref,reactive,watch,computed } from 'vue'
  83. import { usernameRegister, mobileRegister, wechatCheck } from '@/app/api/auth'
  84. import useMemberStore from '@/stores/member'
  85. import useConfigStore from '@/stores/config'
  86. import { FormInstance } from 'element-plus'
  87. import { useRouter } from 'vue-router'
  88. definePageMeta({
  89. layout: "container"
  90. });
  91. let router = useRouter()
  92. const memberStore = useMemberStore()
  93. const configStore = useConfigStore()
  94. configStore.getLoginConfig()
  95. const type = ref('')
  96. const registerType = computed(() => {
  97. const value = []
  98. configStore.login.is_username && (value.push({ type: 'username', title: t('usernameRegister') }))
  99. configStore.login.is_mobile && !configStore.login.is_bind_mobile && (value.push({ type: 'mobile', title: t('mobileRegister') }))
  100. type.value = value[0] ? value[0].type : ''
  101. return value
  102. })
  103. const loading = ref(false)
  104. let active = ref(true)
  105. const formData = reactive({
  106. username: '',
  107. password: '',
  108. confirm_password: '',
  109. mobile: '',
  110. mobile_code: '',
  111. mobile_key: '',
  112. captcha_key: '',
  113. captcha_code: ''
  114. })
  115. const formRules = computed(() => {
  116. return {
  117. 'username': {
  118. type: 'string',
  119. required: type.value == 'username',
  120. message: t('usernamePlaceholder'),
  121. trigger: ['blur', 'change'],
  122. },
  123. 'password': {
  124. type: 'string',
  125. required: type.value == 'username',
  126. message: t('passwordPlaceholder'),
  127. trigger: ['blur', 'change']
  128. },
  129. 'confirm_password': [
  130. {
  131. type: 'string',
  132. required: type.value == 'username',
  133. message: t('confirmPasswordPlaceholder'),
  134. trigger: ['blur', 'change']
  135. },
  136. {
  137. validator(rule: any, value: string, callback: any) {
  138. return value == formData.password
  139. },
  140. message: t('confirmPasswordError'),
  141. trigger: ['change', 'blur'],
  142. }
  143. ],
  144. 'mobile': [
  145. {
  146. type: 'string',
  147. required: type.value == 'mobile' || configStore.login.is_bind_mobile,
  148. message: t('mobilePlaceholder'),
  149. trigger: ['blur', 'change'],
  150. },
  151. {
  152. validator(rule: any, value: string, callback: any) {
  153. if (type.value != 'mobile' && !configStore.login.is_bind_mobile) return true
  154. else return test.mobile(value)
  155. },
  156. message: t('mobileError'),
  157. trigger: ['change', 'blur'],
  158. }
  159. ],
  160. 'mobile_code': {
  161. type: 'string',
  162. required: type.value == 'mobile' || configStore.login.is_bind_mobile,
  163. message: t('codePlaceholder'),
  164. trigger: ['blur', 'change']
  165. },
  166. 'captcha_code': {
  167. type: 'string',
  168. required: type.value == 'username',
  169. message: t('captchaPlaceholder'),
  170. trigger: ['blur', 'change'],
  171. }
  172. }
  173. })
  174. const formRef = ref<FormInstance>()
  175. const handleRegister = async () => {
  176. await formRef.value?.validate(async (valid, fields) => {
  177. if (valid) {
  178. if (loading.value) return
  179. loading.value = true
  180. const register = type.value == 'username' ? usernameRegister : mobileRegister
  181. register(formData).then((res: responseResult) => {
  182. memberStore.setToken(res.data.token)
  183. router.push({ path: '/' })
  184. }).catch(() => {
  185. loading.value = false
  186. captcha.refresh()
  187. })
  188. }
  189. })
  190. }
  191. let show = ref(false)
  192. const wechatCheckFn = () =>{
  193. wechatCheck().then((res:any) =>{
  194. show.value = res.data
  195. })
  196. }
  197. wechatCheckFn()
  198. // 验证码
  199. const captcha = useCaptcha(formData)
  200. captcha.refresh()
  201. // 获取手机验证码
  202. const smsCodeRef = ref<AnyObject | null>(null)
  203. const sendSmsCode = async () => {
  204. await formRef.value?.validateField('mobile', async (valid, fields) => {
  205. if (valid) {
  206. smsCodeRef.value?.send()
  207. }
  208. })
  209. }
  210. const real_name_input = ref(true)
  211. const password_input = ref(true)
  212. const confirm_password_input = ref(true)
  213. </script>
  214. <style lang="scss" scoped>
  215. :deep(.el-form-item) {
  216. .el-input__wrapper {
  217. box-shadow: unset !important;
  218. border-radius: 0;
  219. border-bottom: 1px solid var(--el-input-border-color);
  220. padding: 8px 0;
  221. &.is-focus {
  222. border-bottom: 1px solid var(--el-input-focus-border-color);
  223. }
  224. }
  225. &.is-error {
  226. .el-input__wrapper {
  227. border-bottom: 1px solid var(--el-color-danger);
  228. }
  229. }
  230. }
  231. :deep(.el-form-item__error) {
  232. padding-top: 5px;
  233. }
  234. </style>