index.vue 5.8 KB
Newer Older
1 2 3 4
<template>
  <div id="userLayout">
    <div class="login_panle">
      <div class="login_panle_form">
D
Devil 已提交
5
        <div class="login_panle_form_title">
B
bypanghu 已提交
6 7 8 9 10
          <img
            class="login_panle_form_title_logo"
            :src="$GIN_VUE_ADMIN.appLogo"
            alt
          >
11
          <p class="login_panle_form_title_p">{{ $GIN_VUE_ADMIN.appName }}</p>
D
Devil 已提交
12
        </div>
B
bypanghu 已提交
13 14
        <el-form
          ref="loginForm"
15
          :model="loginFormData"
B
bypanghu 已提交
16 17 18
          :rules="rules"
          @keyup.enter="submitForm"
        >
19
          <el-form-item prop="username">
20 21 22 23
            <el-input
              v-model="loginFormData.username"
              placeholder="请输入用户名"
            >
24
              <template #suffix>
P
piexlmax 已提交
25 26 27 28 29
                <span class="input-icon">
                  <el-icon>
                    <user />
                  </el-icon>
                </span>
30
              </template>
31 32 33 34
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
35
              v-model="loginFormData.password"
36 37 38
              :type="lock === 'lock' ? 'password' : 'text'"
              placeholder="请输入密码"
            >
39
              <template #suffix>
P
piexlmax 已提交
40
                <span class="input-icon">
41 42 43 44 45 46
                  <el-icon>
                    <component
                      :is="lock"
                      @click="changeLock"
                    />
                  </el-icon>
P
piexlmax 已提交
47
                </span>
48
              </template>
49 50
            </el-input>
          </el-form-item>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
          <el-form-item prop="captcha">
            <div class="vPicBox">
              <el-input
                v-model="loginFormData.captcha"
                placeholder="请输入验证码"
                style="width: 60%"
              />
              <div class="vPic">
                <img
                  v-if="picPath"
                  :src="picPath"
                  alt="请输入验证码"
                  @click="loginVerify()"
                >
              </div>
66 67 68
            </div>
          </el-form-item>
          <el-form-item>
B
bypanghu 已提交
69 70 71
            <el-button
              type="primary"
              style="width: 46%"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
72
              size="large"
B
bypanghu 已提交
73 74 75 76
              @click="checkInit"
            >前往初始化</el-button>
            <el-button
              type="primary"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
77
              size="large"
B
bypanghu 已提交
78 79 80
              style="width: 46%; margin-left: 8%"
              @click="submitForm"
            >登 录</el-button>
81 82 83 84 85 86
          </el-form-item>
        </el-form>
      </div>
      <div class="login_panle_right" />
      <div class="login_panle_foot">
        <div class="links">
P
piexlmax 已提交
87
          <a href="http://doc.henrongyi.top/" target="_blank">
88 89
            <img src="@/assets/docs.png" class="link-icon">
          </a>
P
piexlmax 已提交
90 91
          <a href="https://support.qq.com/product/371961" target="_blank">
            <img src="@/assets/kefu.png" class="link-icon">
92
          </a>
93 94 95 96
          <a
            href="https://github.com/flipped-aurora/gin-vue-admin"
            target="_blank"
          >
97 98
            <img src="@/assets/github.png" class="link-icon">
          </a>
P
piexlmax 已提交
99
          <a href="https://space.bilibili.com/322210472" target="_blank">
100 101
            <img src="@/assets/video.png" class="link-icon">
          </a>
102
        </div>
B
bypanghu 已提交
103 104 105
        <div class="copyright">
          <bootomInfo />
        </div>
106 107 108 109
      </div>
    </div>
  </div>
</template>
110

111
<script>
112 113 114 115 116 117
export default {
  name: 'Login',
}
</script>

<script setup>
118
import { captcha } from '@/api/user'
Mr.奇淼('s avatar
Mr.奇淼( 已提交
119
import { checkDB } from '@/api/initdb'
B
bypanghu 已提交
120
import bootomInfo from '@/view/layout/bottomInfo/bottomInfo.vue'
121 122 123
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
Mr.奇淼('s avatar
Mr.奇淼( 已提交
124
import { useUserStore } from '@/pinia/modules/user'
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
const router = useRouter()
// 验证函数
const checkUsername = (rule, value, callback) => {
  if (value.length < 5) {
    return callback(new Error('请输入正确的用户名'))
  } else {
    callback()
  }
}
const checkPassword = (rule, value, callback) => {
  if (value.length < 6) {
    return callback(new Error('请输入正确的密码'))
  } else {
    callback()
  }
}

// 获取验证码
const loginVerify = () => {
  captcha({}).then((ele) => {
    rules.captcha[1].max = ele.data.captchaLength
    rules.captcha[1].min = ele.data.captchaLength
    picPath.value = ele.data.picPath
    loginFormData.captchaId = ele.data.captchaId
  })
}
loginVerify()

// 登录相关操作
const lock = ref('lock')
const changeLock = () => {
  lock.value = lock.value === 'lock' ? 'unlock' : 'lock'
}

const loginForm = ref(null)
const picPath = ref('')
const loginFormData = reactive({
  username: 'admin',
  password: '123456',
  captcha: '',
  captchaId: '',
})
const rules = reactive({
  username: [{ validator: checkUsername, trigger: 'blur' }],
  password: [{ validator: checkPassword, trigger: 'blur' }],
  captcha: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    {
      message: '验证码格式不正确',
      trigger: 'blur',
175
    },
176 177
  ],
})
Mr.奇淼('s avatar
Mr.奇淼( 已提交
178 179

const userStore = useUserStore()
180
const login = async() => {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
181
  return await userStore.LoginIn(loginFormData)
182 183 184 185 186 187 188 189 190 191 192 193 194
}
const submitForm = () => {
  loginForm.value.validate(async(v) => {
    if (v) {
      const flag = await login()
      if (!flag) {
        loginVerify()
      }
    } else {
      ElMessage({
        type: 'error',
        message: '请正确填写登录信息',
        showClose: true,
195
      })
196 197 198 199 200 201 202 203 204 205 206
      loginVerify()
      return false
    }
  })
}

// 跳转初始化
const checkInit = async() => {
  const res = await checkDB()
  if (res.code === 0) {
    if (res.data?.needInit) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
207
      userStore.NeedInit()
208 209 210 211 212
      router.push({ name: 'Init' })
    } else {
      ElMessage({
        type: 'info',
        message: '已配置数据库信息,无法初始化',
213 214 215 216
      })
    }
  }
}
B
bypanghu 已提交
217

218 219 220 221 222
</script>

<style lang="scss" scoped>
@import "@/style/newLogin.scss";
</style>