login.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <template>
  2. <div class="login" id="loginBox">
  3. <div class="login-con">
  4. <div class="img-box">
  5. <img src="../assets/images/login_left.png" alt="">
  6. </div>
  7. <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
  8. <h3 class="title">{{vueAppTitle}}</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%;"
  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>{{companyName}}</span>
  57. <a :href="icpUrl" target="_bank">{{icpRecord}}</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. vueAppTitle: process.env.VUE_APP_TITLE,
  71. companyName: process.env.VUE_APP_VUE_APP_COMPANY_NAME,
  72. icpRecord: process.env.VUE_APP_ICP_RECORD,
  73. icpUrl: process.env.VUE_APP_ICP_URL,
  74. cookiePassword: "",
  75. loginForm: {
  76. username: "",
  77. password: "",
  78. rememberMe: false,
  79. code: "",
  80. uuid: ""
  81. },
  82. loginRules: {
  83. username: [
  84. { required: true, trigger: "blur", message: "用户名不能为空" }
  85. ],
  86. password: [
  87. { required: true, trigger: "blur", message: "密码不能为空" }
  88. ],
  89. code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
  90. },
  91. loading: false,
  92. redirect: undefined
  93. };
  94. },
  95. watch: {
  96. $route: {
  97. handler: function(route) {
  98. this.redirect = route.query && route.query.redirect;
  99. },
  100. immediate: true
  101. }
  102. },
  103. mounted () {
  104. },
  105. created() {
  106. this.getCode();
  107. this.getCookie();
  108. },
  109. methods: {
  110. getCode() {
  111. getCodeImg().then(res => {
  112. this.codeUrl = "data:image/gif;base64," + res.img;
  113. this.loginForm.uuid = res.uuid;
  114. });
  115. },
  116. getCookie() {
  117. const username = Cookies.get("username");
  118. const password = Cookies.get("password");
  119. const rememberMe = Cookies.get('rememberMe')
  120. this.loginForm = {
  121. username: username === undefined ? this.loginForm.username : username,
  122. password: password === undefined ? this.loginForm.password : decrypt(password),
  123. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  124. };
  125. },
  126. handleLogin() {
  127. this.$refs.loginForm.validate(valid => {
  128. if (valid) {
  129. this.loading = true;
  130. if (this.loginForm.rememberMe) {
  131. Cookies.set("username", this.loginForm.username, { expires: 30 });
  132. Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
  133. Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
  134. } else {
  135. Cookies.remove("username");
  136. Cookies.remove("password");
  137. Cookies.remove('rememberMe');
  138. }
  139. this.$store
  140. .dispatch("Login", this.loginForm)
  141. .then(() => {
  142. this.$router.push({ path: this.redirect || "/" });
  143. })
  144. .catch(() => {
  145. this.loading = false;
  146. this.getCode();
  147. });
  148. }
  149. });
  150. }
  151. }
  152. };
  153. </script>
  154. <style rel="stylesheet/scss" lang="scss">
  155. .login {
  156. display: flex;
  157. justify-content: center;
  158. align-items: center;
  159. height: 100%;
  160. width: 100%;
  161. background-image: linear-gradient(#0e68c3, #1e99f5);
  162. background-size: cover;
  163. .login-con{
  164. width: 750px;
  165. display: flex;
  166. align-items: center;
  167. justify-content: space-between;
  168. border-radius: 10px;
  169. overflow: hidden;
  170. background: #ffffff;
  171. .img-box{
  172. width: 420px;
  173. height: 420px;
  174. img{
  175. width: 100%;
  176. height: 100%;
  177. object-fit: cover;
  178. }
  179. }
  180. }
  181. }
  182. .title {
  183. margin: 0px auto 30px auto;
  184. text-align: center;
  185. color: #707070;
  186. }
  187. .login-form {
  188. box-sizing: border-box;
  189. background: #ffffff;
  190. width: 330px;
  191. padding: 25px 25px 5px 25px;
  192. .el-input {
  193. height: 38px;
  194. input {
  195. height: 38px;
  196. }
  197. }
  198. .input-icon {
  199. height: 39px;
  200. width: 14px;
  201. margin-left: 2px;
  202. }
  203. }
  204. .login-tip {
  205. font-size: 13px;
  206. text-align: center;
  207. color: #bfbfbf;
  208. }
  209. .login-code {
  210. width: 33%;
  211. height: 38px;
  212. float: right;
  213. img {
  214. cursor: pointer;
  215. vertical-align: middle;
  216. }
  217. }
  218. .el-login-footer {
  219. height: 40px;
  220. line-height: 40px;
  221. position: fixed;
  222. bottom: 0;
  223. width: 100%;
  224. text-align: center;
  225. color: #fff;
  226. font-family: Arial;
  227. font-size: 12px;
  228. letter-spacing: 1px;
  229. }
  230. .login-code-img {
  231. height: 38px;
  232. }
  233. </style>