login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <!--
  2. * @Descripttion: 登录
  3. * @version:
  4. * @Author: qianlishi
  5. * @Date: 2021-12-11 14:48:27
  6. * @LastEditors: qianlishi
  7. * @LastEditTime: 2021-12-13 09:45:44
  8. -->
  9. <template>
  10. <div class="login_container">
  11. <!-- 顶部logo -->
  12. <div class="login_title">
  13. <img src="@/assets/images/home-logo.png" alt="logo" />
  14. </div>
  15. <div class="login_contant">
  16. <img src="@/assets/images/login.png" alt="image" class="login_img" />
  17. <el-form
  18. ref="loginForm"
  19. :model="loginForm"
  20. :rules="loginRules"
  21. class="login_form"
  22. autocomplete="on"
  23. label-position="left"
  24. @keyup.enter.native="handleLogin"
  25. >
  26. <div class="title_container">
  27. <h3 class="title">
  28. HELLO,
  29. <br />
  30. <p class="title_name">在线大屏</p>
  31. </h3>
  32. </div>
  33. <div class="form_fields">
  34. <!-- 黄色条条 -->
  35. <i :style="{ top: activeTop + '%' }" />
  36. <!-- 中间条条 -->
  37. <b />
  38. <div>
  39. <p>用户名</p>
  40. <el-form-item prop="loginName">
  41. <el-input
  42. ref="loginName"
  43. v-model="loginForm.loginName"
  44. placeholder="用户名"
  45. name="loginName"
  46. type="text"
  47. tabindex="1"
  48. autocomplete="on"
  49. @focus="setTop('0')"
  50. @change="getPsw"
  51. />
  52. </el-form-item>
  53. </div>
  54. <div>
  55. <p>密码</p>
  56. <input
  57. name="password"
  58. type="password"
  59. autocomplete="off"
  60. class="take"
  61. />
  62. <el-tooltip
  63. v-model="capsTooltip"
  64. content="Caps lock is On"
  65. placement="right"
  66. manual
  67. >
  68. <el-form-item prop="password">
  69. <el-input
  70. :key="passwordType"
  71. ref="password"
  72. v-model="loginForm.password"
  73. :type="passwordType"
  74. placeholder="用户密码"
  75. name="password"
  76. tabindex="2"
  77. autocomplete="on"
  78. @blur="capsTooltip = false"
  79. @focus="setTop('50')"
  80. @keyup.native="checkCapslock"
  81. />
  82. <span class="show_pwd" @click="showPwd">
  83. <i class="el-icon-view" />
  84. </span>
  85. </el-form-item>
  86. </el-tooltip>
  87. </div>
  88. </div>
  89. <div class="control">
  90. <div class="remember">
  91. <input v-model="rememberPsw" type="checkbox" />
  92. <p>记住密码</p>
  93. </div>
  94. </div>
  95. <el-button
  96. :loading="loading"
  97. type="primary"
  98. class="login_btn"
  99. @click.native.prevent="handleLogin"
  100. >登录</el-button
  101. >
  102. </el-form>
  103. </div>
  104. <!-- 验证码 -->
  105. <Verify
  106. v-if="needCaptcha"
  107. ref="verify"
  108. :captcha-type="'blockPuzzle'"
  109. :img-size="{ width: '400px', height: '200px' }"
  110. @success="verifylogin"
  111. />
  112. </div>
  113. </template>
  114. <script>
  115. import Verify from "@/components/verifition/Verify";
  116. import cookies from "js-cookie";
  117. import { Decrypt, Encrypt } from "@/utils/index";
  118. import { login } from "@/api/login";
  119. import { transPsw } from "@/utils/encrypted";
  120. import { setToken, setAccessUser } from "@/utils/auth";
  121. export default {
  122. name: "Login",
  123. components: {
  124. Verify
  125. },
  126. data() {
  127. return {
  128. activeTop: "-50%",
  129. rememberPsw: false,
  130. loginForm: {
  131. loginName: "",
  132. password: "",
  133. verifyCode: ""
  134. },
  135. loginRules: {
  136. loginName: [{ required: true, message: "用户名必填", trigger: "blur" }],
  137. password: [{ required: true, message: "用户密码必填", trigger: "blur" }]
  138. },
  139. passwordType: "password",
  140. capsTooltip: false,
  141. loading: false,
  142. redirect: undefined,
  143. otherQuery: {},
  144. needCaptcha: false
  145. };
  146. },
  147. watch: {
  148. $route: {
  149. // 监听路由获取上个路由(from)的地址和参数
  150. handler: function(route) {
  151. const query = route.query;
  152. if (query) {
  153. this.redirect = query.redirect;
  154. this.otherQuery = this.getOtherQuery(query);
  155. }
  156. },
  157. immediate: true
  158. }
  159. },
  160. mounted() {
  161. this.handleLoginFocus();
  162. },
  163. methods: {
  164. handleLoginFocus() {
  165. if (this.loginForm.loginName === "") {
  166. this.$refs.loginName.focus();
  167. } else if (this.loginForm.password === "") {
  168. this.$refs.password.focus();
  169. }
  170. },
  171. // 获取存储的密码并解密
  172. getPsw() {
  173. const cookVal = cookies.get(`u_${this.loginForm.loginName}`);
  174. this.loginForm.password = cookVal && Decrypt(cookVal);
  175. },
  176. // 滑动条块的top控制
  177. setTop(val) {
  178. this.activeTop = val;
  179. },
  180. // 检测大写锁定键是否开启
  181. checkCapslock(e) {
  182. const { key } = e;
  183. this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
  184. },
  185. // 查看密码
  186. showPwd() {
  187. if (this.passwordType === "password") {
  188. this.passwordType = "";
  189. } else {
  190. this.passwordType = "password";
  191. }
  192. this.$nextTick(() => {
  193. this.$refs.password.focus();
  194. });
  195. },
  196. // 滑动验证码
  197. useVerify() {
  198. this.$refs.loginForm.validate(valid => {
  199. if (valid) {
  200. this.$refs.verify.show();
  201. } else {
  202. return false;
  203. }
  204. });
  205. },
  206. // 验证成功的回调
  207. verifylogin(params) {
  208. this.loginForm.verifyCode = params.captchaVerification;
  209. if (this.loginForm.verifyCode) {
  210. this.loginApi();
  211. }
  212. },
  213. // 登录操作
  214. handleLogin() {
  215. this.$refs.loginForm.validate(valid => {
  216. if (valid) {
  217. this.loading = true;
  218. // 登录失败次数过多需要展示滑动验证码
  219. if (this.needCaptcha) {
  220. this.useVerify();
  221. return;
  222. }
  223. this.loginApi();
  224. } else {
  225. return false;
  226. }
  227. });
  228. },
  229. async loginApi() {
  230. const obj = {
  231. loginName: this.loginForm.loginName,
  232. password: transPsw(this.loginForm.password),
  233. verifyCode: ""
  234. };
  235. const { code, data } = await login(obj);
  236. this.loading = false;
  237. if (code != "200") return;
  238. setToken(data.token);
  239. setAccessUser(data);
  240. // 选中记住密码时 把密码存到cookie里,时效15天
  241. this.rememberPsw &&
  242. cookies.set(
  243. `u_${this.loginForm.loginName}`,
  244. Encrypt(this.loginForm.password),
  245. { expires: 15 }
  246. );
  247. if (data && data.captcha) {
  248. this.needCaptcha = true;
  249. } else {
  250. this.needCaptcha = false;
  251. this.$router.push({
  252. path: this.redirect || "/index",
  253. query: this.otherQuery
  254. });
  255. }
  256. },
  257. getOtherQuery(query) {
  258. return Object.keys(query).reduce((acc, cur) => {
  259. if (cur !== "redirect") {
  260. acc[cur] = query[cur];
  261. }
  262. return acc;
  263. }, {});
  264. }
  265. }
  266. };
  267. </script>
  268. <style lang="scss">
  269. .login_container .el-input input {
  270. color: #000;
  271. background: #fff;
  272. }
  273. .login_container {
  274. .el-input {
  275. display: inline-block;
  276. width: 100%;
  277. input {
  278. -webkit-appearance: none;
  279. caret-color: rgba($color: #000000, $alpha: 0.3);
  280. border: 1px solid #fff;
  281. &:-webkit-autofill {
  282. box-shadow: 0 0 0px 1000px #eee inset !important;
  283. -webkit-text-fill-color: #666 !important;
  284. }
  285. }
  286. }
  287. .el-form-item {
  288. border-radius: 5px;
  289. color: #454545;
  290. }
  291. }
  292. .verifybox {
  293. position: absolute;
  294. left: auto;
  295. right: 30%;
  296. transform: translate(50%, -50%);
  297. }
  298. .take {
  299. position: absolute;
  300. top: 0;
  301. left: 0;
  302. z-index: -1;
  303. }
  304. .login_container {
  305. height: 100%;
  306. width: 100%;
  307. overflow: hidden;
  308. .login_title {
  309. width: 100%;
  310. height: 60px;
  311. padding: 10px 60px;
  312. display: flex;
  313. align-items: center;
  314. img {
  315. width: 10%;
  316. display: block;
  317. }
  318. }
  319. .login_contant {
  320. position: relative;
  321. width: 100%;
  322. height: calc(100% - 60px);
  323. .login_img {
  324. display: block;
  325. width: 100%;
  326. height: 100%;
  327. }
  328. .login_form {
  329. position: absolute;
  330. top: 50%;
  331. right: 30%;
  332. transform: translate(50%, -50%);
  333. min-width: 400px;
  334. width: 22%;
  335. height: 460px;
  336. background-color: #ffffff;
  337. border-radius: 11px;
  338. padding: 30px;
  339. overflow: hidden;
  340. .title_container {
  341. position: relative;
  342. .title {
  343. font-size: 24px;
  344. color: #1a1a1a;
  345. .title_name {
  346. margin: 0;
  347. font-size: 18px;
  348. }
  349. }
  350. .set_language {
  351. color: #fff;
  352. position: absolute;
  353. top: 3px;
  354. font-size: 18px;
  355. right: 0px;
  356. cursor: pointer;
  357. }
  358. }
  359. .form_fields {
  360. position: relative;
  361. width: 100%;
  362. overflow: hidden;
  363. padding: 5px 16px;
  364. background: #ffffff;
  365. border: 1px solid #e0e0e0;
  366. box-shadow: 0 0 14px 4px rgba(230, 229, 229, 0.5);
  367. border-radius: 4px 10px 10px 4px;
  368. i {
  369. position: absolute;
  370. top: -50%;
  371. left: 0;
  372. width: 4px;
  373. height: 50%;
  374. transition: top 0.2s;
  375. background: #f5ab1b;
  376. border-radius: 14px;
  377. }
  378. b {
  379. position: absolute;
  380. top: 50%;
  381. left: 0;
  382. width: 100%;
  383. height: 1px;
  384. background: #e0e0e0;
  385. border-radius: 2px;
  386. margin-top: -0.5px;
  387. }
  388. p {
  389. margin: 0;
  390. padding: 0;
  391. line-height: 32px;
  392. height: 32px;
  393. font-size: 12px;
  394. color: #666;
  395. }
  396. .show_pwd {
  397. position: absolute;
  398. right: 10px;
  399. top: 0;
  400. font-size: 16px;
  401. color: #889aa4;
  402. cursor: pointer;
  403. user-select: none;
  404. }
  405. }
  406. .control {
  407. width: 100%;
  408. height: 70px;
  409. display: flex;
  410. align-items: center;
  411. justify-content: space-between;
  412. font-size: 14px;
  413. color: #919191;
  414. .remember {
  415. width: 36%;
  416. display: flex;
  417. align-items: center;
  418. p {
  419. padding-left: 8px;
  420. }
  421. & > input {
  422. position: relative;
  423. width: 14px;
  424. height: 14px;
  425. }
  426. & > input:checked::before {
  427. content: "\2713";
  428. background-color: #f5ab1b;
  429. position: absolute;
  430. top: 0;
  431. left: 0px;
  432. padding-left: 1.5px;
  433. width: 100%;
  434. height: 100%;
  435. border: 1px solid #f5ab1b;
  436. border-radius: 2px;
  437. font-size: 12px;
  438. color: white;
  439. font-weight: bold;
  440. }
  441. }
  442. }
  443. .login_btn {
  444. width: 130px;
  445. height: 40px;
  446. background: #f5ab1b;
  447. border: none;
  448. border-radius: 10px;
  449. font-size: 16px;
  450. color: #ffffff;
  451. text-align: center;
  452. }
  453. }
  454. }
  455. }
  456. </style>