| ¶Ô±ÈÐÂÎļþ |
| | |
| | | import type { ImFacePackApi } from '#/api/im/face/pack' |
| | | import type { ImFaceUserItemApi } from '#/api/im/face/userItem' |
| | | |
| | | import { ref } from 'vue' |
| | | |
| | | import { acceptHMRUpdate, defineStore } from 'pinia' |
| | | |
| | | import { getFacePackList as apiGetFacePackList } from '#/api/im/face/pack' |
| | | import { createFaceUserItem as apiCreateFaceUserItem, deleteFaceUserItem as apiDeleteFaceUserItem, getFaceUserItemList as apiGetFaceUserItemList } from '#/api/im/face/userItem' |
| | | |
| | | /** |
| | | * IM 表æ
颿¿æ°æ® storeï¼ç³»ç»è¡¨æ
å
+ 个人表æ
ï¼ |
| | | * |
| | | * 䏿ä¹
åï¼æ°æ®å°ãä½é¢ï¼æ¯æ¬¡è¿ IM ç¬¬ä¸æ¬¡æå¼è¡¨æ
颿¿æ¶æéæï¼å
³ tab å³ä¸¢å¼ |
| | | * - ç³»ç»å
ï¼IM 主页 onMounted åå°é¢æï¼ä¸é»å¡é¦å±ï¼ï¼æ¶é¤é¢æ¿é¦æ¬¡å±å¼çç½å± |
| | | * - 个人表æ
ï¼åå°ãæ¶èãtab / é¿ææ¶æ¯ãæ·»å å°è¡¨æ
ãæ¶æéæ |
| | | */ |
| | | export const useFaceStore = defineStore('imFace', () => { |
| | | |
| | | /** ç³»ç»è¡¨æ
å
å表ï¼å«æ¯ä¸ªå
ç itemsï¼ï¼è¿è¥ç®¡çåå°ç»´æ¤ */ |
| | | const facePacks = ref<ImFacePackApi.FacePackUser[]>([]) |
| | | /** 个人表æ
å
å表ï¼ç¨æ·é¿æãæ·»å å°è¡¨æ
ã/ ä¸ä¼ 产çï¼ */ |
| | | const faceUserItems = ref<ImFaceUserItemApi.FaceUserItem[]>([]) |
| | | |
| | | /** clear() æ¶éå¢ï¼æ§è´¦å·è¯·æ±è¿ååä¸åå
¥æ°è´¦å·å
å */ |
| | | let storeEpoch = 0 |
| | | |
| | | /** |
| | | * ç³»ç»è¡¨æ
å
æå promiseï¼ensureFacePackList å
cacheï¼ |
| | | * - null = è¿æ²¡æè¿ï¼ä¸æ¬¡è°ç¨çåè¯·æ± |
| | | * - resolve åä¿ç对象 = åç»è°ç¨ await ç«å³è¿åï¼ä¸ååè¯·æ± |
| | | * - reject åç½®å nullï¼è®©è°ç¨æ¹ä¸æ¬¡éè¯ |
| | | */ |
| | | let facePacksPromise: null | Promise<void> = null |
| | | /** æéæåç³»ç»è¡¨æ
å
ï¼å·²æè¿åç´æ¥å¤ç¨ cached promiseï¼ */ |
| | | async function ensureFacePackList(): Promise<void> { |
| | | if (!facePacksPromise) { |
| | | const requestEpoch = storeEpoch |
| | | facePacksPromise = apiGetFacePackList() |
| | | .then((data) => { |
| | | if (requestEpoch !== storeEpoch) { |
| | | return |
| | | } |
| | | facePacks.value = data || [] |
| | | }) |
| | | .catch((error) => { |
| | | console.warn('[IM] æå表æ
å
失败', error) |
| | | if (requestEpoch === storeEpoch) { |
| | | facePacksPromise = null |
| | | } |
| | | throw error |
| | | }) |
| | | } |
| | | return facePacksPromise |
| | | } |
| | | |
| | | /** 个人表æ
æå promiseï¼è¯ä¹åä¸ */ |
| | | let faceUserItemsPromise: null | Promise<void> = null |
| | | /** æéæå个人表æ
ï¼å·²æè¿åç´æ¥å¤ç¨ cached promiseï¼ */ |
| | | async function ensureFaceUserItemList(): Promise<void> { |
| | | if (!faceUserItemsPromise) { |
| | | const requestEpoch = storeEpoch |
| | | faceUserItemsPromise = apiGetFaceUserItemList() |
| | | .then((data) => { |
| | | if (requestEpoch !== storeEpoch) { |
| | | return |
| | | } |
| | | faceUserItems.value = data || [] |
| | | }) |
| | | .catch((error) => { |
| | | console.warn('[IM] æå个人表æ
失败', error) |
| | | if (requestEpoch === storeEpoch) { |
| | | faceUserItemsPromise = null |
| | | } |
| | | throw error |
| | | }) |
| | | } |
| | | return faceUserItemsPromise |
| | | } |
| | | |
| | | /** |
| | | * æ·»å 个人表æ
ï¼æå¡ç«¯å¯¹å URL æ FACE_USER_ITEM_DUPLICATED é误 |
| | | * |
| | | * æ¥æºï¼1. ç¨æ·å¨è¡¨æ
颿¿ã+ãä¸ä¼ å¾ç 2. é¿ææ¶æ¯ãæ·»å å°è¡¨æ
ã |
| | | */ |
| | | async function addFaceUserItem(reqVO: ImFaceUserItemApi.FaceUserItemSaveReqVO): Promise<boolean> { |
| | | const requestEpoch = storeEpoch |
| | | const id = await apiCreateFaceUserItem(reqVO) |
| | | if (!id) { |
| | | return false |
| | | } |
| | | // å·²åè´¦å·æ¶è·³è¿æ§è¯·æ±ç»æ |
| | | if (requestEpoch !== storeEpoch) { |
| | | return false |
| | | } |
| | | // id ä¸å¨ç¼åéææå
¥ï¼æå¡ç«¯å¯ä¸çº¦æå
åºäº raceï¼æ¬å°ç论ä¸ä¸ä¼æ¿å°éå¤ id |
| | | if (!faceUserItems.value.some((item) => item.id === id)) { |
| | | faceUserItems.value.unshift({ |
| | | id, |
| | | url: reqVO.url, |
| | | name: reqVO.name, |
| | | width: reqVO.width, |
| | | height: reqVO.height |
| | | }) |
| | | } |
| | | return true |
| | | } |
| | | |
| | | /** å é¤ä¸ªäººè¡¨æ
ï¼æ¬å°ç«å³ç§»é¤ */ |
| | | async function removeFaceUserItem(id: number): Promise<boolean> { |
| | | const requestEpoch = storeEpoch |
| | | try { |
| | | await apiDeleteFaceUserItem(id) |
| | | // å·²åè´¦å·æ¶è·³è¿æ§è¯·æ±ç»æ |
| | | if (requestEpoch !== storeEpoch) { |
| | | return false |
| | | } |
| | | faceUserItems.value = faceUserItems.value.filter((item) => item.id !== id) |
| | | return true |
| | | } catch (error) { |
| | | console.warn('[IM] å é¤ä¸ªäººè¡¨æ
失败', { id }, error) |
| | | return false |
| | | } |
| | | } |
| | | |
| | | /** æ¸
空表æ
ç¼å */ |
| | | function clear(): void { |
| | | facePacks.value = [] |
| | | faceUserItems.value = [] |
| | | facePacksPromise = null |
| | | faceUserItemsPromise = null |
| | | storeEpoch++ |
| | | } |
| | | |
| | | return { |
| | | facePacks, |
| | | faceUserItems, |
| | | ensureFacePackList, |
| | | ensureFaceUserItemList, |
| | | addFaceUserItem, |
| | | removeFaceUserItem, |
| | | clear |
| | | } |
| | | }) |
| | | |
| | | /** å¨ setup å¤ï¼è·¯ç±å®å«çï¼å store å®ä¾çå·¥å
·æ¹æ³ */ |
| | | export const useFaceStoreWithOut = () => useFaceStore() |
| | | |
| | | if (import.meta.hot) { |
| | | import.meta.hot.accept(acceptHMRUpdate(useFaceStore, import.meta.hot)) |
| | | } |