login.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <div>
  3. <div v-if="active" class="bg-white w-full py-[60px] px-[30px]">
  4. <div class="flex items-end justify-center mb-[30px]">
  5. <div class="text-[18px] cursor-pointer text-[#999] leading-[24px] oppoSans-R" :class="{ '!text-[#333]': type == item.type,'mr-[70px]': (index+1) != loginType.length }" v-for="(item,index) in loginType" @click="type = item.type">{{item.title }}</div>
  6. </div>
  7. <el-form :model="formData" ref="formRef" :rules="formRules" :validate-on-rule-change="false">
  8. <div v-show="type == 'username'">
  9. <el-form-item prop="username">
  10. <div class="flex-1 h-[50px] border-[1px] border-solid border-[#ccc] rounded-[8px] flex items-center">
  11. <el-input v-model="formData.username" :placeholder="t('usernamePlaceholder')" clearable :inline-message="true">
  12. <template #prefix>
  13. <span class="iconfont icon-woV6xx1 !mr-[14px]"></span>
  14. </template>
  15. </el-input>
  16. </div>
  17. </el-form-item>
  18. <el-form-item prop="password">
  19. <div class="flex-1 h-[50px] border-[1px] border-solid border-[#ccc] rounded-[8px] flex items-center">
  20. <el-input v-model="formData.password" :placeholder="t('passwordPlaceholder')" type="password" clearable :show-password="true">
  21. <template #prefix>
  22. <span class="iconfont icon-mima !mr-[14px]"></span>
  23. </template>
  24. </el-input>
  25. </div>
  26. </el-form-item>
  27. </div>
  28. <div v-show="type == 'mobile'">
  29. <el-form-item prop="mobile">
  30. <div class="flex-1 h-[50px] border-[1px] border-solid border-[#ccc] rounded-[8px] flex items-center">
  31. <el-input v-model="formData.mobile" :placeholder="t('mobilePlaceholder')" clearable>
  32. <template #prefix>
  33. <span class="iconfont icon-shoujiV6xx !mr-[14px]"></span>
  34. </template>
  35. </el-input>
  36. </div>
  37. </el-form-item>
  38. <el-form-item prop="mobile_code">
  39. <div class="flex-1 h-[50px] border-[1px] border-solid border-[#ccc] rounded-[8px] flex items-center">
  40. <el-input v-model="formData.mobile_code" :placeholder="t('codePlaceholder')">
  41. <template #prefix>
  42. <span class="iconfont icon-a-zhibao5 !mr-[14px]"></span>
  43. </template>
  44. <template #suffix>
  45. <sms-code :mobile="formData.mobile" type="login" v-model="formData.mobile_key" @click="sendSmsCode" ref="smsCodeRef"></sms-code>
  46. </template>
  47. </el-input>
  48. </div>
  49. </el-form-item>
  50. </div>
  51. <div class="flex justify-between">
  52. <el-button type="primary" link @click="typeChange" class="!text-[12px]">{{ t('noAccount') }},{{ t('toRegister') }}</el-button>
  53. </div>
  54. <div class="mt-[20px]">
  55. <el-button type="primary" class="w-full !h-[50px] !rounded-[8px] oppoSans-M" size="large" @click="handleLogin" :loading="loading">{{ loading ? t('logining') : t('login') }}</el-button>
  56. </div>
  57. <div class="text-[12px] leading-[24px] flex justify-center w-full mt-[20px]" v-if="configStore.login.agreement_show">
  58. {{ t('agreeTips') }}
  59. <NuxtLink to="/auth/agreement?key=service">
  60. <span class="text-primary mx-[4px]">{{ t('userAgreement') }}</span>
  61. </NuxtLink>
  62. {{ t('and') }}
  63. <NuxtLink to="/auth/agreement?key=privacy">
  64. <span class="text-primary mx-[4px]">{{ t('privacyAgreement') }}</span>
  65. </NuxtLink>
  66. </div>
  67. <!-- <div class="mt-[20px] flex justify-center">
  68. <span class="iconfont icon-weixin1 text-[#1AAD19] !text-[24px] cursor-pointer" @click="handleChange"></span>
  69. </div> -->
  70. </el-form>
  71. </div>
  72. <div v-else class="flex flex-col items-center w-[380px] py-[60px] h-[424px]">
  73. <div class="title text-xl">打开手机微信</div>
  74. <div class="tips text-sm mt-[5px]">点击右上角打开扫一扫</div>
  75. <div class="qrcode p-[10px] mt-[30px] border h-[120px] leading-none box-content">
  76. <div class="relative">
  77. <el-image :src="weixinCode.url" class="w-[120px]" />
  78. <div class="flex flex-col justify-center items-center absolute inset-0 bg-gray-50" v-if="weixinCode.pastDue">
  79. <span class="text-xs text-gray-600">{{ weixinCode.pastDueContent }}</span>
  80. <span @click="scanLoginFn()" class="text-xs cursor-pointer text-color mt-2">点击刷新</span>
  81. </div>
  82. </div>
  83. </div>
  84. <div class="mt-[60px] text-base cursor-pointer leading-none" @click="handleChange">账号登录</div>
  85. </div>
  86. </div>
  87. </template>
  88. <script lang="ts" setup>
  89. import { ref,reactive,computed,onUnmounted } from 'vue'
  90. import { FormInstance } from 'element-plus'
  91. import { usernameLogin, mobileLogin, scanlogin, checkscan } from '@/app/api/auth'
  92. import useMemberStore from '@/stores/member'
  93. import useConfigStore from '@/stores/config'
  94. import QRCode from "qrcode";
  95. const memberStore = useMemberStore()
  96. const configStore = useConfigStore()
  97. //当前为二维码还是账户登录
  98. let active = ref(true)
  99. let timer:any = null
  100. const handleChange = () => {
  101. active.value = !active.value
  102. if(!active.value){
  103. scanLoginFn();
  104. }else{
  105. clearTimeout(timer)
  106. }
  107. }
  108. onUnmounted(() => {
  109. clearTimeout(timer)
  110. });
  111. // 校验二维码
  112. const checkScanFn = (key) => {
  113. let parameter = { key };
  114. checkscan(parameter).then((res) => {
  115. let data = res.data;
  116. switch (data.status) {
  117. case 'wait':
  118. timer = setTimeout(() => {
  119. checkScanFn(weixinCode.value.key);
  120. }, 1000);
  121. break;
  122. case 'success':
  123. if (!data.login_data.token) {
  124. useCookie('openId').value = data.login_data.openid
  125. navigateTo(`/auth/bind`)
  126. } else {
  127. memberStore.setToken(data.login_data.token)
  128. memberStore.logClose()
  129. }
  130. break;
  131. case 'fail':
  132. weixinCode.value.pastDueContent = data.fail_reason
  133. weixinCode.value.pastDue = true;
  134. break;
  135. }
  136. }).catch((res) => {
  137. weixinCode.value.pastDue = true;
  138. weixinCode.value.pastDueContent = res.msg;
  139. })
  140. }
  141. // 扫码登录,微信二维码
  142. const weixinCode = ref({
  143. url: '',
  144. key: '',
  145. pastDue: false,
  146. pastDueContent: '二维码生成失败'
  147. })
  148. const scanLoginFn = async () => {
  149. let data = await (await scanlogin()).data;
  150. weixinCode.value.key = data.key
  151. QRCode.toDataURL(data.url, { errorCorrectionLevel: 'L', margin: 0, width: 100 }).then(url => {
  152. weixinCode.value.url = url
  153. });
  154. weixinCode.value.pastDue = false;
  155. setTimeout(() => {
  156. checkScanFn(weixinCode.value.key);
  157. }, 1000);
  158. }
  159. configStore.getLoginConfig()
  160. const loginType = computed(() => {
  161. const value = []
  162. configStore.login.is_username && (value.push({ type: 'username', title: t('usernameLogin') }))
  163. configStore.login.is_mobile && (value.push({ type: 'mobile', title: t('mobileLogin') }))
  164. type.value = value[0] ? value[0].type : ''
  165. return value
  166. })
  167. const loading = ref(false)
  168. const type = ref('')
  169. const formData = reactive({
  170. username: '',
  171. password: '',
  172. mobile: '',
  173. mobile_code: '',
  174. mobile_key: ''
  175. })
  176. const formRef = ref<FormInstance>()
  177. const formRules = computed(() => {
  178. return {
  179. 'username': {
  180. required: type.value == 'username',
  181. message: t('usernamePlaceholder'),
  182. trigger: ['blur', 'change'],
  183. },
  184. 'password': {
  185. required: type.value == 'username',
  186. message: t('passwordPlaceholder'),
  187. trigger: ['blur', 'change']
  188. },
  189. 'mobile': [
  190. {
  191. required: type.value == 'mobile',
  192. message: t('mobilePlaceholder'),
  193. trigger: ['blur', 'change'],
  194. },
  195. {
  196. validator(rule: any, value: string, callback: any) {
  197. if (type.value != 'mobile') return true
  198. else return test.mobile(value)
  199. },
  200. message: t('mobileError'),
  201. trigger: ['blur'],
  202. }
  203. ],
  204. 'mobile_code': {
  205. required: type.value == 'mobile',
  206. message: t('codePlaceholder'),
  207. trigger: ['change']
  208. }
  209. }
  210. })
  211. const handleLogin = async () => {
  212. await formRef.value?.validate(async (valid, fields) => {
  213. if (valid) {
  214. if (loading.value) return
  215. loading.value = true
  216. const login = type.value == 'username' ? usernameLogin : mobileLogin
  217. login(formData).then(async (res) => {
  218. await memberStore.setToken(res.data.token)
  219. memberStore.logClose()
  220. }).catch(() => {
  221. loading.value = false
  222. })
  223. }
  224. })
  225. }
  226. const smsCodeRef = ref<AnyObject | null>(null)
  227. const sendSmsCode = async () => {
  228. await formRef.value?.validateField('mobile', async (valid, fields) => {
  229. if (valid) {
  230. smsCodeRef.value?.send()
  231. }
  232. })
  233. }
  234. //去注册
  235. const emit = defineEmits(['typeChange'])
  236. const typeChange = ()=>{
  237. emit('typeChange','register')
  238. }
  239. </script>
  240. <style lang="scss" scoped>
  241. </style>