VueCli3构建TS项目的方法步骤(3)
使用,在src
根目录下都可以使用。
let res: Ajax.AxiosResponse = { data: {"id": "1533610056745", "result": 1, "error": null, "jsonrpc": "2.0"} }
cookies的处理
安装cookies的包:npm i js-cookie --save
增加项目前缀,封装cookie, localStorage, sessionStorage 增删改等方法
/** * 操作 cookie, localStorage, sessionStorage 封装 */ import Cookies from 'js-cookie' import { isNil } from 'lodash' const prefix = process.env.VUE_APP_PREFIX /** * ============ Cookie ============ */ export function getCookie (name: string): string { return Cookies.get(prefix + name) } export function setCookie (name: string, value: any, params= {}): void { if (isEmpty(value)) return Cookies.set(prefix + name, value, params) } export function removeCookie (name: string, params= {}): void { Cookies.remove(prefix + name, params) } /** * ============ localStorage ============ */ export function setLocalStorage (name: string, value: any): void { if (isEmpty(value)) return window.localStorage.setItem(prefix + name, value) } export function getLocalStorage (name: string) { return window.localStorage.getItem(prefix + name) } export function removeLocalStorage (name: string) { window.localStorage.removeItem(prefix + name) } export function clearLocal () { window.localStorage.clear() } /** * ============ sessionStorage ============ */ export function setSessionStorage (name: string, value: any): void { if (isEmpty(value)) return window.sessionStorage.setItem(prefix + name, value) } export function getSessionStorage (name: string) { window.sessionStorage.getItem(prefix + name) } export function removeSessionStorage (name: string) { window.sessionStorage.removeItem(prefix + name) } /** * 判断值是否为null或者undefined或者''或者'undefined' * @param val value */ function isEmpty (val: any) { if (isNil(val) || val === 'undefined' || val === '') { return true } return false }
fetch
对axios
进行二次封装,增加请求前后的拦截
import axios from 'axios' /** * 创建 axios 实例 */ const service = axios.create({ timeout: 3000 }) /** * req 拦截器 */ service.interceptors.request.use((config: object): object => { return config }, (error: any): object => { return Promise.reject(error) }) /** * res 拦截器 */ service.interceptors.response.use((response: any) => { const res = response.data if (res.error) { if (process.env.NODE_ENV !== 'production') { console.error(res) } return Promise.reject(res) } return Promise.resolve(res) }) export default service
请求参数统一处理
/** * 统一参数处理 * 请求url处理 */ const qs = require('qs') import { merge, isPlainObject } from 'lodash' import { getCookie } from '@/utils/cookies' /** * 接口参数拼接 * @param opts 接口参数 * @param opsIdParams 是否传递opsId * @param requestType post 还是 get 参数处理 * @param otherParams 是否传有其它参数 * @example * commonParams({ * 'method': cmdName.login, * 'params': params * }, false, undefined, false) */ export function commonParams (opts: object, opsIdParams: boolean= true, requestType: string= 'post', otherParams: boolean= true): object { const params = { json: JSON.stringify(merge({ id: new Date().getTime(), jsonrpc: '2.0', params: dealParams(opsIdParams, otherParams), }, opts || {})), } return requestType === 'post' ? qs.stringify(params) : params } /** * 请求接口的地址处理 * @param urlData 请求接口 * @param type 请求路径 * @example url(cmdName.login) */ export function url (urlData: string, type: any = process.env.VUE_APP_API_PATH) { // @example https://example.com + agsgw/api/ + auth.agent.login return process.env.VUE_APP_API_URL + type + urlData } /** * params 参数的处理 * @param opsIdParams 是否传递opsId * @param otherParams 是否传有其它参数 */ function dealParams (opsIdParams: boolean, otherParams: boolean | object): object { let obj: any = {} // opsIdParams 默认传opsId if (opsIdParams) { obj.opsId = getCookie('token') || '' } // otherParams其他默认参数, 如sn if (otherParams === true) { // obj.sn = getCookie('switchSn') || '' } else { // 其他object if (isPlainObject(otherParams)) { obj = {...obj, ...otherParams} } } return obj }
内容版权声明:除非注明,否则皆为本站原创文章。