LoginForm.vue 9.6 KB

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