SocialLogin.vue 12 KB

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