@destiny_zxj/devtools
v1.0.13
Published
前端开发工具箱
Downloads
18
Readme
前端开发工具箱
说明
- sessionStorage、localStorage 支持
- axios 支持
打包发布
yarn build
npm publish
0. Usage
import { Http, Util, Request, Response, Method, ResponseType } from '@destiny_zxj/devtools'
Http
基于 axios 实现网络请求
# Http{
## baseUrl: axios baseUrl,用于设置服务器 api 地址
## tokenFieldName = 'token'
#### token 字段名
## request(request: Request, baseUrl=''): Promise<Response>
#### 发起 Http 请求
#### request: 请求,下见 [Request]
#### baseUrl: axios baseUrl
- Example
import { Http, Response, Request, Method } from '@destiny_zxj/devtools'
Http.baseUrl = 'http://127.0.0.1:8888'
class Api{
/**
* 用户登录
* @param phone 手机号
* @param username 用户名
* @param password 密码
* @param code 验证码
* @returns
*/
public static login(phone: string, username: string, password: string, code: string): Promise<Response> {
const request = new Request('/user/login')
request.method = Method.POST
request.data = {
phone, username, password, code
}
return Http.request(request)
}
/**
* 请求失败
* @param msg 失败消息
* @returns
*/
public static requestFailed(msg: string): Promise<Response> {
return new Promise((resolve, reject) => {
reject(new Response(-1, null, msg))
})
}
}
export {
Api
}
Request
# Request
## url: 请求 url
## method: 请求方法,下见 [Method]
## params: 请求参数
## data: 请求数据 (payload)
## headers: 请求头
## token: token 令牌
## baseUrl: axios baseUrl
## responseType: 响应类型,下见 [ResponseType]
## cache: 缓存
Response
# Response
## code: 响应码,后端自定义
## data: 响应数据
## msg: 响应消息
Method
# Method
## GET: 普通 GET 请求
## POST: 普通 POST 请求,Content-Type=application/x-www-form-urlencoded
## FORMDATA: 自定义 POST 请求,Content-Type=multipart/form-data; boundary=-----ycst.destiny_zxj-----
## POST_JSON: 自定义 POST 请求,Content-Type=application/json
ResponseType
# ResponseType
## BOLB: 响应二进制
Util
工具类
- saveLoginedData: (user_data: any) => boolean;
保存登录数据
user_data 服务端获取到的用户数据
- getUserData: () => any;
获取本地保存的用户数据
- logout: () => void;
注销登录,含服务器注销
- clearData: (all?: boolean) => void;
清除存储的数据
all 全部清除
- VerifyPhone(phone: string): boolean;
手机号校验
phone 手机号
- VerifyUsername(username: string): boolean;
校验给定字符串长度是否为 6~11
username 用户名
- verifyIdCard(idCard: string): boolean;
校验身份证号
idCard 身份证号
- verifyEmail(email: string): boolean;
校验邮箱
email 邮箱
- verifyPassword(password: string): boolean;
校验密码长度是否为 6~16
password 密码
- verifyCode(code: string): boolean;
校验验证码长度是否为 6 位
code 验证码
- verifyURI(uri: string): boolean;
校验 uri
uri
- hasSpace(val: string): boolean;
校验给定字符串中是否含有空格
val 给定字符串
- Verify(val: string, pattern: RegExp, testSpace?: boolean): boolean;
普通模式校验
val 给定字符串
pattern 匹配正则表达式
testSpace 是否检测空格(false)