zhiyong.zhou
2024-02-26 60d911172b1dbebe0ab952ce10366b327d5744f1
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
import application from '@/config/application';
import shiro from '@/common/shiro';
import utils from '@/common/utils'
 
const deepEncode = (obj) => {
    let cloneObj = null
    //深拷贝之后的对象
    if (obj && typeof (obj) != "object") {
        //如果不是对象
        return obj
    }
    cloneObj = Array.isArray(obj) ? [] : {}
    //判断是对象还是数组
    for (const key in obj) {
        if (obj.hasOwnProperty(key)) {
            if (typeof (obj[key]) == "array") {
                cloneObj[key] = obj[key].map(deepEncode);
            }
            if (obj[key] && typeof (obj[key]) == "object") {
                //判断对象的属性是不是object
                cloneObj[key] = deepEncode(obj[key]);
            } else {
                cloneObj[key] = utils.encode(obj[key]);
            }
        }
    }
    return cloneObj
}
 
let REQUEST_NUM = 0;
let TIME_OUT;
const service = async (url, {data, method, handleData, interceptorCheck = true, options = {}}) => {
    if (TIME_OUT) {
        clearTimeout(TIME_OUT);
    }
    TIME_OUT = window.setTimeout(() => {
        REQUEST_NUM = 0;
        uni.hideLoading();
    }, options.timeout || 5000);
 
    if (REQUEST_NUM++ === 0) {
        uni.showLoading({
            mask: true,
            title: '加载中'
        });
    }
    const [_, result] = await uni.request({
        header: {
            Userid: shiro.getUserId(),
        },
        url: `${application.baseURL}${url}?userId=${shiro.getUserId()}`,
        data: deepEncode({
            ...data,
            token: shiro.getUserId()
        }),
        method
    });
    if (typeof result.data === 'string') {
        result.data = JSON.parse(result.data.replace(/[\t\r\n]/g, ''));
    }
    console.log(result)
    if (--REQUEST_NUM === 0) {
        uni.hideLoading();
    }
 
    if (!result.data.success) {
        if (interceptorCheck) {
            uni.showModal({
                title: '提示',
                content: result.data.message,
                showCancel: false,
                confirmText: '确定'
            });
        }
        throw '';
    }
    if (handleData) {
        return handleData(result);
    }
    // if(url==='/rootjemin/invoice/saveInvoiceOrder'){
    //      return result.data.success ? result.data.ids : null;
    // }
    return result.data.success ? result.data.data : null;
}
 
export default service;