login.vue 6.0 KB

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