http.js
let http = {
'setBaseUrl': (url) => {
if (url.charAt(url.length - 1) === "/") {
url = url.substr(0, url.length - 1)
}
http.baseUrl = url;
},
'header': {},
'beforeRequestFilter': (config) => {
return config
},
'beforeResponseFilter': (res) => {
return res
},
'afterResponseFilter': (successResult) => {},
'get': get,
'post': post,
'request': request,
'uploadFile': uploadFile,
'downloadFile': downloadFile
}
function init(con) {
//url
let url = http.baseUrl;
if (url && con.url && !con.url.match(/^(http|https):\/\/([\w.]+\/?)\S*$/)) {
if (con.url.charAt(0) !== "/") {
con.url = "/" + con.url;
}
con.url = url.concat(con.url);
}
//header
if (http.header != undefined && http.header != null) {
if (!con.header) {
con.header = http.header;
} else {
Object.keys(http.header).forEach(function(key) {
con.header[key] = http.header[key]
});
}
}
}
function request(con) {
init(con);
let config = {
url: con.url ? con.url : http.baseUrl,
data: con.data,
header: con.header,
method: con.method ? con.method : 'GET',
dataType: con.dataType ? con.dataType : 'json',
responseType: con.responseType ? con.responseType : 'text',
success: con.success ? (res) => {
if (res.statusCode && res.statusCode != 200) {
uni.showModal({
content: res.data.msg
})
return;
}
http.afterResponseFilter(con.success(http.beforeResponseFilter(res.data)));
} : null,
fail: con.error ? (res) => {
con.error(res);
} : null,
complete: con.complete ? (res) => {
con.complete(res);
} : null
}
return uni.request(http.beforeRequestFilter(config));
}
function get(params) {
let conf = {};
conf.url = params.url || '';
conf.data = params.data || {};
conf.success = params.success || null;
conf.error = params.error || null;
conf.method = "GET";
return request(conf);
}
function post(params) {
let conf = {};
conf.url = params.url || '';
conf.data = params.data || {};
conf.success = params.success || null;
conf.error = params.error || null;
conf.method = "POST";
return request(conf);
}
function uploadFile(con) {
init(con);
let config = {
url: con.url ? con.url : http.baseUrl,
files: con.files,
filesType: con.filesType,
filePath: con.filePath,
name: con.name,
header: con.header,
formData: con.formData,
success: con.success ? (res) => {
http.afterResponseFilter(con.success(http.beforeResponseFilter(res)));
} : null,
fail: con.fail ? (res) => {
con.fail(res);
} : null,
complete: con.complete ? (res) => {
con.complete(res);
} : null
}
return uni.uploadFile(http.beforeRequestFilter(config));
}
function downloadFile(con) {
init(con);
let config = {
url: con.url ? con.url : http.baseUrl,
header: con.header,
success: con.success ? (res) => {
http.afterResponseFilter(con.success(http.beforeResponseFilter(res)));
} : null,
fail: con.fail ? (res) => {
con.fail(res);
} : null,
complete: con.complete ? (res) => {
con.complete(res);
} : null
}
return uni.downloadFile(http.beforeRequestFilter(config));
}
export default http
http_config.js
import http from './http.js';
//设置域名地址
http.setBaseUrl("");
//设置token
if (uni.getStorageSync('token')) {
http.header['token'] = uni.getStorageSync('token')
}
const AUTH_TOKEN = "X-Auth-Token";
http.beforeResponseFilter = function(res) {
//X-Auth-Token
if (res.header) {
var respXAuthToken = res.header[AUTH_TOKEN.toLocaleLowerCase()];
if (respXAuthToken) {
uni.setStorageSync(AUTH_TOKEN, respXAuthToken);
http.header[AUTH_TOKEN] = respXAuthToken;
}
}
return res;
}
let http_config = {
'http': http
}
export default http_config
main.js
import Vue from 'vue'
import App from './App'
import url from './api.js'
import http_config from './static/js/http_config.js';
Vue.config.productionTip = false
App.mpType = 'app'
Vue.prototype.$url = url
Vue.prototype.$api = Api
let http = http_config.http;
Vue.prototype.$http = http
const app = new Vue({
...App
})
app.$mount()
使用方式
let that = this;
this.$http.get({
'url': this.$url.index.indexSy,
'success': function(res) {
that.IndexList = res.data;
}
});
this.$http.post({
url:this.$url.user_yh.message,
data:{
content:content,
},
success:function(res) {
uni.showToast({
title: '留言成功',
duration: 2000,
icon:'none'
});
}
})