index.vue 5.8 KB

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