index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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">
  8. {{
  9. formType === 'login'
  10. ? '登录'
  11. : formType === 'register'
  12. ? '注册'
  13. : '忘记密码'
  14. }}
  15. </div>
  16. <div class="form">
  17. <div class="form-item mb-4">
  18. <input
  19. type="text"
  20. placeholder="手机号"
  21. v-model="formData.mobile"
  22. maxlength="11"
  23. />
  24. </div>
  25. <div class="form-item mb-4 code" v-if="formType !== 'login'">
  26. <input
  27. type="text"
  28. placeholder="验证码"
  29. class="code"
  30. v-model="formData.verifyCode"
  31. maxlength="6"
  32. />
  33. <span class="send" @click="onSend">{{ sendCodeBtnText }}</span>
  34. </div>
  35. <div class="form-item mb-4">
  36. <input
  37. type="password"
  38. placeholder="密码"
  39. v-model="formData.password"
  40. maxlength="11"
  41. />
  42. </div>
  43. <div class="form-item mb-4" v-if="formType !== 'login'">
  44. <input
  45. type="password"
  46. placeholder="确认密码"
  47. v-model="formData.confirmPwd"
  48. />
  49. </div>
  50. <div class="submit" @click="onSubmit">{{ submitText }}</div>
  51. <div
  52. class="flex justify-between control mt-2"
  53. v-if="formType === 'login'"
  54. >
  55. <span class="forget" @click="onForgetPwd">忘记密码</span>
  56. <span class="regist" @click="onRegister">立即注册</span>
  57. </div>
  58. </div>
  59. </div>
  60. </div>
  61. </van-overlay>
  62. </div>
  63. </template>
  64. <script>
  65. import { mapGetters } from 'vuex'
  66. import { isMobile } from '@/utils/validator'
  67. export default {
  68. name: 'simple-login',
  69. props: {
  70. type: {
  71. type: String,
  72. default: 'login',
  73. },
  74. },
  75. data() {
  76. return {
  77. formType: 'login',
  78. show: false,
  79. sendStatus: 0,
  80. formData: {
  81. authUserId: '',
  82. mobile: '',
  83. verifyCode: '',
  84. password: '',
  85. confirmPwd: '',
  86. },
  87. timer: null,
  88. }
  89. },
  90. computed: {
  91. ...mapGetters(['loginVisiable', 'authUserId', 'routePrefix']),
  92. sendCodeBtnText() {
  93. return this.sendStatus === 0
  94. ? '发送验证码'
  95. : `再次发送${this.sendStatus}s`
  96. },
  97. submitText() {
  98. return this.formType === 'login'
  99. ? '登录'
  100. : this.formType === 'forget'
  101. ? '确定'
  102. : '提交'
  103. },
  104. },
  105. created() {
  106. this.formType = this.type
  107. },
  108. watch: {
  109. loginVisiable() {
  110. this.show = this.loginVisiable
  111. },
  112. type: {
  113. handler: function (nval) {
  114. this.formType = nval
  115. console.log(nval)
  116. },
  117. immediate: true,
  118. },
  119. },
  120. methods: {
  121. // 忘记密码
  122. onForgetPwd() {
  123. this.$emit('click', 'forget')
  124. },
  125. // 立即注册
  126. onRegister() {
  127. this.$emit('click', 'register')
  128. },
  129. async onSubmit() {
  130. // 验证手机号是否合法
  131. if (!isMobile(this.formData.mobile)) {
  132. this.$toast('请输入正确的手机号')
  133. return
  134. }
  135. if (this.formType === 'register') {
  136. this.onRegisterSubmit()
  137. } else if (this.formType === 'forget') {
  138. this.onForgetSubmit()
  139. } else {
  140. this.onLoginSubmit()
  141. }
  142. },
  143. // 忘记密码
  144. async onForgetSubmit() {
  145. if (!this.formData.verifyCode) {
  146. this.$toast('请输入验证码')
  147. return
  148. }
  149. if (this.formData.password.length < 6) {
  150. this.$toast('密码长度需大于6位')
  151. return
  152. }
  153. if (this.formData.password !== this.formData.confirmPwd) {
  154. this.$toast('两次输入的密码不一致')
  155. return
  156. }
  157. try {
  158. await this.$http.api.clubUserReset({
  159. mobile: this.formData.mobile,
  160. verifyCode: this.formData.verifyCode,
  161. password: this.formData.password,
  162. authUserId: this.authUserId,
  163. })
  164. } catch (error) {}
  165. },
  166. // 用户注册
  167. async onRegisterSubmit() {
  168. if (!this.formData.verifyCode) {
  169. this.$toast('请输入验证码')
  170. return
  171. }
  172. if (this.formData.password.length < 6) {
  173. this.$toast('密码长度需大于6位')
  174. return
  175. }
  176. if (this.formData.password !== this.formData.confirmPwd) {
  177. this.$toast('两次输入的密码不一致')
  178. return
  179. }
  180. try {
  181. await this.$http.api.clubUserRegister({
  182. mobile: this.formData.mobile,
  183. verifyCode: this.formData.verifyCode,
  184. password: this.formData.password,
  185. authUserId: this.authUserId,
  186. })
  187. this.$toast('注册成功,请登录')
  188. this.$emit('click', 'login')
  189. } catch (error) {
  190. console.log(error)
  191. this.$toast(error.msg)
  192. }
  193. },
  194. // 用户登录
  195. async onLoginSubmit() {
  196. if (!this.formData.password) {
  197. this.$toast('密码不能为空')
  198. return
  199. }
  200. try {
  201. const res = await this.$http.api.clubUserLogin({
  202. mobile: this.formData.mobile,
  203. password: this.formData.password,
  204. authUserId: this.authUserId,
  205. })
  206. this.login(res)
  207. } catch (error) {
  208. console.log(error)
  209. this.$toast(error.msg)
  210. }
  211. },
  212. login(res) {
  213. this.$store.dispatch('user/login', res.data)
  214. this.$setStorage(this.routePrefix, 'userInfo', res.data)
  215. // 关闭登录窗口
  216. this.onClose()
  217. // 重定向
  218. const login_redicret = this.$getStorage(
  219. this.routePrefix,
  220. 'login_redicret'
  221. )
  222. if (login_redicret && login_redicret !== this.$route.path) {
  223. this.$router.push(login_redicret)
  224. }
  225. },
  226. // 关闭登录窗口
  227. onClose() {
  228. this.$store.commit('app/HIDE_LOGIN')
  229. this.formData.mobile = ''
  230. this.formData.verifyCode = ''
  231. this.formData.password = ''
  232. this.formData.confirmPwd = ''
  233. },
  234. async onSend() {
  235. if (this.sendStatus > 0) return
  236. // 验证手机号是否合法
  237. if (!isMobile(this.formData.mobile)) {
  238. this.$toast('请输入正确的手机号')
  239. return
  240. }
  241. try {
  242. // 发送验证码
  243. await this.$http.api.clubUserCodeSend({
  244. mobile: this.formData.mobile,
  245. authUserId: this.authUserId,
  246. type: this.formType === 'register' ? 1 : 2,
  247. })
  248. this.$toast('验证码已发送')
  249. // 开启倒计时
  250. this.countdown()
  251. } catch (error) {
  252. console.log(error)
  253. }
  254. },
  255. countdown() {
  256. this.sendStatus = 30
  257. this.timer = setInterval(() => {
  258. if (this.sendStatus === 0) {
  259. clearInterval(this.timer)
  260. return
  261. }
  262. this.sendStatus--
  263. }, 1000)
  264. },
  265. },
  266. }
  267. </script>
  268. <style scoped lang="scss">
  269. @media screen and (min-width: 768px) {
  270. .wrapper {
  271. height: 100vh;
  272. .block {
  273. position: relative;
  274. width: 400px;
  275. background: #fff;
  276. .close {
  277. position: absolute;
  278. right: 16px;
  279. top: 16px;
  280. width: 24px;
  281. height: 24px;
  282. background: url(https://static.caimei365.com/www/authentic/h5/icon-close.png)
  283. center no-repeat;
  284. background-size: 24px 24px;
  285. cursor: pointer;
  286. }
  287. .title {
  288. font-size: 24px;
  289. color: #101010;
  290. }
  291. .form-item {
  292. position: relative;
  293. width: 326px;
  294. input {
  295. width: 326px;
  296. display: block;
  297. padding: 14px 16px;
  298. font-size: 14px;
  299. border: 1px solid #d8d8d8;
  300. box-sizing: border-box;
  301. &.code {
  302. width: 225px;
  303. }
  304. }
  305. .send {
  306. position: absolute;
  307. right: 0;
  308. top: 50%;
  309. transform: translateY(-50%);
  310. font-size: 16px;
  311. cursor: pointer;
  312. @include themify($themes) {
  313. color: themed('color');
  314. }
  315. }
  316. }
  317. .control {
  318. font-size: 14px;
  319. .forget,
  320. .regist {
  321. cursor: pointer;
  322. }
  323. .forget {
  324. color: #666;
  325. &:hover {
  326. @include themify($themes) {
  327. color: themed('color');
  328. }
  329. }
  330. }
  331. .regist {
  332. @include themify($themes) {
  333. color: themed('color');
  334. }
  335. }
  336. }
  337. .submit {
  338. width: 326px;
  339. height: 46px;
  340. font-size: 16px;
  341. color: #fff;
  342. text-align: center;
  343. line-height: 46px;
  344. transition: all 0.4s;
  345. cursor: pointer;
  346. @include themify($themes) {
  347. background: themed('color');
  348. }
  349. &:hover {
  350. @include themify($themes) {
  351. background: themed('hover-color');
  352. }
  353. }
  354. }
  355. }
  356. }
  357. }
  358. @media screen and (max-width: 768px) {
  359. .wrapper {
  360. height: 100vh;
  361. .block {
  362. position: relative;
  363. width: 76vw;
  364. background: #fff;
  365. .close {
  366. position: absolute;
  367. right: 2.4vw;
  368. top: 2.4vw;
  369. width: 5.6vw;
  370. height: 5.6vw;
  371. background: url(https://static.caimei365.com/www/authentic/h5/icon-close.png)
  372. center no-repeat;
  373. background-size: 4.8vw 4.8vw;
  374. cursor: pointer;
  375. }
  376. .title {
  377. font-size: 4.8vw;
  378. color: #101010;
  379. }
  380. .form-item {
  381. position: relative;
  382. width: 62vw;
  383. input {
  384. width: 62vw;
  385. display: block;
  386. padding: 1.8vw 2.4vw;
  387. font-size: 14px;
  388. border: 0.1vw solid #d8d8d8;
  389. box-sizing: border-box;
  390. &.code {
  391. width: 42.8vw;
  392. }
  393. }
  394. .send {
  395. position: absolute;
  396. right: 0;
  397. top: 50%;
  398. transform: translateY(-50%);
  399. font-size: 3.2vw;
  400. cursor: pointer;
  401. @include themify($themes) {
  402. color: themed('color');
  403. }
  404. }
  405. }
  406. .control {
  407. font-size: 3.2vw;
  408. .forget,
  409. .regist {
  410. cursor: pointer;
  411. }
  412. .forget {
  413. color: #666;
  414. &:hover {
  415. @include themify($themes) {
  416. color: themed('color');
  417. }
  418. }
  419. }
  420. .regist {
  421. @include themify($themes) {
  422. color: themed('color');
  423. }
  424. }
  425. }
  426. .submit {
  427. width: 62vw;
  428. height: 8.8vw;
  429. font-size: 3.2vw;
  430. color: #fff;
  431. text-align: center;
  432. line-height: 8.8vw;
  433. transition: all 0.4s;
  434. @include themify($themes) {
  435. background: themed('color');
  436. }
  437. }
  438. }
  439. }
  440. }
  441. </style>