login.vue 9.9 KB

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