wms-pda迁移
This commit is contained in:
5
static/js/http/baseApi.js
Normal file
5
static/js/http/baseApi.js
Normal file
@@ -0,0 +1,5 @@
|
||||
const BaseApi = 'https://api.wms.test.f2b211.com/api' //测试
|
||||
//const BaseApi = 'https://wmsapi.f2b211.com/api' // 正式
|
||||
export {
|
||||
BaseApi
|
||||
}
|
||||
114
static/js/http/index.js
Normal file
114
static/js/http/index.js
Normal file
@@ -0,0 +1,114 @@
|
||||
import http from './interface'
|
||||
export const $http = (url, method, data, json) => {
|
||||
//设置请求前拦截器
|
||||
http.interceptor.request = (config) => {
|
||||
uni.showLoading({
|
||||
title: '加载中...'
|
||||
})
|
||||
config.header = {
|
||||
'content-type': json ? 'application/json' : 'application/x-www-form-urlencoded',
|
||||
"Authorization": uni.getStorageSync('token')
|
||||
}
|
||||
}
|
||||
//设置请求结束后拦截器
|
||||
http.interceptor.response = async (response) => {
|
||||
//判断返回状态 执行相应操作
|
||||
uni.hideLoading()
|
||||
// 请根据后端规定的状态码判定
|
||||
if (response.data.status === 401) { //token失效
|
||||
console.log('请根据后端规定的状态码判定', response.data.status, response.data.message)
|
||||
uni.showToast({
|
||||
title: response.data.message,
|
||||
icon: 'none',
|
||||
duration: 1500
|
||||
})
|
||||
// return response.data = await doRequest(response, url)//动态刷新token,并重新完成request请求
|
||||
// 登录失效,退回登录也面重新登录
|
||||
uni.reLaunch({
|
||||
url: "/pages/login"
|
||||
})
|
||||
} else {
|
||||
if (response.data.status !== 200 && response.data.message) {
|
||||
uni.showToast({
|
||||
title: response.data.message,
|
||||
icon: 'none',
|
||||
duration: 1500
|
||||
})
|
||||
}
|
||||
}
|
||||
return response.data;
|
||||
}
|
||||
return http.request({
|
||||
method: method,
|
||||
url: url,
|
||||
dataType: 'json',
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
async function login() {
|
||||
//返回环宇token所需的login code
|
||||
return new Promise(resolve => {
|
||||
uni.login({
|
||||
provider: 'weixin',
|
||||
success(loginRes) {
|
||||
resolve(loginRes.code)
|
||||
},
|
||||
fail() {}
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
async function doRequest(response, url) {
|
||||
var code = await login()
|
||||
var res = await post('/Login/RefreshToken', {})
|
||||
if (res && res.data.data.token) {
|
||||
let config = response.config
|
||||
uni.setStorageSync("token", res.data.data.token);
|
||||
config.header['Authorization'] = res.data.data.token
|
||||
let json = config.header["Content-Type"] === 'application/json'
|
||||
const resold = await $http(url, config.method, {
|
||||
...config.data
|
||||
}, json)
|
||||
return resold
|
||||
} else {
|
||||
uni.clearStorage()
|
||||
uni.showToast({
|
||||
title: "授权失效,请重新登录",
|
||||
duration: 1000,
|
||||
})
|
||||
uni.redirectTo({
|
||||
url: '/pages/login'
|
||||
})
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
function postJson(url, data) {
|
||||
return $http(url, 'POST', data)
|
||||
}
|
||||
|
||||
function get(url, data) {
|
||||
|
||||
return $http(url, 'GET', data)
|
||||
}
|
||||
|
||||
function post(url, data) {
|
||||
return $http(url, 'POST', data, true)
|
||||
}
|
||||
|
||||
function put(url, data) {
|
||||
return $http(url, 'PUT', data, true)
|
||||
}
|
||||
|
||||
function del(url, data) {
|
||||
return $http(url, 'DELETE', data, true)
|
||||
}
|
||||
|
||||
export default {
|
||||
postJson,
|
||||
get,
|
||||
post,
|
||||
put,
|
||||
del
|
||||
}
|
||||
114
static/js/http/interface.js
Normal file
114
static/js/http/interface.js
Normal file
@@ -0,0 +1,114 @@
|
||||
import {
|
||||
BaseApi
|
||||
} from './baseApi.js'
|
||||
export default {
|
||||
config: {
|
||||
baseUrl: BaseApi,
|
||||
header: {
|
||||
'Content-Type':'application/json;charset=UTF-8',
|
||||
'Content-Type':'application/x-www-form-urlencoded'
|
||||
},
|
||||
data: {},
|
||||
method: "GET",
|
||||
dataType: "json", /* 如设为json,会对返回的数据做一次 JSON.parse */
|
||||
responseType: "text",
|
||||
success() {},
|
||||
fail() {},
|
||||
complete() {}
|
||||
},
|
||||
interceptor: {
|
||||
request: null,
|
||||
response: null
|
||||
},
|
||||
request(options) {
|
||||
if (!options) {
|
||||
options = {}
|
||||
}
|
||||
options.baseUrl = options.baseUrl || this.config.baseUrl
|
||||
options.dataType = options.dataType || this.config.dataType
|
||||
options.url = options.baseUrl + options.url
|
||||
options.data = options.data || {}
|
||||
options.method = options.method || this.config.method
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
let _config = null
|
||||
options.complete = (response) => {
|
||||
let statusCode = response.statusCode
|
||||
response.config = _config
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
if (statusCode === 200) {
|
||||
////console.log("【" + _config.requestId + "】 结果:" + JSON.stringify(response.data))
|
||||
}
|
||||
}
|
||||
if (this.interceptor.response) {
|
||||
let newResponse = this.interceptor.response(response)
|
||||
if (newResponse) {
|
||||
response = newResponse
|
||||
}
|
||||
}
|
||||
// 统一的响应日志记录
|
||||
//_reslog(response)
|
||||
if (statusCode === 200) { //成功
|
||||
resolve(response);
|
||||
} else {
|
||||
reject(response)
|
||||
}
|
||||
}
|
||||
_config = Object.assign({}, this.config, options)
|
||||
_config.requestId = new Date().getTime()
|
||||
if (this.interceptor.request) {
|
||||
this.interceptor.request(_config)
|
||||
}
|
||||
// 统一的请求日志记录
|
||||
//_reqlog(_config)
|
||||
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
//console.log("【" + _config.requestId + "】 地址:" + _config.url)
|
||||
if (_config.data) {
|
||||
//console.log("【" + _config.requestId + "】 参数:" + JSON.stringify(_config.data))
|
||||
}
|
||||
}
|
||||
uni.request(_config);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 请求接口日志记录
|
||||
*/
|
||||
function _reqlog(req) {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
//console.log("【" + req.requestId + "】 地址:" + req.url)
|
||||
if (req.data) {
|
||||
//console.log("【" + req.requestId + "】 请求参数:" + JSON.stringify(req.data))
|
||||
}
|
||||
}
|
||||
//TODO 调接口异步写入日志数据库
|
||||
}
|
||||
|
||||
/**
|
||||
* 响应接口日志记录
|
||||
*/
|
||||
function _reslog(res) {
|
||||
let _statusCode = res.statusCode;
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
//console.log("【" + res.config.requestId + "】 地址:" + res.config.url)
|
||||
if (res.config.data) {
|
||||
//console.log("【" + res.config.requestId + "】 请求参数:" + JSON.stringify(res.config.data))
|
||||
}
|
||||
//console.log("【" + res.config.requestId + "】 响应结果:" + JSON.stringify(res))
|
||||
}
|
||||
//TODO 除了接口服务错误外,其他日志调接口异步写入日志数据库
|
||||
switch(_statusCode){
|
||||
case 200:
|
||||
break;
|
||||
case 401:
|
||||
break;
|
||||
case 404:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user