cuiqian2004
2025-09-19 d8743368ffda9bc0fb2c6818f695a9a6b1079e57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import {
    session,
} from "@/comm/utils.js"
export default {
    common: {
        data: {},
        // header: {
        //     "Content-Type": "application/json;charset=UTF-8"
        // },
        method: "GET",
        dataType: "json",
        
    },
    addLog(item) {
        const list = session.getValue("request_log") || []
 
        const res = typeof item.data == 'string' ? item.data : JSON.stringify(item.data)
        if (res.length > 64 * 1024) {
            // const maxData = session.getValue("request_log_max_data") || {}
            // const key = `data${new Date().getTime()}`
            // maxData[key] = item.data
            // item.data_key = key
            // delete item.data
            // session.setValue("request_log_max_data", maxData)
             item.data  = res.length
        }
        if(item.url.indexOf("get_agv_state") > 0)
        {
            return
        }
        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)
        {
            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)
                    {
                        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
                        })
                    }
                    this.addLog({
                        date,
                        method: `${options.method || ""}`,
                        url: options.url,
                        param: options.data,
                        statusCode: result.statusCode,
                        data: result.data
                    })
                    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】失败'
                            })
                        }
                    }
                    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);
                }
            })
        })
    },
 
}