index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <template>
  2. <div
  3. :class="[
  4. 'login-wrapper',
  5. ['', 'login-form-right', 'login-form-left'][direction]
  6. ]"
  7. >
  8. <el-form
  9. ref="form"
  10. size="large"
  11. :model="form"
  12. :rules="rules"
  13. class="login-form ele-bg-white"
  14. @keyup.enter.native="submit"
  15. >
  16. <el-popover width="150" trigger="hover">
  17. <div>
  18. <img style="width: 100%" :src="qrcodeUrl" alt="" srcset="" />
  19. <div style="text-align: center">浏览器扫一扫下载</div>
  20. </div>
  21. <div slot="reference" class="switch-btn"></div>
  22. </el-popover>
  23. <h4>{{ $t('login.title') }}</h4>
  24. <el-form-item prop="loginName">
  25. <el-input
  26. clearable
  27. v-model="form.loginName"
  28. prefix-icon="el-icon-user"
  29. placeholder="请输入登录账号"
  30. />
  31. </el-form-item>
  32. <el-form-item prop="loginPwd">
  33. <el-input
  34. show-password
  35. v-model="form.loginPwd"
  36. prefix-icon="el-icon-lock"
  37. placeholder="请输入登录密码"
  38. />
  39. </el-form-item>
  40. <el-form-item prop="captcha">
  41. <div class="login-input-group">
  42. <el-input
  43. clearable
  44. v-model="form.captcha"
  45. prefix-icon="el-icon-_vercode"
  46. :placeholder="$t('login.code')"
  47. />
  48. <img
  49. alt=""
  50. :src="captcha"
  51. class="login-captcha"
  52. @click="changeCaptcha"
  53. />
  54. </div>
  55. </el-form-item>
  56. <div class="el-form-item">
  57. <el-checkbox v-model="form.remember">
  58. {{ $t('login.remember') }}
  59. </el-checkbox>
  60. <el-link
  61. type="primary"
  62. :underline="false"
  63. class="ele-pull-right"
  64. @click="$router.push('/forget')"
  65. >
  66. 忘记密码
  67. </el-link>
  68. </div>
  69. <div class="el-form-item">
  70. <el-button
  71. size="large"
  72. type="primary"
  73. class="login-btn"
  74. :loading="loading"
  75. @click="submit"
  76. >
  77. {{ loading ? $t('login.loading') : $t('login.login') }}
  78. </el-button>
  79. </div>
  80. <div class="ele-text-center" style="margin-bottom: 10px">
  81. <!-- <i class="login-oauth-icon el-icon-_qq" style="background: #3492ed"></i>
  82. <i
  83. class="login-oauth-icon el-icon-_wechat"
  84. style="background: #4daf29"
  85. ></i>
  86. <i
  87. class="login-oauth-icon el-icon-_weibo"
  88. style="background: #cf1900"
  89. ></i> -->
  90. </div>
  91. </el-form>
  92. <div class="login-copyright">
  93. <!-- copyright © 2022 eleadmin.com all rights reserved. -->
  94. </div>
  95. <!-- 多语言切换 -->
  96. <div style="position: absolute; right: 30px; top: 20px">
  97. <i18n-icon
  98. :icon-style="{ fontSize: '22px', color: '#fff', cursor: 'pointer' }"
  99. />
  100. </div>
  101. <!-- 实际项目去掉这段 -->
  102. <div
  103. class="hidden-xs-only"
  104. style="position: absolute; right: 30px; bottom: 20px; z-index: 9"
  105. >
  106. <el-radio-group v-model="direction" size="mini">
  107. <el-radio-button label="2">居左</el-radio-button>
  108. <el-radio-button label="0">居中</el-radio-button>
  109. <el-radio-button label="1">居右</el-radio-button>
  110. </el-radio-group>
  111. </div>
  112. </div>
  113. </template>
  114. <script>
  115. import I18nIcon from '@/layout/components/i18n-icon.vue';
  116. import { getToken } from '@/utils/token-util';
  117. import {
  118. login,
  119. getCaptcha,
  120. getLatestVersion,
  121. getParentIdListByDeptId
  122. } from '@/api/login';
  123. import xyy from '@/assets/xyy.jpg';
  124. import { getPathAddress } from '@/api/system/file';
  125. import { setCurrentUser } from '@/utils/token-util';
  126. import { factoryareaId } from '@/api/system/user';
  127. import QRCode from 'qrcode';
  128. export default {
  129. // eslint-disable-next-line vue/multi-word-component-names
  130. name: 'Login',
  131. components: { I18nIcon },
  132. data() {
  133. return {
  134. // 登录框方向, 0居中, 1居右, 2居左
  135. xyy,
  136. direction: 0,
  137. // 加载状态
  138. loading: false,
  139. // 表单数据
  140. form: {
  141. loginName: localStorage.getItem('accountInfo')
  142. ? JSON.parse(localStorage.getItem('accountInfo')).loginName
  143. : '',
  144. loginPwd: localStorage.getItem('accountInfo')
  145. ? JSON.parse(localStorage.getItem('accountInfo')).loginPwd
  146. : '',
  147. remember: localStorage.getItem('accountInfo')
  148. ? JSON.parse(localStorage.getItem('accountInfo')).remember
  149. : false,
  150. captcha: '',
  151. uuid: ''
  152. },
  153. // 验证码base64数据
  154. captcha: '',
  155. // 验证码内容, 实际项目去掉
  156. text: '',
  157. qrcodeUrl: ''
  158. };
  159. },
  160. computed: {
  161. // 表单验证规则
  162. rules() {
  163. return {
  164. loginName: [
  165. {
  166. required: true,
  167. message: this.$t('请输入登录账号'),
  168. type: 'string',
  169. trigger: 'blur'
  170. }
  171. ],
  172. loginPwd: [
  173. {
  174. required: true,
  175. message: this.$t('请输入登录密码'),
  176. type: 'string',
  177. trigger: 'blur'
  178. }
  179. ],
  180. captcha: [
  181. {
  182. required: true,
  183. message: '验证码不能为空',
  184. type: 'string',
  185. trigger: 'blur'
  186. }
  187. ]
  188. };
  189. }
  190. },
  191. created() {
  192. this.changeCaptcha();
  193. this.generateQRCode();
  194. if (getToken()) {
  195. this.goHome();
  196. } else {
  197. // 重置菜单权限
  198. this.$store.commit('user/setMenus', null);
  199. }
  200. },
  201. methods: {
  202. generateQRCode() {
  203. getLatestVersion().then((res) => {
  204. console.log(res, 'dasdasd');
  205. QRCode.toDataURL(res.fileUrl, {
  206. errorCorrectionLevel: 'H',
  207. margin: 2,
  208. scale: 8
  209. }).then((url) => {
  210. console.log('qrcodeUrl', url);
  211. this.qrcodeUrl = url;
  212. });
  213. });
  214. },
  215. /* 提交 */
  216. submit() {
  217. this.$refs.form.validate((valid) => {
  218. if (!valid) {
  219. return false;
  220. }
  221. this.loading = true;
  222. login(this.form)
  223. .then(async (res) => {
  224. localStorage.setItem('userId', res.data.userId);
  225. localStorage.setItem('accountId', res.data.accountId);
  226. // 用户信息
  227. if (res.data?.loginChangeGroupVOList.length > 0) {
  228. setCurrentUser(
  229. {
  230. currentGroupId: res.data.loginChangeGroupVOList[0].groupId,
  231. currentRoleId:
  232. res.data.loginChangeGroupVOList[0]
  233. .loginChangeRoleVOList[0].roleId
  234. },
  235. this.form.remember
  236. );
  237. }
  238. res.data.groupIdList = await getParentIdListByDeptId(
  239. res.data.groupId
  240. );
  241. const filePath = await getPathAddress();
  242. res.data.avatarAddress =
  243. res.data.avatar && res.data.avatar.length
  244. ? filePath + res.data.avatar[0].storePath
  245. : xyy;
  246. if (res.data.factoryId) {
  247. let factoryareaData = await factoryareaId(res.data.factoryId);
  248. res.data['factoryName'] = factoryareaData.name;
  249. }
  250. this.$store.commit('user/setUserInfo', res.data);
  251. this.loading = false;
  252. this.$message.success(res.message);
  253. if (this.form.remember) {
  254. localStorage.setItem('accountInfo', JSON.stringify(this.form));
  255. } else {
  256. localStorage.removeItem('accountInfo');
  257. }
  258. // await this.$store.dispatch('user/getCurrentUserAuthorityDept');
  259. // await this.$store.dispatch('paramsSetData/setParamsDataInfo');
  260. this.goHome();
  261. })
  262. .catch((e) => {
  263. this.changeCaptcha();
  264. this.loading = false;
  265. // this.$message.error(e.message);
  266. });
  267. });
  268. },
  269. /* 跳转到首页 */
  270. goHome() {
  271. localStorage.setItem('singleUserInfo', '0');
  272. this.$router.push(this.$route?.query?.from ?? '/').catch(() => {});
  273. },
  274. /* 更换图形验证码 */
  275. changeCaptcha() {
  276. this.form.uuid = this.generateUUID();
  277. // 这里演示的验证码是后端返回base64格式的形式, 如果后端地址直接是图片请参考忘记密码页面
  278. getCaptcha({ uuid: this.form.uuid })
  279. .then((data) => {
  280. this.captcha = URL.createObjectURL(data.data);
  281. this.$refs?.form?.clearValidate();
  282. })
  283. .catch((e) => {
  284. // this.$message.error(e.message);
  285. });
  286. },
  287. generateUUID() {
  288. var d = new Date().getTime(); //Timestamp
  289. var d2 =
  290. (performance && performance.now && performance.now() * 1000) || 0; //Time in microseconds since page-load or 0 if unsupported
  291. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
  292. /[xy]/g,
  293. function (c) {
  294. var r = Math.random() * 16; //random number between 0 and 16
  295. if (d > 0) {
  296. //Use timestamp until depleted
  297. r = (d + r) % 16 | 0;
  298. d = Math.floor(d / 16);
  299. } else {
  300. //Use microseconds since page-load if supported
  301. r = (d2 + r) % 16 | 0;
  302. d2 = Math.floor(d2 / 16);
  303. }
  304. return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);
  305. }
  306. );
  307. }
  308. }
  309. };
  310. </script>
  311. <style lang="scss" scoped>
  312. .switch-btn {
  313. background: url(@/assets/qr-code-sprites.png) 0 0 / 40px auto no-repeat;
  314. cursor: pointer;
  315. height: 40px;
  316. left: 8px;
  317. position: absolute;
  318. top: 8px;
  319. width: 40px;
  320. z-index: 1;
  321. }
  322. .switch-btn:hover {
  323. background-position: 0 -40px;
  324. }
  325. /* 背景 */
  326. .login-wrapper {
  327. padding: 50px 20px;
  328. position: relative;
  329. box-sizing: border-box;
  330. background-image: url('@/assets/bg-login.jpg');
  331. background-repeat: no-repeat;
  332. background-size: cover;
  333. min-height: 100vh;
  334. &:before {
  335. content: '';
  336. background-color: rgba(0, 0, 0, 0.2);
  337. position: absolute;
  338. top: 0;
  339. left: 0;
  340. right: 0;
  341. bottom: 0;
  342. }
  343. }
  344. /* 卡片 */
  345. .login-form {
  346. margin: 0 auto;
  347. width: 360px;
  348. max-width: 100%;
  349. padding: 25px 30px;
  350. position: relative;
  351. box-shadow: 0 3px 6px rgba(0, 0, 0, 0.15);
  352. box-sizing: border-box;
  353. border-radius: 4px;
  354. z-index: 2;
  355. h4 {
  356. text-align: center;
  357. margin: 0 0 25px 0;
  358. }
  359. & > .el-form-item {
  360. margin-bottom: 25px;
  361. }
  362. }
  363. .login-form-right .login-form {
  364. margin: 0 15% 0 auto;
  365. }
  366. .login-form-left .login-form {
  367. margin: 0 auto 0 15%;
  368. }
  369. /* 验证码 */
  370. .login-input-group {
  371. display: flex;
  372. align-items: center;
  373. :deep(.el-input) {
  374. flex: 1;
  375. }
  376. }
  377. .login-captcha {
  378. height: 38px;
  379. width: 102px;
  380. margin-left: 10px;
  381. border-radius: 4px;
  382. border: 1px solid #dcdfe6;
  383. text-align: center;
  384. cursor: pointer;
  385. &:hover {
  386. opacity: 0.75;
  387. }
  388. }
  389. .login-btn {
  390. display: block;
  391. width: 100%;
  392. }
  393. /* 第三方登录图标 */
  394. .login-oauth-icon {
  395. color: #fff;
  396. padding: 5px;
  397. margin: 0 10px;
  398. font-size: 18px;
  399. border-radius: 50%;
  400. cursor: pointer;
  401. }
  402. /* 底部版权 */
  403. .login-copyright {
  404. color: #eee;
  405. padding-top: 20px;
  406. text-align: center;
  407. position: relative;
  408. z-index: 1;
  409. }
  410. /* 响应式 */
  411. @media screen and (min-height: 550px) {
  412. .login-form {
  413. position: absolute;
  414. top: 50%;
  415. left: 50%;
  416. transform: translateX(-50%);
  417. margin-top: -220px;
  418. }
  419. .login-form-right .login-form,
  420. .login-form-left .login-form {
  421. left: auto;
  422. right: 15%;
  423. transform: translateX(0);
  424. margin: -220px auto auto auto;
  425. }
  426. .login-form-left .login-form {
  427. right: auto;
  428. left: 15%;
  429. }
  430. .login-copyright {
  431. position: absolute;
  432. bottom: 20px;
  433. right: 0;
  434. left: 0;
  435. }
  436. }
  437. @media screen and (max-width: 768px) {
  438. .login-form-right .login-form,
  439. .login-form-left .login-form {
  440. left: 50%;
  441. right: auto;
  442. transform: translateX(-50%);
  443. margin-right: auto;
  444. }
  445. }
  446. </style>