功能:完善门店能力与用户安全配置
This commit is contained in:
@@ -300,7 +300,7 @@
|
||||
:key="artist.id"
|
||||
:value="artist.id.toString()"
|
||||
>
|
||||
{{ artist.nickname }}
|
||||
{{ artist.nickname }}{{ artist.type === 2 ? '(店长 · 参与制作)' : '' }}
|
||||
</a-select-option>
|
||||
</a-select>
|
||||
</a-form-item>
|
||||
@@ -453,7 +453,7 @@
|
||||
:key="artist?.id"
|
||||
:value="artist?.id?.toString()"
|
||||
>
|
||||
{{ artist?.nickname || '' }}
|
||||
{{ artist?.nickname || '' }}{{ artist?.type === 2 ? '(店长 · 参与制作)' : '' }}
|
||||
</a-select-option>
|
||||
</a-select>
|
||||
</a-form-item>
|
||||
@@ -485,6 +485,10 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
/**
|
||||
* ProductsView page script.
|
||||
* Call guide: the template binds the reactive state below, and the methods are triggered by button clicks, lifecycle hooks, or router events.
|
||||
*/
|
||||
import { computed, ref, reactive, onMounted, watch } from 'vue'
|
||||
import { message } from 'ant-design-vue'
|
||||
import { PlusOutlined } from '@ant-design/icons-vue'
|
||||
@@ -506,33 +510,48 @@ import AdminResponsiveModal from '@/components/admin/AdminResponsiveModal.vue'
|
||||
|
||||
const { hasPermission, loadUserPermissions } = usePermission()
|
||||
const { isMobile } = useAdminViewport()
|
||||
/** loading is a reactive state variable used by the template and event handlers. */
|
||||
const loading = ref(false)
|
||||
/** searchText is a reactive state variable used by the template and event handlers. */
|
||||
const searchText = ref('')
|
||||
const productList = ref<Product[]>([])
|
||||
/** modalVisible is a reactive state variable used by the template and event handlers. */
|
||||
const modalVisible = ref(false)
|
||||
/** viewModalVisible is a reactive state variable used by the template and event handlers. */
|
||||
const viewModalVisible = ref(false)
|
||||
/** modalLoading is a reactive state variable used by the template and event handlers. */
|
||||
const modalLoading = ref(false)
|
||||
/** isEdit is a reactive state variable used by the template and event handlers. */
|
||||
const isEdit = ref(false)
|
||||
const currentProduct = ref<Product | null>(null)
|
||||
const shops = ref<Shop[]>([])
|
||||
/** loadingShops is a reactive state variable used by the template and event handlers. */
|
||||
const loadingShops = ref(false)
|
||||
const tags = ref<any[]>([])
|
||||
/** loadingTags is a reactive state variable used by the template and event handlers. */
|
||||
const loadingTags = ref(false)
|
||||
const artists = ref<any[]>([])
|
||||
/** loadingArtists is a reactive state variable used by the template and event handlers. */
|
||||
const loadingArtists = ref(false)
|
||||
const coupons = ref<any[]>([])
|
||||
/** loadingCoupons is a reactive state variable used by the template and event handlers. */
|
||||
const loadingCoupons = ref(false)
|
||||
/** previewVisible is a reactive state variable used by the template and event handlers. */
|
||||
const previewVisible = ref(false)
|
||||
/** previewImage is a reactive state variable used by the template and event handlers. */
|
||||
const previewImage = ref('')
|
||||
const imageFileList = ref<any[]>([])
|
||||
const uploadedFileIds = ref<string[]>([])
|
||||
const imageUrls = ref<Record<string, string[]>>({})
|
||||
const viewImageUrls = ref<string[]>([])
|
||||
const imageIdCache = ref<Record<string, string>>({})
|
||||
/** isAdmin is a reactive state variable used by the template and event handlers. */
|
||||
const isAdmin = ref(false)
|
||||
const formRef = ref<any>(null)
|
||||
const uploadTaskMap = new Map<string, ChunkUploadTask<any>>()
|
||||
/** activeUploadCount is a reactive state variable used by the template and event handlers. */
|
||||
const activeUploadCount = ref(0)
|
||||
/** isUploadPending is a reactive state variable used by the template and event handlers. */
|
||||
const isUploadPending = computed(() => activeUploadCount.value > 0)
|
||||
const draftSavedAt = ref<number | null>(null)
|
||||
const requestTokens = reactive({
|
||||
@@ -543,13 +562,16 @@ const requestTokens = reactive({
|
||||
products: 0
|
||||
})
|
||||
|
||||
/** nextRequestToken is a local helper used by this page script. */
|
||||
const nextRequestToken = (key: keyof typeof requestTokens) => {
|
||||
requestTokens[key] += 1
|
||||
return requestTokens[key]
|
||||
}
|
||||
|
||||
/** isLatestRequestToken is a local helper used by this page script. */
|
||||
const isLatestRequestToken = (key: keyof typeof requestTokens, token: number) => requestTokens[key] === token
|
||||
|
||||
/** formatDuration is a local helper used by this page script. */
|
||||
const formatDuration = (value?: string | number | null) => {
|
||||
if (value === undefined || value === null || value === '') {
|
||||
return '--'
|
||||
@@ -595,6 +617,7 @@ const formState = reactive({
|
||||
availableCoupons: [] as string[]
|
||||
})
|
||||
|
||||
/** validateInteractionCount is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
||||
const validateInteractionCount = async (_rule: any, value: number | undefined) => {
|
||||
if (value === undefined || value === null) {
|
||||
return Promise.resolve()
|
||||
@@ -629,12 +652,14 @@ const columns = [
|
||||
{ title: '操作', key: 'actions', width: 150, fixed: 'right' as const }
|
||||
]
|
||||
|
||||
/** getShopName is a local helper used by this page script. */
|
||||
const getShopName = (shopId: IdType): string => {
|
||||
const normalizedShopId = normalizeShopIdValue(shopId)
|
||||
const shop = shops.value.find(s => normalizeShopIdValue(s.id) === normalizedShopId)
|
||||
return shop ? shop.shopName : '未知店铺'
|
||||
}
|
||||
|
||||
/** normalizeShopIdValue is a local helper used by this page script. */
|
||||
const normalizeShopIdValue = (value: string | number | undefined | null) => {
|
||||
if (value === undefined || value === null || value === '') {
|
||||
return ''
|
||||
@@ -642,6 +667,7 @@ const normalizeShopIdValue = (value: string | number | undefined | null) => {
|
||||
return String(value)
|
||||
}
|
||||
|
||||
/** normalizeInteractionCount is a local helper used by this page script. */
|
||||
const normalizeInteractionCount = (value: unknown) => {
|
||||
const numericValue = Number(value)
|
||||
if (!Number.isFinite(numericValue) || numericValue < 0) {
|
||||
@@ -650,6 +676,7 @@ const normalizeInteractionCount = (value: unknown) => {
|
||||
return Math.floor(numericValue)
|
||||
}
|
||||
|
||||
/** getCurrentUserInfo is a local helper used by this page script. */
|
||||
const getCurrentUserInfo = () => {
|
||||
try {
|
||||
const userInfoStr = localStorage.getItem('adminUserInfo')
|
||||
@@ -665,13 +692,16 @@ const getCurrentUserInfo = () => {
|
||||
return null
|
||||
}
|
||||
|
||||
/** canManageAvailableCoupons is a local helper used by this page script. */
|
||||
const canManageAvailableCoupons = () => {
|
||||
const userInfo = getCurrentUserInfo()
|
||||
return Boolean(userInfo && (userInfo.type === 2 || userInfo.type === 4))
|
||||
}
|
||||
|
||||
/** isCouponSelectionDisabled is a reactive state variable used by the template and event handlers. */
|
||||
const isCouponSelectionDisabled = computed(() => !canManageAvailableCoupons())
|
||||
|
||||
/** fetchShops is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
||||
const fetchShops = async () => {
|
||||
const requestToken = nextRequestToken('shops')
|
||||
loadingShops.value = true
|
||||
@@ -712,6 +742,7 @@ const fetchShops = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
/** fetchTags is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
||||
const fetchTags = async (shopId?: IdType) => {
|
||||
const requestToken = nextRequestToken('tags')
|
||||
loadingTags.value = true
|
||||
@@ -736,6 +767,7 @@ const fetchTags = async (shopId?: IdType) => {
|
||||
}
|
||||
}
|
||||
|
||||
/** fetchArtists is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
||||
const fetchArtists = async (shopId?: IdType) => {
|
||||
const requestToken = nextRequestToken('artists')
|
||||
loadingArtists.value = true
|
||||
@@ -755,6 +787,7 @@ const fetchArtists = async (shopId?: IdType) => {
|
||||
}
|
||||
}
|
||||
|
||||
/** syncAvailableCouponSelection is a local helper used by this page script. */
|
||||
const syncAvailableCouponSelection = () => {
|
||||
const availableCouponIdSet = new Set(
|
||||
coupons.value
|
||||
@@ -765,6 +798,7 @@ const syncAvailableCouponSelection = () => {
|
||||
formState.availableCoupons = formState.availableCoupons.filter(couponId => availableCouponIdSet.has(String(couponId)))
|
||||
}
|
||||
|
||||
/** fetchCoupons is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
||||
const fetchCoupons = async (shopId?: IdType) => {
|
||||
if (!shopId) {
|
||||
nextRequestToken('coupons')
|
||||
@@ -793,6 +827,7 @@ const fetchCoupons = async (shopId?: IdType) => {
|
||||
}
|
||||
}
|
||||
|
||||
/** fetchProductList is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
||||
const fetchProductList = async () => {
|
||||
const requestToken = nextRequestToken('products')
|
||||
loading.value = true
|
||||
@@ -829,6 +864,7 @@ const fetchProductList = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
/** loadImageUrls is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
||||
const loadImageUrls = async (products: Product[], requestToken?: number) => {
|
||||
const nextImageUrls: Record<string, string[]> = {}
|
||||
for (const product of products) {
|
||||
@@ -870,6 +906,7 @@ const loadImageUrls = async (products: Product[], requestToken?: number) => {
|
||||
return nextImageUrls
|
||||
}
|
||||
|
||||
/** getProductImages is a local helper used by this page script. */
|
||||
const getProductImages = (product: Product | Record<string, any>): string[] => {
|
||||
const productId = 'id' in product ? product.id : (product as any).id
|
||||
if (productId !== undefined) {
|
||||
@@ -878,17 +915,20 @@ const getProductImages = (product: Product | Record<string, any>): string[] => {
|
||||
return [DEFAULT_PRODUCT_IMAGE]
|
||||
}
|
||||
|
||||
/** handleSearch is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
||||
const handleSearch = async () => {
|
||||
pagination.current = 1
|
||||
await runWithGlobalLoading(() => fetchProductList())
|
||||
}
|
||||
|
||||
/** handleTableChange is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
||||
const handleTableChange = async (pag: any) => {
|
||||
pagination.current = pag.current
|
||||
pagination.pageSize = pag.pageSize
|
||||
await runWithGlobalLoading(() => fetchProductList())
|
||||
}
|
||||
|
||||
/** showAddModal is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
||||
const showAddModal = async () => {
|
||||
isEdit.value = false
|
||||
currentProduct.value = null
|
||||
@@ -918,6 +958,7 @@ const showAddModal = async () => {
|
||||
modalVisible.value = true
|
||||
}
|
||||
|
||||
/** showEditModal is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
||||
const showEditModal = async (record: Product | Record<string, any>) => {
|
||||
isEdit.value = true
|
||||
currentProduct.value = record as Product
|
||||
@@ -976,6 +1017,7 @@ const showEditModal = async (record: Product | Record<string, any>) => {
|
||||
modalVisible.value = true
|
||||
}
|
||||
|
||||
/** showViewModal is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
||||
const showViewModal = async (record: Product | Record<string, any>) => {
|
||||
const recordShopId = 'shopId' in record ? record.shopId : (record as any).shopId
|
||||
const recordTitle = 'title' in record ? record.title : (record as any).title
|
||||
@@ -1044,17 +1086,21 @@ const showViewModal = async (record: Product | Record<string, any>) => {
|
||||
viewModalVisible.value = true
|
||||
}
|
||||
|
||||
/** handleViewModalCancel is a local helper used by this page script. */
|
||||
const handleViewModalCancel = () => {
|
||||
viewModalVisible.value = false
|
||||
viewImageUrls.value = []
|
||||
}
|
||||
|
||||
/** cancelAllPendingUploads is a local helper used by this page script. */
|
||||
const cancelAllPendingUploads = () => {
|
||||
uploadTaskMap.forEach(task => task.cancel())
|
||||
}
|
||||
|
||||
/** getDraftKey is a local helper used by this page script. */
|
||||
const getDraftKey = () => buildDraftStorageKey('admin-products', isEdit.value ? 'edit' : 'create', currentProduct.value?.id ?? 'new')
|
||||
|
||||
/** buildDraftPayload is a local helper used by this page script. */
|
||||
const buildDraftPayload = () => ({
|
||||
formState: {
|
||||
shopId: formState.shopId,
|
||||
@@ -1072,6 +1118,7 @@ const buildDraftPayload = () => ({
|
||||
uploadedFileIds: [...uploadedFileIds.value]
|
||||
})
|
||||
|
||||
/** buildImageFileListByIds is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
||||
const buildImageFileListByIds = async (ids: string[]) => {
|
||||
const fileList = []
|
||||
for (const [index, id] of ids.entries()) {
|
||||
@@ -1101,6 +1148,7 @@ const buildImageFileListByIds = async (ids: string[]) => {
|
||||
return fileList
|
||||
}
|
||||
|
||||
/** restoreDraft is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
||||
const restoreDraft = async () => {
|
||||
const draft = loadDraftRecord<ReturnType<typeof buildDraftPayload>>(getDraftKey())
|
||||
if (!draft) {
|
||||
@@ -1117,6 +1165,7 @@ const restoreDraft = async () => {
|
||||
message.success('已恢复暂存内容')
|
||||
}
|
||||
|
||||
/** handleSaveDraft is a local helper used by this page script. */
|
||||
const handleSaveDraft = () => {
|
||||
if (isUploadPending.value) {
|
||||
message.warning('图片上传完成后才能暂存')
|
||||
@@ -1127,6 +1176,7 @@ const handleSaveDraft = () => {
|
||||
message.success('已暂存到本地')
|
||||
}
|
||||
|
||||
/** handleClearDraft is a local helper used by this page script. */
|
||||
const handleClearDraft = (showMessage = true) => {
|
||||
clearDraftRecord(getDraftKey())
|
||||
draftSavedAt.value = null
|
||||
@@ -1135,11 +1185,13 @@ const handleClearDraft = (showMessage = true) => {
|
||||
}
|
||||
}
|
||||
|
||||
/** syncUploadedFileIds is a local helper used by this page script. */
|
||||
const syncUploadedFileIds = (fileIds: string[]) => {
|
||||
uploadedFileIds.value = Array.from(new Set(fileIds.filter(id => Boolean(id))))
|
||||
formState.imageFileIds = uploadedFileIds.value.join(',')
|
||||
}
|
||||
|
||||
/** getUploadResponse is a local helper used by this page script. */
|
||||
const getUploadResponse = (file: any) => {
|
||||
if (!file || typeof file !== 'object') {
|
||||
return null
|
||||
@@ -1147,6 +1199,7 @@ const getUploadResponse = (file: any) => {
|
||||
return file.response || null
|
||||
}
|
||||
|
||||
/** resolveUploadedFileId is a local helper used by this page script. */
|
||||
const resolveUploadedFileId = (file: any) => {
|
||||
const response = getUploadResponse(file)
|
||||
const responseId = response?.id ?? response?.data?.id ?? file?.fileId
|
||||
@@ -1167,6 +1220,7 @@ const resolveUploadedFileId = (file: any) => {
|
||||
return ''
|
||||
}
|
||||
|
||||
/** getPreviewUrlFromFile is a local helper used by this page script. */
|
||||
const getPreviewUrlFromFile = (file: any) => {
|
||||
const fileUrl = typeof file?.url === 'string' ? file.url : ''
|
||||
if (fileUrl && !/^\d+$/.test(fileUrl)) {
|
||||
@@ -1181,6 +1235,7 @@ const getPreviewUrlFromFile = (file: any) => {
|
||||
return ''
|
||||
}
|
||||
|
||||
/** beforeUpload is a local helper used by this page script. */
|
||||
const beforeUpload = (file: File) => {
|
||||
const isImage = file.type.startsWith('image/')
|
||||
if (!isImage) {
|
||||
@@ -1190,6 +1245,7 @@ const beforeUpload = (file: File) => {
|
||||
}
|
||||
|
||||
// 作品图片走分片上传,并在上传前做压缩兜底,保证大图也能稳定进入对象存储。
|
||||
/** handleUploadImage is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
||||
const handleUploadImage = async (options: any) => {
|
||||
const { file, onSuccess, onError, onProgress } = options
|
||||
let successMessage = '图片上传成功'
|
||||
@@ -1247,6 +1303,7 @@ const handleUploadImage = async (options: any) => {
|
||||
}
|
||||
|
||||
// Ant Upload 的文件对象来源很多,预览时优先复用已有 URL,缺失时再回源签名地址。
|
||||
/** handlePreview is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
||||
const handlePreview = async (file: any) => {
|
||||
try {
|
||||
const cachedPreviewUrl = getPreviewUrlFromFile(file)
|
||||
@@ -1274,6 +1331,7 @@ const handlePreview = async (file: any) => {
|
||||
}
|
||||
}
|
||||
|
||||
/** handleRemove is a local helper used by this page script. */
|
||||
const handleRemove = (file: any) => {
|
||||
const uploadTask = uploadTaskMap.get(String(file.uid))
|
||||
if (uploadTask) {
|
||||
@@ -1289,6 +1347,7 @@ const handleRemove = (file: any) => {
|
||||
return true
|
||||
}
|
||||
|
||||
/** handleModalOk is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
||||
const handleModalOk = async () => {
|
||||
try {
|
||||
if (isUploadPending.value) {
|
||||
@@ -1341,12 +1400,14 @@ const handleModalOk = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
/** handleModalCancel is a local helper used by this page script. */
|
||||
const handleModalCancel = () => {
|
||||
cancelAllPendingUploads()
|
||||
modalVisible.value = false
|
||||
formRef.value?.resetFields()
|
||||
}
|
||||
|
||||
/** handleDelete is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
||||
const handleDelete = async (record: Product | Record<string, any>) => {
|
||||
try {
|
||||
await runWithGlobalLoading(async () => {
|
||||
@@ -1363,6 +1424,7 @@ const handleDelete = async (record: Product | Record<string, any>) => {
|
||||
}
|
||||
}
|
||||
|
||||
/** handleSelectAllCoupons is a local helper used by this page script. */
|
||||
const handleSelectAllCoupons = () => {
|
||||
if (isCouponSelectionDisabled.value) {
|
||||
return
|
||||
@@ -1373,6 +1435,7 @@ const handleSelectAllCoupons = () => {
|
||||
.map(id => String(id))
|
||||
}
|
||||
|
||||
/** handleClearAvailableCoupons is a local helper used by this page script. */
|
||||
const handleClearAvailableCoupons = () => {
|
||||
formState.availableCoupons = []
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user