118 lines
3.7 KiB
JavaScript
118 lines
3.7 KiB
JavaScript
import Vue from 'vue'
|
|
import axios from 'axios'
|
|
import router from '@/router'
|
|
import qs from 'qs'
|
|
import merge from 'lodash/merge'
|
|
import {clearLoginInfo} from '@/utils'
|
|
import config from '@/config/index.js'
|
|
export const entryName = '短剧13.2系统';
|
|
|
|
|
|
const baseUrl = config.baseUrl
|
|
// const baseUrl = "http://192.168.1.15:8100/czg/"
|
|
// const baseUrl = "https://video.hnsiyao.cn/czg/"
|
|
// const baseUrl = "https://dj-api.hnsiyao.cn/czg/"
|
|
|
|
// const $urlMap={
|
|
// local:'http://192.168.1.15:8100/czg/',
|
|
// product:'https://dj-api.hnsiyao.cn/czg/',
|
|
// test:'https://dj-api.hnsiyao.cn/czg/',
|
|
// }
|
|
// const $env='product'
|
|
const http = axios.create({
|
|
timeout: 1000 * 300,
|
|
// withCredentials: true,
|
|
headers: {
|
|
'Content-Type': 'application/json; charset=utf-8'
|
|
}
|
|
})
|
|
|
|
/**
|
|
* 请求拦截
|
|
*/
|
|
http.interceptors.request.use(config => {
|
|
config.headers['token'] = Vue.cookie.get('token') // 请求头带上token
|
|
// config.baseURL = $urlMap[$env]
|
|
const arr = config.url.split('admin/')
|
|
config.url = baseUrl + (arr.length >= 2 ? arr[1] : arr[0])
|
|
config.url = config.url.replace('admin//', 'admin/');
|
|
// config.url = "https://dj-api.hnsiyao.cn/czg/"
|
|
return config
|
|
}, error => {
|
|
return Promise.reject(error)
|
|
})
|
|
|
|
/**
|
|
* 响应拦截
|
|
*/
|
|
http.interceptors.response.use(response => {
|
|
if (response.data && response.data.code === 401) { // 401, token失效
|
|
// clearLoginInfo()
|
|
// router.push({name: 'login'})
|
|
}
|
|
return response
|
|
}, error => {
|
|
return Promise.reject(error)
|
|
})
|
|
|
|
/**
|
|
* 请求地址处理
|
|
* @param {*} actionName action方法名称
|
|
*/
|
|
http.adornUrl = (actionName) => {
|
|
// 非生产环境 && 开启代理, 接口前缀统一使用[/proxyApi/]前缀做代理拦截!
|
|
// return (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi/' : window.SITE_CONFIG.baseUrl) + actionName
|
|
// return 'http://192.168.0.132:8100/czg/' + actionName
|
|
// return 'https://duanjuadmin.xianmxkj.com/czg/' + actionName
|
|
actionName = actionName.replace(/^\//, "");
|
|
return baseUrl + actionName
|
|
|
|
}
|
|
http.adornUrlUp = (actionName) => {
|
|
// 非生产环境 && 开启代理, 接口前缀统一使用[/proxyApi/]前缀做代理拦截!
|
|
// return (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi/' : window.SITE_CONFIG.baseUrl) + actionName
|
|
// return 'http://192.168.0.132:8183/czg/' + actionName
|
|
// return 'https://duanjuadmin.xianmxkj.com/czg/' + actionName
|
|
actionName = actionName.replace(/^\//, "");
|
|
return baseUrl + actionName
|
|
|
|
}
|
|
http.adornUrlWX = (actionName) => {
|
|
// 非生产环境 && 开启代理, 接口前缀统一使用[/proxyApi/]前缀做代理拦截!
|
|
// return (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi/' : window.SITE_CONFIG.baseUrl) + actionName
|
|
actionName = actionName.replace(/^\//, "");
|
|
return '/weixin/' + actionName
|
|
// return 'https://api.weixin.qq.com/' + actionName
|
|
// return 'https://dj-api.hnsiyao.cn/czg/' + actionName
|
|
|
|
}
|
|
/**
|
|
* get请求参数处理
|
|
* @param {*} params 参数对象
|
|
* @param {*} openDefultParams 是否开启默认参数?
|
|
*/
|
|
http.adornParams = (params = {}, openDefultParams = false) => {
|
|
var defaults = {
|
|
't': new Date().getTime()
|
|
}
|
|
return openDefultParams ? merge(defaults, params) : params
|
|
}
|
|
|
|
/**
|
|
* post请求数据处理
|
|
* @param {*} data 数据对象
|
|
* @param {*} openDefultdata 是否开启默认数据?
|
|
* @param {*} contentType 数据格式
|
|
* json: 'application/json; charset=utf-8'
|
|
* form: 'application/x-www-form-urlencoded; charset=utf-8'
|
|
*/
|
|
http.adornData = (data = {}, openDefultdata = false, contentType = 'json') => {
|
|
var defaults = {
|
|
't': new Date().getTime()
|
|
}
|
|
data = openDefultdata ? merge(defaults, data) : data
|
|
return contentType === 'json' ? JSON.stringify(data) : qs.stringify(data)
|
|
}
|
|
|
|
export default http
|