login.vue 11 KB

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