login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <template>
  2. <div class="login">
  3. <div class="login-bg" data-title="登录"></div>
  4. <img src="../../static/image/login-logo.png" class="login-logo" />
  5. <div class="login-box" v-loading="isLoading">
  6. <div class="login-content">
  7. <div class="top-icon">
  8. <img src="../../static/image/login-icon.png" />
  9. </div>
  10. <div class="pass-code">
  11. <div class="item" :class="activeIndex == 1 ? 'active' : ''" @click.stop=" activeIndex = 1 ">密码登录</div>
  12. <div class="item" :class="activeIndex == 2 ? 'active' : ''" @click.stop=" activeIndex = 2 ">验证码登录</div>
  13. </div>
  14. <div>
  15. <!-- 密码登录 -->
  16. <div class="login-password" v-if="activeIndex == 1">
  17. <div class="pass-item">
  18. <img src="../../static/image/phone-icon.png" class="phone-icon" />
  19. <el-input @keydown.enter.native="seachEnterFun" placeholder="请输入手机号" v-model="mobile" class="input-phone" :maxlength="11" />
  20. </div>
  21. <div class="pass-item">
  22. <img src="../../static/image/lock-icon.png" class="lock-icon" />
  23. <el-input @keydown.enter.native="seachEnterFun" name='1' placeholder="用户密码" v-model="password" class="input-phone" type="password" />
  24. </div>
  25. </div>
  26. <!-- 验证码登录 -->
  27. <div class="login-password" v-else>
  28. <div class="pass-item">
  29. <img src="../../static/image/phone-icon.png" class="phone-icon" />
  30. <el-input @keydown.enter.native="seachEnterFun" placeholder="请输入手机号" v-model="mobile" class="input-phone" :maxlength="11" />
  31. </div>
  32. <div class="pass-item">
  33. <img src="../../static/image/code-icon.png" class="phone-icon" />
  34. <el-input name="1" style="display:none;" />
  35. <el-input @keydown.enter.native="seachEnterFun" name="2" placeholder="验证码" v-model="code" class="input-phone" :maxlength="4" />
  36. <div class="get-code" v-if="!showCode" @click.stop="getCode">获取短信验证码</div>
  37. <div class="get-code" v-else>{{ count }}秒</div>
  38. </div>
  39. </div>
  40. </div>
  41. <div class="forgot-pass">
  42. <span @click.stop="$router.push('/resetPassword')">忘记密码?</span>
  43. </div>
  44. <div class="login-btn" @click.stop="toLogin">登录</div>
  45. <div class="xie-yi">
  46. <el-checkbox v-model="checked">我已阅读并同意 <a target="_blank" href="http://h5.liebaoai.cn/service.html">《用户协议》</a>与<a target="_blank" href="http://h5.liebaoai.cn/privacy.html">《隐私政策》</a>
  47. </el-checkbox>
  48. </div>
  49. </div>
  50. </div>
  51. </div>
  52. </template>
  53. <script>
  54. import { mapState, mapMutations, mapGetters, mapActions } from "vuex";
  55. import { getPhoneCode, loginPassword, loginVerification } from "../api/user.js";
  56. export default {
  57. data() {
  58. return {
  59. activeIndex: 1,
  60. isLoading: false, // 请求解决展示loading
  61. mobile: "",
  62. password: "",
  63. code: "",
  64. count: 60,
  65. showCode: false,
  66. timer: null, // 计时器
  67. errCount: 0,
  68. checked: false,
  69. };
  70. },
  71. destroyed() {
  72. clearInterval(this.timer);
  73. this.timer = null;
  74. },
  75. computed: {
  76. // ...mapState(["userInfo"]),
  77. // ...mapGetters(["userInfo"]),
  78. },
  79. created() {
  80. console.log("vuex", this.userInfo);
  81. },
  82. methods: {
  83. ...mapMutations(["SET_USERINFO"]),
  84. ...mapActions(["getUserInfo"]),
  85. seachEnterFun(e) {
  86. var keyCode = window.event ? e.keyCode : e.which;
  87. if (keyCode == 13) {
  88. this.toLogin();
  89. }
  90. },
  91. // 短信验证码登录
  92. loginOfCode() {
  93. let params = {
  94. mobile: this.mobile,
  95. code: this.code,
  96. platform: 4,
  97. };
  98. loginVerification(params).then((res) => {
  99. if (res.code == 200) {
  100. localStorage.setItem("token", res.data.token);
  101. this.SET_USERINFO(res.data.member);
  102. this.getUserInfo();
  103. this.$router.push("/");
  104. } else {
  105. return this.$message({
  106. message: res.msg,
  107. type: "error",
  108. });
  109. }
  110. });
  111. },
  112. // 账号密码登录
  113. loginOfPassword() {
  114. let params = {
  115. mobile: this.mobile,
  116. password: this.password,
  117. platform: 4,
  118. };
  119. loginPassword(params).then((res) => {
  120. if (res.code == 200) {
  121. this.errCount = 0;
  122. localStorage.setItem("token", res.data.token);
  123. this.SET_USERINFO(res.data.member);
  124. this.getUserInfo();
  125. this.$store.dispatch("getUsbPrint");
  126. this.$router.push("/");
  127. } else {
  128. this.$message({
  129. message: res.msg,
  130. type: "error",
  131. });
  132. this.errCount = this.errCount + 1;
  133. if (this.errCount === 3) {
  134. this.$confirm(
  135. "您已连续3次输入密码错误,可以尝试点击忘记密码,重置密码!",
  136. "提示",
  137. {
  138. confirmButtonText: "我知道了",
  139. cancelButtonText: "取消",
  140. showCancelButton: false,
  141. type: "warning",
  142. }
  143. ).then(() => {
  144. this.errCount = 0;
  145. });
  146. }
  147. }
  148. });
  149. },
  150. // 登录
  151. toLogin() {
  152. if (!this.mobile.trim()) {
  153. return this.$message({
  154. message: "手机号码不能为空",
  155. type: "error",
  156. });
  157. }
  158. if (!this.checkPhone(this.mobile)) {
  159. return this.$message({
  160. message: "请输入正确的手机号",
  161. type: "error",
  162. });
  163. }
  164. if (!this.checked) {
  165. return this.$message({
  166. message: "请先勾选同意后再登录!",
  167. type: "error",
  168. });
  169. }
  170. if (this.activeIndex == 1) {
  171. // 账号密码登录
  172. if (!this.password.trim()) {
  173. return this.$message({
  174. message: "密码不能为空",
  175. type: "error",
  176. });
  177. }
  178. this.loginOfPassword();
  179. } else {
  180. // 短信验证码登录
  181. if (!this.code.trim()) {
  182. return this.$message({
  183. message: "验证码不能为空",
  184. type: "error",
  185. });
  186. }
  187. this.loginOfCode();
  188. }
  189. },
  190. checkPhone(phone) {
  191. if (!/^1\d{10}$/.test(phone)) {
  192. return false;
  193. } else {
  194. return true;
  195. }
  196. },
  197. // 获取验证码
  198. getCode() {
  199. if (!this.mobile.trim()) {
  200. return this.$message({
  201. message: "手机号码不能为空",
  202. type: "error",
  203. });
  204. }
  205. if (!this.checkPhone(this.mobile)) {
  206. return this.$message({
  207. message: "请输入正确的手机号",
  208. type: "error",
  209. });
  210. }
  211. let params = {
  212. mobile: this.mobile,
  213. type: 1,
  214. };
  215. // this.isLoading = true;
  216. getPhoneCode(params).then((res) => {
  217. if (res.code == 200) {
  218. // this.isLoading = false;
  219. this.showCode = true;
  220. this.$message({
  221. message: "验证码发送成功",
  222. type: "success",
  223. });
  224. this.timer = setInterval(() => {
  225. this.count--;
  226. if (this.count <= 1) {
  227. this.showCode = false;
  228. clearInterval(this.timer);
  229. this.count = 60;
  230. }
  231. }, 1000);
  232. } else {
  233. // this.isLoading = false;
  234. return this.$message({
  235. message: res.msg,
  236. type: "error",
  237. });
  238. }
  239. });
  240. },
  241. },
  242. };
  243. </script>
  244. <style lang="scss" scoped>
  245. /deep/ .el-input__inner {
  246. border: none !important;
  247. }
  248. .login {
  249. position: relative;
  250. width: 100%;
  251. height: 100%;
  252. .login-bg {
  253. position: fixed;
  254. height: 100%;
  255. width: 100%;
  256. background-size: cover;
  257. background-attachment: fixed;
  258. background: url("../../static/image/login-bg.png") no-repeat #010836;
  259. }
  260. .login-logo {
  261. width: 158px;
  262. height: 24px;
  263. position: absolute;
  264. top: 32px;
  265. left: 32px;
  266. }
  267. .login-box {
  268. position: relative;
  269. z-index: 200;
  270. height: 100vh;
  271. display: flex;
  272. display: -webkit-flex;
  273. align-items: center;
  274. justify-content: center;
  275. .login-content {
  276. background-color: #fff;
  277. border-radius: 14px;
  278. width: 360px;
  279. box-sizing: border-box;
  280. align-self: center;
  281. padding: 30px;
  282. .top-icon {
  283. width: 100%;
  284. text-align: center;
  285. font-size: 0;
  286. padding-top: 14px;
  287. img {
  288. width: 194px;
  289. height: 52px;
  290. }
  291. }
  292. .pass-code {
  293. display: flex;
  294. padding-top: 44px;
  295. .item {
  296. font-size: 16px;
  297. font-family: PingFang SC;
  298. font-weight: 400;
  299. color: #565656;
  300. cursor: pointer;
  301. &:nth-child(1) {
  302. margin-right: 30px;
  303. }
  304. &.active {
  305. font-weight: bold;
  306. color: #111111;
  307. padding-bottom: 10px;
  308. border-bottom: 3px solid #fc7200;
  309. }
  310. }
  311. }
  312. .login-password {
  313. padding-top: 30px;
  314. .pass-item {
  315. display: flex;
  316. align-items: center;
  317. padding-bottom: 2px;
  318. border-bottom: 1px solid #eee;
  319. .phone-icon {
  320. width: 14px;
  321. height: 17px;
  322. margin-right: 16px;
  323. }
  324. .lock-icon {
  325. width: 16px;
  326. height: 18px;
  327. margin-right: 14px;
  328. }
  329. .input-phone {
  330. flex: 1;
  331. }
  332. .get-code {
  333. min-width: 120px;
  334. box-sizing: border-box;
  335. text-align: center;
  336. padding: 0 6px;
  337. font-size: 14px;
  338. font-family: PingFang SC;
  339. font-weight: 400;
  340. color: #175199;
  341. cursor: pointer;
  342. }
  343. }
  344. }
  345. .forgot-pass {
  346. text-align: right;
  347. font-size: 14px;
  348. font-family: PingFang SC;
  349. font-weight: 400;
  350. color: #8590a6;
  351. padding-top: 10px;
  352. span {
  353. cursor: pointer;
  354. }
  355. }
  356. .login-btn {
  357. height: 36px;
  358. line-height: 36px;
  359. text-align: center;
  360. background: #fc7200;
  361. border-radius: 3px;
  362. font-size: 14px;
  363. font-family: PingFang SC;
  364. font-weight: 500;
  365. color: #ffffff;
  366. margin-top: 20px;
  367. cursor: pointer;
  368. }
  369. .xie-yi {
  370. font-size: 8px;
  371. font-family: PingFang SC;
  372. font-weight: 400;
  373. color: #808080;
  374. text-align: center;
  375. margin-top: 20px;
  376. a {
  377. text-decoration: none;
  378. color: #175199;
  379. }
  380. }
  381. }
  382. }
  383. }
  384. </style>