@shencom/utils-oss
v1.2.0
Published
oss静态资源工具
Downloads
64
Readme
@shencom/utils-oss
oss 静态资源工具,处理各种环境的 oss 静态路径和 CDN
Install
pnpm add @shencom/utils
# or
pnpm add @shencom/utils-oss
Basic Usage
const oss = new ScOssBase(Option);
Option
interface Option {
/** 环境 */
env: 'pro' | 'uat' | 'tst';
/** pro环境下是否使用CDN,默认为: true */
isCDN?: boolean;
/** oss 项目路径 */
projectPath: string;
/** 开发静态资源路径,为空则是测试服链接 */
devAssetsPath?: string;
}
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
| ------------- | ------------------------------------------------ | -------- | --------------------- | ------ |
| env | 环境 | String | pro
/ uat
/ tst
| 必填 |
| isCDN | pro 环境下是否使用 CDN | Boolean | - | true
|
| projectPath | oss 项目路径 | String | - | 必填 |
| devAssetsPath | 开发静态资源路径,为空则是测试服链接 | String | - | - |
| urlList | oss 域名列表,只有符合域名的链接才能进行文件处理 | String[] | - | - |
urlList
默认存在 ['aliyuncs.com', 'filesvr.shencom.cn', 'oss.shencom.cn']
设置 urlList
属性进行添加
Props
path
- 说明: 项目路径
- 类型:
string
- 示例:
oss.path;
zoom
- 说明: 图片缩放,默认:
m:mfit w:100 h:100
- 类型:
string
- 示例:
oss.zoom;
Methods
zoomPercent
- 说明: 图片百分比缩放,默认:
50%
- 类型:
zoomPercent(percent?: number): string
- 参数:
percent
: 缩放百分比
- 示例:
const zoom = oss.zoomPercent(percent);
zoomCustom
- 说明: 自定义图片缩放,参考链接
- 类型:
zoomCustom(param?: {w?: string;h?: string;m?: string;}): string
- 参数:
param
: 缩放配置
- 示例:
const zoom = oss.zoomCustom(); // '?x-oss-process=image/resize,m_mfit,w_100,h_100' const zoom = oss.zoomCustom({ w: '50' }); // '?x-oss-process=image/resize,m_mfit,w_50,h_100' const zoom = oss.zoomCustom({ h: '50' }); // '?x-oss-process=image/resize,m_mfit,w_100,h_50' const zoom = oss.zoomCustom({ h: '200', w: '200' }); // '?x-oss-process=image/resize,m_mfit,w_200,h_200' const zoom = oss.zoomCustom({ m: 'lfit' }); // '?x-oss-process=image/resize,m_lfit,w_100,h_100'
isOssHost
- 说明: 是否为 oss 链接
- 类型:
isOssHost(url: string): boolean
- 参数:
url
: 链接
- 示例:
const isOssHost = oss.isOssHost(url);
isImageZoom
- 说明: 图片是否可缩放
- 类型:
isImageZoom(url: string): boolean
- 参数:
url
: 链接
- 示例:
const isImageZoom = oss.isImageZoom(url);
handleImageExt
- 说明: 图片是否可缩放
- 类型:
handleImageExt(url: string, fileExt?: string): string
- 参数:
url
: 链接fileExt
: 默认处理为图片后缀名,默认:jpg
- 示例:
const url = oss.handleImageExt(url);
handleImageZoom
- 说明: 处理 oss 图片缩放
- 对链接进行验证 oss 文件验证,图片格式进行兼容处理
- 类型:
handleImageZoom(url: string, zoom?: string): string
- 参数:
图片链接
: 链接zoom
: 图片缩放参数,默认:30%
- 示例:
const url = oss.handleImageZoom(url);
Example
import { ScOssBase } from '@shencom/utils';
// import ScOssBase from '@shencom/utils-oss';
const oss = new ScOssBase({
env: (IsPro ? 'pro' : IsDev) ? 'tst' : 'uat',
projectPath: `app/${name}`,
devAssetsPath: './assets',
});
export default oss;