index.vue 12 KB

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