login.vue 12 KB

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