index.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <template>
  2. <div class="simple-login">
  3. <van-overlay :show="show" @click="show = false">
  4. <div class="wrapper flex justify-center items-center" @click.stop>
  5. <div class="block flex items-center flex-col py-6">
  6. <div class="close" @click="onClose"></div>
  7. <div class="title pb-6">登录</div>
  8. <div class="form">
  9. <div class="form-item mb-4">
  10. <input
  11. type="text"
  12. placeholder="手机号"
  13. v-model="formData.mobile"
  14. maxlength="11"
  15. />
  16. </div>
  17. <div class="form-item mb-4 code">
  18. <input
  19. type="text"
  20. placeholder="验证码"
  21. class="code"
  22. v-model="formData.verifyCode"
  23. maxlength="6"
  24. />
  25. <span class="send" @click="onSend">{{ sendCodeBtnText }}</span>
  26. </div>
  27. <div class="submit" @click="onSubmit">登录</div>
  28. </div>
  29. </div>
  30. </div>
  31. </van-overlay>
  32. </div>
  33. </template>
  34. <script>
  35. import { mapGetters } from 'vuex'
  36. import { isMobile } from '@/utils/validator'
  37. export default {
  38. name: 'simple-login',
  39. data() {
  40. return {
  41. show: false,
  42. sendStatus: 0,
  43. formData: {
  44. authUserId: '',
  45. mobile: '',
  46. verifyCode: '',
  47. },
  48. timer: null,
  49. }
  50. },
  51. computed: {
  52. ...mapGetters(['loginVisiable', 'authUserId', 'routePrefix']),
  53. sendCodeBtnText() {
  54. return this.sendStatus === 0
  55. ? '发送验证码'
  56. : `再次发送${this.sendStatus}s`
  57. },
  58. },
  59. watch: {
  60. loginVisiable() {
  61. this.show = this.loginVisiable
  62. },
  63. },
  64. methods: {
  65. async onSubmit() {
  66. // 验证手机号是否合法
  67. if (!isMobile(this.formData.mobile)) {
  68. this.$toast('请输入正确的手机号')
  69. return
  70. }
  71. if (!this.formData.verifyCode) {
  72. this.$toast('请输入验证码')
  73. return
  74. }
  75. try {
  76. this.formData.authUserId = this.authUserId
  77. const res = await this.$http.api.customLogin(this.formData)
  78. this.$store.dispatch('user/login', res.data)
  79. this.$setStorage(this.routePrefix, 'userInfo', res.data)
  80. // 关闭登录窗口
  81. this.onClose()
  82. // 重定向
  83. const login_redicret = this.$getStorage(
  84. this.routePrefix,
  85. 'login_redicret'
  86. )
  87. if (login_redicret && login_redicret !== this.$route.path) {
  88. this.$router.push(login_redicret)
  89. }
  90. } catch (error) {
  91. console.log(error)
  92. }
  93. },
  94. // 关闭登录窗口
  95. onClose() {
  96. this.$store.commit('app/HIDE_LOGIN')
  97. this.formData.mobile = ''
  98. this.formData.verifyCode = ''
  99. this.formData.authUserId = ''
  100. },
  101. async onSend() {
  102. if (this.sendStatus > 0) return
  103. // 验证手机号是否合法
  104. if (!isMobile(this.formData.mobile)) {
  105. this.$toast('请输入正确的手机号')
  106. return
  107. }
  108. try {
  109. // 发送验证码
  110. const res = await this.$http.api.sendVerifyCode({
  111. mobile: this.formData.mobile,
  112. authUserId: this.authUserId,
  113. type: 1,
  114. })
  115. this.$toast('验证码已发送')
  116. // 开启倒计时
  117. this.countdown()
  118. } catch (error) {
  119. console.log(error)
  120. }
  121. },
  122. countdown() {
  123. this.sendStatus = 30
  124. this.timer = setInterval(() => {
  125. if (this.sendStatus === 0) {
  126. clearInterval(this.timer)
  127. return
  128. }
  129. this.sendStatus--
  130. }, 1000)
  131. },
  132. },
  133. }
  134. </script>
  135. <style scoped lang="scss">
  136. @media screen and (min-width: 768px) {
  137. .wrapper {
  138. height: 100vh;
  139. .block {
  140. position: relative;
  141. width: 400px;
  142. background: #fff;
  143. .close {
  144. position: absolute;
  145. right: 16px;
  146. top: 16px;
  147. width: 24px;
  148. height: 24px;
  149. background: url(https://static.caimei365.com/www/authentic/h5/icon-close.png)
  150. center no-repeat;
  151. background-size: 24px 24px;
  152. cursor: pointer;
  153. }
  154. .title {
  155. font-size: 24px;
  156. color: #101010;
  157. }
  158. .form-item {
  159. position: relative;
  160. width: 326px;
  161. input {
  162. width: 326px;
  163. display: block;
  164. padding: 14px 16px;
  165. font-size: 14px;
  166. border: 1px solid #d8d8d8;
  167. box-sizing: border-box;
  168. &.code {
  169. width: 225px;
  170. }
  171. }
  172. .send {
  173. position: absolute;
  174. right: 0;
  175. top: 50%;
  176. transform: translateY(-50%);
  177. font-size: 16px;
  178. color: #bc1724;
  179. cursor: pointer;
  180. }
  181. }
  182. .submit {
  183. width: 326px;
  184. height: 46px;
  185. background: #bc1724;
  186. font-size: 16px;
  187. color: #fff;
  188. text-align: center;
  189. line-height: 46px;
  190. transition: all 0.4s;
  191. cursor: pointer;
  192. &:hover {
  193. background-color: #960915;
  194. }
  195. }
  196. }
  197. }
  198. }
  199. @media screen and (max-width: 768px) {
  200. .wrapper {
  201. height: 100vh;
  202. .block {
  203. position: relative;
  204. width: 76vw;
  205. background: #fff;
  206. .close {
  207. position: absolute;
  208. right: 2.4vw;
  209. top: 2.4vw;
  210. width: 5.6vw;
  211. height: 5.6vw;
  212. background: url(https://static.caimei365.com/www/authentic/h5/icon-close.png)
  213. center no-repeat;
  214. background-size: 4.8vw 4.8vw;
  215. cursor: pointer;
  216. }
  217. .title {
  218. font-size: 4.8vw;
  219. color: #101010;
  220. }
  221. .form-item {
  222. position: relative;
  223. width: 62vw;
  224. input {
  225. width: 62vw;
  226. display: block;
  227. padding: 1.8vw 2.4vw;
  228. font-size: 14px;
  229. border: 0.1vw solid #d8d8d8;
  230. box-sizing: border-box;
  231. &.code {
  232. width: 42.8vw;
  233. }
  234. }
  235. .send {
  236. position: absolute;
  237. right: 0;
  238. top: 50%;
  239. transform: translateY(-50%);
  240. font-size: 3.2vw;
  241. color: #bc1724;
  242. cursor: pointer;
  243. }
  244. }
  245. .submit {
  246. width: 62vw;
  247. height: 8.8vw;
  248. background: #bc1724;
  249. font-size: 3.2vw;
  250. color: #fff;
  251. text-align: center;
  252. line-height: 8.8vw;
  253. transition: all 0.4s;
  254. &:hover {
  255. background-color: #b60c1a;
  256. }
  257. }
  258. }
  259. }
  260. }
  261. </style>