vue3-element-dict 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,387 @@
1
+ # vue3-element-dict
2
+
3
+ ​ vue3-element-dict是在vue3框架下对element-plus组件库的部分组件进行二开,实现更轻松使用字典数据的字典包插件。引入此包后,可轻松实现select下拉选项,radio单选框,checkbox多选框,cascader联级选项,tree树形控件,tree-select树形选项,table-column等组件。拥有多种字典相关方法及日期格式化,脱敏等方法。
4
+
5
+ **示例:实现select**
6
+
7
+ ```vue
8
+ <el-select-dict @dictChange="dictChange" placeholder="请选择性别" clearable v-model="value" dictType="SEX"></el-select-dict>
9
+ ```
10
+
11
+ **效果如下**
12
+
13
+ ![el-select-dict展示效果](http://qiniu.xiaobusoft.com/npm/select-dict.gif)
14
+
15
+ **示例2:实现cascader**
16
+
17
+ ```vue
18
+ <el-cascader-dict dictType="area" placeholder="请选择地区" clearable :props="props" v-model="value" @dictChange="handleDictChange"></el-cascader-dict>
19
+ ```
20
+
21
+ ![el-cascader-dict组件效果](http://qiniu.xiaobusoft.com/npm/cascader-dict.gif)
22
+
23
+ **此处主要讲包的配置及准备工作,具体用法可前往[vue3-element-dict官网](http://xiaobusoft.com/vue3-element-dict)查看使用文档,[备用官网](https://shenxiaobu.github.io/vue3-element-dict/),如有问题可前往[问题反馈表格进行记录](https://docs.qq.com/sheet/DVmZQb0hyTk9uc1dY), 也可关注微信公众号【爆米花小布】私信进行反馈**
24
+
25
+ ## 使用条件
26
+
27
+ 1. vue3版本
28
+ 2. 引入element-plus相关组件及样式
29
+ 3. 一个请求字典的接口
30
+ 1. 不传参可获取全部字典数据
31
+ 2. 返回字典包的版本号(字典数据更改时,更改字典包版本号,用于清除在浏览器上缓存的旧的字典包数据)
32
+
33
+ ## 快速开始
34
+
35
+ 以下全部已ts为示例,且以最完美的形式配合vue3-element-dict包的使用。
36
+
37
+ #### 后端接口要求
38
+
39
+ 1. 获取全局配置接口,配置中包含当前字典版本号。对字典数据进行操作时,需修改字典版本号。
40
+ 2. 获取字典数据接口,能接受不传参返回所有字典数据,可接受同时获取多个字典类型数据。并且将当前字典版本号返回,用于判断当前项目使用的字典数据是否是数据库最新版的字典数据。
41
+
42
+ #### 安装包
43
+
44
+ ```shell
45
+ npm install vue3-element-dict
46
+ ```
47
+
48
+ #### 新建配置文件
49
+
50
+ src目录下新建dict-config.ts文件,用于配置字典包获取字典的接口,当前字典版本号,数据字典缓存位置等。以下代码列全一点吧, 真实只有 getDictCodeApi 及 version必传
51
+
52
+ ```ts
53
+ //引入请求字典接口
54
+ import {getDictCodeApi} from "@/api/common-api"
55
+ //引入本地缓存数据
56
+ import localDictCodes from "@/assets/data/dict"
57
+
58
+ export default {
59
+ getDictCodeApi: getDictCodeApi, // 获取字典的接口 必传
60
+ version: "0.0.1", // 当前字典版本号 必传 获取全局配置化需 覆盖此版本号数据
61
+ localDictCodes, // 本地字典数据
62
+ versionKey: "xiaobuDictVersion", // 在浏览器缓存的版本号键名 选传 默认值vue3ElementDictVersion
63
+ dictDataKey: "xiaobuDictData", // 在浏览器缓存的字典数据键名 选传 默认值 vue3ElementDictData
64
+ query: "type", // 请求字典接口的参数名 选传 默认 dictType
65
+ format: {
66
+ value: "value",
67
+ label: "label",
68
+ disabled: "disabled",
69
+ type: "type",
70
+ color: "color"
71
+ }, // 配置字典值和显示 字段的配置项 需同时配置 不可只配一个 选传 默认值 label 及 value
72
+ formatterDictList: (data, query) => {
73
+ return data.xiaobuDictData
74
+ }, // data为请求字典接口后返回的data数据 格式化返回字典数据,此处return的为字典数据,用于兼容返回的数据结构与默认不一致问题, 选传 默认返回 data.dictData query为请求的字典类型的参数,用于部分接口不按要求,没返回 dictType: [{},{}] 数据结构的形式,此时可利用query自行拼凑成符合的数据格式
75
+ formatterDictVersion: (data) => {
76
+ return data.xiaobuDictVersion
77
+ }, // data为请求字典接口后返回的data数据 格式化返回版本号,用于兼容返回的数据结构与默认不一致问题 默认返回 data.version 获取到版本号后会 与字典包配置的版本号进行比对
78
+ filterDataFun: (list) => {
79
+ return list.filter(item => item.status === '1')
80
+ }, // 可对返回的字典数据进行过滤 list为某字典类型的数据 选传,默认不过滤 return list
81
+ disabledDataFun: (list) => {
82
+ return list.map(item => {
83
+ ...item,
84
+ disabled: item.isDisabled === '1'
85
+ })
86
+ }, // 可对返回的字典数据配置禁用字段 list为某字典类型的数据 选传,默认不过滤 return list
87
+ formatterRequest: (query, dictType) => {
88
+ // ...此处无法举例 以包默认形式展示
89
+ if (!dictType) {
90
+ return { [query]: "" };
91
+ }
92
+ return { [query]: dictType };
93
+ }, // 格式化请求体,用于兼容接口需要的参数格式,默认dictType为空时 获取全部字典数据,接口需要获取多种字典数据时不同后端开发人员可能需要的格式不一样,因此此处可配置成后端开发人员需要的格式。
94
+
95
+ storage: localstorage, //数据缓存的位置 默认 localstorage 可选为 sessionstorage 兼容iframe嵌套项目
96
+ treeSetting: {
97
+ labelField: "areaName" //label字段名 默认值id
98
+ parentIdField: "parentId", //父节点唯一标识字段名 默认值 parentId
99
+ childrenField: "children", //子节点标识字段名 默认值 children
100
+ firstId: "0", // 根节点值 默认值 字符串 0
101
+ labelField: "label", //label字段名 默认值 label
102
+ labelArrField: "labelArr", //给对象新增的中文数组字段名 默认值 labelArr 字典包会将数组转化为树形结构数据 并每一项生成 labelArr字段,内容为【爷爷级/父级label,...,自己的label】
103
+ idArrField: "idArr", //给对象新增的id数组字段名 默认值 idArr 字典包会将数组转化为树形结构数据 并每一项生成 labelArr字段,内容为【爷爷级/父级id,...,自己的id】
104
+ levelField: "level", //给对象新增的层级字段名 值为层级
105
+ level: 0, // 给根目录配置的层级 配置根目录层级为 0 级
106
+ leafField: "leaf" //叶子节点标识字段名 值为 true或 false
107
+ } // 对树形数据的配置 选传 字典支持将数组结构转化为树形结构
108
+ }
109
+ ```
110
+
111
+ ### 配置
112
+
113
+ src下新建文件dict-config.ts,用于配置字典包相关配置。配置项如下
114
+
115
+ | 字段 | 类型 | 必传 | 说明 | 默认值 |
116
+ | :----------------------: | :------: | :--: | :----------------------------------------------------------- | :----------------------------------------------------------: |
117
+ | getDictCodeApi | Promise | 是 | 请求字典接口 | — |
118
+ | version | String | 是 | 当前字典包版本号 必传 | — |
119
+ | localDictCodes | Object | 否 | 本地字典数据 | {} |
120
+ | versionKey | String | 否 | 在浏览器缓存的版本号键名 | vue3ElementDictVersion |
121
+ | dictDataKey | String | 否 | 在浏览器缓存的字典数据键名 | vue3ElementDictData |
122
+ | query | String | 否 | 请求字典接口的参数名 | dictType |
123
+ | format | Object | 否 | 配置字典值和显示 字段的配置项 需同时配置 不可只配一个 | {label: "label", value: "value", disabled: "disabled", type: "type", color: "color"} |
124
+ | formatterDictList | Function | 否 | data为请求字典接口后返回的data数据 格式化返回字典数据,此处return的为字典数据,用于兼容返回的数据结构与默认不一致问题, 选传 默认返回 data.dictData query为请求的字典类型的参数,用于部分接口不按要求,没返回 dictType: [{},{}] 数据结构的形式,此时可利用query自行拼凑成符合的数据格式 | (data) => {return data.dictData} |
125
+ | formatterDictVersion | Function | 否 | data为请求字典接口后返回的data数据 格式化返回版本号,用于兼容返回的数据结构与默认不一致问题 默认返回 data.version 获取到版本号后会 与字典包配置的版本号进行比对 | (data) => {return data.version} |
126
+ | filterDataFun | Function | 否 | 可对返回的字典数据进行过滤 list为某字典类型的数据 | (list) => {return list} |
127
+ | disabledDataFun | Function | 否 | 可对返回的字典数据配置禁用字段 list为某字典类型的数据 | (list) => {return list} |
128
+ | formatterRequest | Function | 否 | 兼格式化请求体,用于兼容接口需要的参数格式,默认dictType为空时 获取全部字典数据,接口需要获取多种字典数据时不同后端开发人员可能需要的格式不一样,因此此处可配置成后端开发人员需要的格式 | (query, dictType) => {if(!dictType){return { [query]: "" }} return { [query]: dictType }} |
129
+ | storage | storage | 否 | 数据缓存的位置 默认 localstorage 可选为 sessionstorage 兼容 | localstorage |
130
+ | treeSetting | Object | 否 | 树形相关数据配置 | 继续阅读文档 |
131
+ | isGetAll | Boolean | 否 | 获取所有字典类型接口 无传值获取所有字典接口,如果无传与真实需要获取全部字典接口的入参不一致 可配置 formatterRequest | false |
132
+ | usuallyGetDictTypes | String | 否 | 经常用到的字典数据,一进项目就会对缓存字典中无此类型的字典数据进行请求,此配置项在iframe项目中能发挥更大作用,进页面就一次性将全部字典数据一个接口请求掉,避免同时请求太多字典接口 选传 默认空 多个字典以 英文逗号隔开 如 sex,personType | "" |
133
+ | usuallyGetDictTypesByApi | Boolean | 否 | 是否必从接口中获取。 | false |
134
+
135
+
136
+ isGetAll为true时,每次进入项目都将会通过接口获取所有字典数据。
137
+
138
+ isGetAll为false,且usuallyGetDictTypes无配置时,则不做任何处理。
139
+
140
+ isGetAll为false,且usuallyGetDictTypes有配置字典类型时(多个时使用英文逗号分隔),如果缓存中存在的字典类型则不做处理,不存在的则会统一前往请求一次性获取。总之配置在usuallyGetDictTypes的字典类型必然会存在缓存中。
141
+
142
+ isGetAll为false,usuallyGetDictTypes有配置字典类型时(多个时使用英文逗号分隔),且配置了usuallyGetDictTypesByApi为true时,则会统一前往请求一次性获取usuallyGetDictTypes配置的字典类型并与旧缓存合并存储在缓存中。
143
+
144
+
145
+ **localDictCodes的格式及字典数据的的格式如下**
146
+
147
+ ```ts
148
+ export default {
149
+ SEX: [
150
+ {
151
+ value: "1",
152
+ label: "男"
153
+ },
154
+ {
155
+ value: "2",
156
+ label: "女"
157
+ },
158
+ {
159
+ value: "3",
160
+ label: "未知"
161
+ }
162
+ ],
163
+ niceOrBad: [
164
+ {
165
+ "value": "0",
166
+ "label": "好"
167
+ }, {
168
+ "value": "1",
169
+ "label": "差"
170
+ }
171
+ ],
172
+ area: [
173
+ {
174
+ "id": "110000",
175
+ "parentId": "0",
176
+ "label": "北京"
177
+ },
178
+ {
179
+ "id": "110100",
180
+ "parentId": "0",
181
+ "label": "北京市"
182
+ },
183
+ {
184
+ "id": "110101",
185
+ "parentId": "110100",
186
+ "areaName": "东城区"
187
+ },
188
+ {
189
+ "id": "110102",
190
+ "parentId": "110100",
191
+ "areaName": "西城区"
192
+ },
193
+ {
194
+ "id": "110105",
195
+ "parentId": "110100",
196
+ "areaName": "朝阳区"
197
+ }
198
+ ]
199
+ }
200
+ ```
201
+
202
+ :::warning
203
+
204
+ localDictCodes本地配置的value及label 以及 树形结构的 label及id须与dict-setting中的一致
205
+
206
+ 接口返回的字段的数据也同理
207
+
208
+ :::
209
+
210
+ #### treeSetting配置
211
+
212
+ | 字段 | 类型 | 必传 | 说明 | 默认值 |
213
+ | :-----------: | :------------: | :--: | ------------------------------------------------------------ | :------: |
214
+ | idField | String | 否 | 树形数据值的键名 | id |
215
+ | parentIdField | String | 否 | 父节点的键名 | parentId |
216
+ | childrenField | String | 否 | 生成的树形结构子节点的字段名 | children |
217
+ | firstId | String,Number | 否 | 根节点的值 | “0” |
218
+ | labelField | String | 否 | 显示的值的字段名 | label |
219
+ | labelArrField | String | 否 | 字典包会将数组转化为树形结构数据 并每一项生成 labelArr字段,内容为【爷爷级/父级label,...,自己的label】 | labelArr |
220
+ | idArrField | String | 否 | 字典包会将数组转化为树形结构数据 并每一项生成 labelArr字段,内容为【爷爷级/父级id,...,自己的id】 | idArr |
221
+ | levelField | String | 否 | 给对象新增的层级字段名 | level |
222
+ | level | Number | 否 | 配置根目录的层级 | 0 |
223
+ | leafField | String | 否 | 是否叶子节点的字段名 值为boolean | leaf |
224
+
225
+ #### 在main.ts中的使用
226
+
227
+ ```ts
228
+ import { createApp } from "vue";
229
+ import App from "./App.vue";
230
+ // 引入组件库
231
+ import ElementPlus from 'element-plus'
232
+ import 'element-plus/dist/index.css'
233
+ import {getGlobalConfigApi} from "@/api/modules/common-api.ts"
234
+
235
+ import router from './router'
236
+ const app = createApp(App);
237
+ app.use(router)
238
+ app.use(ElementPlus)
239
+ // 引入图标库
240
+ import * as ElementPlusIconsVue from '@element-plus/icons-vue'
241
+ for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
242
+ app.component(key, component)
243
+ }
244
+ // 引入字典包配置
245
+ import dictConfig from "./dict-config"
246
+ import vue3ElementDict from "vue3-element-dict";
247
+
248
+ getGlobalConfigApi().then(data => {
249
+ dictConfig.version = data.dictVersion
250
+ app.use(vue3ElementDict, dictConfig); //注册
251
+ app.mount("#app");
252
+ })
253
+
254
+ ```
255
+
256
+ 至此已全部配置完成,就可以轻松使用了。
257
+
258
+
259
+
260
+ ### 使用
261
+
262
+ 此包拥有组件及方法在此列出来
263
+
264
+ > 组件
265
+ >
266
+ > > el-select-dict 下拉组件
267
+ > >
268
+ > > el-radio-dict 单选框组件
269
+ > >
270
+ > > el-radio-button-dict 单选框按钮样式组件
271
+ > >
272
+ > > el-checkbox-dict 复选框组件
273
+ > >
274
+ > > el-checkbox-button-dict 复选框组件按钮样式
275
+ > >
276
+ > > el-tabs-dict tabs栏组件
277
+ > >
278
+ > > el-table-column-dict 表格列组件
279
+ > >
280
+ > > el-cascader-dict 联级选项组件
281
+ > >
282
+ > > el-tree-dict 树形控件组件
283
+ > >
284
+ > > el-tree-select-dict 树形选择器组件
285
+ > >
286
+ > > el-button--dict 按钮组件
287
+ > >
288
+ > > el-check-tag-dict 标签栏组件
289
+ > >
290
+ > > el-link-dict link组件
291
+ > >
292
+ > > el-tag-dict 标签组件
293
+ > >
294
+ > > el-text-dict text文字组件
295
+ >
296
+ > 过滤器:字典过滤方法 函数形式调用
297
+ >
298
+ > > getLabelByCodeFilter
299
+ > >
300
+ > > getLabelByCodesFilter
301
+ > >
302
+ > > getCodeByLabelFilter
303
+ > >
304
+ > > getCodeByLabelsFilter
305
+ > >
306
+ > > getTreeLabelByCodeFilter
307
+ > >
308
+ > > getTreeLabelByCodesFilter
309
+ > >
310
+ > > getTreeCodeByLabelFilter
311
+ > >
312
+ > > getTreeCodeByLabelsFilter
313
+ >
314
+ > 方法
315
+ >
316
+ > > 普通方法 函数
317
+ > >
318
+ > > > getDictConfig
319
+ > > >
320
+ > > > getDictConfigByKey
321
+ > >
322
+ > > 字典相关 promise方法
323
+ > >
324
+ > > > getDictObjByDictTypes
325
+ > > >
326
+ > > > getLabelByCode
327
+ > > >
328
+ > > > getLabelByCodes
329
+ > > >
330
+ > > > getCodeByLabel
331
+ > > >
332
+ > > > getCodeByLabels
333
+ > > >
334
+ > > > getTreeLabelByCode
335
+ > > >
336
+ > > > getTreeLabelByCodes
337
+ > > >
338
+ > > > getTreeCodeByLabel
339
+ > > >
340
+ > > > getTreeCodeByLabels
341
+ > >
342
+ > > 日期格式化 函数
343
+ > >
344
+ > > > formatDate
345
+ > > >
346
+ > > > isDate
347
+ > >
348
+ > > 脱敏相关 函数
349
+ > >
350
+ > > > mask
351
+ > > >
352
+ > > > maskAddress
353
+ > > >
354
+ > > > maskIdCard
355
+ > > >
356
+ > > > maskName
357
+ > > >
358
+ > > > maskPhone
359
+ > > >
360
+ > > > desensitization
361
+ > >
362
+ > > 树形结构相关 函数
363
+ > >
364
+ > > > listToTree
365
+ > > >
366
+ > > > getTreeItemByCode
367
+ > > >
368
+ > > > getTreeItemByLabel
369
+
370
+
371
+
372
+ **此处主要讲包的配置及准备工作,具体用法可前往[vue3-element-dict官网](http://xiaobusoft.com/vue3-element-dict)查看使用文档,[备用官网](https://shenxiaobu.github.io/vue3-element-dict/),如有问题可前往[问题反馈表格进行记录](https://docs.qq.com/sheet/DVmZQb0hyTk9uc1dY), 也可关注微信公众号【爆米花小布】私信进行反馈**
373
+
374
+ ![公众号二维码](http://qiniu.xiaobusoft.com/npm/gongzhonghao_wechat.png)
375
+
376
+ ## 微信赞助
377
+
378
+ 开发不易,如果对您有所帮助,可赞助作者,利于官网服务器运营。您的支持,是我继续努力的最大动力。
379
+
380
+ ![赞助码](http://qiniu.xiaobusoft.com/npm/qr-card.jpg)
381
+
382
+
383
+
384
+ ## 更新日志
385
+
386
+ ### 0.0.1
387
+ vue3-element-dict发布
package/lib/vite.svg ADDED
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>