|
|
|
|
|
|
|
|
|
|
|
var config = require('./utils/config')
|
|
|
|
|
|
|
|
const uploadFilePromise = (filePath) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
wx.uploadFile({
|
|
|
|
url: `${config.BASEURL()}/common/upload`,
|
|
|
|
name: 'file',
|
|
|
|
filePath: filePath,
|
|
|
|
header: {
|
|
|
|
"Content-type": "multipart/form-data",
|
|
|
|
'Authorization': wx.getStorageSync('token')
|
|
|
|
},
|
|
|
|
success(res) {
|
|
|
|
const data = JSON.parse(res.data)
|
|
|
|
if (data.code === 200) {
|
|
|
|
// 假设返回的 url 在 res.data 里,根据实际情况调整
|
|
|
|
resolve(data.url);
|
|
|
|
} else {
|
|
|
|
reject(new Error('文件上传失败'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
fail(err) {
|
|
|
|
console.log(err);
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const ocrAction = async (filePath) => {
|
|
|
|
wx.showLoading({
|
|
|
|
title: '正在处理',
|
|
|
|
});
|
|
|
|
console.log(filePath,'拿到的文件地址');
|
|
|
|
try {
|
|
|
|
// 上传文件到 CDN 并获取 url
|
|
|
|
const img_url = await uploadFilePromise(filePath);
|
|
|
|
// 调用服务市场接口
|
|
|
|
const result = await wx.serviceMarket.invokeService({
|
|
|
|
api: 'OcrAllInOne',
|
|
|
|
service: 'wx79ac3de8be320b71',
|
|
|
|
data: {
|
|
|
|
img_url,
|
|
|
|
data_type: 3,
|
|
|
|
ocr_type: 1, // 详细见文档
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
wx.hideLoading();
|
|
|
|
console.log(result,'调接口返回');
|
|
|
|
return { success: true, result };
|
|
|
|
} catch (error) {
|
|
|
|
wx.hideLoading();
|
|
|
|
console.error('ocrAction 失败', error);
|
|
|
|
return { success: false, error };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const takePhoto = async (tempFilePath) => {
|
|
|
|
if(tempFilePath){
|
|
|
|
return ocrAction(tempFilePath)
|
|
|
|
}
|
|
|
|
const filePath = await chooseImage()
|
|
|
|
return ocrAction(filePath)
|
|
|
|
}
|