LoginForm.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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="iconSize"
  108. class="cursor-pointer anticon"
  109. :color="iconColor"
  110. @click="doSocialLogin('github')"
  111. />
  112. <Icon
  113. icon="ant-design:wechat-filled"
  114. :size="iconSize"
  115. class="cursor-pointer anticon"
  116. :color="iconColor"
  117. @click="doSocialLogin('wechat')"
  118. />
  119. <Icon
  120. icon="ant-design:alipay-circle-filled"
  121. :size="iconSize"
  122. :color="iconColor"
  123. class="cursor-pointer anticon"
  124. @click="doSocialLogin('alipay')"
  125. />
  126. <Icon
  127. icon="ant-design:dingtalk-circle-filled"
  128. :size="iconSize"
  129. :color="iconColor"
  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 { useIcon } from '@/hooks/web/useIcon'
  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. } from 'element-plus'
  152. import { reactive, ref, unref, onMounted, computed, watch } from 'vue'
  153. import * as LoginApi from '@/api/login'
  154. import { setToken, setTenantId } from '@/utils/auth'
  155. import { usePermissionStore } from '@/store/modules/permission'
  156. import { useRouter } from 'vue-router'
  157. import { useI18n } from '@/hooks/web/useI18n'
  158. import { required } from '@/utils/formRules'
  159. import { Icon } from '@/components/Icon'
  160. import { LoginStateEnum, useLoginState, useFormValid } from './useLogin'
  161. import type { RouteLocationNormalizedLoaded } from 'vue-router'
  162. import { Verify } from '@/components/Verifition'
  163. import Cookies from 'js-cookie'
  164. import { decrypt, encrypt } from '@/utils/jsencrypt'
  165. const { currentRoute, push } = useRouter()
  166. const permissionStore = usePermissionStore()
  167. const formLogin = ref()
  168. const { validForm } = useFormValid(formLogin)
  169. const { setLoginState, getLoginState } = useLoginState()
  170. const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN)
  171. const iconSize = 30
  172. const iconColor = '#999'
  173. const redirect = ref<string>('')
  174. const { t } = useI18n()
  175. const iconHouse = useIcon({ icon: 'ep:house' })
  176. const iconAvatar = useIcon({ icon: 'ep:avatar' })
  177. const iconLock = useIcon({ icon: 'ep:lock' })
  178. const LoginRules = {
  179. tenantName: [required],
  180. username: [required],
  181. password: [required]
  182. }
  183. const loginLoading = ref(false)
  184. const loginData = reactive({
  185. isShowPassword: false,
  186. captchaEnable: import.meta.env.VITE_APP_CAPTCHA_ENABLE,
  187. tenantEnable: import.meta.env.VITE_APP_TENANT_ENABLE,
  188. token: '',
  189. loading: {
  190. signIn: false
  191. },
  192. loginForm: {
  193. tenantName: '芋道源码',
  194. username: 'admin',
  195. password: 'admin123',
  196. captchaVerification: '',
  197. rememberMe: false
  198. }
  199. })
  200. // blockPuzzle 滑块 clickWord 点击文字
  201. const verify = ref()
  202. const captchaType = ref('blockPuzzle')
  203. // 获取验证码
  204. const getCode = async () => {
  205. // 情况一,未开启:则直接登录
  206. if (loginData.captchaEnable === 'false') {
  207. await handleLogin({})
  208. return
  209. }
  210. // 情况二,已开启:则展示验证码;只有完成验证码的情况,才进行登录
  211. // 弹出验证码
  212. verify.value.show()
  213. }
  214. //获取租户ID
  215. const getTenantId = async () => {
  216. const res = await LoginApi.getTenantIdByNameApi(loginData.loginForm.tenantName)
  217. setTenantId(res)
  218. }
  219. // 记住我
  220. const getCookie = () => {
  221. const username = Cookies.get('username')
  222. const password = Cookies.get('password') ? decrypt(Cookies.get('password')) : undefined
  223. const rememberMe = Cookies.get('rememberMe')
  224. const tenantName = Cookies.get('tenantName')
  225. loginData.loginForm = {
  226. ...loginData.loginForm,
  227. username: username ? username : loginData.loginForm.username,
  228. password: password ? password : loginData.loginForm.password,
  229. rememberMe: rememberMe ? true : false,
  230. tenantName: tenantName ? tenantName : loginData.loginForm.tenantName
  231. }
  232. }
  233. // 登录
  234. const handleLogin = async (params) => {
  235. loginLoading.value = true
  236. try {
  237. await getTenantId()
  238. const data = await validForm()
  239. if (!data) {
  240. return
  241. }
  242. loginData.loginForm.captchaVerification = params.captchaVerification
  243. const res = await LoginApi.loginApi(loginData.loginForm)
  244. if (!res) {
  245. return
  246. }
  247. if (loginData.loginForm.rememberMe) {
  248. Cookies.set('username', loginData.loginForm.username, { expires: 30 })
  249. Cookies.set('password', encrypt(loginData.loginForm.password), { expires: 30 })
  250. Cookies.set('rememberMe', loginData.loginForm.rememberMe, { expires: 30 })
  251. Cookies.set('tenantName', loginData.loginForm.tenantName, { expires: 30 })
  252. } else {
  253. Cookies.remove('username')
  254. Cookies.remove('password')
  255. Cookies.remove('rememberMe')
  256. Cookies.remove('tenantName')
  257. }
  258. setToken(res)
  259. if (!redirect.value) {
  260. redirect.value = '/'
  261. }
  262. push({ path: redirect.value || permissionStore.addRouters[0].path })
  263. } catch {
  264. loginLoading.value = false
  265. }
  266. }
  267. // 社交登录
  268. const doSocialLogin = async (type: string) => {
  269. loginLoading.value = true
  270. // 计算 redirectUri
  271. const redirectUri =
  272. location.origin + '/social-login?type=' + type + '&redirect=' + (redirect.value || '/')
  273. // 进行跳转
  274. const res = await LoginApi.socialAuthRedirectApi(type, encodeURIComponent(redirectUri))
  275. window.location.href = res
  276. }
  277. watch(
  278. () => currentRoute.value,
  279. (route: RouteLocationNormalizedLoaded) => {
  280. redirect.value = route?.query?.redirect as string
  281. },
  282. {
  283. immediate: true
  284. }
  285. )
  286. onMounted(() => {
  287. getCookie()
  288. })
  289. </script>
  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>