Comparing version 1.0.0 to 1.0.1
147
lib/isArr.js
@@ -1,4 +0,143 @@ | ||
var isArr = function() { | ||
return Object.prototype.toString().slice(8, -1).toLowerCase() === 'array'; | ||
} | ||
module.exports = isArr; | ||
import axios from 'axios' | ||
import md5 from 'js-md5' | ||
import store from '../store' | ||
/* eslint-disable */ | ||
// http请求设置 | ||
axios.defaults.headers['Content-Type'] = 'application/json;charset=UTF-8' | ||
// 请求前配置 去首位空格 | ||
axios.interceptors.request.use(config => { | ||
let requestData = '' // 防串改 参数集合 | ||
try { | ||
if (config.headers['Content-Type'] === 'application/json;charset=UTF-8' || config.headers['content-type'] === 'application/json;charset=UTF-8') { | ||
let {data} = {...config} | ||
if (config.method === 'get') { | ||
// get请求 | ||
if (config.params) { | ||
for (let i in config.params) { | ||
requestData += `${i}=${config.params[i]}&` | ||
} | ||
requestData = requestData.replace(/&$/, '') | ||
} else { | ||
let num = config.url.indexOf('?') | ||
requestData = num > -1 ? config.url.substr(num + 1) : null | ||
} | ||
} else { | ||
if (typeof data === 'string') { | ||
data = JSON.parse(data) | ||
} | ||
if (typeof data === 'object') { | ||
let arr = Object.keys(data) | ||
if (arr.length > 0) { | ||
arr = arr.sort(function(string1, string2) { | ||
let arrList = [] | ||
let num = string1.length > string2.length ? string1.length : string2.length | ||
for (var i = 0; i < num; i++) { | ||
var val1 = string1[i] || '' | ||
var val2 = string2[i] || '' | ||
if (val1 < val2) { | ||
arrList.push(false) | ||
} else if (val1 > val2) { | ||
arrList.push(true) | ||
} | ||
} | ||
return arrList[0] ? 1 : -1 | ||
}) | ||
} | ||
let newData = {} | ||
for (let i in arr) { | ||
newData = {...newData, ...{[arr[i]]: data[arr[i]]}} | ||
} | ||
data = newData | ||
for (let i in data) { | ||
if (typeof data[i] === 'string') { | ||
data[i] = data[i].replace(/(^\s*)|(\s*$)/g, '') | ||
} | ||
} | ||
} | ||
requestData = JSON.stringify(data) | ||
} | ||
} else if (config.headers['Content-Type'].indexOf('multipart/form-data') > -1) { | ||
const obj = new Object() | ||
/* form-data 格式 获取参数 */ | ||
config.data.forEach((value, key) => { | ||
if (typeof value !== 'object') { | ||
obj[key] = typeof value === 'number' ? String(value) : value | ||
} | ||
}) | ||
let arr = Object.keys(obj) | ||
if (arr.length > 0) { | ||
arr = arr.sort(function(string1, string2) { | ||
let arrList = [] | ||
let num = string1.length > string2.length ? string1.length : string2.length | ||
for (var i = 0; i < num; i++) { | ||
var val1 = string1[i] || '' | ||
var val2 = string2[i] || '' | ||
if (val1 < val2) { | ||
arrList.push(false) | ||
} else if (val1 > val2) { | ||
arrList.push(true) | ||
} | ||
} | ||
return arrList[0] ? 1 : -1 | ||
}) | ||
const sortData = {} | ||
arr.forEach(item => { | ||
sortData[item] = obj[item] | ||
}) | ||
requestData = JSON.stringify(sortData) | ||
} | ||
} | ||
} catch (e) {} | ||
const timestamp = new Date().getTime() + Number(store.state.timestampDiff) | ||
const SIGN = store.state.TOKEN + '&' + (Boolean(requestData) && requestData !== '{}' ? timestamp + '&' + requestData : timestamp) | ||
console.log(SIGN) | ||
config.headers['sign'] = md5(SIGN) | ||
config.headers['timestamp'] = timestamp | ||
config.headers['X-XSRF-TOKEN'] = store.state.TOKEN | ||
return config | ||
}) | ||
// 过滤响应 | ||
axios.interceptors.response.use( | ||
result => { | ||
/* result 为服务器响应回来的数据 */ | ||
/* 方式一:直接返回处理后的 result */ | ||
if (result.data.code === 400) { | ||
/* 方式二:返回一个 Promise 对象 可以根据服务端返回的错误码进行判断 */ | ||
let time = 0 | ||
result.data.data.length > 0 && | ||
result.data.data.forEach(item => { | ||
setTimeout(() => { | ||
v.$notify.error({ | ||
title: '提示', | ||
message: item | ||
}) | ||
}, time) | ||
time = time + 500 | ||
}) | ||
} else if (result.data.code === 200 && result.data.msg.trim() !== '') { | ||
v.$message({ | ||
type: 'success', | ||
message: result.data.msg | ||
}) | ||
} else if (result.data.code === 302) { | ||
v.$message.error(result.data.msg) | ||
sessionStorage.clear() | ||
store.commit('setName', '') | ||
store.commit('setUserName', '') | ||
store.commit('setUserId', '') | ||
store.commit('setAuthority', []) | ||
v.$router.push('/zfLogin') | ||
} else if (result.data.msg && result.data.msg.trim() !== '') { | ||
v.$message.error(result.data.msg) | ||
} | ||
return result | ||
}, | ||
err => { | ||
/* 当响应出错的时候 */ | ||
return Promise.reject(err) | ||
} | ||
) | ||
export default axios |
{ | ||
"name": "one-sport", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
5263
140
0