// const baseurl = "/"; //公共路径 测试
// const baseurl = ""; //公共路径 测试
const baseurl = " "; //公共路径 测试
// 请求封装
function request(option) {
const userInfo = uni.getStorageSync('token') || {};
let url = baseurl + option.url; //地址
let data = option.data || {};
let success = option.success;
let method = option.method || 'POST';
uni.request({
url: url,
data: data,
header: {
'User-Token': userInfo.Token
// 'User-Token': "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoi5rWL6K-V5LyB5LiaIiwiaWQiOiI4IiwidXNlcnR5cGUiOiIyIiwiY29tcGFueWlkIjoiMSIsImNvbXBhbnluYW1lIjoi5rWL6K-V5LyB5LiaIiwibmJmIjoxNTk1MzgxNDY1LCJleHAiOjE1OTc4MDA2NjUsImlzcyI6InJlbGlnaW9uYXBpIiwiYXVkIjoiYXBpIn0.HBMi3RKAJYviIDLX5W-ZHeEC4UNZtd8goOcAm7kzCFQ"
},
method: method,
success: function(res) {
// 判断token是否过期
if (res.data.status == 601) {
uni.showModal({
title: '提示',
content: res.data.statusMsg,
showCancel: false,
success: function(res) {
if (res.confirm) {
uni.redirectTo({
url: '../Login/register'
})
}
}
})
return
}
success(res)
},
fail: function(res) {},
complete: function(res) {
// console.log("请求成功")
},
})
}
// 请求封装
function Request1(option) {
const userInfo = uni.getStorageSync('token') || {};
let url = baseurl + option.url; //地址
let data = option.data || {};
let success = option.success;
let method = option.method || 'POST';
uni.request({
url: url,
data: data,
header: {
'content-type': 'application/x-www-form-urlencoded',
"from-source":'app'
//'User-Token': userInfo.Token,
// 'User-Token':"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoi5rWL6K-V5LyB5LiaIiwiaWQiOiI4IiwidXNlcnR5cGUiOiIyIiwiY29tcGFueWlkIjoiMSIsImNvbXBhbnluYW1lIjoi5rWL6K-V5LyB5LiaIiwibmJmIjoxNTk1OTI4MzExLCJleHAiOjE1OTgzNDc1MTEsImlzcyI6InJlbGlnaW9uYXBpIiwiYXVkIjoiYXBpIn0.sQKXgyNVwGVSqp9phxi4CZfl5UslqmYyoSKyDwC_QeU"
},
method: method,
success: function(res) {
// 判断token是否过期
if (res.data.status == 601) {
uni.showModal({
title: '提示',
content: res.data.statusMsg,
showCancel: false,
success: function(res) {
if (res.confirm) {
// uni.redirectTo({
// // url: '../Login/register'
// })
}
}
})
return
}
success(res)
},
fail: function(res) {},
complete: function(res) {
console.log("请求成功")
},
})
}
function Request2(option) {
const userInfo = uni.getStorageSync('token') || {};
let url = option.url; //地址
let data = option.data || {};
let success = option.success;
let method = option.method || 'POST';
uni.request({
url: url,
data: data,
header: {
'content-type': 'application/x-www-form-urlencoded',
'User-Token': userInfo.Token,
// 'User-Token':"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoi5rWL6K-V5LyB5LiaIiwiaWQiOiI4IiwidXNlcnR5cGUiOiIyIiwiY29tcGFueWlkIjoiMSIsImNvbXBhbnluYW1lIjoi5rWL6K-V5LyB5LiaIiwibmJmIjoxNTk1OTI4MzExLCJleHAiOjE1OTgzNDc1MTEsImlzcyI6InJlbGlnaW9uYXBpIiwiYXVkIjoiYXBpIn0.sQKXgyNVwGVSqp9phxi4CZfl5UslqmYyoSKyDwC_QeU"
},
method: method,
success: function(res) {
// 判断token是否过期
if (res.data.status == 601) {
uni.showModal({
title: '提示',
content: res.data.statusMsg,
showCancel: false,
success: function(res) {
if (res.confirm) {
uni.redirectTo({
url: '../Login/register'
})
}
}
})
return
}
success(res)
},
fail: function(res) {},
complete: function(res) {
console.log("请求成功")
},
})
}
// 上传图片封装
function uploadFile(option) {
let url = baseurl + 'file-list/uploadImage'; //服务器地址
let filePath = option.filePath; //要上传文件资源的路径。
let formData = option.formData || {
'user': 'test'
};
let success = option.success;
let name = option.name || 'filePath'; //文件对应的 key
let method = option.method || 'POST'; //默认post请求
uni.uploadFile({
url: url,
filePath: filePath,
name: name,
formData: formData,
header: {
'token': token,
},
success: function(res) {
success(res)
},
fail: function(res) {},
complete: function(res) {},
})
}
// // 添加图片 上传多张图片 到服务器
// upload_pictures() {
// let that = this
// uni.chooseImage({
// count:9, //图片数量
// sizeType: ['copressed', 'original'], //原图货压缩图
// sourceType: ['album', 'camera'], //相册或者拍照
// success: (chooseImageRes) => {
// const arrimg = chooseImageRes.tempFilePaths //临时地址
// for (var i = 0; i < arrimg.length; i++) {
// console.log(arrimg[i])
// httpsTypes.uploadFile({ //上传服务器
// filePath: arrimg[i],
// success: (res) => {
// let arr = JSON.parse(res.data); //转换格式
// let imgurl = arr.msg
// that.img.push(imgurl) //push进数组
// },
// });
// }
// }
// });
// },
export default {
request: request,
uploadFile: uploadFile,
Request1: Request1,
Request2:Request2
};