index.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. try {
  67. this.formData.authUserId = this.authUserId
  68. const res = await this.$http.api.customLogin(this.formData)
  69. this.$store.dispatch('user/login', res.data)
  70. this.$setStorage(this.routePrefix, 'userInfo', res.data)
  71. // 关闭登录窗口
  72. this.onClose()
  73. // 重定向
  74. const login_redicret = this.$getStorage(
  75. this.routePrefix,
  76. 'login_redicret'
  77. )
  78. if (login_redicret && login_redicret !== this.$route.path) {
  79. this.$router.push(login_redicret)
  80. }
  81. } catch (error) {
  82. console.log(error)
  83. }
  84. },
  85. // 关闭登录窗口
  86. onClose() {
  87. this.$store.commit('app/HIDE_LOGIN')
  88. this.formData.mobile = ''
  89. this.formData.verifyCode = ''
  90. this.formData.authUserId = ''
  91. },
  92. async onSend() {
  93. if (this.sendStatus > 0) return
  94. // 验证手机号是否合法
  95. if (!isMobile(this.formData.mobile)) {
  96. this.$toast('请输入正确的手机号')
  97. return
  98. }
  99. try {
  100. // 发送验证码
  101. const res = await this.$http.api.sendVerifyCode({
  102. mobile: this.formData.mobile,
  103. authUserId: this.authUserId,
  104. type: 1,
  105. })
  106. this.$toast('验证码已发送')
  107. // 开启倒计时
  108. this.countdown()
  109. } catch (error) {
  110. console.log(error)
  111. }
  112. },
  113. countdown() {
  114. this.sendStatus = 30
  115. this.timer = setInterval(() => {
  116. if (this.sendStatus === 0) {
  117. clearInterval(this.timer)
  118. return
  119. }
  120. this.sendStatus--
  121. }, 1000)
  122. },
  123. },
  124. }
  125. </script>
  126. <style scoped lang="scss">
  127. @media screen and (min-width: 768px) {
  128. .wrapper {
  129. height: 100vh;
  130. .block {
  131. position: relative;
  132. width: 400px;
  133. background: #fff;
  134. .close {
  135. position: absolute;
  136. right: 16px;
  137. top: 16px;
  138. width: 24px;
  139. height: 24px;
  140. background: url(https://static.caimei365.com/www/authentic/h5/icon-close.png)
  141. center no-repeat;
  142. background-size: 24px 24px;
  143. cursor: pointer;
  144. }
  145. .title {
  146. font-size: 24px;
  147. color: #101010;
  148. }
  149. .form-item {
  150. position: relative;
  151. width: 326px;
  152. input {
  153. width: 326px;
  154. display: block;
  155. padding: 14px 16px;
  156. font-size: 14px;
  157. border: 1px solid #d8d8d8;
  158. box-sizing: border-box;
  159. &.code {
  160. width: 225px;
  161. }
  162. }
  163. .send {
  164. position: absolute;
  165. right: 0;
  166. top: 50%;
  167. transform: translateY(-50%);
  168. font-size: 16px;
  169. color: #bc1724;
  170. cursor: pointer;
  171. }
  172. }
  173. .submit {
  174. width: 326px;
  175. height: 46px;
  176. background: #bc1724;
  177. font-size: 16px;
  178. color: #fff;
  179. text-align: center;
  180. line-height: 46px;
  181. transition: all 0.4s;
  182. cursor: pointer;
  183. &:hover {
  184. background-color: #960915;
  185. }
  186. }
  187. }
  188. }
  189. }
  190. @media screen and (max-width: 768px) {
  191. .wrapper {
  192. height: 100vh;
  193. .block {
  194. position: relative;
  195. width: 76vw;
  196. background: #fff;
  197. .close {
  198. position: absolute;
  199. right: 2.4vw;
  200. top: 2.4vw;
  201. width: 5.6vw;
  202. height: 5.6vw;
  203. background: url(https://static.caimei365.com/www/authentic/h5/icon-close.png)
  204. center no-repeat;
  205. background-size: 4.8vw 4.8vw;
  206. cursor: pointer;
  207. }
  208. .title {
  209. font-size: 4.8vw;
  210. color: #101010;
  211. }
  212. .form-item {
  213. position: relative;
  214. width: 62vw;
  215. input {
  216. width: 62vw;
  217. display: block;
  218. padding: 1.8vw 2.4vw;
  219. font-size: 14px;
  220. border: 0.1vw solid #d8d8d8;
  221. box-sizing: border-box;
  222. &.code {
  223. width: 42.8vw;
  224. }
  225. }
  226. .send {
  227. position: absolute;
  228. right: 0;
  229. top: 50%;
  230. transform: translateY(-50%);
  231. font-size: 3.2vw;
  232. color: #bc1724;
  233. cursor: pointer;
  234. }
  235. }
  236. .submit {
  237. width: 62vw;
  238. height: 8.8vw;
  239. background: #bc1724;
  240. font-size: 3.2vw;
  241. color: #fff;
  242. text-align: center;
  243. line-height: 8.8vw;
  244. transition: all 0.4s;
  245. &:hover {
  246. background-color: #b60c1a;
  247. }
  248. }
  249. }
  250. }
  251. }
  252. </style>