upload-behavior.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const uploadImage = require('../../utils/uploadImage');
  2. module.exports = Behavior({
  3. methods: {
  4. async upload(e) {
  5. const { isAuto } = e.detail;
  6. if (!isAuto) {
  7. wx.showLoading({
  8. title: '识别中',
  9. mask: true,
  10. });
  11. };
  12. // 获取临时密钥
  13. const { getAuthorization } = wx.clientInfo;
  14. if (getAuthorization) {
  15. const authRes = await getAuthorization();
  16. console.log('authRes', authRes);
  17. const { tmpSecretId, tmpSecretKey, token } = authRes;
  18. if (!tmpSecretId || !tmpSecretKey) {
  19. wx.showToast({
  20. icon: 'none',
  21. duration: 3000,
  22. title: '获取密钥失败',
  23. });
  24. return false;
  25. }
  26. wx.clientInfo.tmpSecretId = tmpSecretId;
  27. wx.clientInfo.tmpSecretKey = tmpSecretKey;
  28. wx.clientInfo.token = token;
  29. }
  30. const res = await uploadImage(e.detail);
  31. wx.hideLoading();
  32. if (res.Error) {
  33. if (isAuto) {
  34. this.selectComponent('#takeImage').startAuto();
  35. } else {
  36. this.onOcrFail(e.detail, res);
  37. };
  38. } else {
  39. if (isAuto) {
  40. this.setData({ imageReady: true });
  41. };
  42. this.onOcrSuccess(e.detail, res);
  43. }
  44. },
  45. },
  46. });