login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 } 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. seachEnterFun(e) {
  85. var keyCode = window.event ? e.keyCode : e.which;
  86. if (keyCode == 13) {
  87. this.toLogin();
  88. }
  89. },
  90. // 短信验证码登录
  91. loginOfCode() {
  92. let params = {
  93. mobile: this.mobile,
  94. code: this.code,
  95. platform: 4,
  96. };
  97. loginVerification(params).then((res) => {
  98. if (res.code == 200) {
  99. localStorage.setItem("token", res.data.token);
  100. this.SET_USERINFO(res.data.member);
  101. this.$router.push("/");
  102. } else {
  103. return this.$message({
  104. message: res.msg,
  105. type: "error",
  106. });
  107. }
  108. });
  109. },
  110. // 账号密码登录
  111. loginOfPassword() {
  112. let params = {
  113. mobile: this.mobile,
  114. password: this.password,
  115. platform: 4,
  116. };
  117. loginPassword(params).then((res) => {
  118. if (res.code == 200) {
  119. this.errCount = 0;
  120. localStorage.setItem("token", res.data.token);
  121. this.SET_USERINFO(res.data.member);
  122. this.$router.push("/");
  123. } else {
  124. this.$message({
  125. message: res.msg,
  126. type: "error",
  127. });
  128. this.errCount = this.errCount + 1;
  129. if (this.errCount === 3) {
  130. this.$confirm(
  131. "您已连续3次输入密码错误,可以尝试点击忘记密码,重置密码!",
  132. "提示",
  133. {
  134. confirmButtonText: "我知道了",
  135. cancelButtonText: "取消",
  136. showCancelButton: false,
  137. type: "warning",
  138. }
  139. ).then(() => {
  140. this.errCount = 0;
  141. });
  142. }
  143. }
  144. });
  145. },
  146. // 登录
  147. toLogin() {
  148. if (!this.mobile.trim()) {
  149. return this.$message({
  150. message: "手机号码不能为空",
  151. type: "error",
  152. });
  153. }
  154. if (!this.checkPhone(this.mobile)) {
  155. return this.$message({
  156. message: "请输入正确的手机号",
  157. type: "error",
  158. });
  159. }
  160. if (!this.checked) {
  161. return this.$message({
  162. message: "请先勾选同意后再充值!",
  163. type: "info",
  164. });
  165. }
  166. if (this.activeIndex == 1) {
  167. // 账号密码登录
  168. if (!this.password.trim()) {
  169. return this.$message({
  170. message: "密码不能为空",
  171. type: "error",
  172. });
  173. }
  174. this.loginOfPassword();
  175. } else {
  176. // 短信验证码登录
  177. if (!this.code.trim()) {
  178. return this.$message({
  179. message: "验证码不能为空",
  180. type: "error",
  181. });
  182. }
  183. this.loginOfCode();
  184. }
  185. },
  186. checkPhone(phone) {
  187. if (!/^1\d{10}$/.test(phone)) {
  188. return false;
  189. } else {
  190. return true;
  191. }
  192. },
  193. // 获取验证码
  194. getCode() {
  195. if (!this.mobile.trim()) {
  196. return this.$message({
  197. message: "手机号码不能为空",
  198. type: "error",
  199. });
  200. }
  201. if (!this.checkPhone(this.mobile)) {
  202. return this.$message({
  203. message: "请输入正确的手机号",
  204. type: "error",
  205. });
  206. }
  207. let params = {
  208. mobile: this.mobile,
  209. type: 1,
  210. };
  211. // this.isLoading = true;
  212. getPhoneCode(params).then((res) => {
  213. if (res.code == 200) {
  214. // this.isLoading = false;
  215. this.showCode = true;
  216. this.$message({
  217. message: "验证码发送成功",
  218. type: "success",
  219. });
  220. this.timer = setInterval(() => {
  221. this.count--;
  222. if (this.count <= 1) {
  223. this.showCode = false;
  224. clearInterval(this.timer);
  225. this.count = 60;
  226. }
  227. }, 1000);
  228. } else {
  229. // this.isLoading = false;
  230. return this.$message({
  231. message: res.msg,
  232. type: "error",
  233. });
  234. }
  235. });
  236. },
  237. },
  238. };
  239. </script>
  240. <style lang="scss" scoped>
  241. /deep/ .el-input__inner {
  242. border: none !important;
  243. }
  244. .login {
  245. position: relative;
  246. width: 100%;
  247. height: 100%;
  248. .login-bg {
  249. position: fixed;
  250. height: 100%;
  251. width: 100%;
  252. background-size: cover;
  253. background-attachment: fixed;
  254. background: url("/static/image/login-bg.png") no-repeat #010836;
  255. }
  256. .login-logo {
  257. width: 158px;
  258. height: 24px;
  259. position: absolute;
  260. top: 32px;
  261. left: 32px;
  262. }
  263. .login-box {
  264. position: relative;
  265. z-index: 200;
  266. height: 100vh;
  267. display: flex;
  268. display: -webkit-flex;
  269. align-items: center;
  270. justify-content: center;
  271. .login-content {
  272. background-color: #fff;
  273. border-radius: 14px;
  274. width: 360px;
  275. box-sizing: border-box;
  276. align-self: center;
  277. padding: 30px;
  278. .top-icon {
  279. width: 100%;
  280. text-align: center;
  281. font-size: 0;
  282. padding-top: 14px;
  283. img {
  284. width: 194px;
  285. height: 52px;
  286. }
  287. }
  288. .pass-code {
  289. display: flex;
  290. padding-top: 44px;
  291. .item {
  292. font-size: 16px;
  293. font-family: PingFang SC;
  294. font-weight: 400;
  295. color: #565656;
  296. cursor: pointer;
  297. &:nth-child(1) {
  298. margin-right: 30px;
  299. }
  300. &.active {
  301. font-weight: bold;
  302. color: #111111;
  303. padding-bottom: 10px;
  304. border-bottom: 3px solid #fc7200;
  305. }
  306. }
  307. }
  308. .login-password {
  309. padding-top: 30px;
  310. .pass-item {
  311. display: flex;
  312. align-items: center;
  313. padding-bottom: 2px;
  314. border-bottom: 1px solid #eee;
  315. .phone-icon {
  316. width: 14px;
  317. height: 17px;
  318. margin-right: 16px;
  319. }
  320. .lock-icon {
  321. width: 16px;
  322. height: 18px;
  323. margin-right: 14px;
  324. }
  325. .input-phone {
  326. flex: 1;
  327. }
  328. .get-code {
  329. min-width: 120px;
  330. box-sizing: border-box;
  331. text-align: center;
  332. padding: 0 6px;
  333. font-size: 14px;
  334. font-family: PingFang SC;
  335. font-weight: 400;
  336. color: #175199;
  337. cursor: pointer;
  338. }
  339. }
  340. }
  341. .forgot-pass {
  342. text-align: right;
  343. font-size: 14px;
  344. font-family: PingFang SC;
  345. font-weight: 400;
  346. color: #8590a6;
  347. padding-top: 10px;
  348. span {
  349. cursor: pointer;
  350. }
  351. }
  352. .login-btn {
  353. height: 36px;
  354. line-height: 36px;
  355. text-align: center;
  356. background: #fc7200;
  357. border-radius: 3px;
  358. font-size: 14px;
  359. font-family: PingFang SC;
  360. font-weight: 500;
  361. color: #ffffff;
  362. margin-top: 20px;
  363. cursor: pointer;
  364. }
  365. .xie-yi {
  366. font-size: 8px;
  367. font-family: PingFang SC;
  368. font-weight: 400;
  369. color: #808080;
  370. text-align: center;
  371. margin-top: 20px;
  372. a {
  373. text-decoration: none;
  374. color: #175199;
  375. }
  376. }
  377. }
  378. }
  379. }
  380. </style>