index.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. <h4>{{ $t('login.title') }}</h4>
  17. <el-form-item prop="loginName">
  18. <el-input
  19. clearable
  20. v-model="form.loginName"
  21. prefix-icon="el-icon-user"
  22. :placeholder="$t('login.loginName')"
  23. />
  24. </el-form-item>
  25. <el-form-item prop="loginPwd">
  26. <el-input
  27. show-password
  28. v-model="form.loginPwd"
  29. prefix-icon="el-icon-lock"
  30. :placeholder="$t('login.loginPwd')"
  31. />
  32. </el-form-item>
  33. <!-- <el-form-item prop="code">
  34. <div class="login-input-group">
  35. <el-input
  36. clearable
  37. v-model="form.code"
  38. prefix-icon="el-icon-_vercode"
  39. :placeholder="$t('login.code')"
  40. />
  41. <img
  42. alt=""
  43. v-if="captcha"
  44. :src="captcha"
  45. class="login-captcha"
  46. @click="changeCaptcha"
  47. />
  48. </div>
  49. </el-form-item> -->
  50. <div class="el-form-item">
  51. <el-checkbox v-model="form.remember">
  52. {{ $t('login.remember') }}
  53. </el-checkbox>
  54. <el-link
  55. type="primary"
  56. :underline="false"
  57. class="ele-pull-right"
  58. @click="$router.push('/forget')"
  59. >
  60. 忘记密码
  61. </el-link>
  62. </div>
  63. <div class="el-form-item">
  64. <el-button
  65. size="large"
  66. type="primary"
  67. class="login-btn"
  68. :loading="loading"
  69. @click="submit"
  70. >
  71. {{ loading ? $t('login.loading') : $t('login.login') }}
  72. </el-button>
  73. </div>
  74. <div class="ele-text-center" style="margin-bottom: 10px">
  75. <i class="login-oauth-icon el-icon-_qq" style="background: #3492ed"></i>
  76. <i
  77. class="login-oauth-icon el-icon-_wechat"
  78. style="background: #4daf29"
  79. ></i>
  80. <i
  81. class="login-oauth-icon el-icon-_weibo"
  82. style="background: #cf1900"
  83. ></i>
  84. </div>
  85. </el-form>
  86. <div class="login-copyright">
  87. copyright © 2022 eleadmin.com all rights reserved.
  88. </div>
  89. <!-- 多语言切换 -->
  90. <div style="position: absolute; right: 30px; top: 20px">
  91. <i18n-icon
  92. :icon-style="{ fontSize: '22px', color: '#fff', cursor: 'pointer' }"
  93. />
  94. </div>
  95. <!-- 实际项目去掉这段 -->
  96. <div
  97. class="hidden-xs-only"
  98. style="position: absolute; right: 30px; bottom: 20px; z-index: 9"
  99. >
  100. <el-radio-group v-model="direction" size="mini">
  101. <el-radio-button label="2">居左</el-radio-button>
  102. <el-radio-button label="0">居中</el-radio-button>
  103. <el-radio-button label="1">居右</el-radio-button>
  104. </el-radio-group>
  105. </div>
  106. </div>
  107. </template>
  108. <script>
  109. import I18nIcon from '@/layout/components/i18n-icon.vue';
  110. import { getToken } from '@/utils/token-util';
  111. import { login, getCaptcha, sub } from '@/api/login';
  112. import { SYSTEM_NAME } from '@/config/setting';
  113. import { setCurrentUser } from '@/utils/token-util';
  114. export default {
  115. // eslint-disable-next-line vue/multi-word-component-names
  116. name: 'Login',
  117. components: { I18nIcon },
  118. data () {
  119. return {
  120. // 登录框方向, 0居中, 1居右, 2居左
  121. direction: 0,
  122. // 加载状态
  123. loading: false,
  124. // 表单数据
  125. form: {
  126. loginName: 'mes111',
  127. loginPwd: '123456',
  128. remember: true
  129. },
  130. // 验证码base64数据
  131. captcha: '',
  132. // 验证码内容, 实际项目去掉
  133. text: ''
  134. };
  135. },
  136. computed: {
  137. // 表单验证规则
  138. rules () {
  139. return {
  140. loginName: [
  141. {
  142. required: true,
  143. message: this.$t('login.loginName'),
  144. type: 'string',
  145. trigger: 'blur'
  146. }
  147. ],
  148. loginPwd: [
  149. {
  150. required: true,
  151. message: this.$t('login.loginPwd'),
  152. type: 'string',
  153. trigger: 'blur'
  154. }
  155. ]
  156. };
  157. }
  158. },
  159. created () {
  160. if (getToken()) {
  161. this.goHome();
  162. } else {
  163. // 重置菜单权限
  164. this.$store.commit('user/setMenus', null);
  165. }
  166. },
  167. methods: {
  168. /* 提交 */
  169. submit () {
  170. this.$refs.form.validate((valid) => {
  171. if (!valid) {
  172. return false;
  173. }
  174. this.loading = true;
  175. login(this.form)
  176. .then((res) => {
  177. localStorage.setItem(`userId-${SYSTEM_NAME}`, res.data.userId);
  178. // 用户信息
  179. if (res.data?.loginChangeGroupVOList.length > 0) {
  180. setCurrentUser(
  181. {
  182. currentGroupId: res.data.loginChangeGroupVOList[0].groupId,
  183. currentRoleId:
  184. res.data.loginChangeGroupVOList[0]
  185. .loginChangeRoleVOList[0].roleId
  186. },
  187. this.form.remember
  188. );
  189. }
  190. this.$store.commit('user/setUserInfo', res.data);
  191. this.loading = false;
  192. this.$message.success(res.message);
  193. // this.$store.dispatch('user/getCurrentUserAuthorityDept');
  194. this.goHome();
  195. })
  196. .catch((e) => {
  197. this.loading = false;
  198. // this.$message.error(e.message);
  199. });
  200. });
  201. },
  202. /* 跳转到首页 */
  203. goHome () {
  204. this.$router.push(this.$route?.query?.from ?? '/').catch(() => {});
  205. },
  206. /* 更换图形验证码 */
  207. changeCaptcha () {
  208. // 这里演示的验证码是后端返回base64格式的形式, 如果后端地址直接是图片请参考忘记密码页面
  209. getCaptcha()
  210. .then((data) => {
  211. this.captcha = data.base64;
  212. // 实际项目后端一般会返回验证码的key而不是直接返回验证码的内容, 登录用key去验证, 可以根据自己后端接口修改
  213. this.text = data.text;
  214. // 自动回填验证码, 实际项目去掉这个
  215. this.form.code = this.text;
  216. this.$refs?.form?.clearValidate();
  217. })
  218. .catch((e) => {
  219. // this.$message.error(e.message);
  220. });
  221. }
  222. }
  223. };
  224. </script>
  225. <style lang="scss" scoped>
  226. /* 背景 */
  227. .login-wrapper {
  228. padding: 50px 20px;
  229. position: relative;
  230. box-sizing: border-box;
  231. background-image: url('@/assets/bg-login.jpg');
  232. background-repeat: no-repeat;
  233. background-size: cover;
  234. min-height: 100vh;
  235. &:before {
  236. content: '';
  237. background-color: rgba(0, 0, 0, 0.2);
  238. position: absolute;
  239. top: 0;
  240. left: 0;
  241. right: 0;
  242. bottom: 0;
  243. }
  244. }
  245. /* 卡片 */
  246. .login-form {
  247. margin: 0 auto;
  248. width: 360px;
  249. max-width: 100%;
  250. padding: 25px 30px;
  251. position: relative;
  252. box-shadow: 0 3px 6px rgba(0, 0, 0, 0.15);
  253. box-sizing: border-box;
  254. border-radius: 4px;
  255. z-index: 2;
  256. h4 {
  257. text-align: center;
  258. margin: 0 0 25px 0;
  259. }
  260. & > .el-form-item {
  261. margin-bottom: 25px;
  262. }
  263. }
  264. .login-form-right .login-form {
  265. margin: 0 15% 0 auto;
  266. }
  267. .login-form-left .login-form {
  268. margin: 0 auto 0 15%;
  269. }
  270. /* 验证码 */
  271. .login-input-group {
  272. display: flex;
  273. align-items: center;
  274. :deep(.el-input) {
  275. flex: 1;
  276. }
  277. }
  278. .login-captcha {
  279. height: 38px;
  280. width: 102px;
  281. margin-left: 10px;
  282. border-radius: 4px;
  283. border: 1px solid #dcdfe6;
  284. text-align: center;
  285. cursor: pointer;
  286. &:hover {
  287. opacity: 0.75;
  288. }
  289. }
  290. .login-btn {
  291. display: block;
  292. width: 100%;
  293. }
  294. /* 第三方登录图标 */
  295. .login-oauth-icon {
  296. color: #fff;
  297. padding: 5px;
  298. margin: 0 10px;
  299. font-size: 18px;
  300. border-radius: 50%;
  301. cursor: pointer;
  302. }
  303. /* 底部版权 */
  304. .login-copyright {
  305. color: #eee;
  306. padding-top: 20px;
  307. text-align: center;
  308. position: relative;
  309. z-index: 1;
  310. }
  311. /* 响应式 */
  312. @media screen and (min-height: 550px) {
  313. .login-form {
  314. position: absolute;
  315. top: 50%;
  316. left: 50%;
  317. transform: translateX(-50%);
  318. margin-top: -220px;
  319. }
  320. .login-form-right .login-form,
  321. .login-form-left .login-form {
  322. left: auto;
  323. right: 15%;
  324. transform: translateX(0);
  325. margin: -220px auto auto auto;
  326. }
  327. .login-form-left .login-form {
  328. right: auto;
  329. left: 15%;
  330. }
  331. .login-copyright {
  332. position: absolute;
  333. bottom: 20px;
  334. right: 0;
  335. left: 0;
  336. }
  337. }
  338. @media screen and (max-width: 768px) {
  339. .login-form-right .login-form,
  340. .login-form-left .login-form {
  341. left: 50%;
  342. right: auto;
  343. transform: translateX(-50%);
  344. margin-right: auto;
  345. }
  346. }
  347. </style>