LoginForm.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <template>
  2. <el-form
  3. :model="loginData.loginForm"
  4. :rules="LoginRules"
  5. label-position="top"
  6. class="login-form"
  7. label-width="120px"
  8. size="large"
  9. v-show="getShow"
  10. ref="formLogin"
  11. >
  12. <el-row style="maring-left: -10px; maring-right: -10px">
  13. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  14. <el-form-item>
  15. <LoginFormTitle style="width: 100%" />
  16. </el-form-item>
  17. </el-col>
  18. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  19. <el-form-item prop="tenantName">
  20. <el-input
  21. type="text"
  22. v-model="loginData.loginForm.tenantName"
  23. :placeholder="t('login.tenantNamePlaceholder')"
  24. :prefix-icon="iconHouse"
  25. />
  26. </el-form-item>
  27. </el-col>
  28. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  29. <el-form-item prop="username">
  30. <el-input
  31. v-model="loginData.loginForm.username"
  32. :placeholder="t('login.usernamePlaceholder')"
  33. :prefix-icon="iconAvatar"
  34. />
  35. </el-form-item>
  36. </el-col>
  37. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  38. <el-form-item prop="password">
  39. <el-input
  40. v-model="loginData.loginForm.password"
  41. type="password"
  42. :placeholder="t('login.passwordPlaceholder')"
  43. show-password
  44. @keyup.enter="getCode()"
  45. :prefix-icon="iconLock"
  46. />
  47. </el-form-item>
  48. </el-col>
  49. <el-col
  50. :span="24"
  51. style="padding-left: 10px; padding-right: 10px; margin-top: -20px; margin-bottom: -20px"
  52. >
  53. <el-form-item>
  54. <el-row justify="space-between" style="width: 100%">
  55. <el-col :span="6">
  56. <el-checkbox v-model="loginData.loginForm.rememberMe">
  57. {{ t('login.remember') }}
  58. </el-checkbox>
  59. </el-col>
  60. <el-col :span="12" :offset="6">
  61. <el-link type="primary" style="float: right">{{ t('login.forgetPassword') }}</el-link>
  62. </el-col>
  63. </el-row>
  64. </el-form-item>
  65. </el-col>
  66. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  67. <el-form-item>
  68. <el-button :loading="loginLoading" type="primary" class="w-[100%]" @click="getCode()">
  69. {{ t('login.login') }}
  70. </el-button>
  71. </el-form-item>
  72. </el-col>
  73. <Verify
  74. ref="verify"
  75. mode="pop"
  76. :captchaType="captchaType"
  77. :imgSize="{ width: '400px', height: '200px' }"
  78. @success="handleLogin"
  79. />
  80. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  81. <el-form-item>
  82. <el-row justify="space-between" style="width: 100%" :gutter="5">
  83. <el-col :span="8">
  84. <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.MOBILE)">
  85. {{ t('login.btnMobile') }}
  86. </el-button>
  87. </el-col>
  88. <el-col :span="8">
  89. <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.QR_CODE)">
  90. {{ t('login.btnQRCode') }}
  91. </el-button>
  92. </el-col>
  93. <el-col :span="8">
  94. <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.REGISTER)">
  95. {{ t('login.btnRegister') }}
  96. </el-button>
  97. </el-col>
  98. </el-row>
  99. </el-form-item>
  100. </el-col>
  101. <el-divider content-position="center">{{ t('login.otherLogin') }}</el-divider>
  102. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  103. <el-form-item>
  104. <div class="flex justify-between w-[100%]">
  105. <Icon
  106. icon="ant-design:github-filled"
  107. :size="30"
  108. class="cursor-pointer anticon"
  109. color="#999"
  110. @click="doSocialLogin('github')"
  111. />
  112. <Icon
  113. icon="ant-design:wechat-filled"
  114. :size="30"
  115. class="cursor-pointer anticon"
  116. color="#999"
  117. @click="doSocialLogin('wechat')"
  118. />
  119. <Icon
  120. icon="ant-design:alipay-circle-filled"
  121. :size="30"
  122. color="#999"
  123. class="cursor-pointer anticon"
  124. @click="doSocialLogin('alipay')"
  125. />
  126. <Icon
  127. icon="ant-design:dingtalk-circle-filled"
  128. :size="30"
  129. color="#999"
  130. class="cursor-pointer anticon"
  131. @click="doSocialLogin('dingtalk')"
  132. />
  133. </div>
  134. </el-form-item>
  135. </el-col>
  136. </el-row>
  137. </el-form>
  138. </template>
  139. <script setup lang="ts">
  140. import { reactive, ref, unref, onMounted, computed, watch } from 'vue'
  141. import LoginFormTitle from './LoginFormTitle.vue'
  142. import {
  143. ElForm,
  144. ElFormItem,
  145. ElInput,
  146. ElCheckbox,
  147. ElCol,
  148. ElLink,
  149. ElRow,
  150. ElDivider,
  151. ElLoading
  152. } from 'element-plus'
  153. import { useRouter } from 'vue-router'
  154. import type { RouteLocationNormalizedLoaded } from 'vue-router'
  155. import { useI18n } from '@/hooks/web/useI18n'
  156. import { useIcon } from '@/hooks/web/useIcon'
  157. import { useCache } from '@/hooks/web/useCache'
  158. import { required } from '@/utils/formRules'
  159. import { setToken, setTenantId } from '@/utils/auth'
  160. import { decrypt, encrypt } from '@/utils/jsencrypt'
  161. import { Icon } from '@/components/Icon'
  162. import { Verify } from '@/components/Verifition'
  163. import { usePermissionStore } from '@/store/modules/permission'
  164. import * as LoginApi from '@/api/login'
  165. import { LoginStateEnum, useLoginState, useFormValid } from './useLogin'
  166. const { t } = useI18n()
  167. const formLogin = ref()
  168. const { wsCache } = useCache()
  169. const { validForm } = useFormValid(formLogin)
  170. const { setLoginState, getLoginState } = useLoginState()
  171. const { currentRoute, push } = useRouter()
  172. const permissionStore = usePermissionStore()
  173. const redirect = ref<string>('')
  174. const loginLoading = ref(false)
  175. const iconHouse = useIcon({ icon: 'ep:house' })
  176. const iconAvatar = useIcon({ icon: 'ep:avatar' })
  177. const iconLock = useIcon({ icon: 'ep:lock' })
  178. const verify = ref()
  179. const captchaType = ref('blockPuzzle') // blockPuzzle 滑块 clickWord 点击文字
  180. const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN)
  181. const LoginRules = {
  182. tenantName: [required],
  183. username: [required],
  184. password: [required]
  185. }
  186. const loginData = reactive({
  187. isShowPassword: false,
  188. captchaEnable: import.meta.env.VITE_APP_CAPTCHA_ENABLE,
  189. tenantEnable: import.meta.env.VITE_APP_TENANT_ENABLE,
  190. token: '',
  191. loading: {
  192. signIn: false
  193. },
  194. loginForm: {
  195. tenantName: wsCache.get('tenantName') ? wsCache.get('tenantName') : '芋道源码',
  196. username: wsCache.get('username') ? wsCache.get('username') : 'admin',
  197. password: wsCache.get('password')
  198. ? (decrypt(wsCache.get('password')) as unknown as string)
  199. : 'admin123',
  200. captchaVerification: '',
  201. rememberMe: false
  202. }
  203. })
  204. // 获取验证码
  205. const getCode = async () => {
  206. // 情况一,未开启:则直接登录
  207. if (loginData.captchaEnable === 'false') {
  208. await handleLogin({})
  209. } else {
  210. // 情况二,已开启:则展示验证码;只有完成验证码的情况,才进行登录
  211. // 弹出验证码
  212. verify.value.show()
  213. }
  214. }
  215. //获取租户ID
  216. const getTenantId = async () => {
  217. const res = await LoginApi.getTenantIdByNameApi(loginData.loginForm.tenantName)
  218. setTenantId(res)
  219. }
  220. // 记住我
  221. const getCookie = () => {
  222. const username = wsCache.get('username')
  223. const password = wsCache.get('password')
  224. ? (decrypt(wsCache.get('password')) as unknown as string)
  225. : undefined
  226. const rememberMe = wsCache.get('rememberMe')
  227. const tenantName = wsCache.get('tenantName')
  228. loginData.loginForm = {
  229. ...loginData.loginForm,
  230. username: username ? username : loginData.loginForm.username,
  231. password: password ? password : loginData.loginForm.password,
  232. rememberMe: rememberMe ? true : false,
  233. tenantName: tenantName ? tenantName : loginData.loginForm.tenantName
  234. }
  235. }
  236. // 登录
  237. const handleLogin = async (params) => {
  238. loginLoading.value = true
  239. try {
  240. await getTenantId()
  241. const data = await validForm()
  242. if (!data) {
  243. return
  244. }
  245. loginData.loginForm.captchaVerification = params.captchaVerification
  246. const res = await LoginApi.loginApi(loginData.loginForm)
  247. if (!res) {
  248. return
  249. }
  250. ElLoading.service({
  251. lock: true,
  252. text: '正在加载系统中...',
  253. background: 'rgba(0, 0, 0, 0.7)'
  254. })
  255. if (loginData.loginForm.rememberMe) {
  256. wsCache.set('username', loginData.loginForm.username, { exp: 30 * 24 * 60 * 60 })
  257. wsCache.set('password', encrypt(loginData.loginForm.password as unknown as string), {
  258. exp: 30
  259. })
  260. wsCache.set('rememberMe', loginData.loginForm.rememberMe, { exp: 30 * 24 * 60 * 60 })
  261. wsCache.set('tenantName', loginData.loginForm.tenantName, { exp: 30 * 24 * 60 * 60 })
  262. } else {
  263. wsCache.delete('username')
  264. wsCache.delete('password')
  265. wsCache.delete('rememberMe')
  266. wsCache.delete('tenantName')
  267. }
  268. setToken(res)
  269. if (!redirect.value) {
  270. redirect.value = '/'
  271. }
  272. push({ path: redirect.value || permissionStore.addRouters[0].path })
  273. } catch {
  274. loginLoading.value = false
  275. } finally {
  276. setTimeout(() => {
  277. const loadingInstance = ElLoading.service()
  278. loadingInstance.close()
  279. }, 400)
  280. }
  281. }
  282. // 社交登录
  283. const doSocialLogin = async (type: string) => {
  284. loginLoading.value = true
  285. // 计算 redirectUri
  286. const redirectUri =
  287. location.origin + '/social-login?type=' + type + '&redirect=' + (redirect.value || '/')
  288. // 进行跳转
  289. const res = await LoginApi.socialAuthRedirectApi(type, encodeURIComponent(redirectUri))
  290. window.location.href = res
  291. }
  292. watch(
  293. () => currentRoute.value,
  294. (route: RouteLocationNormalizedLoaded) => {
  295. redirect.value = route?.query?.redirect as string
  296. },
  297. {
  298. immediate: true
  299. }
  300. )
  301. onMounted(() => {
  302. getCookie()
  303. })
  304. </script>
  305. <style lang="scss" scoped>
  306. :deep(.anticon) {
  307. &:hover {
  308. color: var(--el-color-primary) !important;
  309. }
  310. }
  311. .login-code {
  312. width: 100%;
  313. height: 38px;
  314. float: right;
  315. img {
  316. cursor: pointer;
  317. width: 100%;
  318. max-width: 100px;
  319. height: auto;
  320. vertical-align: middle;
  321. }
  322. }
  323. </style>