Methods
(static) ajax(url, optionsopt) → {Promise.<object>}
- Description:
请求
注意:该方法仅适用于浏览器端。
- Source:
- Since:
- 4.16.0
- See:
Example
ajax('/somefile').then(res=>{
// do something
});
ajax('/api', { method: 'post' }).then(res=>{
// do something
});
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
url |
string | 地址 |
|
options |
AjaxOptions |
<optional> |
配置项 |
Returns:
XHR 事件对象
- Type
- Promise.<object>
(static) calculateCursorPosition(prevPos, prevCtrlValue, rawValue, ctrlValue, optionsopt) → {number}
- Description:
计算输入框的值格式化后光标位置
- Source:
- Since:
- 4.6.0
- See:
Parameters:
Name | Type | Attributes | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
prevPos |
number | 赋值前的光标位置,onChange/onInput的光标位置 e.target.selectionEnd |
|||||||||||||||||||||
prevCtrlValue |
string | 上一个格式化后的值 |
|||||||||||||||||||||
rawValue |
string | 当前输入原值 |
|||||||||||||||||||||
ctrlValue |
string | 当前格式化后的值 |
|||||||||||||||||||||
options |
Object |
<optional> |
配置项 Properties
|
Returns:
格式化后的光标位置
- Type
- number
(static) compressImage(img, optionsopt) → {Promise.<(Blob|string)>}
- Description:
压缩图片。
注意:该方法仅适用于浏览器端。
如果是半透明图片并且导出
image/png
格式,建议将背景变成透明background=transparent
,避免出现白边。注意正常图片压缩导出image/png
格式后文件可能会比原图大。
- Source:
- Since:
- 4.20.0
- See:
Example
// 默认返回压缩后的 blob 对象
compressImage(file).then(blob=>{
// do something
});
// 设置返回格式 data url
compressImage(file, { format: 'dataURL' }).then(url=>{
// do something
});
// 自定义配置
compressImage('https://dummyimage.com/200x300', {
width: 100,
height: 100,
quality: 0.5,
beforeCompress({ image, blob }){},
beforeDraw({ image, blob, canvas, context }){}
afterDraw({ image, blob, canvas, context }){}
}).then(blob=>{
// do something
});
// 支持不同形式的图片文件
compressImage('data:image/png;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=').then(blob=>{
// do something
});
Parameters:
Name | Type | Attributes | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
img |
string | Blob | 图片地址或 blob 对象 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
options |
Object |
<optional> |
配置项 Properties
|
Returns:
blob 对象 或 data url 图片
- Type
- Promise.<(Blob|string)>
(static) download(data, optionsopt) → {Promise.<void>}
- Description:
下载
注意:该方法仅适用于浏览器端,兼容 IE10+ 和现代浏览器。
注意:微信浏览器不支持H5下载文件。
- Source:
- Since:
- 4.16.0
- See:
Example
// 文本
download('hello world', 'text.txt');
// 远程文件1
// 不带协议的绝对地址,需要通过 dataType 指定为 url 类型
download('/xxx.jpg', { dataType: 'url', fileName: 'test.jpg' });
// 远程文件2
download('https://example.com/xxx.jpg');
// base64
download('data:image/png;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=', 'test.png');
// blob文件
download(new Blob(['hello world']), 'text.txt');
// 本地文件
download(File, 'filename.ext');
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
data |
string | Blob | ArrayBuffer | TypedArray | 字符串、blob数据或url地址 |
|
options |
string | DownloadOptions |
<optional> |
文件名称 或 配置项 |
Returns:
- Type
- Promise.<void>
(static) getFileBlob(file, ajaxOptionsopt) → {Promise.<Blob>}
- Description:
获取文件 Blob 。
- Source:
- Since:
- 4.21.0
Example
getFileBlob(file).then((blob)=>{
// do something
});
getFileBlob('https://dummyimage.com/200x300').then((blob)=>{
// do something
});
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
file |
string | Blob | 文件地址或对象。 |
|
ajaxOptions |
AjaxOptions |
<optional> |
ajax 请求配置项,当传入的图片为字符串时才会触发请求。 |
Returns:
文件 Blob 。
- Type
- Promise.<Blob>
(static) getImageInfo(img, cacheOptionsopt, ajaxOptionsopt) → {Promise.<ImageInfo>}
- Description:
获取图片信息。
注意:该方法仅适用于浏览器端。
- Source:
- Since:
- 4.20.0
Example
getImageInfo(file).then(imageInfo=>{
// do something
});
getImageInfo('https://dummyimage.com/200x300').then(imageInfo=>{
// do something
});
getImageInfo('data:image/png;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=').then(imageInfo=>{
// do something
});
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
img |
string | Blob | 图片地址或 blob 对象 |
||
cacheOptions |
boolean | CacheOptions |
<optional> |
true
|
是否使用缓存。开启后,自动缓存最近上一次成功的结果,当图片地址或 blob 对象一致时,直接返回该缓存。避免连续请求同一个图片资源,重复加载。当缓存下一次成功加载的图片时,会自动释放上一次缓存的图片,也可以通过 |
ajaxOptions |
AjaxOptions |
<optional> |
ajax 请求配置项,当传入的图片为字符串时才会触发请求。 |
Returns:
图片信息
- Type
- Promise.<ImageInfo>
(static) loadImage(img, cacheOptionsopt) → {Promise.<HTMLImageElement>}
- Description:
加载图片。
注意:该方法仅适用于浏览器端。
- Source:
- Since:
- 4.20.0
Example
loadImage(file).then(image=>{
// do something
});
loadImage('https://dummyimage.com/200x300').then(image=>{
// do something
});
loadImage('data:image/png;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=').then(image=>{
// do something
});
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
img |
string | Blob | 图片地址或 blob 对象 |
||
cacheOptions |
boolean | CacheOptions |
<optional> |
true
|
是否使用缓存。开启后,自动缓存最近上一次成功的结果,当图片地址或 blob 对象一致时,直接返回该缓存。避免连续请求同一个图片资源,重复加载。当缓存下一次成功加载的图片时,会自动释放上一次缓存的图片,也可以通过 |
Returns:
HTML图片元素
- Type
- Promise.<HTMLImageElement>
(static) loadImageWithBlob(img, cacheOptionsopt, ajaxOptionsopt) → {Promise.<ImageWithBlob>}
- Description:
加载图片,返回图片元素和 blob 对象。
注意:该方法仅适用于浏览器端。
- Source:
- Since:
- 4.20.0
Example
loadImage(file).then(({image, blob})=>{
// do something
});
loadImage('https://dummyimage.com/200x300').then(({image, blob})=>{
// do something
});
loadImage('data:image/png;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=').then(({image, blob})=>{
// do something
});
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
img |
string | Blob | 图片地址或 blob 对象 |
||
cacheOptions |
boolean | CacheOptions |
<optional> |
true
|
是否使用缓存。开启后,自动缓存最近上一次成功的结果,当图片地址或 blob 对象一致时,直接返回该缓存。避免连续请求同一个图片资源,重复加载。当缓存下一次成功加载的图片时,会自动释放上一次缓存的图片,也可以通过 |
ajaxOptions |
AjaxOptions |
<optional> |
ajax 请求配置项,当传入的图片为字符串时才会触发请求。 |
Returns:
HTML图片元素和 blob 对象
- Type
- Promise.<ImageWithBlob>
(static) loadScript(src, optionsopt) → {Promise.<HTMLScriptElement>}
- Description:
加载 js 文件。
注意:该方法仅适用于浏览器端。
- Source:
- Since:
- 4.19.0
Example
loadScript('some.js').then(script=>{
// do something
})
loadScript('some.js', { id: 'xxx', async: false, attrs: { foo: 'bar' } }).then(script=>{
// do something
})
Parameters:
Name | Type | Attributes | Description | |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
src |
string | js 地址。 |
||||||||||||||||||||||||||
options |
Object |
<optional> |
script 标签属性。比如 Properties
|
Returns:
异步返回 script 元素。
- Type
- Promise.<HTMLScriptElement>
(static) randomString(lenopt, optionalCharsopt) → {string}
- Description:
生成随机字符串
- Source:
- Since:
- 4.8.0
Example
randomString(5); // slk23
randomString(8); // 71mHqo2A
// 自定义允许的字符
randomString(5, 'abc'); // ccbcb
randomString(8, 'abcefg'); // bcgcfabg
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
len |
number |
<optional> |
0
|
长度 |
optionalChars |
string |
<optional> |
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
允许的字符,默认为数字和大小写字母 |
Returns:
随机字符串
- Type
- string
(static) strlen(str) → {number}
Example
strlen('你好a'); // 5
strlen('你好,世界!'); // 12
strlen('严両丞丽'); // 8
strlen('abcde'); // 5
strlen('𠮷'); // 4
strlen('🍎'); // 4
Parameters:
Name | Type | Description |
---|---|---|
str |
string | 字符串 |
Returns:
字符长度
- Type
- number