LoginForm.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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" v-if="loginData.tenantEnable === 'true'">
  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. <XButton
  69. :loading="loginLoading"
  70. type="primary"
  71. class="w-[100%]"
  72. :title="t('login.login')"
  73. @click="getCode()"
  74. />
  75. </el-form-item>
  76. </el-col>
  77. <Verify
  78. ref="verify"
  79. mode="pop"
  80. :captchaType="captchaType"
  81. :imgSize="{ width: '400px', height: '200px' }"
  82. @success="handleLogin"
  83. />
  84. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  85. <el-form-item>
  86. <el-row justify="space-between" style="width: 100%" :gutter="5">
  87. <el-col :span="8">
  88. <XButton
  89. class="w-[100%]"
  90. :title="t('login.btnMobile')"
  91. @click="setLoginState(LoginStateEnum.MOBILE)"
  92. />
  93. </el-col>
  94. <el-col :span="8">
  95. <XButton
  96. class="w-[100%]"
  97. :title="t('login.btnQRCode')"
  98. @click="setLoginState(LoginStateEnum.QR_CODE)"
  99. />
  100. </el-col>
  101. <el-col :span="8">
  102. <XButton
  103. class="w-[100%]"
  104. :title="t('login.btnRegister')"
  105. @click="setLoginState(LoginStateEnum.REGISTER)"
  106. />
  107. </el-col>
  108. </el-row>
  109. </el-form-item>
  110. </el-col>
  111. <el-divider content-position="center">{{ t('login.otherLogin') }}</el-divider>
  112. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  113. <el-form-item>
  114. <div class="flex justify-between w-[100%]">
  115. <Icon
  116. v-for="(item, key) in socialList"
  117. :key="key"
  118. :icon="item.icon"
  119. :size="30"
  120. class="cursor-pointer anticon"
  121. color="#999"
  122. @click="doSocialLogin(item.type)"
  123. />
  124. </div>
  125. </el-form-item>
  126. </el-col>
  127. </el-row>
  128. </el-form>
  129. </template>
  130. <script setup lang="ts">
  131. import { reactive, ref, unref, onMounted, computed, watch } from 'vue'
  132. import LoginFormTitle from './LoginFormTitle.vue'
  133. import {
  134. ElForm,
  135. ElFormItem,
  136. ElInput,
  137. ElCheckbox,
  138. ElCol,
  139. ElLink,
  140. ElRow,
  141. ElDivider,
  142. ElLoading
  143. } from 'element-plus'
  144. import { useRouter } from 'vue-router'
  145. import type { RouteLocationNormalizedLoaded } from 'vue-router'
  146. import { useI18n } from '@/hooks/web/useI18n'
  147. import { useIcon } from '@/hooks/web/useIcon'
  148. import { useMessage } from '@/hooks/web/useMessage'
  149. import { required } from '@/utils/formRules'
  150. import * as authUtil from '@/utils/auth'
  151. import { decrypt } from '@/utils/jsencrypt'
  152. import { Verify } from '@/components/Verifition'
  153. import { usePermissionStore } from '@/store/modules/permission'
  154. import * as LoginApi from '@/api/login'
  155. import { LoginStateEnum, useLoginState, useFormValid } from './useLogin'
  156. const { t } = useI18n()
  157. const message = useMessage()
  158. const iconHouse = useIcon({ icon: 'ep:house' })
  159. const iconAvatar = useIcon({ icon: 'ep:avatar' })
  160. const iconLock = useIcon({ icon: 'ep:lock' })
  161. const formLogin = ref()
  162. const { validForm } = useFormValid(formLogin)
  163. const { setLoginState, getLoginState } = useLoginState()
  164. const { currentRoute, push } = useRouter()
  165. const permissionStore = usePermissionStore()
  166. const redirect = ref<string>('')
  167. const loginLoading = ref(false)
  168. const verify = ref()
  169. const captchaType = ref('blockPuzzle') // blockPuzzle 滑块 clickWord 点击文字
  170. const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN)
  171. const LoginRules = {
  172. tenantName: [required],
  173. username: [required],
  174. password: [required]
  175. }
  176. const loginData = reactive({
  177. isShowPassword: false,
  178. captchaEnable: import.meta.env.VITE_APP_CAPTCHA_ENABLE,
  179. tenantEnable: import.meta.env.VITE_APP_TENANT_ENABLE,
  180. token: '',
  181. loading: {
  182. signIn: false
  183. },
  184. loginForm: {
  185. tenantName: '芋道源码',
  186. username: 'admin',
  187. password: 'admin123',
  188. captchaVerification: '',
  189. rememberMe: false
  190. }
  191. })
  192. const socialList = [
  193. {
  194. icon: 'ant-design:github-filled',
  195. type: 0
  196. },
  197. {
  198. icon: 'ant-design:wechat-filled',
  199. type: 30
  200. },
  201. {
  202. icon: 'ant-design:alipay-circle-filled',
  203. type: 0
  204. },
  205. {
  206. icon: 'ant-design:dingtalk-circle-filled',
  207. type: 20
  208. }
  209. ]
  210. // 获取验证码
  211. const getCode = async () => {
  212. // 情况一,未开启:则直接登录
  213. if (loginData.captchaEnable === 'false') {
  214. await handleLogin({})
  215. } else {
  216. // 情况二,已开启:则展示验证码;只有完成验证码的情况,才进行登录
  217. // 弹出验证码
  218. verify.value.show()
  219. }
  220. }
  221. //获取租户ID
  222. const getTenantId = async () => {
  223. if (loginData.tenantEnable === 'true') {
  224. const res = await LoginApi.getTenantIdByNameApi(loginData.loginForm.tenantName)
  225. authUtil.setTenantId(res)
  226. }
  227. }
  228. // 记住我
  229. const getCookie = () => {
  230. const username = authUtil.getUsername()
  231. const password = authUtil.getPassword()
  232. ? decrypt(authUtil.getPassword() as unknown as string)
  233. : undefined
  234. const rememberMe = authUtil.getRememberMe()
  235. const tenantName = authUtil.getTenantName()
  236. loginData.loginForm = {
  237. ...loginData.loginForm,
  238. username: username ? username : loginData.loginForm.username,
  239. password: password ? password : loginData.loginForm.password,
  240. rememberMe: rememberMe ? true : false,
  241. tenantName: tenantName ? tenantName : loginData.loginForm.tenantName
  242. }
  243. }
  244. // 登录
  245. const handleLogin = async (params) => {
  246. loginLoading.value = true
  247. try {
  248. await getTenantId()
  249. const data = await validForm()
  250. if (!data) {
  251. return
  252. }
  253. loginData.loginForm.captchaVerification = params.captchaVerification
  254. const res = await LoginApi.loginApi(loginData.loginForm)
  255. if (!res) {
  256. return
  257. }
  258. ElLoading.service({
  259. lock: true,
  260. text: '正在加载系统中...',
  261. background: 'rgba(0, 0, 0, 0.7)'
  262. })
  263. if (loginData.loginForm.rememberMe) {
  264. authUtil.setUsername(loginData.loginForm.username)
  265. authUtil.setPassword(loginData.loginForm.password)
  266. authUtil.setRememberMe(loginData.loginForm.rememberMe)
  267. authUtil.setTenantName(loginData.loginForm.tenantName)
  268. } else {
  269. authUtil.removeUsername()
  270. authUtil.removePassword()
  271. authUtil.removeRememberMe()
  272. authUtil.removeTenantName()
  273. }
  274. authUtil.setToken(res)
  275. if (!redirect.value) {
  276. redirect.value = '/'
  277. }
  278. push({ path: redirect.value || permissionStore.addRouters[0].path })
  279. } catch {
  280. loginLoading.value = false
  281. } finally {
  282. setTimeout(() => {
  283. const loadingInstance = ElLoading.service()
  284. loadingInstance.close()
  285. }, 400)
  286. }
  287. }
  288. // 社交登录
  289. const doSocialLogin = async (type: number) => {
  290. if (type === 0) {
  291. message.error('此方式未配置')
  292. } else {
  293. loginLoading.value = true
  294. if (loginData.tenantEnable === 'true') {
  295. await message.prompt('请输入租户名称', t('common.reminder')).then(async ({ value }) => {
  296. const res = await LoginApi.getTenantIdByNameApi(value)
  297. authUtil.setTenantId(res)
  298. })
  299. }
  300. // 计算 redirectUri
  301. const redirectUri =
  302. location.origin + '/social-login?type=' + type + '&redirect=' + (redirect.value || '/')
  303. // 进行跳转
  304. const res = await LoginApi.socialAuthRedirectApi(type, encodeURIComponent(redirectUri))
  305. window.location.href = res
  306. }
  307. }
  308. watch(
  309. () => currentRoute.value,
  310. (route: RouteLocationNormalizedLoaded) => {
  311. redirect.value = route?.query?.redirect as string
  312. },
  313. {
  314. immediate: true
  315. }
  316. )
  317. onMounted(() => {
  318. getCookie()
  319. })
  320. </script>
  321. <style lang="scss" scoped>
  322. :deep(.anticon) {
  323. &:hover {
  324. color: var(--el-color-primary) !important;
  325. }
  326. }
  327. .login-code {
  328. width: 100%;
  329. height: 38px;
  330. float: right;
  331. img {
  332. cursor: pointer;
  333. width: 100%;
  334. max-width: 100px;
  335. height: auto;
  336. vertical-align: middle;
  337. }
  338. }
  339. </style>