LoginForm.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <template>
  2. <el-form
  3. v-show="getShow"
  4. ref="formLogin"
  5. :model="loginData.loginForm"
  6. :rules="LoginRules"
  7. class="login-form"
  8. label-position="top"
  9. label-width="120px"
  10. size="large"
  11. >
  12. <el-row style="display: flex;justify-content: center">
  13. <el-image v-if="loginType === 'uname'" :src="imgsaoma" class="clickable-image" alt="扫码登录" title="扫码登录" @click="changeLogin('dingding')"/>
  14. <el-image v-if="loginType === 'dingding'" :src="imgreturn" class="clickable-image" alt="返回" title="返回" @click="changeLogin('uname')"/>
  15. <el-image :src="img" class=" h-100px" style="margin-top: 16%"/>
  16. <div v-if="loginType === 'uname'" style="width: 90%">
  17. <el-col :span="24" style="padding-right: 10px; padding-left: 10px; margin-top: 85px">
  18. <el-form-item prop="username" >
  19. <el-input v-model="loginData.loginForm.username" :placeholder="t('login.usernamePlaceholder')" type="text" auto-complete="off" :prefix-icon="iconAvatar"/>
  20. </el-form-item>
  21. </el-col>
  22. <el-col :span="24" style="padding-right: 10px; padding-left: 10px;margin-top: 15px">
  23. <el-form-item prop="password">
  24. <el-input
  25. v-model="loginData.loginForm.password"
  26. :placeholder="t('login.passwordPlaceholder')"
  27. :prefix-icon="iconLock"
  28. show-password
  29. type="password"
  30. @keyup.enter="getCode()"
  31. />
  32. </el-form-item>
  33. </el-col>
  34. <el-col
  35. :span="24"
  36. style="padding-right: 10px; padding-left: 10px; margin-top: 20px;"
  37. >
  38. <el-form-item>
  39. <el-row justify="space-between" style="width: 100%;">
  40. <el-col :span="6">
  41. <el-checkbox v-model="loginData.loginForm.rememberMe">
  42. {{ t('login.remember') }}
  43. </el-checkbox>
  44. </el-col>
  45. <el-col :offset="6" :span="12">
  46. <el-link
  47. style="float: right"
  48. type="primary"
  49. @click="setLoginState(LoginStateEnum.RESET_PASSWORD)"
  50. >
  51. {{ t('login.forgetPassword') }}
  52. </el-link>
  53. </el-col>
  54. </el-row>
  55. </el-form-item>
  56. </el-col>
  57. <el-col :span="24" style="padding-right: 10px; padding-left: 10px;margin-bottom: 10px">
  58. <el-form-item>
  59. <XButton
  60. style="height: 4em;background-color: rgb(11, 26, 50);border: none;opacity: 1;color: rgb(186, 205, 224)"
  61. :loading="loginLoading"
  62. :title="t('login.login')"
  63. class="w-[100%]"
  64. type="primary"
  65. @click="getCode()"
  66. />
  67. </el-form-item>
  68. </el-col>
  69. <Verify
  70. v-if="loginData.captchaEnable === 'true'"
  71. ref="verify"
  72. :captchaType="captchaType"
  73. :imgSize="{ width: '400px', height: '200px' }"
  74. mode="pop"
  75. @success="handleLogin"
  76. />
  77. </div>
  78. <div v-if="loginType === 'dingding'" id="login_container" style="margin-left: 8%;margin-top: 18%">
  79. </div>
  80. </el-row>
  81. </el-form>
  82. </template>
  83. <script lang="ts" setup>
  84. import img from '@/assets/imgs/img.png'
  85. import imgsaoma from '@/assets/imgs/saoma.png'
  86. import imgreturn from '@/assets/imgs/return.png'
  87. import { ElLoading } from 'element-plus'
  88. import type { RouteLocationNormalizedLoaded } from 'vue-router'
  89. import { useIcon } from '@/hooks/web/useIcon'
  90. import * as DictDataApi from '@/api/system/dict/dict.data'
  91. import * as authUtil from '@/utils/auth'
  92. import { usePermissionStore } from '@/store/modules/permission'
  93. import * as LoginApi from '@/api/login'
  94. import { LoginStateEnum, useFormValid, useLoginState } from './useLogin'
  95. import { ref,nextTick,onMounted } from 'vue';
  96. const appId = 'dingik345qmyhtysvs2x'
  97. const dingRedirectUrl = 'http://1.94.244.160:91/login?loginType=dingding&type=20'
  98. onMounted(()=>{
  99. getTenantName();
  100. })
  101. const getTenantName=async ()=>{
  102. const queryParams = reactive({
  103. pageNo: 1,
  104. pageSize: 10,
  105. label: '',
  106. status: undefined,
  107. dictType: 'tenant_name'
  108. })
  109. const data = await DictDataApi.getDictDataPage(queryParams)
  110. loginData.loginForm.tenantName = data.list[0].label;
  111. }
  112. const loginType = ref('uname');
  113. const changeLogin = async (type) => {
  114. console.log(loginData.loginForm.tenantName)
  115. await getTenantId()
  116. loginType.value = type;
  117. if (type === 'dingding') {
  118. initDingLogin();
  119. }
  120. };
  121. const initDingLogin=()=> {
  122. // if (window.DDLogin) {
  123. var url = dingRedirectUrl
  124. var state = _getRandomString(10);
  125. var gotoUrl = encodeURIComponent(
  126. "https://oapi.dingtalk.com/connect/oauth2/sns_authorize?" +
  127. "appid=" + appId +
  128. "&response_type=code" +
  129. "&scope=snsapi_login" +
  130. "&state=" + state +
  131. "&redirect_uri=" + url
  132. );
  133. nextTick(() => {
  134. var obj = window.DDLogin({
  135. id: "login_container",
  136. goto: gotoUrl,
  137. style: "border:none;background-color:#FFFFFF;",
  138. width: '90%', // 二维码的宽度
  139. height: "290", // 二维码的高度
  140. })
  141. // 重置扫码登录框的样式,让登录框居中
  142. let box = document.getElementById('login_container')
  143. box.querySelector('iframe').style.top = '0'
  144. box.querySelector('iframe').style.bottom = '0'
  145. box.querySelector('iframe').style.left = '0'
  146. box.querySelector('iframe').style.right = '0'
  147. box.querySelector('iframe').style.margin = 'auto'
  148. })
  149. /* new window.DDLogin({
  150. id: "login_container",
  151. goto: gotoUrl,
  152. style: "border:none;background-color:#FFFFFF;",
  153. width: "350",
  154. height: "350",
  155. }); */
  156. var handleMessage = function (event) {
  157. var origin = event.origin;
  158. if (origin === "https://login.dingtalk.com") {
  159. var loginTmpCode = event.data;
  160. console.log(loginTmpCode)
  161. window.location.href =
  162. "https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=dingik345qmyhtysvs2x&response_type=code&scope=snsapi_login&state=" +
  163. state +
  164. "&redirect_uri=" +
  165. url +
  166. "&loginTmpCode=" +
  167. loginTmpCode;
  168. }
  169. };
  170. if (typeof window.addEventListener != "undefined") {
  171. window.addEventListener("message", handleMessage, false);
  172. } else if (typeof window.attachEvent != "undefined") {
  173. window.attachEvent("onmessage", handleMessage);
  174. }
  175. // }
  176. }
  177. const _getRandomString=(len)=> {
  178. len = len || 10;
  179. let $chars = "ABCDEFGHIJKMNOPQRSTUVWXYZ"; // 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
  180. let maxPos = $chars.length;
  181. let pwd = "";
  182. for (var i = 0; i < len; i++) {
  183. pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
  184. }
  185. return pwd;
  186. }
  187. defineOptions({ name: 'LoginForm' })
  188. const { t } = useI18n()
  189. const message = useMessage()
  190. const iconHouse = useIcon({ icon: 'ep:house' })
  191. const iconAvatar = useIcon({ icon: 'ep:avatar',size:26 })
  192. const iconLock = useIcon({ icon: 'ep:lock',size:26 })
  193. const formLogin = ref()
  194. const { validForm } = useFormValid(formLogin)
  195. const { setLoginState, getLoginState } = useLoginState()
  196. const { currentRoute, push } = useRouter()
  197. const permissionStore = usePermissionStore()
  198. const redirect = ref<string>('')
  199. const loginLoading = ref(false)
  200. const verify = ref()
  201. const captchaType = ref('blockPuzzle') // blockPuzzle 滑块 clickWord 点击文字
  202. const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN)
  203. const LoginRules = {
  204. tenantName: [required],
  205. username: [required],
  206. password: [required]
  207. }
  208. const loginData = reactive({
  209. isShowPassword: false,
  210. captchaEnable: import.meta.env.VITE_APP_CAPTCHA_ENABLE,
  211. tenantEnable: import.meta.env.VITE_APP_TENANT_ENABLE,
  212. loginForm: {
  213. tenantName: '瑞气能源',
  214. username: import.meta.env.VITE_APP_DEFAULT_LOGIN_USERNAME || '',
  215. password: import.meta.env.VITE_APP_DEFAULT_LOGIN_PASSWORD || '',
  216. captchaVerification: '',
  217. rememberMe: true // 默认记录我。如果不需要,可手动修改
  218. }
  219. })
  220. // const socialList = [
  221. // { icon: 'ant-design:wechat-filled', type: 30 },
  222. // { icon: 'ant-design:dingtalk-circle-filled', type: 20 },
  223. // { icon: 'ant-design:github-filled', type: 0 },
  224. // { icon: 'ant-design:alipay-circle-filled', type: 0 }
  225. // ]
  226. // 获取验证码
  227. const getCode = async () => {
  228. // 情况一,未开启:则直接登录
  229. if (loginData.captchaEnable === 'false') {
  230. await handleLogin({})
  231. } else {
  232. // 情况二,已开启:则展示验证码;只有完成验证码的情况,才进行登录
  233. // 弹出验证码
  234. verify.value.show()
  235. }
  236. }
  237. // 获取租户 ID
  238. const getTenantId = async () => {
  239. if (loginData.tenantEnable === 'true') {
  240. const res = await LoginApi.getTenantIdByName(loginData.loginForm.tenantName)
  241. authUtil.setTenantId(res)
  242. }
  243. }
  244. // 记住我
  245. const getLoginFormCache = () => {
  246. const loginForm = authUtil.getLoginForm()
  247. if (loginForm) {
  248. loginData.loginForm = {
  249. ...loginData.loginForm,
  250. username: loginForm.username ? loginForm.username : loginData.loginForm.username,
  251. password: loginForm.password ? loginForm.password : loginData.loginForm.password,
  252. rememberMe: loginForm.rememberMe,
  253. tenantName: loginForm.tenantName ? loginForm.tenantName : loginData.loginForm.tenantName
  254. }
  255. }
  256. }
  257. // 根据域名,获得租户信息
  258. const getTenantByWebsite = async () => {
  259. const website = location.host
  260. const res = await LoginApi.getTenantByWebsite(website)
  261. if (res) {
  262. loginData.loginForm.tenantName = res.name
  263. authUtil.setTenantId(res.id)
  264. }
  265. }
  266. const loading = ref() // ElLoading.service 返回的实例
  267. // 登录
  268. const handleLogin = async (params: any) => {
  269. loginLoading.value = true
  270. try {
  271. await getTenantId()
  272. const data = await validForm()
  273. if (!data) {
  274. return
  275. }
  276. const loginDataLoginForm = { ...loginData.loginForm }
  277. loginDataLoginForm.captchaVerification = params.captchaVerification
  278. const res = await LoginApi.login(loginDataLoginForm)
  279. if (!res) {
  280. return
  281. }
  282. loading.value = ElLoading.service({
  283. lock: true,
  284. text: '正在加载系统中...',
  285. background: 'rgba(0, 0, 0, 0.7)'
  286. })
  287. if (loginDataLoginForm.rememberMe) {
  288. authUtil.setLoginForm(loginDataLoginForm)
  289. } else {
  290. authUtil.removeLoginForm()
  291. }
  292. authUtil.setToken(res)
  293. if (!redirect.value) {
  294. redirect.value = '/'
  295. }
  296. // 判断是否为SSO登录
  297. if (redirect.value.indexOf('sso') !== -1) {
  298. window.location.href = window.location.href.replace('/login?redirect=', '')
  299. } else {
  300. await push({ path: redirect.value || permissionStore.addRouters[0].path })
  301. }
  302. } finally {
  303. loginLoading.value = false
  304. loading.value.close()
  305. }
  306. }
  307. // 社交登录
  308. watch(
  309. () => currentRoute.value,
  310. (route: RouteLocationNormalizedLoaded) => {
  311. redirect.value = route?.query?.redirect as string
  312. },
  313. {
  314. immediate: true
  315. }
  316. )
  317. onMounted(() => {
  318. getLoginFormCache()
  319. getTenantByWebsite()
  320. })
  321. </script>
  322. <style lang="scss" scoped>
  323. ::v-deep .el-input__wrapper {
  324. background-color: transparent !important;
  325. height: 3.5em;
  326. color: white;
  327. box-shadow: none !important;
  328. border-radius: 0;
  329. border: none !important;
  330. border-bottom: 1px solid #bacde0 !important;
  331. outline: none;
  332. }
  333. :deep(.anticon) {
  334. &:hover {
  335. color: var(--el-color-primary) !important;
  336. }
  337. }
  338. .login-code {
  339. float: right;
  340. width: 100%;
  341. height: 38px;
  342. img {
  343. width: 100%;
  344. height: auto;
  345. max-width: 100px;
  346. vertical-align: middle;
  347. cursor: pointer;
  348. }
  349. }
  350. ::v-deep .rqinput .el-input__inner {
  351. background-color: transparent !important;
  352. border: none !important;
  353. border-bottom: 1px solid rgb(186, 205, 224) !important;
  354. border-radius: 0;
  355. height: 2.8em;
  356. //left: 18%;
  357. padding-left: 3em;
  358. color: white;
  359. }
  360. .clickable-image {
  361. cursor: pointer;
  362. position:absolute;
  363. //top: 2%;
  364. //right: 2%;
  365. width: 6%;
  366. margin-top: -9%;
  367. margin-left: 111%;
  368. }
  369. </style>