uni-app请求接口封装

在common文件夹新建mAjax.js

module.exports = {

	get: function(url, data, callback,contentType, headers) {
		switch (contentType) {
			case "form":
				var headerObj = {
					'content-type': 'application/x-www-form-urlencoded',
					// 'cookie':getApp().globalData.cookie
                    //一般小程序没有token(小程序使用将下面一行注释掉)
					token: uni.getStorageSync('token')
				};
				break;
			case "json":
				var headerObj = {
					'content-type': 'application/json',
					// 'cookie':getApp().globalData.cookie
					token: uni.getStorageSync('token')
				};
				break;
			default:
				var headerObj = {
					'content-type': 'application/x-www-form-urlencoded',
					// 'cookie':getApp().globalData.cookie
					token: uni.getStorageSync('token')
				};
		}
		for (var k in headers) {
			headerObj[k] = headers[k];
		}
		uni.request({
			url: url,
			data: data,
			method: "GET",
			dataType: "json",
			header: headerObj,
			success: (res) => {
				callback(res.data);
			},
			fail: () => {
				uni.hideLoading()
				uni.stopPullDownRefresh()
				uni.showToast({
					title: "网络请求失败",
					icon: "none"
				});
			},
			complete: () => {

			}
		});
	},

	post: function(url, data, callback, type, errorback = () => {}, contentType, headers) {
		switch (contentType) {
			case "form":
				var headerObj = {
					'content-type': 'application/x-www-form-urlencoded',
					// 'cookie':getApp().globalData.cookie
					token: uni.getStorageSync('token')
				};
				break;
			case "json":
				var headerObj = {
					'content-type': 'application/json',
					// 'cookie':getApp().globalData.cookie
					token: uni.getStorageSync('token')
				};
				break;
			default:
				var headerObj = {
					'content-type': 'application/x-www-form-urlencoded',
					// 'cookie':getApp().globalData.cookie
					token: uni.getStorageSync('token')
				};
		}
		for (var k in headers) {
			headerObj[k] = headers[k];
		}
		uni.request({
			url: url,
			data: data,
			method: "POST",
			dataType: "json",
			header: headerObj,
			success: (res) => {
				if (res.data.code === 401) {
					if (data.isIdx == 1) {
						uni.reLaunch({
							url: '/pages/login/login'
						})
						setTimeout(() => {
							plus.navigator.closeSplashscreen()
						}, 1000)
					} else {
						uni.showModal({
							title: '提示',
							content: '登录信息已过期请重新登录',
							confirmColor: '#007AFF',
							showCancel: false,
							success: () => {
								uni.reLaunch({
									url: '/pages/login/login'
								})
							}
						})
					}
				} else {
					callback(res.data);
				}
			},
			fail: (error) => {
				uni.showToast({
					title: "网络请求失败",
					icon: "none"
				});
				uni.hideLoading()
				uni.stopPullDownRefresh()
				errorback(error)
			},
			complete: () => {

			}
		});
	},

	imgUpload: function(url, data, imgName, callback) {
		uni.chooseImage({
			count: 1,
			success: (chooseImageRes) => {
				const tempFilePaths = chooseImageRes.tempFilePaths;
				uni.showLoading({
					title: '上传中',
					mask: true
				})
				uni.uploadFile({
					url: url,
					filePath: tempFilePaths[0],
					name: imgName,
					formData: data,
					success: (uploadFileRes) => {
						callback(uploadFileRes.data);
					},
					fail: () => {
						uni.showToast({
							title: "网络请求失败",
							icon: "none"
						});
					},
					complete() {
						uni.hideLoading()
					}
				})
			}
		})
	},
}

在common文件夹新建request.js引用mAjax.js

import {
	get,
	post,
	imgUpload
} from './mAjax.js'

function loading() {
	uni.showLoading({
		title: '请稍后',
		mask: true
	})
}

//未全局 修改头像
function request() {
	this.baseUrl = 'http://39.105.197.43:503/api'
	
	buildRequest(this.baseUrl)
}

function buildRequest(baseUrl) {
	
	
	request.prototype.send = function(params, callBack) {
		post(baseUrl + '/user/login', params, callBack)
	}
}
export default {
	request
}

main.js全局注册

import Api from './common/requests.js'
Vue.prototype._Api = new Api.request

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值