index.vue 8.8 KB

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