index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. export default {
  114. // eslint-disable-next-line vue/multi-word-component-names
  115. name: 'Login',
  116. components: { I18nIcon },
  117. data () {
  118. return {
  119. // 登录框方向, 0居中, 1居右, 2居左
  120. direction: 0,
  121. // 加载状态
  122. loading: false,
  123. // 表单数据
  124. form: {
  125. loginName: 'mes111',
  126. loginPwd: '123456',
  127. remember: true
  128. },
  129. // 验证码base64数据
  130. captcha: '',
  131. // 验证码内容, 实际项目去掉
  132. text: ''
  133. };
  134. },
  135. computed: {
  136. // 表单验证规则
  137. rules () {
  138. return {
  139. loginName: [
  140. {
  141. required: true,
  142. message: this.$t('login.loginName'),
  143. type: 'string',
  144. trigger: 'blur'
  145. }
  146. ],
  147. loginPwd: [
  148. {
  149. required: true,
  150. message: this.$t('login.loginPwd'),
  151. type: 'string',
  152. trigger: 'blur'
  153. }
  154. ]
  155. };
  156. }
  157. },
  158. created () {
  159. if (getToken()) {
  160. this.goHome();
  161. } else {
  162. // 重置菜单权限
  163. this.$store.commit('user/setMenus', null);
  164. }
  165. },
  166. methods: {
  167. /* 提交 */
  168. submit () {
  169. this.$refs.form.validate((valid) => {
  170. if (!valid) {
  171. return false;
  172. }
  173. this.loading = true;
  174. login(this.form)
  175. .then((res) => {
  176. localStorage.setItem(`userId-${SYSTEM_NAME}`, res.data.userId);
  177. // 用户信息
  178. if (res.data?.loginChangeGroupVOList.length > 0) {
  179. sessionStorage['currentUser'] = JSON.stringify({
  180. currentGroupId: res.data.loginChangeGroupVOList[0].groupId,
  181. currentRoleId:
  182. res.data.loginChangeGroupVOList[0].loginChangeRoleVOList[0]
  183. .roleId
  184. });
  185. }
  186. this.$store.commit('user/setUserInfo', res.data);
  187. this.loading = false;
  188. this.$message.success(res.message);
  189. this.$store.dispatch('user/getCurrentUserAuthorityDept');
  190. this.goHome();
  191. })
  192. .catch((e) => {
  193. this.loading = false;
  194. // this.$message.error(e.message);
  195. });
  196. });
  197. },
  198. /* 跳转到首页 */
  199. goHome () {
  200. this.$router.push(this.$route?.query?.from ?? '/').catch(() => {});
  201. },
  202. /* 更换图形验证码 */
  203. changeCaptcha () {
  204. // 这里演示的验证码是后端返回base64格式的形式, 如果后端地址直接是图片请参考忘记密码页面
  205. getCaptcha()
  206. .then((data) => {
  207. this.captcha = data.base64;
  208. // 实际项目后端一般会返回验证码的key而不是直接返回验证码的内容, 登录用key去验证, 可以根据自己后端接口修改
  209. this.text = data.text;
  210. // 自动回填验证码, 实际项目去掉这个
  211. this.form.code = this.text;
  212. this.$refs?.form?.clearValidate();
  213. })
  214. .catch((e) => {
  215. // this.$message.error(e.message);
  216. });
  217. }
  218. }
  219. };
  220. </script>
  221. <style lang="scss" scoped>
  222. /* 背景 */
  223. .login-wrapper {
  224. padding: 50px 20px;
  225. position: relative;
  226. box-sizing: border-box;
  227. background-image: url('@/assets/bg-login.jpg');
  228. background-repeat: no-repeat;
  229. background-size: cover;
  230. min-height: 100vh;
  231. &:before {
  232. content: '';
  233. background-color: rgba(0, 0, 0, 0.2);
  234. position: absolute;
  235. top: 0;
  236. left: 0;
  237. right: 0;
  238. bottom: 0;
  239. }
  240. }
  241. /* 卡片 */
  242. .login-form {
  243. margin: 0 auto;
  244. width: 360px;
  245. max-width: 100%;
  246. padding: 25px 30px;
  247. position: relative;
  248. box-shadow: 0 3px 6px rgba(0, 0, 0, 0.15);
  249. box-sizing: border-box;
  250. border-radius: 4px;
  251. z-index: 2;
  252. h4 {
  253. text-align: center;
  254. margin: 0 0 25px 0;
  255. }
  256. & > .el-form-item {
  257. margin-bottom: 25px;
  258. }
  259. }
  260. .login-form-right .login-form {
  261. margin: 0 15% 0 auto;
  262. }
  263. .login-form-left .login-form {
  264. margin: 0 auto 0 15%;
  265. }
  266. /* 验证码 */
  267. .login-input-group {
  268. display: flex;
  269. align-items: center;
  270. :deep(.el-input) {
  271. flex: 1;
  272. }
  273. }
  274. .login-captcha {
  275. height: 38px;
  276. width: 102px;
  277. margin-left: 10px;
  278. border-radius: 4px;
  279. border: 1px solid #dcdfe6;
  280. text-align: center;
  281. cursor: pointer;
  282. &:hover {
  283. opacity: 0.75;
  284. }
  285. }
  286. .login-btn {
  287. display: block;
  288. width: 100%;
  289. }
  290. /* 第三方登录图标 */
  291. .login-oauth-icon {
  292. color: #fff;
  293. padding: 5px;
  294. margin: 0 10px;
  295. font-size: 18px;
  296. border-radius: 50%;
  297. cursor: pointer;
  298. }
  299. /* 底部版权 */
  300. .login-copyright {
  301. color: #eee;
  302. padding-top: 20px;
  303. text-align: center;
  304. position: relative;
  305. z-index: 1;
  306. }
  307. /* 响应式 */
  308. @media screen and (min-height: 550px) {
  309. .login-form {
  310. position: absolute;
  311. top: 50%;
  312. left: 50%;
  313. transform: translateX(-50%);
  314. margin-top: -220px;
  315. }
  316. .login-form-right .login-form,
  317. .login-form-left .login-form {
  318. left: auto;
  319. right: 15%;
  320. transform: translateX(0);
  321. margin: -220px auto auto auto;
  322. }
  323. .login-form-left .login-form {
  324. right: auto;
  325. left: 15%;
  326. }
  327. .login-copyright {
  328. position: absolute;
  329. bottom: 20px;
  330. right: 0;
  331. left: 0;
  332. }
  333. }
  334. @media screen and (max-width: 768px) {
  335. .login-form-right .login-form,
  336. .login-form-left .login-form {
  337. left: 50%;
  338. right: auto;
  339. transform: translateX(-50%);
  340. margin-right: auto;
  341. }
  342. }
  343. </style>