SocialLogin.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <template>
  2. <div
  3. :class="prefixCls"
  4. class="relative h-[100%] lt-xl:bg-[var(--login-bg-color)] lt-md:px-10px lt-sm:px-10px lt-xl:px-10px"
  5. >
  6. <div class="relative mx-auto h-full flex">
  7. <div
  8. :class="`${prefixCls}__left flex-1 bg-gray-500 bg-opacity-20 relative p-30px lt-xl:hidden`"
  9. >
  10. <!-- 左上角的 logo + 系统标题 -->
  11. <div class="relative flex items-center text-white">
  12. <img alt="" class="mr-10px h-48px w-48px" src="@/assets/imgs/logo.png" />
  13. <span class="text-20px font-bold">{{ underlineToHump(appStore.getTitle) }}</span>
  14. </div>
  15. <!-- 左边的背景图 + 欢迎语 -->
  16. <div class="h-[calc(100%-60px)] flex items-center justify-center">
  17. <TransitionGroup
  18. appear
  19. enter-active-class="animate__animated animate__bounceInLeft"
  20. tag="div"
  21. >
  22. <img key="1" alt="" class="w-350px" src="@/assets/svgs/login-box-bg.svg" />
  23. <div key="2" class="text-3xl text-white">{{ t('login.welcome') }}</div>
  24. <div key="3" class="mt-5 text-14px font-normal text-white">
  25. {{ t('login.message') }}
  26. </div>
  27. </TransitionGroup>
  28. </div>
  29. </div>
  30. <div class="relative flex-1 p-30px dark:bg-[var(--login-bg-color)] lt-sm:p-10px">
  31. <!-- 右上角的主题、语言选择 -->
  32. <div
  33. class="flex items-center justify-between text-white at-2xl:justify-end at-xl:justify-end"
  34. >
  35. <div class="flex items-center at-2xl:hidden at-xl:hidden">
  36. <img alt="" class="mr-10px h-48px w-48px" src="@/assets/imgs/logo.png" />
  37. <span class="text-20px font-bold">{{ underlineToHump(appStore.getTitle) }}</span>
  38. </div>
  39. <div class="flex items-center justify-end space-x-10px">
  40. <ThemeSwitch />
  41. <LocaleDropdown class="dark:text-white lt-xl:text-white" />
  42. </div>
  43. </div>
  44. <!-- 右边的登录界面 -->
  45. <Transition appear enter-active-class="animate__animated animate__bounceInRight">
  46. <div
  47. class="m-auto h-full w-[100%] flex items-center at-2xl:max-w-500px at-lg:max-w-500px at-md:max-w-500px at-xl:max-w-500px"
  48. >
  49. <!-- 账号登录 -->
  50. <el-form
  51. v-show="getShow"
  52. ref="formLogin"
  53. :model="loginData.loginForm"
  54. :rules="LoginRules"
  55. class="login-form"
  56. label-position="top"
  57. label-width="120px"
  58. size="large"
  59. >
  60. <el-row style="margin-right: -10px; margin-left: -10px">
  61. <el-col :span="24" style="padding-right: 10px; padding-left: 10px">
  62. <el-form-item>
  63. <LoginFormTitle style="width: 100%" />
  64. </el-form-item>
  65. </el-col>
  66. <el-col :span="24" style="padding-right: 10px; padding-left: 10px">
  67. <el-form-item v-if="loginData.tenantEnable === 'true'" prop="tenantName">
  68. <el-input
  69. v-model="loginData.loginForm.tenantName"
  70. :placeholder="t('login.tenantNamePlaceholder')"
  71. :prefix-icon="iconHouse"
  72. link
  73. type="primary"
  74. />
  75. </el-form-item>
  76. </el-col>
  77. <el-col :span="24" style="padding-right: 10px; padding-left: 10px">
  78. <el-form-item prop="username">
  79. <el-input
  80. v-model="loginData.loginForm.username"
  81. :placeholder="t('login.usernamePlaceholder')"
  82. :prefix-icon="iconAvatar"
  83. />
  84. </el-form-item>
  85. </el-col>
  86. <el-col :span="24" style="padding-right: 10px; padding-left: 10px">
  87. <el-form-item prop="password">
  88. <el-input
  89. v-model="loginData.loginForm.password"
  90. :placeholder="t('login.passwordPlaceholder')"
  91. :prefix-icon="iconLock"
  92. show-password
  93. type="password"
  94. @keyup.enter="getCode()"
  95. />
  96. </el-form-item>
  97. </el-col>
  98. <el-col
  99. :span="24"
  100. style="
  101. padding-right: 10px;
  102. padding-left: 10px;
  103. margin-top: -20px;
  104. margin-bottom: -20px;
  105. "
  106. >
  107. <el-form-item>
  108. <el-row justify="space-between" style="width: 100%">
  109. <el-col :span="6">
  110. <el-checkbox v-model="loginData.loginForm.rememberMe">
  111. {{ t('login.remember') }}
  112. </el-checkbox>
  113. </el-col>
  114. <el-col :offset="6" :span="12">
  115. <el-link style="float: right" type="primary">{{
  116. t('login.forgetPassword')
  117. }}</el-link>
  118. </el-col>
  119. </el-row>
  120. </el-form-item>
  121. </el-col>
  122. <el-col :span="24" style="padding-right: 10px; padding-left: 10px">
  123. <el-form-item>
  124. <XButton
  125. :loading="loginLoading"
  126. :title="t('login.login')"
  127. class="w-[100%]"
  128. type="primary"
  129. @click="getCode()"
  130. />
  131. </el-form-item>
  132. </el-col>
  133. <Verify
  134. ref="verify"
  135. :captchaType="captchaType"
  136. :imgSize="{ width: '400px', height: '200px' }"
  137. mode="pop"
  138. @success="handleLogin"
  139. />
  140. </el-row>
  141. </el-form>
  142. </div>
  143. </Transition>
  144. </div>
  145. </div>
  146. </div>
  147. </template>
  148. <script lang="ts" setup>
  149. import { underlineToHump } from '@/utils'
  150. import { ElLoading } from 'element-plus'
  151. import { useDesign } from '@/hooks/web/useDesign'
  152. import { useAppStore } from '@/store/modules/app'
  153. import { useIcon } from '@/hooks/web/useIcon'
  154. import { usePermissionStore } from '@/store/modules/permission'
  155. import * as LoginApi from '@/api/login'
  156. import * as authUtil from '@/utils/auth'
  157. import { ThemeSwitch } from '@/layout/components/ThemeSwitch'
  158. import { LocaleDropdown } from '@/layout/components/LocaleDropdown'
  159. import { LoginStateEnum, useFormValid, useLoginState } from './components/useLogin'
  160. import LoginFormTitle from './components/LoginFormTitle.vue'
  161. import router from '@/router'
  162. defineOptions({ name: 'SocialLogin' })
  163. const { t } = useI18n()
  164. const route = useRoute()
  165. const appStore = useAppStore()
  166. const { getPrefixCls } = useDesign()
  167. const prefixCls = getPrefixCls('login')
  168. const iconHouse = useIcon({ icon: 'ep:house' })
  169. const iconAvatar = useIcon({ icon: 'ep:avatar' })
  170. const iconLock = useIcon({ icon: 'ep:lock' })
  171. const formLogin = ref<any>()
  172. const { validForm } = useFormValid(formLogin)
  173. const { getLoginState } = useLoginState()
  174. const { push } = useRouter()
  175. const permissionStore = usePermissionStore()
  176. const loginLoading = ref(false)
  177. const verify = ref()
  178. const captchaType = ref('blockPuzzle') // blockPuzzle 滑块 clickWord 点击文字
  179. const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN)
  180. const LoginRules = {
  181. tenantName: [required],
  182. username: [required],
  183. password: [required]
  184. }
  185. const loginData = reactive({
  186. isShowPassword: false,
  187. captchaEnable: import.meta.env.VITE_APP_CAPTCHA_ENABLE !== 'false',
  188. tenantEnable: import.meta.env.VITE_APP_TENANT_ENABLE !== 'false',
  189. loginForm: {
  190. tenantName: '源码',
  191. username: 'admin',
  192. password: 'admin123',
  193. captchaVerification: '',
  194. rememberMe: false
  195. }
  196. })
  197. // 获取验证码
  198. const getCode = async () => {
  199. // 情况一,未开启:则直接登录
  200. if (loginData.captchaEnable) {
  201. await handleLogin({})
  202. } else {
  203. // 情况二,已开启:则展示验证码;只有完成验证码的情况,才进行登录
  204. // 弹出验证码
  205. verify.value.show()
  206. }
  207. }
  208. //获取租户ID
  209. const getTenantId = async () => {
  210. if (loginData.tenantEnable) {
  211. const res = await LoginApi.getTenantIdByName(loginData.loginForm.tenantName)
  212. authUtil.setTenantId(res)
  213. }
  214. }
  215. // 记住我
  216. const getCookie = () => {
  217. const loginForm = authUtil.getLoginForm()
  218. if (loginForm) {
  219. loginData.loginForm = {
  220. ...loginData.loginForm,
  221. username: loginForm.username ? loginForm.username : loginData.loginForm.username,
  222. password: loginForm.password ? loginForm.password : loginData.loginForm.password,
  223. rememberMe: loginForm.rememberMe ? true : false,
  224. tenantName: loginForm.tenantName ? loginForm.tenantName : loginData.loginForm.tenantName
  225. }
  226. }
  227. }
  228. const loading = ref() // ElLoading.service 返回的实例
  229. // tricky: 配合LoginForm.vue中redirectUri需要对参数进行encode,需要在回调后进行decode
  230. function getUrlValue(key: string): string {
  231. const url = new URL(decodeURIComponent(location.href))
  232. return url.searchParams.get(key) ?? ''
  233. }
  234. // 尝试登录: 当账号已经绑定,socialLogin会直接获得token
  235. const tryLogin = async () => {
  236. try {
  237. const type = getUrlValue('type')
  238. const redirect = getUrlValue('redirect')
  239. const code = route?.query?.code as string
  240. const state = route?.query?.state as string
  241. const res = await LoginApi.socialLogin(type, code, state)
  242. authUtil.setToken(res)
  243. router.push({ path: redirect || '/' })
  244. } catch (err) {}
  245. }
  246. // 登录
  247. const handleLogin = async (params) => {
  248. loginLoading.value = true
  249. try {
  250. await getTenantId()
  251. const data = await validForm()
  252. if (!data) {
  253. return
  254. }
  255. let redirect = getUrlValue('redirect')
  256. const type = getUrlValue('type')
  257. const code = route?.query?.code as string
  258. const state = route?.query?.state as string
  259. const res = await LoginApi.login({
  260. // 账号密码登录
  261. username: loginData.loginForm.username,
  262. password: loginData.loginForm.password,
  263. captchaVerification: params.captchaVerification,
  264. // 社交登录
  265. socialCode: code,
  266. socialState: state,
  267. socialType: type
  268. })
  269. if (!res) {
  270. return
  271. }
  272. loading.value = ElLoading.service({
  273. lock: true,
  274. text: '正在加载系统中...',
  275. background: 'rgba(0, 0, 0, 0.7)'
  276. })
  277. if (loginData.loginForm.rememberMe) {
  278. authUtil.setLoginForm(loginData.loginForm)
  279. } else {
  280. authUtil.removeLoginForm()
  281. }
  282. authUtil.setToken(res)
  283. if (!redirect) {
  284. redirect = '/'
  285. }
  286. // 判断是否为SSO登录
  287. if (redirect.indexOf('sso') !== -1) {
  288. window.location.href = window.location.href.replace('/login?redirect=', '')
  289. } else {
  290. push({ path: redirect || permissionStore.addRouters[0].path })
  291. }
  292. } finally {
  293. loginLoading.value = false
  294. loading.value.close()
  295. }
  296. }
  297. onMounted(() => {
  298. getCookie()
  299. tryLogin()
  300. })
  301. </script>
  302. <style lang="scss" scoped>
  303. $prefix-cls: #{$namespace}-login;
  304. .#{$prefix-cls} {
  305. overflow: auto;
  306. &__left {
  307. &::before {
  308. position: absolute;
  309. top: 0;
  310. left: 0;
  311. z-index: -1;
  312. width: 100%;
  313. height: 100%;
  314. background-image: url('@/assets/svgs/login-bg.svg');
  315. background-position: center;
  316. background-repeat: no-repeat;
  317. content: '';
  318. }
  319. }
  320. }
  321. </style>