LoginForm.vue 8.7 KB

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