在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