login.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <template>
  2. <div class="loginBox">
  3. <div class="loginInnerBox">
  4. <h2 class="bigTit">智慧用电监控平台</h2>
  5. <p class="loginTitle">用户登录</p>
  6. <el-form
  7. :label-position="labelPosition"
  8. :model="loginForm"
  9. :rules="rules"
  10. ref="loginForm"
  11. class="demo-loginForm"
  12. >
  13. <el-form-item prop="username" class="oneSec">
  14. <img class="icon" src="../assets/images/userIcon.png" alt="" />
  15. <el-input
  16. v-model="loginForm.username"
  17. placeholder="请输入登录ID"
  18. ></el-input>
  19. </el-form-item>
  20. <el-form-item prop="password" class="oneSec">
  21. <img class="icon" src="../assets/images/passwordIcon.png" alt="" />
  22. <el-input
  23. v-model="loginForm.password"
  24. type="password"
  25. placeholder="请输入密码"
  26. auto-complete="new-password"
  27. clearable
  28. autocomplete="off"
  29. ></el-input>
  30. </el-form-item>
  31. <div class="remember">
  32. <el-checkbox label="记住密码" v-model="isChecked"></el-checkbox>
  33. </div>
  34. <el-form-item>
  35. <el-button @click="submitForm" class="submitBox">登录</el-button>
  36. <!-- <el-button @click="resetForm">重置</el-button> -->
  37. </el-form-item>
  38. </el-form>
  39. </div>
  40. </div>
  41. </template>
  42. <script>
  43. import api from "../api/login/user.js";
  44. export default {
  45. data() {
  46. return {
  47. loginData: "",
  48. labelPosition: "right",
  49. loginForm: {
  50. username: "",
  51. password: "",
  52. },
  53. rules: {
  54. username: [
  55. // required 是否为必填项, trigger:表单验证的触发时机,失去焦点进行验证
  56. { required: true, message: "请输入用户名", trigger: "blur" },
  57. {
  58. min: 3,
  59. max: 6,
  60. message: "用户名长度在 3 到 6 个字符",
  61. trigger: "blur",
  62. },
  63. ],
  64. password: [
  65. { required: true, message: "请输密码", trigger: "blur" },
  66. {
  67. min: 3,
  68. max: 6,
  69. message: "密码长度在 3 到 6 个字符",
  70. trigger: "blur",
  71. },
  72. ],
  73. },
  74. isChecked: true, // 记住密码
  75. };
  76. },
  77. mounted() {
  78. this.getCookie();
  79. },
  80. methods: {
  81. //登录数据对接
  82. login_api(query = {}) {
  83. api.login_api(query).then((requset) => {
  84. this.loginData = requset.data;
  85. console.log(this.loginData);
  86. });
  87. },
  88. submitForm() {
  89. // ref 用在组件中,就表示当前组件 通过 $refs.xxx可以拿到所有ref的集合
  90. // this.$refs.loginForm //当前的表单对象
  91. this.$refs.loginForm.validate((valid) => {
  92. if (valid) {
  93. //valid成功为ture,失败为false
  94. //发送请求 ,调用登录接口
  95. console.log(this.loginForm);
  96. this.login_api({
  97. name: this.loginForm.username,
  98. password: this.loginForm.password,
  99. });
  100. this.$router.push({ path: "/" });
  101. // alert('submit!');
  102. } else {
  103. console.log("校验失败");
  104. return false;
  105. }
  106. });
  107. if (this.isChecked) {
  108. // 记住密码
  109. this.setCookie(this.loginForm.username, this.loginForm.password, 30); // 保存期限为30天
  110. } else {
  111. this.clearCookie(); // 清空 Cookie
  112. }
  113. },
  114. // 设置Cookie
  115. setCookie(username, password, exdays) {
  116. // 用户名, 密码, 保存天数
  117. let exdate = new Date(); // 获取时间
  118. exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays);
  119. // 字符串拼接cookie
  120. window.document.cookie =
  121. "userName=" + username + ";path=/;expires=" + exdate.toGMTString();
  122. window.document.cookie =
  123. "userPwd=" + password + ";path=/;expires=" + exdate.toGMTString();
  124. },
  125. // 读取Cookie
  126. getCookie() {
  127. // console.log(document.cookie);
  128. if (document.cookie.length > 0) {
  129. let arr = document.cookie.split("; "); // 这里显示的格式需要切割一下自己可输出看下
  130. for (let i = 0; i < arr.length; i++) {
  131. let arr2 = arr[i].split("="); // 再次切割
  132. // 判断查找相对应的值
  133. if (arr2[0] == "userName") {
  134. this.loginForm.usernameText = arr2[1]; // 保存到保存数据的地方
  135. } else if (arr2[0] == "userPwd") {
  136. this.loginForm.passwordText = arr2[1];
  137. }
  138. }
  139. }
  140. },
  141. // 清除Cookie
  142. clearCookie() {
  143. this.setCookie("", "", -1); // 修改2值都为空,天数为负1天就好了
  144. },
  145. // resetForm() {
  146. // //表单重置
  147. // this.$refs.loginForm.resetFields();
  148. // },
  149. },
  150. };
  151. </script>
  152. <style lang="scss">
  153. #header {
  154. display: none;
  155. }
  156. .bigTit {
  157. position: absolute;
  158. top: -90px;
  159. left: 7px;
  160. text-align: center;
  161. width: 100%;
  162. color: #00e1eb;
  163. font-size: 0.625rem;
  164. // margin-left:-0.775rem;
  165. font-family: PangMenZhengDao Regular, PangMenZhengDao Regular-Regular;
  166. font-weight: bold;
  167. letter-spacing: 17px;
  168. }
  169. .loginBox {
  170. background-image: url(../assets/images/login-bg.png) !important;
  171. background-repeat: no-repeat;
  172. background-size: cover;
  173. width: 100%;
  174. height: 100vh;
  175. min-width: 1024px;
  176. min-height: 600px;
  177. background-position: center center;
  178. display: block;
  179. display: flex;
  180. align-items: center;
  181. justify-content: center;
  182. color: red;
  183. .loginInnerBox {
  184. position: relative;
  185. width: 8.3875rem;
  186. height: 5.8rem;
  187. background-image: url(../assets/images/loginIn-bg.png) !important;
  188. background-size: cover;
  189. padding: 0.45rem 0.775rem;
  190. // text-align: center;
  191. .loginTitle {
  192. font-size: 0.45rem;
  193. color: #00e1eb;
  194. text-align: center;
  195. letter-spacing: 12px;
  196. }
  197. .oneSec {
  198. text-align: left;
  199. width: 6.85rem;
  200. height: 0.7375rem;
  201. line-height: 0.7375rem;
  202. background: rgba(11, 161, 248, 0.4);
  203. border: 1px solid #0ba1f8;
  204. margin-top: 0.4875rem;
  205. position: relative;
  206. .icon {
  207. position: absolute;
  208. top: 0.2rem;
  209. left: 0.2rem;
  210. width: 0.3rem;
  211. height: 0.3rem;
  212. }
  213. input {
  214. background: transparent;
  215. border: 0;
  216. -webkit-appearance: none;
  217. border-radius: 0;
  218. // padding: 12px 5px 12px 0;
  219. color: #fff;
  220. height: 47px;
  221. caret-color: #fff;
  222. outline: none;
  223. font-size: 0.325rem;
  224. padding-left: 0.85rem;
  225. line-height: 0.7375rem;
  226. height: 0.7375rem;
  227. }
  228. }
  229. .remember {
  230. margin: 0.275rem 0;
  231. .el-checkbox__inner {
  232. width: 0.3rem;
  233. height: 0.3rem;
  234. }
  235. .el-checkbox__label {
  236. font-size: 0.325rem;
  237. font-weight: 300;
  238. }
  239. .el-checkbox__inner::after {
  240. top: 0.07rem;
  241. width: 0.0625rem;
  242. left: 0.1125rem;
  243. }
  244. .el-checkbox {
  245. color: #fff;
  246. }
  247. }
  248. .submitBox {
  249. width: 6.85rem;
  250. // height: 0.7375rem;
  251. opacity: 1;
  252. background: #00e1eb;
  253. color: #001a2b;
  254. font-size: 0.45rem;
  255. text-align: center;
  256. // line-height: 0.7375rem;
  257. cursor: pointer;
  258. }
  259. }
  260. }
  261. </style>