import {
|
session,
|
} from "@/comm/utils.js"
|
export default {
|
common: {
|
data: {},
|
// header: {
|
// "Content-Type": "application/json;charset=UTF-8"
|
// },
|
method: "GET",
|
dataType: "json",
|
|
},
|
apiCount: {
|
agv_state: 0,
|
laser_data: 0,
|
current_teaching:0
|
},
|
addLog(item) {
|
if (!getApp().globalData.withLog) {
|
return
|
}
|
if (item.statusCode == 200) {
|
const ret = JSON.parse(item.data)
|
if (ret.code == 0) {
|
if (item.url.indexOf("get_agv_state") > 0) {
|
|
if (this.apiCount.agv_state % 20 != 0) {
|
this.apiCount.agv_state++;
|
return
|
}
|
this.apiCount.agv_state = 1;
|
}
|
if (item.url.indexOf("get_current_teaching_data") > 0) {
|
|
if (this.apiCount.current_teaching % 20 != 0) {
|
this.apiCount.current_teaching++;
|
return
|
}
|
this.apiCount.current_teaching = 1;
|
}
|
|
// if (item.url.indexOf("laser_data") > 0) {
|
|
// if (ret.data?.base_map?.image_base64) {
|
// this.apiCount.laser_data++;
|
// } else {
|
// if (this.apiCount.laser_data % 20 != 0) {
|
// this.apiCount.laser_data++;
|
// return
|
// }
|
// this.apiCount.laser_data = 1;
|
// }
|
// }
|
const res = ret.data
|
if (res) {
|
if (item.url.indexOf("getMapUrl") > 0) {
|
if (res.filedata) {
|
res.data_length = res.filedata.length
|
res.filedata = "..."
|
item.data = JSON.stringify(ret)
|
}
|
} else if (item.url.indexOf("laser_data") > 0) {
|
if (res?.base_map?.image_base64) {
|
res.base_map.image_length = res.base_map.image_base64.length
|
res.base_map.image_base64 = "..."
|
item.data = JSON.stringify(ret)
|
}
|
|
}
|
if (JSON.stringify(res).length > 100 * 1024) {
|
// const maxData = session.getValue("request_log_max_data") || {}
|
// const key = `data${new Date().getTime()}`
|
// maxData[key] = item.data
|
// item.data_key = key
|
// session.setValue("request_log_max_data", maxData)
|
ret.data = "..."
|
ret.data_length = JSON.stringify(res).length
|
item.data = JSON.stringify(ret)
|
}
|
}
|
}
|
|
}
|
const list = session.getValue("request_log") || []
|
|
list.unshift(item)
|
|
if (list.length > 512) {
|
const oldItem = list.pop()
|
if (oldItem.data_key) {
|
const maxData = session.getValue("request_log_max_data") || {}
|
delete maxData[oldItem.data_key]
|
session.setValue("request_log_max_data", maxData)
|
}
|
}
|
session.setValue("request_log", list)
|
|
},
|
request(options = {}) {
|
options.data = options.data || this.common.data;
|
options.header = options.header || {
|
"Content-Type": "application/json;charset=UTF-8"
|
};
|
options.method = options.method || this.common.method;
|
options.dataType = options.dataType || this.common.dataType;
|
|
if (options.url.indexOf("get_agv_state") < 0 && options.url.indexOf("laser_data") < 0
|
&& options.url.indexOf("taskGroupStatus") < 0 &&options.url.indexOf("get_current_teaching_data") < 0) {
|
console.log("url", options.url, options.data)
|
}
|
return new Promise((resolve, reject) => {
|
const app = getApp()
|
uni.request({
|
url: options.url,
|
data: options.data,
|
// header: options.header,
|
method: options.method,
|
dataType: options.dataType,
|
success: (result) => {
|
if (options.url.indexOf("get_agv_state") < 0 && options.url.indexOf("laser_data") < 0
|
&& options.url.indexOf("taskGroupStatus") < 0 &&options.url.indexOf("get_current_teaching_data") < 0) { //
|
console.log("result", result)
|
}
|
|
const now = new Date()
|
const date = `${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`
|
if (result.statusCode != 200) {
|
this.addLog({
|
date,
|
method: `${options.method || ""}`,
|
url: options.url,
|
param: options.data,
|
statusCode: result.statusCode,
|
data: ""
|
})
|
if (result.statusCode == 404) {
|
|
return reject({
|
msg: "地址不对,请检查该地址:" + options.url
|
});
|
}
|
return reject({
|
msg: "访问失败,状态码:" + result.statusCode
|
})
|
}
|
|
var ret = result.data
|
if (typeof ret == 'string') {
|
try {
|
try {
|
try {
|
ret = JSON.parse(ret.replace(/\\"/g, '"'));
|
} catch (ex) {
|
ret = JSON.parse(ret.replace(/\\/g, '\\\\'))
|
}
|
} catch (ex) {
|
ret = JSON.parse(ret.replace(/\\"/g, "'").replace(
|
/[\r\n]/g,
|
'<br>').replace(/[\t]/g, ' '));
|
}
|
} catch (ex) {
|
|
return reject({
|
msg: '将【json string】转换为【json object】失败'
|
})
|
}
|
}
|
this.addLog({
|
date,
|
method: `${options.method || ""}`,
|
url: options.url,
|
param: options.data,
|
statusCode: result.statusCode,
|
data: JSON.stringify(ret)
|
})
|
if (ret.code == 0) {
|
|
resolve(ret.data);
|
} else {
|
|
reject({
|
msg: ret.msg || ret.message || ""
|
});
|
}
|
},
|
fail: (err) => {
|
console.log("fail", err)
|
const now = new Date()
|
const date = `${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`
|
this.addLog({
|
date,
|
method: `${options.method || ""}`,
|
url: options.url,
|
param: options.data,
|
statusCode: -1,
|
data: err
|
})
|
return reject(err);
|
}
|
})
|
})
|
},
|
|
}
|