idinfo.js
5.9 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
var OSSInit;
var baseUrl = getApp().globalData.baseUrl;
var OSSUrl = getApp().globalData.OSSUrl;
Page({
/**
* 页面的初始数据
*/
data: {
facepath: '',
front_card_info: {},
backpath: '',
back_card_info: {},
idInfo: {},
flag: ''
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
this.initOSS()
},
initOSS: function() {
var that = this
var Authorization = getApp().globalData.Authorization;
//OSS 上传前init
wx.showLoading()
wx.request({
url: baseUrl + "filemeta/v1/inits",
header: {
'Authorization': Authorization
},
method: 'POST',
data: {
"access_type": "web_upload",
"action ": "put_object",
"instance_id": "",
"object_type": "wx_image"
},
success: function(result) {
OSSInit = result.data
console.log(' OSS init 成功', OSSInit)
},
fail: function(res) {
console.log('OSS init 失败', res)
},
complete: function() {
wx.hideLoading()
}
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function() {
},
chooseImage: function(e) {
console.log(e)
var that = this
wx.chooseImage({
sourceType: ['camera', 'album'],
sizeType: ['original'],
count: 1,
success: function(res) {
console.log('success', res)
if ("id_face" == e.currentTarget.id) {
that.setData({
facepath: res.tempFilePaths[0]
})
that.uploadImage(that.data.facepath, 'id_face')
} else if ("id_back" == e.currentTarget.id) {
that.setData({
backpath: res.tempFilePaths[0]
})
that.uploadImage(that.data.backpath, 'id_back')
}
}
})
},
uploadImage: function(path, str) {
var that = this
wx.showLoading({
title: '上传图片中...',
})
console.log('key', 'imagepath_' + path.substring(path.length - 10, path.length))
wx.uploadFile({
url: OSSUrl,
filePath: path,
name: 'file',
formData: {
'key': 'imagepath_' + path.substring(path.length - 10, path.length)+".png",
'OSSAccessKeyId': OSSInit.access_key_id,
'policy': OSSInit.policy,
'signature': OSSInit.signature,
'callback': OSSInit.callback_body,
'x:access_token': OSSInit.callback_token,
'success_action_status': '200',
},
success: function(res) {
console.log('uploadFile',res.data)
if (res.statusCode == 200) {
var data = JSON.parse(res.data)
console.log('上传成功', res)
that.oss_bucket = data.bucket
if ("id_face" == str) {
that.setData({
front_card_info: data
})
} else if ("id_back" == str) {
that.setData({
back_card_info: data
})
}
that.checkAndupload()
} else {
var title = '图片上传失败,请重新上传'
if (res.statusCode == 413) {
title = '图片体积过大,请选择较小图片上传'
}
wx.showModal({
title: '上传失败',
content: title,
showCancel: false,
confirmColor: '#4E8FE7'
})
}
wx.hideLoading()
},
fail: function(err) {
console.log('fail', err)
wx.showModal({
title: '上传失败',
content: '图片上传失败,请重新上传',
showCancel: false,
confirmColor: '#4E8FE7'
})
wx.hideLoading()
},
complete(res){
console.log('complete', res)
wx.hideLoading()
}
})
},
checkAndupload: function() {
var that = this
var frontdata = that.data.front_card_info
var backdata = that.data.back_card_info
console.log('frontdata', frontdata)
console.log('backdata', backdata)
if (!frontdata || !frontdata.object_id) {
return;
}
if (!backdata || !backdata.object_id) {
return;
}
wx.showLoading({
title: '解析图片中...',
})
var Authorization = getApp().globalData.Authorization;
wx.request({
url: baseUrl + 'common/v1/ocr/id-card-verify',
method: "POST",
header: {
'content-type': 'application/json', // 默认值
"Authorization": Authorization
},
data: {
front_card_copy: that.data.front_card_info,
back_card_copy: backdata
},
success: function(res) {
console.log('suc', res)
if (res.statusCode == 300) {
that.setData({
idInfo: res.data.item
})
console.log('suc', that.data.idInfo)
} else {
wx.showModal({
title: '',
content: res.data.message ? res.data.message : "数据错误",
})
}
},
fail(res) {
console.log('fail', res.data)
},
complete(res){
console.log('complete', res.data)
wx.hideLoading()
}
})
},
savedata: function() {
wx.setStorage({
key: 'id_info',
data: JSON.stringify(this.data.idInfo),
})
wx.navigateBack({
delta: 1
})
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function() {
// wx.showModal({
// title: '',
// content: '\n您的信息还未保存是否确认返回?\n',
// })
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function() {
}
})