ll
liukangdong
2024-10-28 19f0eca493f7e17ab7b67d28cf77d80de364576c
admin/src/views/login.vue
@@ -42,14 +42,15 @@
          <div class="df_ac">
            <div class="label">手机号</div>
            <div class="line">
              <el-input v-model="password" placeholder="请输入手机号" type="password" maxlength="30" show-password />
              <el-input v-model="phone" placeholder="请输入手机号" type="text" maxlength="16" />
            </div>
          </div>
          <div class="df_ac">
            <div class="label">验证码</div>
            <div class="line">
              <el-input v-model="captcha.value" placeholder="请输入验证码" maxlength="4" @keypress.enter.native="login" />
              <span class="code_get" @click="getCode">获取验证码</span>
              <el-input v-model="smsCode" placeholder="请输入验证码" maxlength="6" @keypress.enter.native="login" />
              <span v-if="downTime == 0" class="code_get" @click="getCode">获取验证码</span>
              <span v-else class="code_get downtime">{{ downTime }}</span>
            </div>
          </div>
        </template>
@@ -57,7 +58,8 @@
        <el-button :loading="loading" type="primary" class="login_btn" @click.native.prevent="login">立即登录</el-button>
        <div class="btn_wrap">
          <el-checkbox v-model="isRemPsd">
            <span style="font-size: 13px; font-weight: 400" class="placeholder9">记住密码</span>
            <span style="font-size: 13px; font-weight: 400" class="placeholder9">{{ activeTab == 0 ? '记住密码' : '记住手机号'
              }}</span>
          </el-checkbox>
        </div>
        <div class="google">
@@ -71,8 +73,9 @@
<script>
import { mapMutations } from 'vuex'
import { getCaptcha, loginByPassword } from '@/api/system/common'
import { getCaptcha, loginByPassword, sendSmsPost, loginByPhone } from '@/api/system/common'
import Cookies from "js-cookie"
import { Message } from 'element-ui'
export default {
  name: 'Login',
@@ -82,6 +85,10 @@
      isRemPsd: false,
      username: '',
      password: '',
      phone: '',
      downTime: 0,
      smsCode: '',
      activeTab: '0',
      // 验证码
      captcha: {
@@ -95,6 +102,7 @@
  mounted() {
    this.username = localStorage.getItem('username') || ''
    this.password = localStorage.getItem('password') || ''
    this.phone = localStorage.getItem('phone') || ''
    const isRemPsd = localStorage.getItem('isRemPsd') || false
    this.isRemPsd = JSON.parse(isRemPsd)
    this.refreshCaptcha()
@@ -106,41 +114,73 @@
      if (this.loading) {
        return
      }
      if (!this.__check()) {
        return
      }
      this.loading = true
      loginByPassword({
        username: this.username.trim(),
        password: this.password,
        code: this.captcha.value.trim(),
        uuid: this.captcha.uuid
      })
        .then((res) => {
          // window.location.href = process.env.VUE_APP_CONTEXT_PATH
      if (this.activeTab == 0) {
        if (!this.__check()) {
          return
        }
        loginByPassword({
          username: this.username.trim(),
          password: this.password,
          code: this.captcha.value.trim(),
          uuid: this.captcha.uuid
        })
          .then((res) => {
            // window.location.href = process.env.VUE_APP_CONTEXT_PATH
            if (this.isRemPsd) {
              localStorage.setItem('username', this.username.trim())
              localStorage.setItem('password', this.password.trim())
              localStorage.setItem('isRemPsd', this.isRemPsd)
            } else {
              localStorage.removeItem('username')
              localStorage.removeItem('password')
              localStorage.removeItem('isRemPsd')
            }
            Cookies.set('dm_user_token', res)
            this.$router.push('index')
          })
          .catch(e => {
            this.refreshCaptcha()
          })
          .finally(() => {
            this.loading = false
          })
      } else {
        const { smsCode, phone } = this
        if (!phone || phone.length != 11) {
          return Message.error('请输入正确的手机号')
        }
        if (!smsCode) {
          return Message.error('请输入验证码')
        }
        loginByPhone({ phone, code: smsCode }).then(res => {
          if (this.isRemPsd) {
            localStorage.setItem('username', this.username.trim())
            localStorage.setItem('password', this.password.trim())
            localStorage.setItem('isRemPsd', this.isRemPsd)
          } else {
            localStorage.removeItem('username')
            localStorage.removeItem('password')
            localStorage.removeItem('isRemPsd')
            localStorage.setItem('phone', this.username.trim())
          }
          Cookies.set('dm_user_token', res)
          this.$router.push('index')
        })
        .catch(e => {
          this.refreshCaptcha()
          // this.$tip.apiFailed(e)
        })
        .finally(() => {
        }).finally(() => {
          this.loading = false
        })
      }
    },
    getCode() {
      console.log('getCode');
      console.log('getCode')
      const { phone } = this
      if (!phone || phone.length != 11) {
        return Message.error('请输入正确的手机号')
      }
      sendSmsPost({ phone, type: 0 }).then(res => {
        Message.success('短信发送成功')
        this.downTime = 10
        let timer = setInterval(() => {
          if (this.downTime == 0) {
            return clearInterval(timer)
          }
          this.downTime--
        }, 1000)
      })
    },
    tabClick(val) {
      this.activeTab = val
@@ -314,9 +354,11 @@
        width: 100%;
        align-items: center;
        position: relative;
        .el-input {
          height: 40px;
          flex: 1;
          // font-size: 14px;
          ::v-deep .el-input__inner {
            height: 40px !important;
@@ -327,7 +369,8 @@
          width: 100px;
          margin-left: 10px;
        }
        .code_get{
        .code_get {
          position: absolute;
          top: 10px;
          right: 10px;
@@ -337,6 +380,11 @@
          margin-left: 10px;
          cursor: pointer;
          color: $primary-color;
          text-align: right;
        }
        .downtime {
          color: #999999;
        }
      }