Commit 14a2170c authored by Nature's avatar Nature

修改http配置 添加http导出

parent 89cc875d
This diff is collapsed.
This diff is collapsed.
// 引入axios // 引入axios
import axios from 'axios' import axios from 'axios'
import hlsPopup from './hlsPopup' import hlsPopup from './hlsPopup'
...@@ -13,13 +14,16 @@ axios.interceptors.request.use(config => { ...@@ -13,13 +14,16 @@ axios.interceptors.request.use(config => {
} else { } else {
promiseArr[config.url] = cancel promiseArr[config.url] = cancel
} }
config.cancelToken = new CancelToken(c => {
cancel = c
})
return config return config
}, error => { }, error => {
return Promise.reject(error) return Promise.reject(error)
}) })
// 响应拦截器即异常处理 // 响应拦截器即异常处理
axios.interceptors.response.use(response => { axios.interceptors.response.use(response => {
if (process.env.debug) { if ($config.debug) {
let postName = 'post' let postName = 'post'
console.log(postName + ' success') console.log(postName + ' success')
console.log(postName + ' response ' + JSON.stringify(response.data, '', 2)) console.log(postName + ' response ' + JSON.stringify(response.data, '', 2))
...@@ -79,7 +83,29 @@ axios.interceptors.response.use(response => { ...@@ -79,7 +83,29 @@ axios.interceptors.response.use(response => {
} else { } else {
err.message = '连接到服务器失败' err.message = '连接到服务器失败'
} }
hlsPopup.showError(err.message) if (err.response && err.response.status === 401) {
hlsPopup.hideLoading()
hlsPopup.showPopup({
title: '登录失效,重新登录',
onConfirm: () => {
// router.push({name: 'Login'})
if (vum.Platform.isIOS()) {
cordova.exec(null, null, 'BridgePlugin', 'closeWebView', [])
} else if (vum.Platform.isIOS()) {
var dict = {
'className': 'WebBridge',
'function': 'close',
'successCallBack': 'sCallBack',
'failureCallBack': 'eCallBack',
}
HandBridge.postMessage(JSON.stringify(dict))
}
},
})
} else {
hlsPopup.hideLoading()
hlsPopup.showError(err.message)
}
return Promise.resolve(err) return Promise.resolve(err)
}) })
axios.defaults.baseURL = '' axios.defaults.baseURL = ''
...@@ -98,15 +124,17 @@ export function get (url) { ...@@ -98,15 +124,17 @@ export function get (url) {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
} }
} }
if ($config.debug) {
let postName = 'GET'
console.log(postName + ' Start!')
console.log(postName + ' url ' + url)
}
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios({ axios({
method: 'get', method: 'get',
url, url,
headers: headers, headers: headers,
params: param, params: param,
cancelToken: new CancelToken(c => {
cancel = c
}),
}).then(res => { }).then(res => {
resolve(res) resolve(res)
}).catch(err => { }).catch(err => {
...@@ -129,7 +157,7 @@ export function post (url, param) { ...@@ -129,7 +157,7 @@ export function post (url, param) {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
} }
} }
if (process.env.debug) { if ($config.debug) {
let postName = 'POST' let postName = 'POST'
console.log(postName + ' Start!') console.log(postName + ' Start!')
console.log(postName + ' url ' + url) console.log(postName + ' url ' + url)
...@@ -141,9 +169,6 @@ export function post (url, param) { ...@@ -141,9 +169,6 @@ export function post (url, param) {
headers: headers, headers: headers,
url, url,
data: param, data: param,
cancelToken: new CancelToken(c => {
cancel = c
}),
}).then(res => { }).then(res => {
resolve(res) resolve(res)
}).catch(err => { }).catch(err => {
......
...@@ -30,7 +30,13 @@ import componentInstall from './components/component' ...@@ -30,7 +30,13 @@ import componentInstall from './components/component'
import appStyle from '../packages/common/styles/app.core.less' import appStyle from '../packages/common/styles/app.core.less'
const version = '0.0.1' import hlsPopup from '../packages/common/scripts/hlsPopup'
import hlsUtil from '../packages/common/scripts/hlsUtil'
import { post, get } from '../packages/common/scripts/hlsHttp'
import directives from '../packages/common/scripts/directives'
import filter from '../packages/common/scripts/filter'
const version = '0.0.2'
export { export {
appStyle, appStyle,
...@@ -62,4 +68,10 @@ export { ...@@ -62,4 +68,10 @@ export {
NumberKeyboardPlugin, NumberKeyboardPlugin,
version, version,
componentInstall, componentInstall,
hlsPopup,
hlsUtil,
post,
get,
directives,
filter,
} }
...@@ -9,14 +9,7 @@ import router from './router/index' ...@@ -9,14 +9,7 @@ import router from './router/index'
import flexible from './common/ydui.flexible' import flexible from './common/ydui.flexible'
import {componentInstall, appStyle} from '../packages/index' import {componentInstall, appStyle, hlsUtil, get, post, hlsPopup, directives, filter} from '../packages/index'
/**
* 指令
*/
import directives from './scripts/directives'
import filter from './scripts/filter'
/** /**
* 组件 * 组件
...@@ -34,20 +27,6 @@ import { ...@@ -34,20 +27,6 @@ import {
import './scripts/prototype' import './scripts/prototype'
import './scripts/vuePlatform' import './scripts/vuePlatform'
/**
* 弹框组件
*/
import hlsPopup from './scripts/hlsPopup'
/**
* http
*/
import { post, get } from './scripts/hlsHttp'
/** 全局函数hlsUtil**/
import hlsUtil from './scripts/hlsUtil'
/** end**/ /** end**/
if (process.env.CONFIG_ENV === 'uat') { if (process.env.CONFIG_ENV === 'uat') {
...@@ -88,6 +67,7 @@ let hlsHttp = { ...@@ -88,6 +67,7 @@ let hlsHttp = {
get: get, get: get,
post: post, post: post,
} }
Vue.prototype.hlsHttp = window.hlsHttp = hlsHttp Vue.prototype.hlsHttp = window.hlsHttp = hlsHttp
Vue.prototype.hlsUtil = window.hlsUtil = hlsUtil Vue.prototype.hlsUtil = window.hlsUtil = hlsUtil
......
...@@ -205,6 +205,7 @@ export default { ...@@ -205,6 +205,7 @@ export default {
font-size: 16px; font-size: 16px;
letter-spacing: 1px; /*no*/ letter-spacing: 1px; /*no*/
text-align: left; text-align: left;
word-break: break-all;
} }
.triangle-left { .triangle-left {
left: 45px; left: 45px;
......
This diff is collapsed.
export default (Vue) => {
Vue.filter('currency', function (val) {
if (!val) return '0.00'
var intPart = Number(val).toFixed(0) // 获取整数部分
var intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') // 将整数部分逢三一断
var floatPart = '.00' // 预定义小数部分
var value2Array = (val + '').split('.')
// =2表示数据有小数位
if (value2Array.length === 2) {
floatPart = value2Array[1].toString() // 拿到小数部分
if (floatPart.length === 1) { // 补0,实际上用不着
return intPartFormat + '.' + floatPart + '0'
} else {
return intPartFormat + '.' + floatPart
}
} else {
return intPartFormat + floatPart
}
})
Vue.filter('datetime', timestamp => {
function format (number) {
return number.toString().padStart(2, '0')
}
const date = new Date(Number.parseInt(timestamp, 10))
const YYYY = date.getFullYear()
const MM = date.getMonth() + 1
const DD = date.getDate()
const hh = date.getHours()
const mm = date.getMinutes()
const ss = date.getSeconds()
return `${YYYY}-${format(MM)}-${format(DD)} ${format(hh)}:${format(mm)}:${format(ss)}`
})
}
// 引入axios
import axios from 'axios'
import hlsPopup from './hlsPopup'
import router from '../router/index'
let promiseArr = {}
let cancel = {}
const CancelToken = axios.CancelToken
// 请求拦截器
axios.interceptors.request.use(config => {
// 发起请求时,取消掉当前正在进行的相同请求
if (promiseArr[config.url]) {
promiseArr[config.url]('操作取消')
promiseArr[config.url] = cancel
} else {
promiseArr[config.url] = cancel
}
return config
}, error => {
return Promise.reject(error)
})
// 响应拦截器即异常处理
axios.interceptors.response.use(response => {
if (process.env.debug) {
let postName = 'post'
console.log(postName + ' success')
console.log(postName + ' response ' + JSON.stringify(response.data, '', 2))
console.log(postName + ' End!')
}
if (response.data.result === 'E' || response.data.code === 'E') {
hlsPopup.hideLoading()
const err = {}
err.message = response.data.message
hlsPopup.showError(err.message)
return Promise.resolve(err)
} else {
return response.data
}
}, err => {
if (err && err.response) {
switch (err.response.status) {
case 400:
err.message = '错误请求'
break
case 401:
err.message = '登录已失效,请重新登录'
break
case 403:
err.message = '拒绝访问'
break
case 404:
err.message = '请求错误,未找到该资源'
break
case 405:
err.message = '不支持的请求类型'
break
case 408:
err.message = '请求超时'
break
case 500:
err.message = '服务器端出错'
break
case 501:
err.message = '网络未实现'
break
case 502:
err.message = '网络错误'
break
case 503:
err.message = '服务不可用'
break
case 504:
err.message = '网络超时'
break
case 505:
err.message = 'http版本不支持该请求'
break
default:
err.message = `连接错误${err.response.status}`
}
} else {
err.message = '连接到服务器失败'
}
if (err.response && err.response.status === 401) {
hlsPopup.showPopup({
title: '登录失效,重新登录',
onConfirm: () => {
router.push({name: 'Login'})
},
})
} else {
hlsPopup.showError(err.message)
}
return Promise.resolve(err)
})
axios.defaults.baseURL = ''
axios.defaults.timeout = 10000
// get请求
export function get (url) {
let param = {}
let headers = {}
if (window.localStorage.access_token) {
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + window.localStorage.access_token,
}
} else {
headers = {
'Content-Type': 'application/json',
}
}
return new Promise((resolve, reject) => {
axios({
method: 'get',
url,
headers: headers,
params: param,
cancelToken: new CancelToken(c => {
cancel = c
}),
}).then(res => {
resolve(res)
}).catch(err => {
reject(err)
})
})
}
// post请求
export function post (url, param) {
param.user_id = window.localStorage.user_id
param.access_token = window.localStorage.access_token
let headers = {}
if (window.localStorage.access_token) {
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + window.localStorage.access_token,
}
} else {
headers = {
'Content-Type': 'application/json',
}
}
if (process.env.debug) {
let postName = 'POST'
console.log(postName + ' Start!')
console.log(postName + ' url ' + url)
console.log(postName + ' parameter ' + JSON.stringify(param, '', 2))
}
return new Promise((resolve, reject) => {
axios({
method: 'post',
headers: headers,
url,
data: param,
cancelToken: new CancelToken(c => {
cancel = c
}),
}).then(res => {
resolve(res)
}).catch(err => {
reject(err)
})
})
}
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment