6 changed files with 625 additions and 4 deletions
@ -0,0 +1,35 @@ |
|||||
|
const VueAxios = { |
||||
|
vm: {}, |
||||
|
// eslint-disable-next-line no-unused-vars
|
||||
|
install (Vue, instance) { |
||||
|
if (this.installed) { |
||||
|
return |
||||
|
} |
||||
|
this.installed = true |
||||
|
|
||||
|
if (!instance) { |
||||
|
// eslint-disable-next-line no-console
|
||||
|
console.error('You have to install axios') |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
Vue.axios = instance |
||||
|
|
||||
|
Object.defineProperties(Vue.prototype, { |
||||
|
axios: { |
||||
|
get: function get () { |
||||
|
return instance |
||||
|
} |
||||
|
}, |
||||
|
$http: { |
||||
|
get: function get () { |
||||
|
return instance |
||||
|
} |
||||
|
} |
||||
|
}) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export { |
||||
|
VueAxios |
||||
|
} |
||||
@ -0,0 +1,89 @@ |
|||||
|
import axios from 'axios' |
||||
|
// import store from '@/store'
|
||||
|
// import storage from 'store'
|
||||
|
// import notification from 'ant-design-vue/es/notification'
|
||||
|
// import message from 'ant-design-vue/es/message'
|
||||
|
import { VueAxios } from './axios' |
||||
|
import { isObject } from './util' |
||||
|
// import { ACCESS_TOKEN } from '@/store/mutation-types'
|
||||
|
|
||||
|
// 站点配置文件
|
||||
|
// window.publicConfig => public/config.js
|
||||
|
// const publicConfig = store.getters.publicConfig
|
||||
|
|
||||
|
// 创建 axios 实例
|
||||
|
const service = axios.create({ |
||||
|
// 服务端api地址
|
||||
|
// baseURL: process.env.NODE_ENV === 'production' ? publicConfig.BASE_API : process.env.VUE_APP_API_BASE_URL,
|
||||
|
// baseURL: publicConfig.BASE_API,
|
||||
|
timeout: 60 * 1000 // 请求超时时间,60秒, 如果上传大文件需要更长
|
||||
|
}) |
||||
|
|
||||
|
// 接口请求拦截
|
||||
|
service.interceptors.request.use(config => { |
||||
|
// 设置每个请求的Content-Type
|
||||
|
// config.headers['Content-Type'] = 'application/json;charset=utf-8'
|
||||
|
|
||||
|
// 让每个请求携带当前用户token
|
||||
|
// const token = storage.get(ACCESS_TOKEN)
|
||||
|
// if (token) {
|
||||
|
// config.headers['Access-Token'] = token
|
||||
|
// }
|
||||
|
return config |
||||
|
}) |
||||
|
|
||||
|
// 是否显示未登录提示
|
||||
|
let notLoggedMessage = false |
||||
|
|
||||
|
// 接口响应拦截
|
||||
|
service.interceptors.response.use((response) => { |
||||
|
const result = response.data |
||||
|
if (!isObject(result)) { |
||||
|
const error = { message: '服务端api返回的数据格式不正确' } |
||||
|
return Promise.reject(error) |
||||
|
} |
||||
|
// result.status [ 200正常 500有错误 401未登录 403没有权限访问 ]
|
||||
|
// api报错信息
|
||||
|
if (result.status === 500) { |
||||
|
message.error(result.message, 1.8) |
||||
|
return Promise.reject(result) |
||||
|
} |
||||
|
// 鉴权失败: 未登录
|
||||
|
if (result.status === 401) { |
||||
|
if (!notLoggedMessage) { |
||||
|
notLoggedMessage = true |
||||
|
store.dispatch('Logout').then(() => { |
||||
|
notification.error({ |
||||
|
key: 'notLoggedMessage', |
||||
|
message: '错误', |
||||
|
description: result.message, |
||||
|
duration: 3 |
||||
|
}) |
||||
|
setTimeout(() => window.location.reload(), 1500) |
||||
|
}) |
||||
|
} |
||||
|
return Promise.reject(result) |
||||
|
} |
||||
|
return result |
||||
|
}, (error) => { |
||||
|
// 网络请求出错
|
||||
|
const errMsg = ((error.response || {}).data || {}).message || '请求出现错误,请稍后再试' |
||||
|
notification.error({ |
||||
|
message: '网络请求出错', |
||||
|
description: errMsg, |
||||
|
duration: 3 |
||||
|
}) |
||||
|
return Promise.reject(error) |
||||
|
}) |
||||
|
|
||||
|
const installer = { |
||||
|
vm: {}, |
||||
|
install (Vue) { |
||||
|
Vue.use(VueAxios, service) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export { |
||||
|
installer as VueAxios, |
||||
|
service as axios |
||||
|
} |
||||
@ -0,0 +1,230 @@ |
|||||
|
|
||||
|
import _ from 'lodash' |
||||
|
|
||||
|
export function timeFix () { |
||||
|
const time = new Date() |
||||
|
const hour = time.getHours() |
||||
|
return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好' |
||||
|
} |
||||
|
|
||||
|
export function welcome () { |
||||
|
const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要打一把 DOTA', '我猜你可能累了'] |
||||
|
const index = Math.floor(Math.random() * arr.length) |
||||
|
return arr[index] |
||||
|
} |
||||
|
|
||||
|
// 触发 window.resize
|
||||
|
export function triggerWindowResizeEvent () { |
||||
|
const event = document.createEvent('HTMLEvents') |
||||
|
event.initEvent('resize', true, true) |
||||
|
event.eventType = 'message' |
||||
|
window.dispatchEvent(event) |
||||
|
} |
||||
|
|
||||
|
export function handleScrollHeader (callback) { |
||||
|
let timer = 0 |
||||
|
|
||||
|
let beforeScrollTop = window.pageYOffset |
||||
|
callback = callback || function () { } |
||||
|
window.addEventListener( |
||||
|
'scroll', |
||||
|
event => { |
||||
|
clearTimeout(timer) |
||||
|
timer = setTimeout(() => { |
||||
|
let direction = 'up' |
||||
|
const afterScrollTop = window.pageYOffset |
||||
|
const delta = afterScrollTop - beforeScrollTop |
||||
|
if (delta === 0) { |
||||
|
return false |
||||
|
} |
||||
|
direction = delta > 0 ? 'down' : 'up' |
||||
|
callback(direction) |
||||
|
beforeScrollTop = afterScrollTop |
||||
|
}, 50) |
||||
|
}, |
||||
|
false |
||||
|
) |
||||
|
} |
||||
|
|
||||
|
export function isIE () { |
||||
|
const bw = window.navigator.userAgent |
||||
|
const compare = (s) => bw.indexOf(s) >= 0 |
||||
|
const ie11 = (() => 'ActiveXObject' in window)() |
||||
|
return compare('MSIE') || ie11 |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Remove loading animate |
||||
|
* @param id parent element id or class |
||||
|
* @param timeout |
||||
|
*/ |
||||
|
export function removeLoadingAnimate (id = '', timeout = 1500) { |
||||
|
if (id === '') { |
||||
|
return |
||||
|
} |
||||
|
setTimeout(() => { |
||||
|
document.body.removeChild(document.getElementById(id)) |
||||
|
}, timeout) |
||||
|
} |
||||
|
|
||||
|
// 节流
|
||||
|
// 思路: 第一次先设定一个变量true,
|
||||
|
// 第二次执行这个函数时,会判断变量是否true,
|
||||
|
// 是则返回。当第一次的定时器执行完函数最后会设定变量为flase。
|
||||
|
// 那么下次判断变量时则为flase,函数会依次运行。
|
||||
|
export function throttle (fn, delay = 100) { |
||||
|
// 首先设定一个变量,在没有执行我们的定时器时为null
|
||||
|
var timer = null |
||||
|
return function () { |
||||
|
// 当我们发现这个定时器存在时,则表示定时器已经在运行中,需要返回
|
||||
|
if (timer) return |
||||
|
timer = setTimeout(() => { |
||||
|
fn.apply(this, arguments) |
||||
|
timer = null |
||||
|
}, delay) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 防抖
|
||||
|
// 首次运行时把定时器赋值给一个变量, 第二次执行时,
|
||||
|
// 如果间隔没超过定时器设定的时间则会清除掉定时器,
|
||||
|
// 重新设定定时器, 依次反复, 当我们停止下来时,
|
||||
|
// 没有执行清除定时器, 超过一定时间后触发回调函数。
|
||||
|
export function debounce (fun, delay) { |
||||
|
return function (args) { |
||||
|
// 获取函数的作用域和变量
|
||||
|
const that = this |
||||
|
const _args = args |
||||
|
// 每次事件被触发,都会清除当前的timeer,然后重写设置超时调用
|
||||
|
clearTimeout(fun.id) |
||||
|
fun.id = setTimeout(function () { |
||||
|
fun.call(that, _args) |
||||
|
}, delay) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 判断是否为空对象 |
||||
|
* @param {*} object 源对象 |
||||
|
*/ |
||||
|
export function isEmptyObject (object) { |
||||
|
return Object.keys(object).length === 0 |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 判断是否为对象 |
||||
|
* @param {*} object |
||||
|
*/ |
||||
|
export function isObject (object) { |
||||
|
return Object.prototype.toString.call(object) === '[object Object]' |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 判断是否为对象 |
||||
|
* @param {*} array |
||||
|
*/ |
||||
|
export function isArray (array) { |
||||
|
return Object.prototype.toString.call(array) === '[object Array]' |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 判断是否为空 |
||||
|
* @param {*} object 源对象 |
||||
|
*/ |
||||
|
export function isEmpty (value) { |
||||
|
if (isArray(value)) { |
||||
|
return value.length === 0 |
||||
|
} |
||||
|
if (isObject(value)) { |
||||
|
return isEmptyObject(value) |
||||
|
} |
||||
|
return !value |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 判断是否在数组中 |
||||
|
* @param {*} search |
||||
|
* @param {*} array |
||||
|
*/ |
||||
|
export function inArray (search, array) { |
||||
|
return array.includes(search) |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取指定天数的日期 |
||||
|
* @param day |
||||
|
* @returns {string} |
||||
|
*/ |
||||
|
export function getDateByDay (day) { |
||||
|
var today = new Date() |
||||
|
var targetdaySeconds = today.getTime() + 1000 * 60 * 60 * 24 * day |
||||
|
today.setTime(targetdaySeconds) // 注意,这行是关键代码
|
||||
|
return today.getFullYear() + '-' + zeroFillLeft(today.getMonth() + 1) + '-' + zeroFillLeft(today.getDate()) |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 左侧补0 |
||||
|
* @param value |
||||
|
* @returns {*} |
||||
|
*/ |
||||
|
export function zeroFillLeft (value) { |
||||
|
return (value.toString().length === 1) ? ('0' + value) : value |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 批量给指定对象赋值 |
||||
|
* @param obj obj 指定的对象,一般为vue实例 |
||||
|
* @param obj assignment 赋值的元素 { a: '123' } |
||||
|
*/ |
||||
|
export function assignment (obj, assignment) { |
||||
|
Object.keys(assignment).forEach(key => { |
||||
|
obj[key] = assignment[key] |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 对象转URL |
||||
|
* @param {object} obj |
||||
|
*/ |
||||
|
export const urlEncode = (obj = {}) => { |
||||
|
const result = [] |
||||
|
for (const key in obj) { |
||||
|
const item = obj[key] |
||||
|
if (!item) { |
||||
|
continue |
||||
|
} |
||||
|
if (isArray(item)) { |
||||
|
item.forEach(val => { |
||||
|
result.push(key + '=' + val) |
||||
|
}) |
||||
|
} else { |
||||
|
result.push(key + '=' + item) |
||||
|
} |
||||
|
} |
||||
|
return result.join('&') |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 生成url (带参数) |
||||
|
* @param {string} path 链接 |
||||
|
* @param {object} query query参数 |
||||
|
* @returns {*} |
||||
|
*/ |
||||
|
export function buildUrL (path, query) { |
||||
|
const queryStr = isObject(query) ? urlEncode(query) : query |
||||
|
if (!isEmpty(queryStr)) { |
||||
|
return path + '?' + queryStr |
||||
|
} |
||||
|
return path |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 合并表单数据 (用于form-model) |
||||
|
* @param {object} detail 表单数据 (后端返回) |
||||
|
* @param {object} defaultData 默认数据 |
||||
|
* @returns {object} |
||||
|
*/ |
||||
|
export function assignFormData (detail, defaultData) { |
||||
|
const filterData = _.pick(detail, _.keys(defaultData)) |
||||
|
return Object.assign({}, defaultData, filterData) |
||||
|
} |
||||
Loading…
Reference in new issue