1.0.0 • Published 5 years ago
pes-utils v1.0.0
概述
提供工具方法提供业务使用,所有方法均经过自动化测试
快速使用
syarn add "pes-utils" -S
- 使用CDN方式
// 版本号和package.json的version保持一致
const { filter, storage, query } = window.utils.defautl
- use 到项目中
// 全部引用
import utils from 'pes-utils'
Vue.use(utils)
// or
// 根据需要引入对应的方法
import { filter, storage, query, tools } from 'pes-utils'
Vue.prototype.$filter = Vue.filter = filter
// 在项目中使用
this.$filter.formatAmount
this.$storage.get()
- 使用reset.css
import '@wuyxp/utils/dist/style/reset.css'
API
filter
方法 | 作用 | 参数 | 说明 |
---|---|---|---|
formatAmount | 格式化金额 | number | |
removeEmptyArgInObj | 清除对象里面没有值的属性 | Array | Object | |
get | 从对象中获取长路径的值 | (source: Array | Object, path: string, defaultVaule: any) | |
prefixInteger | 数字前面要补0 | (num: number, length: number) | |
uniqBy | 对数组进行去重排序 | (arr: Array, fn: Function | string) | 第二个参数用于根据什么key去排序,或者是一个函数,根据函数执行后的结果去排序 |
conver | 格式化B-KB-MB-GB | number |
tools
方法 | 作用 | 参数 | 说明 |
---|---|---|---|
isEmptyObject | 判读对象是否为空 | Object | |
debounce | 函数防抖 | (function, number, options={leading:false}) | 默认时间是500s, options 传入leading:true 可以立即调用 |
throttle | 函数节流 | (function, number) | 默认时间是300s |
isTest | 判断当前环境是否为测试环境 | string | 默认是 production |
clone | 深度拷贝对象 | Object | 对于对象和数组是深度拷贝,否则返回引用 |
storage
方法 | 作用 | 参数 | 说明 |
---|---|---|---|
get | 获取当前域名下指定的cookie | key: string | |
set | 设置当前域名下cookie的值 | (key: string, val: string) | |
del | 删除当前域名下指定的cookie | key: string | |
all | 获取当前域名下所有的cookie | - | |
empty | 清空当前域名下所有的cookie | - |
query
方法 | 作用 | 参数 | 说明 |
---|---|---|---|
parseQuery | 将path路径的参数部分转换为对象 | path: string | |
stringifyQuery | 将对象转化为path路径的参数 | Object | |
decodeParam | 更彻底的decodeURIComponent方法 | path: string | 兼容解码"%" |
request
request 依赖外部引入的axios,也就是项目中必须使用外链的方式去引入axios
// 此种方法即将废弃 import { request } from 'pes-utils' const baseURL = 'https://x.com' const api = request(baseURL) api.get(xxx)
// 使用如下方法 import { createRequest } from 'pes-utils' const request = createRequest(axios)
const baseURL = 'https://x.com' const api = request(baseURL) api.get(xxx)
### qs
> utils 引入过qs了,所以项目中引入utils后使用qs的时候也正常的去引用use,那么qs就会挂载到Vue上,使用方法 ```https://github.com/ljharb/qs ```
#### createRequest 方法参数
| 配置 | 作用 | 参数 | 说明 |
|---------- |-------- |---------- |------------- |
|axios| 创建request方法 | axios实例,或者是不传 | 如果不传,那么axios就默认是使用script引入,如果使用import 引入的axios,那么则需要去传入当前参数 |
#### request 方法参数
| 配置 | 作用 | 参数 | 说明 |
|---------- |-------- |---------- |------------- |
|config| 设置基本的配置 | string, Config | 如果是传入字符串,那么就会默认的是baseUrl,如果是对象那么是Config |
|axiosSetting| axios的config配置| axios Config | 会覆盖所有默认的设置
| Config | 作用 | 参数 | 说明 |
|---------- |-------- |---------- |------------- |
|baseUrl| 设置基本路径 | string | 如果请求方法传入绝对路径,那么会覆盖baseUrl |
|logoutAction| 返回未登录时的时间 | function | |
|errorAction| 其他的请求错误事件 | function | |
|failureAction| 请求失败的事件 | function | |
|AuthorizationV2| 需要去cookie的哪个值作为token | string | 'cloudToken' 或者 'seaToken' |
|token|直接传入token 值 | string | |
|isRequestArrayRepeat| 是否对数组处理中括号 | boolean | 默认 true |
|env | 根据当前的环境传入 | 一般传入 process.env.NODE_ENV| 默认production |
| 方法 | 作用 | 参数 | 说明 |
|---------- |-------- |---------- |------------- |
|get| get 请求 | url, param | |
|post| post 请求 | url, param | |
|JPost| json请求 | url, param | |
|blob| 下载blob文件 | url, param | |
1.0.0
5 years ago