login.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <template>
  2. <div class="login">
  3. <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
  4. <h3 class="title">大木仓储管理系统</h3>
  5. <el-form-item prop="username">
  6. <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
  7. <!-- <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />-->
  8. <i slot="prefix" class="el-icon-user" style="font-size: 20px;padding-top: 11px;padding-left: 2px;color: #565656;"></i>
  9. </el-input>
  10. </el-form-item>
  11. <el-form-item prop="password">
  12. <el-input
  13. v-model="loginForm.password"
  14. type="password"
  15. auto-complete="off"
  16. placeholder="密码"
  17. @keyup.enter.native="handleLogin"
  18. >
  19. <!-- <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />-->
  20. <i slot="prefix" class="el-icon-lock" style="font-size: 20px;padding-top: 11px;padding-left: 2px;color: #565656;"></i>
  21. </el-input>
  22. </el-form-item>
  23. <el-form-item prop="code">
  24. <el-input
  25. v-model="loginForm.code"
  26. auto-complete="off"
  27. placeholder="验证码"
  28. style="width: 63%"
  29. @keyup.enter.native="handleLogin"
  30. >
  31. <!-- <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />-->
  32. <i slot="prefix" class="el-icon-connection" style="font-size: 20px;padding-top: 11px;padding-left: 2px;color: #565656;"></i>
  33. </el-input>
  34. <div class="login-code">
  35. <img :src="codeUrl" @click="getCode" class="login-code-img"/>
  36. </div>
  37. </el-form-item>
  38. <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;color: rgba(245, 248, 250, 0.8)">记住密码</el-checkbox>
  39. <el-form-item style="width:100%;">
  40. <el-button
  41. :loading="loading"
  42. size="medium"
  43. type="primary"
  44. style="width:100%;border-radius: 200px"
  45. @click.native.prevent="handleLogin"
  46. >
  47. <span v-if="!loading">登 录</span>
  48. <span v-else>登 录 中...</span>
  49. </el-button>
  50. </el-form-item>
  51. </el-form>
  52. <!-- 底部 -->
  53. <div class="el-login-footer">
  54. <span>Copyright © 2020-2022 大木仓储管理系统</span>
  55. </div>
  56. </div>
  57. </template>
  58. <script>
  59. import { getCodeImg } from "@/api/login";
  60. import Cookies from "js-cookie";
  61. import { encrypt, decrypt } from '@/utils/jsencrypt'
  62. export default {
  63. name: "Login",
  64. data() {
  65. return {
  66. codeUrl: "",
  67. cookiePassword: "",
  68. loginForm: {
  69. username: "admin",
  70. password: "admin123",
  71. rememberMe: false,
  72. code: "",
  73. uuid: ""
  74. },
  75. loginRules: {
  76. username: [
  77. { required: true, trigger: "blur", message: "用户名不能为空" }
  78. ],
  79. password: [
  80. { required: true, trigger: "blur", message: "密码不能为空" }
  81. ],
  82. code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
  83. },
  84. loading: false,
  85. redirect: undefined
  86. };
  87. },
  88. watch: {
  89. $route: {
  90. handler: function(route) {
  91. this.redirect = route.query && route.query.redirect;
  92. },
  93. immediate: true
  94. }
  95. },
  96. created() {
  97. this.getCode();
  98. this.getCookie();
  99. },
  100. methods: {
  101. getCode() {
  102. getCodeImg().then(res => {
  103. this.codeUrl = "data:image/gif;base64," + res.img;
  104. this.loginForm.uuid = res.uuid;
  105. });
  106. },
  107. getCookie() {
  108. const username = Cookies.get("username");
  109. const password = Cookies.get("password");
  110. const rememberMe = Cookies.get('rememberMe')
  111. this.loginForm = {
  112. username: username === undefined ? this.loginForm.username : username,
  113. password: password === undefined ? this.loginForm.password : decrypt(password),
  114. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  115. };
  116. },
  117. handleLogin() {
  118. this.$refs.loginForm.validate(valid => {
  119. if (valid) {
  120. this.loading = true;
  121. if (this.loginForm.rememberMe) {
  122. Cookies.set("username", this.loginForm.username, { expires: 30 });
  123. Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
  124. Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
  125. } else {
  126. Cookies.remove("username");
  127. Cookies.remove("password");
  128. Cookies.remove('rememberMe');
  129. }
  130. this.$store
  131. .dispatch("Login", this.loginForm)
  132. .then(() => {
  133. this.$router.push({ path: this.redirect || "/" });
  134. })
  135. .catch(() => {
  136. this.loading = false;
  137. this.getCode();
  138. });
  139. }
  140. });
  141. }
  142. }
  143. };
  144. </script>
  145. <style rel="stylesheet/scss" lang="scss">
  146. .login {
  147. display: flex;
  148. justify-content: center;
  149. align-items: center;
  150. height: 100%;
  151. background-image: url("../assets/images/login-background.jpg");
  152. background-size: cover;
  153. }
  154. .title {
  155. margin: 0px auto 30px auto;
  156. text-align: center;
  157. font-size: 30px;
  158. color: #FFFFFF;
  159. }
  160. .login-form {
  161. border-radius: 6px;
  162. //background: none;
  163. background-color: rgba(0,0,0,0.05);
  164. width: 400px;
  165. padding: 25px 25px 5px 25px;
  166. .el-input {
  167. height: 38px;
  168. input {
  169. height: 40px;
  170. border-radius: 200px;
  171. color: #000000;
  172. background-color: rgba(245,248,250,0.8);
  173. border-color:rgba(0,0,0,0.1);
  174. }
  175. input::placeholder {
  176. color: #c1c1c1;
  177. }
  178. }
  179. .input-icon {
  180. height: 39px;
  181. width: 15px;
  182. margin-left: 2px;
  183. color: #565656;
  184. }
  185. }
  186. .login-tip {
  187. font-size: 13px;
  188. text-align: center;
  189. color: #bfbfbf;
  190. }
  191. .login-code {
  192. width: 33%;
  193. height: 38px;
  194. float: right;
  195. border-color: #fff;
  196. img {
  197. cursor: pointer;
  198. vertical-align: middle;
  199. }
  200. }
  201. .el-login-footer {
  202. height: 40px;
  203. line-height: 40px;
  204. position: fixed;
  205. bottom: 0;
  206. width: 100%;
  207. text-align: center;
  208. color: #fff;
  209. font-family: Arial;
  210. font-size: 12px;
  211. letter-spacing: 1px;
  212. }
  213. .login-code-img {
  214. height: 38px;
  215. }
  216. </style>