index.vue 5.7 KB

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