| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <template>
- <ElContainer class="h-full bg-cover bg-center" @keyup.enter="login" :style="{ backgroundImage: `url(${bg})` }">
- <ElMain class="backdrop-blur1 dark:backdrop-brightness-50 !flex flex-col items-center justify-center">
- <span class="text-3xl font-[sh] text-white [text-shadow:_0_1px_0_rgb(0_0_0_/_40%)]">{{ title }}</span>
- <ElCard class="w-full max-w-lg !rounded-xl mt-8 mb-16">
- <ElForm :model="model" :rules="rules" label-position="top" ref="form">
- <ElFormItem prop="username" label="用户名">
- <ElInput v-model="model.username"></ElInput>
- </ElFormItem>
- <ElFormItem prop="username" label="密码">
- <ElInput type="password" v-model="model.password"></ElInput>
- </ElFormItem>
- <ElFormItem prop="code" label="认证码">
- <div class="flex items-center justify-between w-full">
- <ElInput v-model="model.code" maxlength="6" class="max-w-[55%]"></ElInput>
- <div v-if="isTips" class="text-red-500 text-xs cursor-pointer pl-2"
- style="width: calc(50% - 5px); white-space: pre-wrap;">
- 注意:第一次登录请先<br>
- <span class="text-blue-500 text-xs cursor-pointer"
- @click="openQrCodeModal"> 绑定谷歌验证器</span>
- 再获取认证码
- </div>
- </div>
- </ElFormItem>
- </ElForm>
- <el-button class="mt-8 w-full !block" type="primary" @click="login" :loading="loading">登录</el-button>
- </ElCard>
- <div class="fixed top-0 right-0 px-4 py-4">
- <DarkSwitch />
- </div>
- </ElMain>
- </ElContainer>
- <ElDialog
- v-model="qrCodeModalVisible"
- width="350"
- center
- >
- <template #title>
- <div class="text-left">
- <div class="text-lg">
- 绑定谷歌验证器
- </div>
- <div class="text-xs">
- 请使用该App扫描下方二维码
- <a href="https://apkpure.com/cn/google-authenticator/com.google.android.apps.authenticator2/download"
- target="_blank" class="text-blue-500">(点击下载)</a>
- </div>
- </div>
- </template>
- <div v-if="qrCode">
- <img :src="qrCode" alt="Google Authenticator QR Code"
- style="width: 260px; height: 260px;"
- class="mx-auto" />
- </div>
- <div v-else>
- 正在加载二维码...
- </div>
- <div class="text-center text-xs">
- app出现认证码后,点击按钮返回登录页面输入认证码
- </div>
- <div>
- <el-button
- class="custom-button"
- @click="qrCodeModalVisible = false"
- >
- 确认绑定
- </el-button>
- </div>
- </ElDialog>
- </template>
- <script setup>
- import { ref } from 'vue'
- import DarkSwitch from '@/components/DarkSwitch.vue'
- import { http } from '@/plugins/http'
- import { useUserStore } from '@/stores/user'
- import { storeToRefs } from 'pinia'
- import { useRouter } from 'vue-router'
- import { ElMessage } from 'element-plus'
- import QRCode from 'qrcode'
- const bg = new URL(`../assets/${import.meta.env.VITE_BG}`, import.meta.url)
- const title = '47.98.193.71' === location.host ? '悟空传媒' : import.meta.env.VITE_TITLE
- const router = useRouter()
- const { user, setUser } = storeToRefs(useUserStore())
- const model = ref({
- username: '',
- password: '',
- code: ''
- })
- const rules = {
- username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
- password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
- code: [{ required: true, message: '请输入6位认证码', trigger: 'blur' }]
- }
- const form = ref(null)
- const loading = ref(false)
- function login() {
- form.value.validate().then(async () => {
- loading.value = true
- try {
- const res = await http.post(process.env.VITE_LOGIN_URL, model.value)
- http.setToken(res.access_token)
- router.replace({ name: 'home' })
- loading.value = false
- } catch (e) {
- loading.value = false
- ElMessage.error(e.message)
- }
- })
- }
- const isTips = ref(true)
- const qrCodeModalVisible = ref(false)
- const qrCode = ref(null)
- async function openQrCodeModal() {
- try {
- const url = await http.post('/auth/binding', model.value)
- if (url === 'success') {
- ElMessage.success('您已绑定过谷歌验证器,无需再次绑定.')
- } else {
- qrCodeModalVisible.value = true
- qrCode.value = null
- qrCode.value = await QRCode.toDataURL(url)
- }
- } catch (e) {
- ElMessage.error('获取二维码失败')
- }
- }
- </script>
- <style scoped>
- .custom-button {
- width: 260px;
- background-color: #2f86ea;
- color: white;
- text-align: center;
- display: block;
- margin: 15px auto 0;
- }
- .custom-button:hover {
- background-color: #197ced;
- }
- </style>
|