LoginForm.vue 9.7 KB

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