login.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <template>
  2. <div class="login">
  3. <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
  4. <div class="title">欢迎登录</div>
  5. <div class="title2">茂名智慧应急一张图</div>
  6. <div class="login-box">
  7. <div class="login-tabs">
  8. <div v-for="(item, index) in tabs" :key="index" :class="tabActive === index ? 'login-tab tab-active' : 'login-tab'" @click="handleTab(index)">{{ item }}</div>
  9. </div>
  10. <el-form-item prop="username">
  11. <el-input v-model="loginForm.username" type="text" size="large" auto-complete="off" placeholder="账号">
  12. <template #prefix><i class="icon-user" /></template>
  13. </el-input>
  14. </el-form-item>
  15. <el-form-item prop="password">
  16. <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="密码" @keyup.enter="handleLogin">
  17. <template #prefix><i class="icon-password" /></template>
  18. </el-input>
  19. </el-form-item>
  20. <el-form-item v-if="captchaEnabled" prop="code">
  21. <el-input v-model="loginForm.code" size="large" auto-complete="off" placeholder="验证码" style="width: 63%" @keyup.enter="handleLogin">
  22. <template #prefix><i class="icon-verify" /></template>
  23. </el-input>
  24. <div class="login-code">
  25. <img :src="codeUrl" class="login-code-img" @click="getCode" />
  26. </div>
  27. </el-form-item>
  28. <!-- <el-checkbox v-model="loginForm.rememberMe" style="margin: 0 0 25px 0">记住密码</el-checkbox>-->
  29. <!--
  30. <el-form-item style="float: right">
  31. <el-button circle title="微信登录" @click="doSocialLogin('wechat')">
  32. <svg-icon icon-class="wechat" />
  33. </el-button>
  34. <el-button circle title="MaxKey登录" @click="doSocialLogin('maxkey')">
  35. <svg-icon icon-class="maxkey" />
  36. </el-button>
  37. <el-button circle title="TopIam登录" @click="doSocialLogin('topiam')">
  38. <svg-icon icon-class="topiam" />
  39. </el-button>
  40. <el-button circle title="Gitee登录" @click="doSocialLogin('gitee')">
  41. <svg-icon icon-class="gitee" />
  42. </el-button>
  43. <el-button circle title="Github登录" @click="doSocialLogin('github')">
  44. <svg-icon icon-class="github" />
  45. </el-button>
  46. </el-form-item>
  47. -->
  48. <el-form-item style="width: 100%">
  49. <el-button :loading="loading" size="large" type="primary" style="width: 100%" @click.prevent="handleLogin">
  50. <span v-if="!loading">登 录</span>
  51. <span v-else>登 录 中...</span>
  52. </el-button>
  53. <div v-if="register" style="float: right">
  54. <router-link class="link-type" :to="'/register'">立即注册</router-link>
  55. </div>
  56. </el-form-item>
  57. </div>
  58. </el-form>
  59. <!-- 底部 -->
  60. <div class="el-login-footer">
  61. <span v-if="false"></span>
  62. </div>
  63. </div>
  64. </template>
  65. <script setup lang="ts">
  66. import { getCodeImg, getTenantList } from '@/api/login';
  67. import { authBinding } from '@/api/system/social/auth';
  68. import { useUserStore } from '@/store/modules/user';
  69. import { LoginData, TenantVO } from '@/api/types';
  70. import { to } from 'await-to-js';
  71. import { HttpStatus } from '@/enums/RespEnum';
  72. const userStore = useUserStore();
  73. const router = useRouter();
  74. const tabActive = ref(0);
  75. // '粤政易扫码登录',
  76. const tabs = reactive(['账号密码登录', '粤政易登录']);
  77. const loginForm = ref<LoginData>({
  78. tenantId: '000000',
  79. username: 'admin',
  80. password: 'admin123',
  81. rememberMe: false,
  82. code: '',
  83. uuid: ''
  84. } as LoginData);
  85. const loginRules: ElFormRules = {
  86. tenantId: [{ required: true, trigger: 'blur', message: '请输入您的租户编号' }],
  87. username: [{ required: true, trigger: 'blur', message: '请输入您的账号' }],
  88. password: [{ required: true, trigger: 'blur', message: '请输入您的密码' }],
  89. code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
  90. };
  91. const codeUrl = ref('');
  92. const loading = ref(false);
  93. // 验证码开关
  94. const captchaEnabled = ref(true);
  95. // 租户开关
  96. const tenantEnabled = ref(true);
  97. // 注册开关
  98. const register = ref(false);
  99. const redirect = ref(undefined);
  100. const loginRef = ref<ElFormInstance>();
  101. // 租户列表
  102. const tenantList = ref<TenantVO[]>([]);
  103. watch(
  104. () => router.currentRoute.value,
  105. (newRoute: any) => {
  106. redirect.value = newRoute.query && newRoute.query.redirect;
  107. },
  108. { immediate: true }
  109. );
  110. const handleLogin = () => {
  111. loginRef.value?.validate(async (valid: boolean, fields: any) => {
  112. if (valid) {
  113. loading.value = true;
  114. // 勾选了需要记住密码设置在 localStorage 中设置记住用户名和密码
  115. // if (loginForm.value.rememberMe) {
  116. // localStorage.setItem('tenantId', String(loginForm.value.tenantId));
  117. // localStorage.setItem('username', String(loginForm.value.username));
  118. // localStorage.setItem('password', String(loginForm.value.password));
  119. // localStorage.setItem('rememberMe', String(loginForm.value.rememberMe));
  120. // } else {
  121. // // 否则移除
  122. // localStorage.removeItem('tenantId');
  123. // localStorage.removeItem('username');
  124. // localStorage.removeItem('password');
  125. // localStorage.removeItem('rememberMe');
  126. // }
  127. // 调用action的登录方法
  128. const [err] = await to(userStore.login(loginForm.value));
  129. if (!err) {
  130. const redirectUrl = redirect.value || '/';
  131. await router.push(redirectUrl);
  132. loading.value = false;
  133. } else {
  134. loading.value = false;
  135. // 重新获取验证码
  136. if (captchaEnabled.value) {
  137. await getCode();
  138. }
  139. }
  140. } else {
  141. console.log('error submit!', fields);
  142. }
  143. });
  144. };
  145. /**
  146. * 获取验证码
  147. */
  148. const getCode = async () => {
  149. const res = await getCodeImg();
  150. const { data } = res;
  151. captchaEnabled.value = data.captchaEnabled === undefined ? true : data.captchaEnabled;
  152. if (captchaEnabled.value) {
  153. codeUrl.value = 'data:image/gif;base64,' + data.img;
  154. loginForm.value.uuid = data.uuid;
  155. }
  156. };
  157. const getLoginData = () => {
  158. const tenantId = localStorage.getItem('tenantId');
  159. const username = localStorage.getItem('username');
  160. const password = localStorage.getItem('password');
  161. const rememberMe = localStorage.getItem('rememberMe');
  162. loginForm.value = {
  163. tenantId: tenantId === null ? String(loginForm.value.tenantId) : tenantId,
  164. username: username === null ? String(loginForm.value.username) : username,
  165. password: password === null ? String(loginForm.value.password) : String(password),
  166. rememberMe: rememberMe === null ? false : Boolean(rememberMe)
  167. } as LoginData;
  168. };
  169. /**
  170. * 获取租户列表
  171. */
  172. const initTenantList = async () => {
  173. const { data } = await getTenantList();
  174. tenantEnabled.value = data.tenantEnabled === undefined ? true : data.tenantEnabled;
  175. if (tenantEnabled.value) {
  176. tenantList.value = data.voList;
  177. if (tenantList.value != null && tenantList.value.length !== 0) {
  178. loginForm.value.tenantId = tenantList.value[0].tenantId;
  179. }
  180. }
  181. };
  182. /**
  183. * 第三方登录
  184. * @param type
  185. */
  186. const doSocialLogin = (type: string) => {
  187. authBinding(type, loginForm.value.tenantId).then((res: any) => {
  188. if (res.code === HttpStatus.SUCCESS) {
  189. // 获取授权地址跳转
  190. window.location.href = res.data;
  191. } else {
  192. ElMessage.error(res.msg);
  193. }
  194. });
  195. };
  196. const handleTab = (tab) => {
  197. if(tab == 1) {
  198. // const redirect_uri = encodeURIComponent(import.meta.env.YZY_REDIRECT_URI);
  199. // const tyrz_url = import.meta.env.YZY_TYRZ_URL + redirect_uri;
  200. const redirect_uri = encodeURIComponent("http://19.155.220.206:8086/tyrz/login?src=yjdp&redirect="+redirect.value);
  201. const tyrz_url = "https://xtbg.digitalgd.com.cn/zwrz/rz/sso/oauth/authorize?response_type=code&scope=all&client_id=zwrz_mmzhyj&redirect_uri="+redirect_uri;
  202. window.location.href = tyrz_url;
  203. }
  204. }
  205. onMounted(() => {
  206. getCode();
  207. initTenantList();
  208. getLoginData();
  209. });
  210. </script>
  211. <style lang="scss" scoped>
  212. .login {
  213. display: flex;
  214. justify-content: center;
  215. align-items: center;
  216. height: 100%;
  217. background-image: url('../assets/images/login-background.jpg');
  218. background-size: cover;
  219. }
  220. .title {
  221. font-size: 24px;
  222. font-weight: bold;
  223. color: #25282e;
  224. }
  225. .title2 {
  226. font-size: 36px;
  227. font-weight: bold;
  228. margin: 6px 0 20px;
  229. color: #25282e;
  230. }
  231. .login-form {
  232. width: 400px;
  233. .login-box {
  234. border-radius: 6px;
  235. box-shadow: 0 0 4px #d4e9fe;
  236. width: 400px;
  237. padding: 25px 25px 5px 25px;
  238. }
  239. .el-input {
  240. height: 40px;
  241. input {
  242. height: 40px;
  243. }
  244. }
  245. .input-icon {
  246. height: 39px;
  247. width: 14px;
  248. margin-left: 0px;
  249. }
  250. }
  251. .login-tip {
  252. font-size: 13px;
  253. text-align: center;
  254. color: #bfbfbf;
  255. }
  256. .login-code {
  257. width: 33%;
  258. height: 40px;
  259. float: right;
  260. img {
  261. cursor: pointer;
  262. vertical-align: middle;
  263. }
  264. }
  265. .el-login-footer {
  266. height: 40px;
  267. line-height: 40px;
  268. position: fixed;
  269. bottom: 0;
  270. width: 100%;
  271. text-align: center;
  272. color: #fff;
  273. font-family: Arial, serif;
  274. font-size: 12px;
  275. letter-spacing: 1px;
  276. }
  277. .login-code-img {
  278. height: 40px;
  279. padding-left: 12px;
  280. }
  281. .login-tabs {
  282. display: flex;
  283. justify-content: center;
  284. align-items: center;
  285. width: 100%;
  286. height: 40px;
  287. line-height: 40px;
  288. border-bottom: 1px solid #eeeeee;
  289. margin-bottom: 15px;
  290. .login-tab {
  291. padding: 0 10px;
  292. }
  293. .tab-active {
  294. color: #5695eb;
  295. position: relative;
  296. &::before {
  297. content: '';
  298. width: 100%;
  299. height: 1px;
  300. position: absolute;
  301. bottom: 0;
  302. left: 0;
  303. background-color: #5695eb;
  304. }
  305. }
  306. }
  307. :deep(.el-input--large) {
  308. .el-input__wrapper {
  309. padding: 1px 5px !important;
  310. }
  311. }
  312. .icon-user {
  313. background: url('@/assets/images/user2.png') no-repeat;
  314. }
  315. .icon-password {
  316. background: url('@/assets/images/password.png') no-repeat;
  317. }
  318. .icon-verify {
  319. background: url('@/assets/images/verify.png') no-repeat;
  320. }
  321. .icon-user,
  322. .icon-password,
  323. .icon-verify {
  324. width: 24px;
  325. height: 24px;
  326. background-size: 100% 100%;
  327. margin-right: 10px;
  328. position: relative;
  329. &::after {
  330. content: '';
  331. display: inline-block;
  332. width: 1px;
  333. height: 18px;
  334. background-color: #d9d9d9;
  335. position: absolute;
  336. top: 3px;
  337. right: -5px;
  338. }
  339. }
  340. </style>