index.vue 12 KB

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