cshenfu
2024-06-27 58aa6e282bb343503be3cb198948328ea3ed7b79
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
 * 百度API
 */
var baidu = {};
var vk = {};
var baiduOpenApiAccessTokenCacheName = "vk_openapi_baidu_open_token";
baidu.init = function(util={}) {
    vk =  util.vk;
};
 
// 百度开放平台api
baidu.open = {};
// 文字识别
baidu.open.ocr = {};
 
/**
 * 营业执照识别
 * 以下data参数三选一即可
 * @param {File} file     文件对象
 * @param {String} image  图像base64编码后进行urlencode
 * @param {String} url    图片完整URL
 *
vk.openapi.baidu.open.ocr.business_license({
    title:"识别中...",
    data: {
        file: res.tempFiles[0]
    },
    success: (res) => {
        that.data = res.data;
    },
});
 */
baidu.open.ocr.business_license = function(obj={}){
    obj.action = "ocr/v1/business_license";
    let file = obj.data.file;
    if(file){
        vk.pubfn.fileToBase64({ file }).then(base64 => {
            obj.data.image = base64;
            delete obj.data.file;
            baidu.request(obj);
        });
    }else{
        baidu.request(obj);
    }
}
 
/**
 * 身份证识别
 * 以下data参数三选一即可
 * @param {File} file     文件对象
 * @param {String} image  图像base64编码后进行urlencode
 * @param {String} url    图片完整URL
vk.openapi.baidu.open.ocr.idcard({
    title:"识别中...",
    data: {
        image:base64,
        id_card_side:"front", // front:身份证含照片的一面 back:身份证带国徽的一面
    },
    success: (res) => {
        that.data = res.data;
    },
});
 */
baidu.open.ocr.idcard = function(obj={}){
    obj.action = "ocr/v1/idcard";
    let file = obj.data.file;
    if(!obj.data.id_card_side) obj.data.id_card_side = "front";
    if(file){
        vk.pubfn.fileToBase64({ file }).then(base64 => {
            obj.data.image = base64;
            delete obj.data.file;
            baidu.request(obj);
        });
    }else{
        baidu.request(obj);
    }
}
 
/**
 * 百度开放平台通用请求接口
 * @param {String} action        接口名称
 * @param {String} actionVersion 接口版本名称 默认2.0
 * @param {String} title         loading文字
 * @param {object} data          请求参数
 * @param {String} success       成功回调
 * @param {String} fail          失败回调
 * @param {String} complete      完成回调
 * 使用示例
vk.openapi.baidu.request({
     action: 'ocr/v1/business_license',
     title:"识别中...",
     data: {
         image:base64
     },
     success: (data) => {
         that.data = data;
     },
});
 */
baidu.request = function(obj={}){
    let {
        title
    } = obj;
    if(title) vk.showLoading(title);
    let baiduApiAccessToken = vk.getStorageSync(baiduOpenApiAccessTokenCacheName);
    if(baiduApiAccessToken && baiduApiAccessToken.expTime > new Date().getTime()){
        // 获取缓存中的token
        obj.accessToken = baiduApiAccessToken.accessToken;
        request(obj);
    }else{
        // 发起请求获取token
        vk.callFunction({
            url: 'plugs/baidu/client/pub/getAccessToken',
            success:function(tokenRes) {
                vk.setStorageSync(baiduOpenApiAccessTokenCacheName,{
                    accessToken : tokenRes.access_token,
                    expTime :  2590000000 + new Date().getTime(),
                });
                obj.accessToken = tokenRes.access_token;
                request(obj);
            },
            fail:function(res){
                if(title) vk.hideLoading();
                if(typeof obj.fail === "function") obj.fail(res);
                if(typeof obj.complete === "function") obj.complete(res);
            }
        });
    }
}
 
function request(obj={}){
    let {
        action,
        actionVersion = "2.0",
        accessToken,
        header = { "content-type": "application/x-www-form-urlencoded" },
        data,
        title
    } = obj;
    vk.request({
        url: `https://aip.baidubce.com/rest/${actionVersion}/${action}?access_token=${accessToken}`,
        method:"POST",
        header:{
            "content-type": "application/x-www-form-urlencoded",
        },
        errorCodeName:"error_code",
        errorMsgName:"error_msg",
        data,
        needAlert:true,
        success: function(data){
            if(title) vk.hideLoading();
            if(data.code){
                if(typeof obj.fail === "function") obj.fail(data);
            }else{
                if(typeof obj.success === "function") obj.success(data);
            }
        },
        fail: function(data){
            if(title) vk.hideLoading();
            // 如果是因为token失效报的错误,则清空下本地token缓存
            if(data && data.code === 110) vk.removeStorageSync(baiduOpenApiAccessTokenCacheName);
            if(typeof obj.fail === "function") obj.fail(data);
        },
        complete: function(data){
            if(typeof obj.complete === "function") obj.complete(data);
        }
    });
}
 
export default baidu;