index.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. this.$store.commit('user/setUserInfo', res.data);
  179. this.loading = false;
  180. this.$message.success(res.message);
  181. this.goHome();
  182. })
  183. .catch((e) => {
  184. this.loading = false;
  185. // this.$message.error(e.message);
  186. });
  187. });
  188. },
  189. /* 跳转到首页 */
  190. goHome () {
  191. this.$router.push(this.$route?.query?.from ?? '/').catch(() => {});
  192. },
  193. /* 更换图形验证码 */
  194. changeCaptcha () {
  195. // 这里演示的验证码是后端返回base64格式的形式, 如果后端地址直接是图片请参考忘记密码页面
  196. getCaptcha()
  197. .then((data) => {
  198. this.captcha = data.base64;
  199. // 实际项目后端一般会返回验证码的key而不是直接返回验证码的内容, 登录用key去验证, 可以根据自己后端接口修改
  200. this.text = data.text;
  201. // 自动回填验证码, 实际项目去掉这个
  202. this.form.code = this.text;
  203. this.$refs?.form?.clearValidate();
  204. })
  205. .catch((e) => {
  206. // this.$message.error(e.message);
  207. });
  208. }
  209. }
  210. };
  211. </script>
  212. <style lang="scss" scoped>
  213. /* 背景 */
  214. .login-wrapper {
  215. padding: 50px 20px;
  216. position: relative;
  217. box-sizing: border-box;
  218. background-image: url('@/assets/bg-login.jpg');
  219. background-repeat: no-repeat;
  220. background-size: cover;
  221. min-height: 100vh;
  222. &:before {
  223. content: '';
  224. background-color: rgba(0, 0, 0, 0.2);
  225. position: absolute;
  226. top: 0;
  227. left: 0;
  228. right: 0;
  229. bottom: 0;
  230. }
  231. }
  232. /* 卡片 */
  233. .login-form {
  234. margin: 0 auto;
  235. width: 360px;
  236. max-width: 100%;
  237. padding: 25px 30px;
  238. position: relative;
  239. box-shadow: 0 3px 6px rgba(0, 0, 0, 0.15);
  240. box-sizing: border-box;
  241. border-radius: 4px;
  242. z-index: 2;
  243. h4 {
  244. text-align: center;
  245. margin: 0 0 25px 0;
  246. }
  247. & > .el-form-item {
  248. margin-bottom: 25px;
  249. }
  250. }
  251. .login-form-right .login-form {
  252. margin: 0 15% 0 auto;
  253. }
  254. .login-form-left .login-form {
  255. margin: 0 auto 0 15%;
  256. }
  257. /* 验证码 */
  258. .login-input-group {
  259. display: flex;
  260. align-items: center;
  261. :deep(.el-input) {
  262. flex: 1;
  263. }
  264. }
  265. .login-captcha {
  266. height: 38px;
  267. width: 102px;
  268. margin-left: 10px;
  269. border-radius: 4px;
  270. border: 1px solid #dcdfe6;
  271. text-align: center;
  272. cursor: pointer;
  273. &:hover {
  274. opacity: 0.75;
  275. }
  276. }
  277. .login-btn {
  278. display: block;
  279. width: 100%;
  280. }
  281. /* 第三方登录图标 */
  282. .login-oauth-icon {
  283. color: #fff;
  284. padding: 5px;
  285. margin: 0 10px;
  286. font-size: 18px;
  287. border-radius: 50%;
  288. cursor: pointer;
  289. }
  290. /* 底部版权 */
  291. .login-copyright {
  292. color: #eee;
  293. padding-top: 20px;
  294. text-align: center;
  295. position: relative;
  296. z-index: 1;
  297. }
  298. /* 响应式 */
  299. @media screen and (min-height: 550px) {
  300. .login-form {
  301. position: absolute;
  302. top: 50%;
  303. left: 50%;
  304. transform: translateX(-50%);
  305. margin-top: -220px;
  306. }
  307. .login-form-right .login-form,
  308. .login-form-left .login-form {
  309. left: auto;
  310. right: 15%;
  311. transform: translateX(0);
  312. margin: -220px auto auto auto;
  313. }
  314. .login-form-left .login-form {
  315. right: auto;
  316. left: 15%;
  317. }
  318. .login-copyright {
  319. position: absolute;
  320. bottom: 20px;
  321. right: 0;
  322. left: 0;
  323. }
  324. }
  325. @media screen and (max-width: 768px) {
  326. .login-form-right .login-form,
  327. .login-form-left .login-form {
  328. left: 50%;
  329. right: auto;
  330. transform: translateX(-50%);
  331. margin-right: auto;
  332. }
  333. }
  334. </style>