资源统一管理工具包
> 获取文件和点位信息
``sh
pnpm add @shencom/utils
pnpm add @shencom/utils-resource
`
`ts`
import { ScResourceFileBase, ScResourceGisBase } from '@shencom/utils';
// import { ScResourceFileBase, ScResourceGisBase } from '@shencom/utils-resource';
`ts
interface CacheController {
set: (key: string, data: any) => void;
get:
remove: (key: string) => void;
}
interface ResourceOptions {
/* 存储控制器 /
Cache: CacheController;
/* 数据缓存时间,单位毫秒;默认: 7天 /
CacheTime?: number;
/* 缓存数据量,单位 KB;默认: 2M /
CacheSize?: number;
/* 通过ID请求数据方法 /
Fetch: (ids: string[]) => Promise
}
interface ResourceFileOptions extends ResourceOptions
/* 保留字段, 默认: ['id', 'fileName', 'remoteUrl', 'name'] /
Fields?: (keyof FileItem)[];
}
interface ResourceGisOptions extends ResourceOptions
/* 保留字段, 默认: ['id', 'addr', 'lat', 'lng'] /
Fields?: (keyof GisItem)[];
}
`
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
| --------- | ---------------------- | --------------- | ------ | ------ |
| Cache | 存储控制器 | CacheController | - | 必填 |
| CacheTime | 数据缓存时间,单位毫秒 | number | - | 7天 |2M
| CacheSize | 缓存数据量,单位 KB | number | - | |
| Fetch | 通过 ID 请求数据方法 | Function | - | 必填 |
- 说明: 获取文件信息
- 类型: (options?: ResourceFileOptions): Objectoptions
- 参数:
- - 初始化配置
#### init
`ts
// 本地存储对象
const Storage = new ScStorageBase({
/ ... /
});
const Cache = {
set: Storage.setLasting.bind(Storage),
get: Storage.getLasting.bind(Storage),
remove: Storage.removeLasting.bind(Storage),
};
// 调接口获取文件详情的方法
const getFileInfo = (ids: string[]) => Promise
// 初始化文件资源对象
const ResourceFile = ScResourceFileBase({
Cache,
Fetch: getFileInfo,
});
`
#### ResourceFile.getData
> 获取文件信息的方法
`ts
ResourceFile.getData('1449561090540367872');
// Promise<[{id: '1449561090540367872', fileName: 'xx', remoteUrl: 'https://...'}]>
ResourceFile.getData('url 字符串');
// Promise<[{id: '随机 id', fileName: 'xx', remoteUrl: 'url 字符串'}]>
ResourceFile.getData(['1449561090540367872', '1449122610622427136']);
/*
Promise<[
{id: '1449561090540367872', fileName: 'xx', remoteUrl: 'https://...'},
{id: '1449122610622427136', fileName: 'xx', remoteUrl: 'https://...'}
]>
*/
ResourceFile.getData({ id: '1449561090540367872', fileName: 'xx', remoteUrl: 'https://...' });
// Promise<[{id: '1449561090540367872', fileName: 'xx', remoteUrl: 'https://...'}]>
ResourceFile.getData([
'1449561090540367872',
'url 字符串',
{ id: '1449122610622427136', fileName: 'xx', remoteUrl: 'https://...' },
]);
/*
Promise<[
{id: '1449561090540367872', fileName: 'xx', remoteUrl: 'https://...'},
{id: '随机 id', fileName: 'xx', remoteUrl: 'url 字符串'},
{id: '1449122610622427136', fileName: 'xx', remoteUrl: 'https://...'}
]>
*/
`
#### ResourceFile.all
> 获取全部的文件信息
`ts`
const CacheData = ResourceFile.all();
#### ResourceFile.show
> 通过 id 获取缓存内对应的文件信息
`ts`
const files = ResourceFile.show(['id1', 'id2']);
#### ResourceFile.keys
> 获取存储的 id
`ts`
const CacheIds = ResourceFile.keys();
> 删除指定 id 的文件信息
`ts`
ResourceFile.remove('id1');
> 清空所有的文件信息
`ts`
ResourceFile.clear();
- 说明: 获取点位信息
- 类型: (options: ResourceGisOptions): Objectoptions
- 参数:
- - 初始化配置
#### init
`ts
// 本地存储对象
const Storage = new ScStorageBase({
/ ... /
});
const Cache = {
set: Storage.setLasting.bind(Storage),
get: Storage.getLasting.bind(Storage),
remove: Storage.removeLasting.bind(Storage),
};
// 调接口获取点位详情的方法
const getGisInfo = (ids: string[]) => Promise
// 初始化点位资源对象
const ResourceGis = ScResourceGisBase({
Cache,
Fetch: getGisInfo,
});
`
#### ResourceGis.getData
> 获取文件信息的方法
`ts
ResourceGis.getData('1449561090540367222');
// Promise<[{ id: '1449561090540367222', addr: '地址地址', lng: '114.06667', lat: '22.61667' }]>
ResourceGis.getData('地址地址');
// Promise<[{ id: '随机 id', addr: '地址地址' }]>
ResourceGis.getData(['1449561090540367222', '1449122610622427316']);
/*
Promise<[
{ id: '1449561090540367222', addr: '地址地址', lng: '114.06667', lat: '22.61667' },
{ id: '1449122610622427316', addr: '地址地址', lng: '114.06667', lat: '22.61667' },
]>
*/
ResourceGis.getData({ id: '1449561090540367222', lng: '114.06667', lat: '22.61667' });
// Promise<[{ id: '1449561090540367222', lng: '114.06667', lat: '22.61667' }]>
ResourceGis.getData([
'1449561090540367222',
'地址地址',
{ id: '1449561090540367222', lng: '114.06667', lat: '22.61667' },
]);
/*
Promise<[
{ id: '1449561090540367222', addr: '地址地址', lng: '114.06667', lat: '22.61667' },
{ id: '随机 id', addr: '地址地址' },
{ id: '1449561090540367222', lng: '114.06667', lat: '22.61667' }
]>
*/
`
#### ResourceGis.all
> 获取全部的 GIS 信息
`ts`
const CacheData = ResourceGis.all();
#### ResourceGis.show
> 通过 id 获取缓存内对应的 GIS 信息
`ts`
const files = ResourceGis.show(['id1', 'id2']);
#### ResourceGis.keys
> 获取存储的 id
`ts`
const CacheIds = ResourceGis.keys();
> 删除指定 id 的 GIS 信息
`ts`
ResourceGis.remove('id1');
> 清空所有的 GIS 信息
`ts``
ResourceGis.clear();