123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- <template>
- <div class="simple-login">
- <van-overlay :show="show" @click="show = false">
- <div class="wrapper flex justify-center items-center" @click.stop>
- <div class="block flex items-center flex-col py-6">
- <div class="close" @click="onClose"></div>
- <div class="title pb-6">
- {{
- formType === 'login'
- ? '登录'
- : formType === 'register'
- ? '注册'
- : '忘记密码'
- }}
- </div>
- <div class="form">
- <div class="form-item mb-4">
- <input
- type="text"
- placeholder="手机号"
- v-model="formData.mobile"
- maxlength="11"
- />
- </div>
- <div class="form-item mb-4 code" v-if="formType !== 'login'">
- <input
- type="text"
- placeholder="验证码"
- class="code"
- v-model="formData.verifyCode"
- maxlength="6"
- />
- <span class="send" @click="onSend">{{ sendCodeBtnText }}</span>
- </div>
- <div class="form-item mb-4">
- <input
- type="password"
- placeholder="密码"
- v-model="formData.password"
- maxlength="11"
- />
- </div>
- <div class="form-item mb-4" v-if="formType !== 'login'">
- <input
- type="password"
- placeholder="确认密码"
- v-model="formData.confirmPwd"
- />
- </div>
- <div class="submit" @click="onSubmit">{{ submitText }}</div>
- <div
- class="flex justify-between control mt-2"
- v-if="formType === 'login'"
- >
- <span class="forget" @click="onForgetPwd">忘记密码</span>
- <span class="regist" @click="onRegister">立即注册</span>
- </div>
- </div>
- </div>
- </div>
- </van-overlay>
- </div>
- </template>
- <script>
- import { mapGetters } from 'vuex'
- import { isMobile } from '@/utils/validator'
- export default {
- name: 'simple-login',
- props: {
- type: {
- type: String,
- default: 'login',
- },
- },
- data() {
- return {
- formType: 'login',
- show: false,
- sendStatus: 0,
- formData: {
- authUserId: '',
- mobile: '',
- verifyCode: '',
- password: '',
- confirmPwd: '',
- },
- timer: null,
- }
- },
- computed: {
- ...mapGetters(['loginVisiable', 'authUserId', 'routePrefix']),
- sendCodeBtnText() {
- return this.sendStatus === 0
- ? '发送验证码'
- : `再次发送${this.sendStatus}s`
- },
- submitText() {
- return this.formType === 'login'
- ? '登录'
- : this.formType === 'forget'
- ? '确定'
- : '提交'
- },
- },
- created() {
- this.formType = this.type
- },
- watch: {
- loginVisiable() {
- this.show = this.loginVisiable
- },
- type: {
- handler: function (nval) {
- this.formType = nval
- console.log(nval)
- },
- immediate: true,
- },
- },
- methods: {
- // 忘记密码
- onForgetPwd() {
- this.$emit('click', 'forget')
- },
- // 立即注册
- onRegister() {
- this.$emit('click', 'register')
- },
- async onSubmit() {
- // 验证手机号是否合法
- if (!isMobile(this.formData.mobile)) {
- this.$toast('请输入正确的手机号')
- return
- }
- if (this.formType === 'register') {
- this.onRegisterSubmit()
- } else if (this.formType === 'forget') {
- this.onForgetSubmit()
- } else {
- this.onLoginSubmit()
- }
- },
- // 忘记密码
- async onForgetSubmit() {
- if (!this.formData.verifyCode) {
- this.$toast('请输入验证码')
- return
- }
- if (this.formData.password.length < 6) {
- this.$toast('密码长度需大于6位')
- return
- }
- if (this.formData.password !== this.formData.confirmPwd) {
- this.$toast('两次输入的密码不一致')
- return
- }
- try {
- await this.$http.api.clubUserReset({
- mobile: this.formData.mobile,
- verifyCode: this.formData.verifyCode,
- password: this.formData.password,
- authUserId: this.authUserId,
- })
- this.$toast('密码修改成功')
- this.$emit('click', 'login')
- } catch (error) {
- console.log(error)
- }
- },
- // 用户注册
- async onRegisterSubmit() {
- if (!this.formData.verifyCode) {
- this.$toast('请输入验证码')
- return
- }
- if (this.formData.password.length < 6) {
- this.$toast('密码长度需大于6位')
- return
- }
- if (this.formData.password !== this.formData.confirmPwd) {
- this.$toast('两次输入的密码不一致')
- return
- }
- try {
- await this.$http.api.clubUserRegister({
- mobile: this.formData.mobile,
- verifyCode: this.formData.verifyCode,
- password: this.formData.password,
- authUserId: this.authUserId,
- })
- this.$toast('注册成功,请登录')
- this.$emit('click', 'login')
- } catch (error) {
- console.log(error)
- this.$toast(error.msg)
- }
- },
- // 用户登录
- async onLoginSubmit() {
- if (!this.formData.password) {
- this.$toast('密码不能为空')
- return
- }
- try {
- const res = await this.$http.api.clubUserLogin({
- mobile: this.formData.mobile,
- password: this.formData.password,
- authUserId: this.authUserId,
- })
- this.login(res)
- } catch (error) {
- console.log(error)
- this.$toast(error.msg)
- }
- },
- login(res) {
- this.$store.dispatch('user/login', res.data)
- this.$setStorage(this.routePrefix, 'userInfo', res.data)
- // 关闭登录窗口
- this.onClose()
- // this.$router.push(this.routePrefix)
- const clubRegisterLink = this.$getStorage(
- this.routePrefix,
- 'club-register-link'
- )
- // if (clubRegisterLink) {
- // this.$removeStorage(this.routePrefix, 'club-register-link')
- // this.$setStorage(this.routePrefix, 'bind-flag', true)
- // this.$router.push(clubRegisterLink)
- // } else {
- // this.$router.push(this.routePrefix)
- // }
- window.location.reload()
- // 重定向
- // const login_redicret = this.$getStorage(
- // this.routePrefix,
- // 'login_redicret'
- // )
- // if (login_redicret && login_redicret !== this.$route.path) {
- // this.$router.push(login_redicret)
- // }
- },
- // 关闭登录窗口
- onClose() {
- this.$store.commit('app/HIDE_LOGIN')
- this.formData.mobile = ''
- this.formData.verifyCode = ''
- this.formData.password = ''
- this.formData.confirmPwd = ''
- },
- async onSend() {
- if (this.sendStatus > 0) return
- // 验证手机号是否合法
- if (!isMobile(this.formData.mobile)) {
- this.$toast('请输入正确的手机号')
- return
- }
- try {
- // 发送验证码
- await this.$http.api.clubUserCodeSend({
- mobile: this.formData.mobile,
- authUserId: this.authUserId,
- type: this.formType === 'register' ? 1 : 2,
- })
- this.$toast('验证码已发送')
- // 开启倒计时
- this.countdown()
- } catch (error) {
- console.log(error)
- }
- },
- countdown() {
- this.sendStatus = 30
- this.timer = setInterval(() => {
- if (this.sendStatus === 0) {
- clearInterval(this.timer)
- return
- }
- this.sendStatus--
- }, 1000)
- },
- },
- }
- </script>
- <style scoped lang="scss">
- @media screen and (min-width: 768px) {
- .van-overlay {
- z-index: 20;
- }
- .wrapper {
- height: 100vh;
- .block {
- position: relative;
- width: 400px;
- background: #fff;
- .close {
- position: absolute;
- right: 16px;
- top: 16px;
- width: 24px;
- height: 24px;
- background: url(~assets/theme-images/common/pc-icon-close.png) center
- no-repeat;
- background-size: 24px 24px;
- cursor: pointer;
- }
- .title {
- font-size: 24px;
- color: #101010;
- }
- .form-item {
- position: relative;
- width: 326px;
- input {
- width: 326px;
- display: block;
- padding: 14px 16px;
- font-size: 14px;
- border: 1px solid #d8d8d8;
- box-sizing: border-box;
- &.code {
- width: 225px;
- }
- }
- .send {
- position: absolute;
- right: 0;
- top: 50%;
- transform: translateY(-50%);
- font-size: 16px;
- cursor: pointer;
- @include themify($themes) {
- color: themed('color');
- }
- }
- }
- .control {
- font-size: 14px;
- .forget,
- .regist {
- cursor: pointer;
- }
- .forget {
- color: #666;
- &:hover {
- @include themify($themes) {
- color: themed('color');
- }
- }
- }
- .regist {
- @include themify($themes) {
- color: themed('color');
- }
- }
- }
- .submit {
- width: 326px;
- height: 46px;
- font-size: 16px;
- color: #fff;
- text-align: center;
- line-height: 46px;
- transition: all 0.4s;
- cursor: pointer;
- @include themify($themes) {
- background: themed('color');
- }
- &:hover {
- @include themify($themes) {
- background: themed('hover-color');
- }
- }
- }
- }
- }
- }
- @media screen and (max-width: 768px) {
- .van-overlay {
- z-index: 20;
- }
- .wrapper {
- height: 100vh;
- .block {
- position: relative;
- width: 76vw;
- background: #fff;
- .close {
- position: absolute;
- right: 2.4vw;
- top: 2.4vw;
- width: 5.6vw;
- height: 5.6vw;
- background: url(~assets/theme-images/common/h5-icon-close.png) center
- no-repeat;
- background-size: 4.8vw 4.8vw;
- cursor: pointer;
- }
- .title {
- font-size: 4.8vw;
- color: #101010;
- }
- .form-item {
- position: relative;
- width: 62vw;
- input {
- width: 62vw;
- display: block;
- padding: 1.8vw 2.4vw;
- font-size: 14px;
- border: 0.1vw solid #d8d8d8;
- box-sizing: border-box;
- &.code {
- width: 42.8vw;
- }
- }
- .send {
- position: absolute;
- right: 0;
- top: 50%;
- transform: translateY(-50%);
- font-size: 3.2vw;
- cursor: pointer;
- @include themify($themes) {
- color: themed('color');
- }
- }
- }
- .control {
- font-size: 3.2vw;
- .forget,
- .regist {
- cursor: pointer;
- }
- .forget {
- color: #666;
- &:hover {
- @include themify($themes) {
- color: themed('color');
- }
- }
- }
- .regist {
- @include themify($themes) {
- color: themed('color');
- }
- }
- }
- .submit {
- width: 62vw;
- height: 8.8vw;
- font-size: 3.2vw;
- color: #fff;
- text-align: center;
- line-height: 8.8vw;
- transition: all 0.4s;
- @include themify($themes) {
- background: themed('color');
- }
- }
- }
- }
- }
- </style>
|