index.vue 8.4 KB

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