From ae27fc8951fa576acbe6deb7caf5396e4a05b660 Mon Sep 17 00:00:00 2001 From: Quit <56326802+1451544363@users.noreply.github.com> Date: Wed, 1 May 2024 17:36:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=97=E5=85=B8=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/src/app/api/sys.ts | 7 +++ admin/src/main.ts | 2 + admin/src/stores/modules/dict.ts | 60 +++++++++++++++++++ admin/src/utils/dict.ts | 25 ++++++++ .../app/adminapi/controller/dict/Dict.php | 10 ++++ niucloud/app/adminapi/route/sys.php | 3 + .../app/service/admin/dict/DictService.php | 26 ++++++++ 7 files changed, 133 insertions(+) create mode 100644 admin/src/stores/modules/dict.ts create mode 100644 admin/src/utils/dict.ts diff --git a/admin/src/app/api/sys.ts b/admin/src/app/api/sys.ts index 796c200d1..bffc0434b 100644 --- a/admin/src/app/api/sys.ts +++ b/admin/src/app/api/sys.ts @@ -700,3 +700,10 @@ export function getDeveloperToken() { export function setDeveloperToken(params: Record) { return request.put(`sys/config/developer_token`, params, { showSuccessMessage: true }) } + +/** + * 获取字典数据 + */ +export function getDictData(key: string) { + return request.get('sys/dict/' + key) +} \ No newline at end of file diff --git a/admin/src/main.ts b/admin/src/main.ts index 708a19ac9..b63f6e501 100644 --- a/admin/src/main.ts +++ b/admin/src/main.ts @@ -9,11 +9,13 @@ import { useElementIcon } from './utils/common' import 'highlight.js/styles/stackoverflow-light.css'; import hljs from 'highlight.js/lib/common' import hljsVuePlugin from '@highlightjs/vue-plugin' +import { useDict } from './utils/dict' window.hl = hljs async function run() { const app = createApp(App) + app.config.globalProperties.useDict = useDict app.use(pinia) app.use(lang) app.use(roter) diff --git a/admin/src/stores/modules/dict.ts b/admin/src/stores/modules/dict.ts new file mode 100644 index 000000000..53a4effbd --- /dev/null +++ b/admin/src/stores/modules/dict.ts @@ -0,0 +1,60 @@ +import { defineStore } from 'pinia' + + +const useDictStore = defineStore( + 'dict', + { + state: () => ({ + dict: [] as any[] + }), + actions: { + // 获取字典 + getDict(_key: string) { + if (_key == null && _key == "") { + return null; + } + try { + for (let i = 0; i < this.dict.length; i++) { + if (this.dict[i].key == _key) { + return this.dict[i].value; + } + } + } catch (e) { + return null; + } + }, + // 设置字典 + setDict(_key:string, value:any) { + if (_key !== null && _key !== "") { + this.dict.push({ + key: _key, + value: value + }); + } + }, + // 删除字典 + removeDict(_key:string) { + var bln = false; + try { + for (let i = 0; i < this.dict.length; i++) { + if (this.dict[i].key == _key) { + this.dict.splice(i, 1); + return true; + } + } + } catch (e) { + bln = false; + } + return bln; + }, + // 清空字典 + cleanDict() { + this.dict = []; + }, + // 初始字典 + initDict() { + } + } + }) + +export default useDictStore diff --git a/admin/src/utils/dict.ts b/admin/src/utils/dict.ts new file mode 100644 index 000000000..cecc56f15 --- /dev/null +++ b/admin/src/utils/dict.ts @@ -0,0 +1,25 @@ +import useDictStore from '@/stores/modules/dict' +import { getDictData } from '@/app/api/sys' +import { ref, toRefs } from 'vue'; + +/** + * 获取字典数据 + */ +export function useDict(...args: string[]) { + const res = ref({}); + return (() => { + args.forEach((dictType: string, index: any) => { + res.value[dictType] = []; + const dicts = useDictStore().getDict(dictType); + if (dicts) { + res.value[dictType] = dicts; + } else { + getDictData(dictType).then(resp => { + res.value[dictType] = resp.data + useDictStore().setDict(dictType, res.value[dictType]); + }) + } + }) + return toRefs(res.value); + })() +} \ No newline at end of file diff --git a/niucloud/app/adminapi/controller/dict/Dict.php b/niucloud/app/adminapi/controller/dict/Dict.php index 5a07fd3e9..076346772 100644 --- a/niucloud/app/adminapi/controller/dict/Dict.php +++ b/niucloud/app/adminapi/controller/dict/Dict.php @@ -116,4 +116,14 @@ class Dict extends BaseAdminController $res = (new DictService())->getKeyInfo($type); return success($res); } + + + /** + * 获取字典数据 缓存 + */ + public function getDict(string $key){ + $res = (new DictService)->getDictData($key); + return success($res); + } + } diff --git a/niucloud/app/adminapi/route/sys.php b/niucloud/app/adminapi/route/sys.php index 8addde494..8e2a26999 100644 --- a/niucloud/app/adminapi/route/sys.php +++ b/niucloud/app/adminapi/route/sys.php @@ -220,4 +220,7 @@ Route::group('sys', function () { Route::get('web/website', 'sys.Config/getWebsite'); // 获取版权信息 Route::get('web/copyright', 'sys.Config/getCopyright'); + // 获取字典 + Route::get('dict/:key', 'dict.Dict/getDictDataCache'); + }); diff --git a/niucloud/app/service/admin/dict/DictService.php b/niucloud/app/service/admin/dict/DictService.php index 9660c3f0e..757e59693 100644 --- a/niucloud/app/service/admin/dict/DictService.php +++ b/niucloud/app/service/admin/dict/DictService.php @@ -13,6 +13,7 @@ namespace app\service\admin\dict; use app\model\dict\Dict; use core\base\BaseAdminService; +use think\facade\Cache; /** @@ -69,6 +70,7 @@ class DictService extends BaseAdminService { $res = $this->model->create($data); + Cache::tag('sys_dict')->clear(); return $res->id; } @@ -83,6 +85,7 @@ class DictService extends BaseAdminService { $data['update_time'] = time(); $this->model->where([['id', '=', $id]])->update($data); + Cache::tag('sys_dict')->clear(); return true; } @@ -94,6 +97,7 @@ class DictService extends BaseAdminService public function del(int $id) { $res = $this->model->where([['id', '=', $id]])->delete(); + Cache::tag('sys_dict')->clear(); return $res; } @@ -122,4 +126,26 @@ class DictService extends BaseAdminService } + public function getDictData($key) + { + + $cache_name = 'dict_' . __FUNCTION__ . '_' . serialize(func_get_args()); + + $cache = Cache::get($cache_name); + + if (!empty($cache)) return $cache; + + $value = $this->model->field('dictionary')->where('key', $key)->column('dictionary'); + + if (empty($value)){ + $list = []; + }else{ + $list = array_column($value,null,'value'); + } + + Cache::tag('sys_dict')->set($cache_name, $list, 24 * 3600); + + return $list; + } + } -- Gitee