封装uni-app的request请求

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'
				});
			
		    }
		})
	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值