index2.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <template>
  2. <div class="login-container">
  3. <el-form
  4. ref="loginForm"
  5. :model="loginForm"
  6. :rules="loginRules"
  7. class="login-form"
  8. auto-complete="on"
  9. label-position="left"
  10. >
  11. <!-- <div class="title-container">
  12. <img src="@/assets/login/title.png" alt="">
  13. </div> -->
  14. <div class="image-container">
  15. <img src="@/assets/login/image.png" alt="" />
  16. </div>
  17. <div class="right-container">
  18. <div class="empty-height" />
  19. <div class="form-container">
  20. <div class="title">供应链管理系统</div>
  21. <!-- <div class="logo">-->
  22. <!-- <img src="@/assets/login/logo.png" alt="">-->
  23. <!-- </div>-->
  24. <el-form-item prop="username">
  25. <span class="svg-container">
  26. <svg-icon icon-class="user" />
  27. </span>
  28. <el-input
  29. ref="username"
  30. v-model="loginForm.username"
  31. placeholder="请输入用户名"
  32. name="username"
  33. type="text"
  34. tabindex="1"
  35. auto-complete="on"
  36. />
  37. </el-form-item>
  38. <el-form-item prop="password">
  39. <span class="svg-container">
  40. <svg-icon icon-class="password" />
  41. </span>
  42. <el-input
  43. :key="passwordType"
  44. ref="password"
  45. v-model="loginForm.password"
  46. :type="passwordType"
  47. placeholder="请输入密码"
  48. name="password"
  49. tabindex="2"
  50. auto-complete="off"
  51. @keyup.enter.native="handleLogin"
  52. />
  53. <span class="show-pwd" @click="showPwd">
  54. <svg-icon
  55. :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
  56. />
  57. </span>
  58. </el-form-item>
  59. <el-form-item prop="codeValue">
  60. <span class="svg-container">
  61. <svg-icon icon-class="password" />
  62. </span>
  63. <el-input
  64. ref="codeValue"
  65. v-model="loginForm.codeValue"
  66. placeholder="请输入验证码"
  67. name="codeValue"
  68. type="text"
  69. tabindex="3"
  70. auto-complete="off"
  71. @keyup.enter.native="handleLogin"
  72. />
  73. <div class="code" @click="getCode">
  74. <img :src="'data:image/jpeg;base64,' + codeImage" alt="" />
  75. </div>
  76. </el-form-item>
  77. <el-checkbox v-model="isRemenberPw">记住密码</el-checkbox>
  78. <div class="button-container">
  79. <el-button
  80. :loading="loading"
  81. type="primary"
  82. @click.native.prevent="handleLogin"
  83. >登录</el-button
  84. >
  85. </div>
  86. </div>
  87. </div>
  88. </el-form>
  89. </div>
  90. </template>
  91. <script>
  92. import { validUsername } from "@/utils/validate";
  93. import Cookies from "js-cookie";
  94. import { getCode } from "@/api/user";
  95. export default {
  96. name: "Login",
  97. data() {
  98. const validateUsername = (rule, value, callback) => {
  99. if (value.length <= 0) {
  100. callback(new Error("请输入用户名"));
  101. } else {
  102. callback();
  103. }
  104. };
  105. const validatePassword = (rule, value, callback) => {
  106. if (value.length <= 0) {
  107. callback(new Error("请输入密码"));
  108. } else {
  109. callback();
  110. }
  111. };
  112. const validateCode = (rule, value, callback) => {
  113. if (value.length <= 0) {
  114. callback(new Error("请输入验证码"));
  115. } else {
  116. callback();
  117. }
  118. };
  119. return {
  120. loginForm: {
  121. username: "",
  122. password: "",
  123. code: "",
  124. codeValue: "",
  125. },
  126. loginRules: {
  127. username: [
  128. { required: true, trigger: "change", validator: validateUsername },
  129. ],
  130. password: [
  131. { required: true, trigger: "change", validator: validatePassword },
  132. ],
  133. codeValue: [
  134. { required: true, trigger: "change", validator: validateCode },
  135. ],
  136. },
  137. loading: false,
  138. passwordType: "password",
  139. redirect: undefined,
  140. isRemenberPw: false,
  141. codeImage: "",
  142. };
  143. },
  144. watch: {
  145. $route: {
  146. handler: function (route) {
  147. this.redirect = route.query && route.query.redirect;
  148. },
  149. immediate: true,
  150. },
  151. },
  152. created() {
  153. // 获取缓存信息
  154. if (localStorage.getItem("supply_login")) {
  155. let storageData = JSON.parse(localStorage.getItem("supply_login"));
  156. this.loginForm.username = storageData.username;
  157. this.isRemenberPw = storageData.isRemenberPw;
  158. }
  159. if (this.isRemenberPw) {
  160. this.getCookie();
  161. }
  162. this.getCode();
  163. },
  164. methods: {
  165. // 获取验证码
  166. getCode() {
  167. getCode().then((res) => {
  168. console.log(res);
  169. this.loginForm.code = res.data.code;
  170. this.codeImage = res.data.pic;
  171. });
  172. },
  173. // 显示隐藏密码
  174. showPwd() {
  175. if (this.passwordType === "password") {
  176. this.passwordType = "";
  177. } else {
  178. this.passwordType = "password";
  179. }
  180. this.$nextTick(() => {
  181. this.$refs.password.focus();
  182. });
  183. },
  184. // 登录
  185. handleLogin() {
  186. console.log(this.loginForm);
  187. this.$refs.loginForm.validate((valid) => {
  188. if (valid) {
  189. this.loading = true;
  190. this.$store
  191. .dispatch("user/login", this.loginForm)
  192. .then(() => {
  193. console.log(this.redirect);
  194. this.$router.push({ path: this.redirect || "/" });
  195. this.$store.commit("user/showMessage", "yes");
  196. this.saveUnAndPw();
  197. this.loading = false;
  198. })
  199. .catch(() => {
  200. this.getCode();
  201. this.loginForm.codeValue = "";
  202. this.loading = false;
  203. });
  204. } else {
  205. console.log("error submit!!");
  206. return false;
  207. }
  208. });
  209. },
  210. // 处理账号密码的储存
  211. saveUnAndPw() {
  212. let storageData = {
  213. username: this.loginForm.username,
  214. isRemenberPw: this.isRemenberPw,
  215. };
  216. localStorage.setItem("supply_login", JSON.stringify(storageData));
  217. if (this.isRemenberPw) {
  218. this.setCookie(this.loginForm.username, this.loginForm.password, 7);
  219. }
  220. },
  221. //设置cookie
  222. setCookie(c_name, c_pwd, exdays) {
  223. var exdate = new Date(); //获取时间
  224. exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //保存的天数
  225. //字符串拼接cookie
  226. window.document.cookie =
  227. "supply_username" +
  228. "=" +
  229. c_name +
  230. ";path=/;expires=" +
  231. exdate.toGMTString();
  232. window.document.cookie =
  233. "supply_password" +
  234. "=" +
  235. c_pwd +
  236. ";path=/;expires=" +
  237. exdate.toGMTString();
  238. },
  239. //读取cookie
  240. getCookie: function () {
  241. if (document.cookie.length > 0) {
  242. var arr = document.cookie.split("; "); //这里显示的格式需要切割一下自己可输出看下
  243. for (var i = 0; i < arr.length; i++) {
  244. var arr2 = arr[i].split("="); //再次切割
  245. //判断查找相对应的值
  246. if (arr2[0] == "supply_username") {
  247. this.loginForm.username = arr2[1]; //保存到保存数据的地方
  248. } else if (arr2[0] == "supply_password") {
  249. this.loginForm.password = arr2[1];
  250. }
  251. }
  252. }
  253. },
  254. //清除cookie
  255. clearCookie: function () {
  256. this.setCookie("", "", -1); //修改2值都为空,天数为负1天就好了
  257. },
  258. },
  259. };
  260. </script>
  261. <style lang="scss">
  262. /* 修复input 背景不协调 和光标变色 */
  263. /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
  264. $bg: #283443;
  265. $light_gray: #fff;
  266. $cursor: #fff;
  267. $back: #333;
  268. @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  269. .login-container .el-input input {
  270. color: $cursor;
  271. }
  272. }
  273. /* reset element-ui css */
  274. .login-container {
  275. background: url("~@/assets/login/background.png") center center;
  276. background-size: cover;
  277. .el-input {
  278. display: inline-block;
  279. height: 47px;
  280. width: 85%;
  281. input {
  282. background: transparent;
  283. border: 0px;
  284. -webkit-appearance: none;
  285. border-radius: 0px;
  286. padding: 12px 5px 12px 15px;
  287. color: $back;
  288. height: 47px;
  289. caret-color: $back;
  290. border-bottom: 1px solid #eaeaea;
  291. &:-webkit-autofill {
  292. box-shadow: 0 0 0px 1000px $cursor inset !important;
  293. -webkit-text-fill-color: $back !important;
  294. }
  295. }
  296. }
  297. .el-form-item {
  298. border: 1px solid rgba(255, 255, 255, 0.1);
  299. // background: rgba(0, 0, 0, 0.1);
  300. border-radius: 5px;
  301. color: #454545;
  302. margin-bottom: 10px;
  303. }
  304. .el-checkbox {
  305. margin-top: 10px;
  306. margin-left: 6px;
  307. }
  308. .el-form-item__error {
  309. left: 30px;
  310. }
  311. }
  312. </style>
  313. <style lang="scss" scoped>
  314. $bg: #2d3a4b;
  315. $dark_gray: #889aa4;
  316. $light_gray: #eee;
  317. .login-container {
  318. min-height: 100%;
  319. width: 100%;
  320. background-color: $bg;
  321. overflow: hidden;
  322. .login-form {
  323. position: relative;
  324. width: 1070px;
  325. max-width: 100%;
  326. padding: 0 35px 0;
  327. padding-top: calc(50vh - 230px);
  328. margin: 0 auto;
  329. overflow: hidden;
  330. display: flex;
  331. }
  332. .image-container {
  333. img {
  334. width: 560px;
  335. height: 460px;
  336. }
  337. }
  338. .right-container {
  339. width: 440px;
  340. height: 460px;
  341. .empty-height {
  342. height: 77px;
  343. }
  344. .form-container {
  345. padding: 20px 30px 27px 40px;
  346. background: #fff;
  347. border-radius: 0 15px 15px 0;
  348. .title {
  349. font-size: 24px;
  350. font-weight: bold;
  351. letter-spacing: 4px;
  352. text-align: center;
  353. padding: 10px 0 13px;
  354. color: #3a8ee6;
  355. }
  356. }
  357. }
  358. .tips {
  359. font-size: 14px;
  360. color: #fff;
  361. margin-bottom: 10px;
  362. span {
  363. &:first-of-type {
  364. margin-right: 16px;
  365. }
  366. }
  367. }
  368. .svg-container {
  369. padding: 6px 5px 6px 5px;
  370. color: #33aef7;
  371. vertical-align: middle;
  372. width: 30px;
  373. display: inline-block;
  374. }
  375. .title-container {
  376. border-radius: 15px 15px 0 0;
  377. overflow: hidden;
  378. img {
  379. width: 100%;
  380. display: block;
  381. }
  382. }
  383. .show-pwd {
  384. position: absolute;
  385. right: 30px;
  386. top: 7px;
  387. font-size: 16px;
  388. color: $dark_gray;
  389. cursor: pointer;
  390. user-select: none;
  391. }
  392. .code {
  393. position: absolute;
  394. right: 30px;
  395. top: 7px;
  396. cursor: pointer;
  397. img {
  398. height: 30px;
  399. }
  400. }
  401. .button-container {
  402. text-align: center;
  403. margin-top: 20px;
  404. button {
  405. font-size: 16px;
  406. width: 100%;
  407. height: 45px;
  408. border-radius: 45px;
  409. background: #33aef7;
  410. box-shadow: 2px 3px 8px 0px rgba(5, 155, 245, 0.75);
  411. }
  412. }
  413. }
  414. @media only screen and (max-width: 600px) {
  415. .image-container {
  416. display: none;
  417. }
  418. .form-container {
  419. border-radius: 15px !important;
  420. }
  421. }
  422. </style>