index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. // this.$router.push(this.routePrefix)
  218. const clubRegisterLink = this.$getStorage(
  219. this.routePrefix,
  220. 'club-register-link'
  221. )
  222. if (clubRegisterLink) {
  223. this.$removeStorage(this.routePrefix, 'club-register-link')
  224. this.$setStorage(this.routePrefix, 'bind-flag', true)
  225. this.$router.push(clubRegisterLink)
  226. } else {
  227. this.$router.push(this.routePrefix)
  228. }
  229. // 重定向
  230. // const login_redicret = this.$getStorage(
  231. // this.routePrefix,
  232. // 'login_redicret'
  233. // )
  234. // if (login_redicret && login_redicret !== this.$route.path) {
  235. // this.$router.push(login_redicret)
  236. // }
  237. },
  238. // 关闭登录窗口
  239. onClose() {
  240. this.$store.commit('app/HIDE_LOGIN')
  241. this.formData.mobile = ''
  242. this.formData.verifyCode = ''
  243. this.formData.password = ''
  244. this.formData.confirmPwd = ''
  245. },
  246. async onSend() {
  247. if (this.sendStatus > 0) return
  248. // 验证手机号是否合法
  249. if (!isMobile(this.formData.mobile)) {
  250. this.$toast('请输入正确的手机号')
  251. return
  252. }
  253. try {
  254. // 发送验证码
  255. await this.$http.api.clubUserCodeSend({
  256. mobile: this.formData.mobile,
  257. authUserId: this.authUserId,
  258. type: this.formType === 'register' ? 1 : 2,
  259. })
  260. this.$toast('验证码已发送')
  261. // 开启倒计时
  262. this.countdown()
  263. } catch (error) {
  264. console.log(error)
  265. }
  266. },
  267. countdown() {
  268. this.sendStatus = 30
  269. this.timer = setInterval(() => {
  270. if (this.sendStatus === 0) {
  271. clearInterval(this.timer)
  272. return
  273. }
  274. this.sendStatus--
  275. }, 1000)
  276. },
  277. },
  278. }
  279. </script>
  280. <style scoped lang="scss">
  281. @media screen and (min-width: 768px) {
  282. .wrapper {
  283. height: 100vh;
  284. .block {
  285. position: relative;
  286. width: 400px;
  287. background: #fff;
  288. .close {
  289. position: absolute;
  290. right: 16px;
  291. top: 16px;
  292. width: 24px;
  293. height: 24px;
  294. background: url(https://static.caimei365.com/www/authentic/h5/icon-close.png)
  295. center no-repeat;
  296. background-size: 24px 24px;
  297. cursor: pointer;
  298. }
  299. .title {
  300. font-size: 24px;
  301. color: #101010;
  302. }
  303. .form-item {
  304. position: relative;
  305. width: 326px;
  306. input {
  307. width: 326px;
  308. display: block;
  309. padding: 14px 16px;
  310. font-size: 14px;
  311. border: 1px solid #d8d8d8;
  312. box-sizing: border-box;
  313. &.code {
  314. width: 225px;
  315. }
  316. }
  317. .send {
  318. position: absolute;
  319. right: 0;
  320. top: 50%;
  321. transform: translateY(-50%);
  322. font-size: 16px;
  323. cursor: pointer;
  324. @include themify($themes) {
  325. color: themed('color');
  326. }
  327. }
  328. }
  329. .control {
  330. font-size: 14px;
  331. .forget,
  332. .regist {
  333. cursor: pointer;
  334. }
  335. .forget {
  336. color: #666;
  337. &:hover {
  338. @include themify($themes) {
  339. color: themed('color');
  340. }
  341. }
  342. }
  343. .regist {
  344. @include themify($themes) {
  345. color: themed('color');
  346. }
  347. }
  348. }
  349. .submit {
  350. width: 326px;
  351. height: 46px;
  352. font-size: 16px;
  353. color: #fff;
  354. text-align: center;
  355. line-height: 46px;
  356. transition: all 0.4s;
  357. cursor: pointer;
  358. @include themify($themes) {
  359. background: themed('color');
  360. }
  361. &:hover {
  362. @include themify($themes) {
  363. background: themed('hover-color');
  364. }
  365. }
  366. }
  367. }
  368. }
  369. }
  370. @media screen and (max-width: 768px) {
  371. .wrapper {
  372. height: 100vh;
  373. .block {
  374. position: relative;
  375. width: 76vw;
  376. background: #fff;
  377. .close {
  378. position: absolute;
  379. right: 2.4vw;
  380. top: 2.4vw;
  381. width: 5.6vw;
  382. height: 5.6vw;
  383. background: url(https://static.caimei365.com/www/authentic/h5/icon-close.png)
  384. center no-repeat;
  385. background-size: 4.8vw 4.8vw;
  386. cursor: pointer;
  387. }
  388. .title {
  389. font-size: 4.8vw;
  390. color: #101010;
  391. }
  392. .form-item {
  393. position: relative;
  394. width: 62vw;
  395. input {
  396. width: 62vw;
  397. display: block;
  398. padding: 1.8vw 2.4vw;
  399. font-size: 14px;
  400. border: 0.1vw solid #d8d8d8;
  401. box-sizing: border-box;
  402. &.code {
  403. width: 42.8vw;
  404. }
  405. }
  406. .send {
  407. position: absolute;
  408. right: 0;
  409. top: 50%;
  410. transform: translateY(-50%);
  411. font-size: 3.2vw;
  412. cursor: pointer;
  413. @include themify($themes) {
  414. color: themed('color');
  415. }
  416. }
  417. }
  418. .control {
  419. font-size: 3.2vw;
  420. .forget,
  421. .regist {
  422. cursor: pointer;
  423. }
  424. .forget {
  425. color: #666;
  426. &:hover {
  427. @include themify($themes) {
  428. color: themed('color');
  429. }
  430. }
  431. }
  432. .regist {
  433. @include themify($themes) {
  434. color: themed('color');
  435. }
  436. }
  437. }
  438. .submit {
  439. width: 62vw;
  440. height: 8.8vw;
  441. font-size: 3.2vw;
  442. color: #fff;
  443. text-align: center;
  444. line-height: 8.8vw;
  445. transition: all 0.4s;
  446. @include themify($themes) {
  447. background: themed('color');
  448. }
  449. }
  450. }
  451. }
  452. }
  453. </style>