1. 未封装前index.vue页面使用
commitUnqualifiedInfoAndFile:function(filePath){
uni.uploadFile({
url: 'http://' + _self.getIP() + '/' + 'ticket/toCheck',
filePath: filePath,
name: 'image',
formData: {
token : uni.getStorageSync(this.sessionKey),
timestamp : Date.parse(new Date()),
style_id : "7",
production_id : "SCD778",
qualified_num:0
},
success: (res) => {
console.log("res:" + JSON.stringify(res));//json对象转json字符串
console.log("statusCode:" + res.statusCode);
console.log("uniapp上传文件api返回的data是字符串类型:"+ res.data);
var dataString = res.data;//json字符串
var res = JSON.parse(dataString);//json字符串转json对象
console.log("code:"+ res.code);
console.log("msg:"+ res.msg);
uni.showToast({
title:res.data.msg ? res.data.msg : "成功",
icon:'none'
});
console.log("data_1:"+ res.data.qualified_num);
console.log("data_2:"+ res.data.failed_num);
},
fail: (e) => {
console.log("网络请求fail");
},
complete: () => {
console.log("网络请求complete");
}
});
},
备注:后台返回的data值如下
{
"data": {
"msg": "质检完成",
"qualified_num": 41,
"failed_num": "17"
},
"code": "0",
"msg": "success"
}
打印结果:uniapp上传文件api返回的data是字符串类型,需先将data转换为json对象,之后再取里面的值
。
2. main.js中封装网络请求
//上传文件
Vue.prototype.uploadFileRequest = function(param){
var _self = this,
url = param.url || "",
path = param.path || "",
name = param.name || "file",
data = param.data || {},
token = "";
if(url == ""){
url = _self.getUploadFileUrl();//默认的上传文件地址
}else{
url = "http://" + this.getIP() + "/" + url;
}
if(!data.token){
token = uni.getStorageSync(this.sessionKey);
console.log("当前token:" + token);
if(!token){
uni.redirectTo({url:'/pages/login1/login1'});
return;
}else{
data.token = token;
}
}
var timestamp = Date.parse(new Date());//时间戳
data["timestamp"] = timestamp;
console.log("网络请求start:url:" + url + ",params:" +JSON.stringify(data));
uni.uploadFile({
url: url,
filePath: path,
name: name,
formData: data,
success: (res) => {
console.log("网络请求success-res:" + JSON.stringify(res));//json对象转json字符串
console.log("网络请求success-statusCode:" + res.statusCode);
console.log("uniapp上传文件api返回的data是字符串类型:" + res.data);
if (res.statusCode && res.statusCode != 200) {//api错误(Error StatusCode)
uni.showToast({
/* title:res.errMsg */
title:"api错误",
icon:'none'
});
return;
}
var dataString = res.data;//json字符串
var res = JSON.parse(dataString);//json字符串转json对象
if (res.code) {
if (res.code != "0") {//Error ResultCode
uni.showToast({
title:res.msg,
icon:'none'
});
return;
}
} else {//No ResultCode
uni.showToast({
/* title:res.msg */
title:"无结果码",
icon:'none'
});
return;
}
typeof param.success == "function" && param.success(res);
},
fail: (e) => {
console.log("网络请求fail");
uni.showToast({
/* title: e.errMsg */
title:"请检查网络",
icon:'none'
});
typeof param.fail == "function" && param.fail(e.data);
},
complete: () => {
console.log("网络请求complete");
typeof param.complete == "function" && param.complete();
return;
}
});
}
3. 封装后index.vue页面调用
commitUnqualifiedInfoAndFile:function(filePath){
_self.uploadFileRequest({
url:'ticket/toCheck',
path: filePath,
name: 'image',
data:{
style_id : "7",
production_id : "SCD778",
qualified_num:0,
},
success: (res) => {
console.log("res:" + JSON.stringify(res));//json对象转json字符串
uni.showToast({
title:res.data.msg ? res.data.msg : "成功",
icon:'none'
});
},
fail: (e) => {
},
complete: () => {
}
});
},
示例
commitUnqualifiedInfoAndFile:function(){
if(_self.inputValue == ""){
uni.showToast({
title: '未选择原因'
});
return;
}
var p = [];
p.push({
process_id:_self.currentProcess.pro_id,
return_num:1,
failed_reason : _self.inputValue
});
var return_list = JSON.stringify(p);
//上传文件-已封装
_self.uploadFileRequest({
url:'ticket/toCheck',
path: _self.filePath,
name:'image',
data:{
style_id : _self.styleId,
production_id : _self.productionId,
qualified_num:0,
return_list : return_list,
failed_reason : _self.inputValue
},
success: (res) => {
console.log("success:"+ JSON.stringify(res));
uni.showToast({
title:res.data.msg ? res.data.msg : "成功",
icon:'none'
});
_self.qualifiedQuantity = res.data.qualified_num ? res.data.qualified_num : 0;
_self.unqualifiedQuantity = res.data.failed_num ? res.data.failed_num : 0;
//恢复默认
_self.inputValue = "";
_self.reasonList.forEach(function(item){
item.isSelected = false;
});
_self.togglePopup("");
_self.filePath = "";
},
fail: (e) => {
},
complete: () => {
}
});
return;
//上传文件-未封装
uni.uploadFile({
url: 'http://' + _self.getIP() + '/' + 'ticket/toCheck',
filePath: _self.filePath,
name: 'image',
formData: {
token : uni.getStorageSync(this.sessionKey),
timestamp : Date.parse(new Date()),
style_id : _self.styleId,
production_id : _self.productionId,
qualified_num:0,
return_list : return_list,
failed_reason : _self.inputValue
},
success: (res) => {
console.log("success-res:"+ JSON.stringify(res));
console.log("success-data:"+ res.data);
var dataString = res.data;//json字符串
var res = JSON.parse(dataString);//json字符串转json对象
uni.showToast({
title:res.data.msg ? res.data.msg : "成功",
icon:'none'
});
_self.qualifiedQuantity = res.data.qualified_num ? res.data.qualified_num : 0;
_self.unqualifiedQuantity = res.data.failed_num ? res.data.failed_num : 0;
//恢复默认
_self.inputValue = "";
_self.reasonList.forEach(function(item){
item.isSelected = false;
});
_self.togglePopup("");
_self.filePath = "";
},
fail: (e) => {
console.log("网络请求fail");
},
complete: () => {
console.log("网络请求complete");
}
});
},
网友评论