login.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. pattern: /^(?=.*[A-Za-z])(?=.*\d)(?=.*[^A-Za-z0-9]).{8,20}$/,
  90. message: "密码长度为8-20 位,必须包含字母、数字和特殊字符",
  91. trigger: ["blur", "change"],
  92. }
  93. ],
  94. code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
  95. },
  96. loading: false,
  97. redirect: undefined
  98. };
  99. },
  100. watch: {
  101. $route: {
  102. handler: function(route) {
  103. this.redirect = route.query && route.query.redirect;
  104. },
  105. immediate: true
  106. }
  107. },
  108. mounted () {
  109. },
  110. created() {
  111. this.getCode();
  112. this.getCookie();
  113. },
  114. methods: {
  115. getCode() {
  116. getCodeImg().then(res => {
  117. this.codeUrl = "data:image/gif;base64," + res.img;
  118. this.loginForm.uuid = res.uuid;
  119. });
  120. },
  121. getCookie() {
  122. const username = Cookies.get("username");
  123. const password = Cookies.get("password");
  124. const rememberMe = Cookies.get('rememberMe')
  125. this.loginForm = {
  126. username: username === undefined ? this.loginForm.username : username,
  127. password: password === undefined ? this.loginForm.password : decrypt(password),
  128. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  129. };
  130. },
  131. handleLogin() {
  132. this.$refs.loginForm.validate(valid => {
  133. if (valid) {
  134. this.loading = true;
  135. if (this.loginForm.rememberMe) {
  136. Cookies.set("username", this.loginForm.username, { expires: 30 });
  137. Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
  138. Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
  139. } else {
  140. Cookies.remove("username");
  141. Cookies.remove("password");
  142. Cookies.remove('rememberMe');
  143. }
  144. this.$store
  145. .dispatch("Login", this.loginForm)
  146. .then(() => {
  147. this.$router.push({ path: this.redirect || "/" });
  148. })
  149. .catch(() => {
  150. this.loading = false;
  151. this.getCode();
  152. });
  153. }
  154. });
  155. }
  156. }
  157. };
  158. </script>
  159. <style rel="stylesheet/scss" lang="scss">
  160. .login {
  161. display: flex;
  162. justify-content: center;
  163. align-items: center;
  164. height: 100%;
  165. width: 100%;
  166. background-image: linear-gradient(#0e68c3, #1e99f5);
  167. background-size: cover;
  168. .login-con{
  169. width: 750px;
  170. display: flex;
  171. align-items: center;
  172. justify-content: space-between;
  173. border-radius: 10px;
  174. overflow: hidden;
  175. background: #ffffff;
  176. .img-box{
  177. width: 420px;
  178. height: 420px;
  179. img{
  180. width: 100%;
  181. height: 100%;
  182. object-fit: cover;
  183. }
  184. }
  185. }
  186. }
  187. .title {
  188. margin: 0px auto 30px auto;
  189. text-align: center;
  190. color: #707070;
  191. }
  192. .login-form {
  193. box-sizing: border-box;
  194. background: #ffffff;
  195. width: 330px;
  196. padding: 25px 25px 5px 25px;
  197. .el-input {
  198. height: 38px;
  199. input {
  200. height: 38px;
  201. }
  202. }
  203. .input-icon {
  204. height: 39px;
  205. width: 14px;
  206. margin-left: 2px;
  207. }
  208. }
  209. .login-tip {
  210. font-size: 13px;
  211. text-align: center;
  212. color: #bfbfbf;
  213. }
  214. .login-code {
  215. width: 33%;
  216. height: 38px;
  217. float: right;
  218. img {
  219. cursor: pointer;
  220. vertical-align: middle;
  221. }
  222. }
  223. .el-login-footer {
  224. height: 40px;
  225. line-height: 40px;
  226. position: fixed;
  227. bottom: 0;
  228. width: 100%;
  229. text-align: center;
  230. color: #fff;
  231. font-family: Arial;
  232. font-size: 12px;
  233. letter-spacing: 1px;
  234. }
  235. .login-code-img {
  236. height: 38px;
  237. }
  238. </style>