功能:后台登录增加店铺必选

This commit is contained in:
xiaowang
2026-03-20 17:20:23 +08:00
parent 8fb50fb884
commit 4d4439dc94
5 changed files with 148 additions and 5 deletions
+71 -4
View File
@@ -32,6 +32,19 @@
@pressEnter="handleLogin"
/>
</a-form-item>
<a-form-item label="登录店铺" name="shopId">
<a-select
v-model:value="formState.shopId"
size="large"
placeholder="请选择登录店铺"
:loading="shopLoading"
:options="shopOptions"
:disabled="shopLoading || shopOptions.length === 0"
show-search
:filter-option="filterShopOption"
/>
</a-form-item>
<a-form-item>
<a-button
@@ -55,32 +68,80 @@
</template>
<script setup lang="ts">
import { ref, reactive } from 'vue'
import { computed, onMounted, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import type { FormInstance } from 'ant-design-vue'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'
import { adminAuthApi } from '@/api/admin'
import { shopApi, type ShopSimpleVO } from '@/api/shop'
import { usePermission } from '@/utils/permission'
const router = useRouter()
const formRef = ref<FormInstance>()
const loading = ref(false)
const shopLoading = ref(false)
const shops = ref<ShopSimpleVO[]>([])
const { loadUserPermissions } = usePermission()
const formState = reactive({
account: '',
password: ''
password: '',
shopId: undefined as number | undefined
})
const shopOptions = computed(() =>
shops.value.map(shop => ({
label: shop.name,
value: shop.id
}))
)
const formRules = {
account: [
{ required: true, message: '请输入用户名', trigger: 'blur' },
{ pattern: /^[a-zA-Z0-9_\-@.]+$/, message: '账号只能包含数字、字母、下划线、连字符、@和点号', trigger: 'blur' }
],
password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
shopId: [{ required: true, message: '请选择登录店铺', trigger: 'change' }]
} as any
const filterShopOption = (input: string, option: { label?: string | number } | undefined) =>
String(option?.label ?? '').toLowerCase().includes(input.toLowerCase())
const restoreLoginShop = () => {
const cached = localStorage.getItem('adminUserInfo')
if (!cached) {
return
}
try {
const parsed = JSON.parse(cached)
if (parsed?.shopId) {
formState.shopId = parsed.shopId
}
} catch (error) {
console.error('恢复后台登录店铺失败:', error)
}
}
const loadShops = async () => {
shopLoading.value = true
try {
shops.value = await shopApi.getShopsSimple() as unknown as ShopSimpleVO[]
if (!formState.shopId && shops.value.length === 1) {
const [onlyShop] = shops.value
if (onlyShop) {
formState.shopId = onlyShop.id
}
}
} catch (error) {
console.error('获取登录店铺列表失败:', error)
message.error('店铺列表加载失败,请刷新后重试')
} finally {
shopLoading.value = false
}
}
const handleLogin = async () => {
try {
await formRef.value?.validate()
@@ -88,7 +149,8 @@ const handleLogin = async () => {
const res = await adminAuthApi.login({
account: formState.account,
password: formState.password
password: formState.password,
shopId: formState.shopId as number
})
@@ -116,6 +178,11 @@ const handleLogin = async () => {
}
}
onMounted(() => {
restoreLoginShop()
loadShops()
})
const goToFrontend = () => {
localStorage.removeItem('adminAccessToken')
localStorage.removeItem('adminRefreshToken')