1.0.0 • Published 5 years ago

pes-utils v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
5 years ago

概述

提供工具方法提供业务使用,所有方法均经过自动化测试

快速使用

syarn add "pes-utils" -S
  1. 使用CDN方式
// 版本号和package.json的version保持一致
const { filter, storage, query } = window.utils.defautl
  1. 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()
  1. 使用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-GBnumber

tools

方法作用参数说明
isEmptyObject判读对象是否为空Object
debounce函数防抖(function, number, options={leading:false})默认时间是500s, options 传入leading:true 可以立即调用
throttle函数节流(function, number)默认时间是300s
isTest判断当前环境是否为测试环境string默认是 production
clone深度拷贝对象Object对于对象和数组是深度拷贝,否则返回引用

storage

方法作用参数说明
get获取当前域名下指定的cookiekey: string
set设置当前域名下cookie的值(key: string, val: string)
del删除当前域名下指定的cookiekey: 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 | |