-
Notifications
You must be signed in to change notification settings - Fork 902
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
127 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { http, HttpResponse, delay } from 'msw'; | ||
import { getQuery, resultSuccess, serverApi } from '../_util'; | ||
|
||
const getDictData = (dictType: string) => { | ||
if (dictType === 'gender') { | ||
return [ | ||
{ | ||
label: '男', | ||
value: 1, | ||
}, | ||
{ | ||
label: '女', | ||
value: 0, | ||
}, | ||
]; | ||
} else if (dictType === 'sell_status') { | ||
return [ | ||
{ | ||
label: '已售罄', | ||
value: 0, | ||
}, | ||
{ | ||
label: '热卖中', | ||
value: 1, | ||
}, | ||
]; | ||
} | ||
return []; | ||
}; | ||
|
||
export default [ | ||
http.get(serverApi('/dict/data'), async ({ request }) => { | ||
await delay(1800); | ||
const { type } = getQuery(request); | ||
return HttpResponse.json(resultSuccess(getDictData(type))); | ||
}), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { request } from '@/utils/request'; | ||
|
||
export type DictType = 'gender' | 'sell_status'; | ||
|
||
export async function getDictData(params: { type: DictType }) { | ||
return request<LabelValueOptions>({ | ||
url: '/dict/data', | ||
method: 'GET', | ||
params, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { ref } from 'vue'; | ||
import { defineStore } from 'pinia'; | ||
import { type DictType, getDictData } from '@/api/demo/dict'; | ||
|
||
export const useDictStore = defineStore('dict', () => { | ||
const dictMap = ref(new Map<string, LabelValueOptions>()); | ||
const dictValueLabelMap = ref(new Map<string, Map<any, string>>()); | ||
const dictPendingMap = ref(new Map<string, boolean>()); | ||
|
||
const fetchDict = (dictType: DictType | DictType[]) => { | ||
const dictTypes = Array.isArray(dictType) ? dictType : [dictType]; | ||
const promises = dictTypes.map(async (type) => { | ||
if (dictMap.value.has(type) && !dictPendingMap.value.has(type)) { | ||
return dictMap.value.get(type)!; | ||
} | ||
|
||
dictMap.value.set(type, []); | ||
dictPendingMap.value.set(type, true); | ||
const res = await getDictData({ type }).finally(() => dictPendingMap.value.delete(type)); | ||
dictMap.value.set(type, res); | ||
dictValueLabelMap.value.set(type, new Map(res.map((item) => [item.value, item.label]))); | ||
return res; | ||
}); | ||
return Promise.all(promises); | ||
}; | ||
|
||
const dictData = new Proxy({} as Record<DictType, LabelValueOptions>, { | ||
get(_, prop: DictType) { | ||
if (prop.startsWith('__v_')) { | ||
// console.trace('get', prop); | ||
return; | ||
} | ||
if (dictMap.value.has(prop)) { | ||
return dictMap.value.get(prop); | ||
} | ||
if (!dictPendingMap.value.has(prop)) { | ||
fetchDict(prop); | ||
} | ||
return dictMap.value.get(prop); | ||
}, | ||
}); | ||
|
||
const showDictLabel = (dictType: DictType, code: any) => { | ||
dictData[dictType]; | ||
return dictValueLabelMap.value.get(dictType)?.get(code) || ''; | ||
}; | ||
|
||
const dictPending = new Proxy({} as Record<DictType, boolean>, { | ||
get(_, prop: DictType) { | ||
return dictPendingMap.value.get(prop) || false; | ||
}, | ||
}); | ||
|
||
return { dictData, dictPending, fetchDict, showDictLabel }; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters