字典接口
接口列表
| 接口 | 方法 | 说明 |
|---|---|---|
/api/v1/dict/types | GET | 获取字典类型列表 |
/api/v1/dict/types/{typeCode} | GET | 获取指定类型的字典项 |
/api/v1/dict/types | POST | 新增字典类型 |
/api/v1/dict/types/{typeCode} | PUT | 修改字典类型 |
/api/v1/dict/types/{typeCode} | DELETE | 删除字典类型 |
/api/v1/dict/items | POST | 新增字典项 |
/api/v1/dict/items/{id} | PUT | 修改字典项 |
/api/v1/dict/items/{id} | DELETE | 删除字典项 |
获取字典类型列表
http
GET /api/v1/dict/types?pageNum=1&pageSize=10
Authorization: Bearer {accessToken}响应示例:
json
{
"code": "00000",
"message": "成功",
"data": {
"total": 25,
"list": [
{
"id": 1,
"typeCode": "gender",
"typeName": "性别",
"status": 1,
"remark": "用户性别字典"
}
]
}
}获取指定类型的字典项
http
GET /api/v1/dict/types/gender
Authorization: Bearer {accessToken}响应示例:
json
{
"code": "00000",
"message": "成功",
"data": [
{
"id": 1,
"typeCode": "gender",
"label": "男",
"value": "1",
"sort": 1,
"status": 1
},
{
"id": 2,
"typeCode": "gender",
"label": "女",
"value": "2",
"sort": 2,
"status": 1
}
]
}新增字典类型
http
POST /api/v1/dict/types
Authorization: Bearer {accessToken}
Content-Type: application/json
{
"typeCode": "status",
"typeName": "状态",
"status": 1,
"remark": "通用状态字典"
}字典项字段说明
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| typeCode | string | ✅ | 字典类型编码 |
| label | string | ✅ | 显示文本 |
| value | string | ✅ | 字典值 |
| sort | number | ❌ | 排序号(默认 0) |
| status | number | ❌ | 状态(1 启用,0 禁用) |
| remark | string | ❌ | 备注 |
字典缓存机制
系统对字典数据进行缓存,提高查询效率:
- 缓存键:
dict:{typeCode} - 缓存时长:24 小时
- 过期策略:LRU(最近最少使用)
修改字典后会自动刷新缓存,无需手动处理。
实时同步
启用 SSE 后,字典变更会实时推送到前端:
typescript
// SSE 事件格式
{
event: 'dict_change',
data: {
typeCode: 'gender',
action: 'update' // create | update | delete
}
}详见 字典系统。
