single-side-behavior.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const mapOcrToDisplay = require('../../utils/mapOcrToDisplay');
  2. const ocrFinishBehavior = require('./ocr-finish-behavior');
  3. const modifyResultBehavior = require('./modify-result-behavior');
  4. const uploadBehavior = require('./upload-behavior');
  5. const initBehavior = require('./init-behavior');
  6. module.exports = Behavior({
  7. behaviors: [initBehavior, ocrFinishBehavior, modifyResultBehavior, uploadBehavior],
  8. data: {
  9. imageReady: false,
  10. imageDetail: {},
  11. ocrResponse: null,
  12. },
  13. methods: {
  14. /**
  15. * 用户选择好了照片(拍摄或者从相册选择),送检
  16. */
  17. async onImageReady(e) {
  18. const { isAuto } = e.detail;
  19. if (!isAuto) {
  20. await this.setData({
  21. imageReady: true,
  22. });
  23. }
  24. await this.setData({
  25. imageDetail: e.detail,
  26. });
  27. this.upload(e);
  28. },
  29. /**
  30. * ocr成功callback
  31. */
  32. onOcrSuccess(detail, res) {
  33. const { resultPage } = this.data;
  34. const ocrResponse = mapOcrToDisplay(res);
  35. this.setData({
  36. ocrResponse,
  37. originResult: res,
  38. }, () => {
  39. if (!resultPage) {
  40. this.onFinished();
  41. }
  42. });
  43. },
  44. /**
  45. * ocr失败callback
  46. */
  47. onOcrFail(detail, res) {
  48. const { fail } = wx.clientInfo;
  49. const { resultPage } = this.data;
  50. const { Message } = res.Error;
  51. wx.showToast({
  52. title: `${Message},${resultPage ? '请点击图片重新尝试' : '请重新拍摄/选择照片'}`,
  53. icon: 'none',
  54. duration: 3000,
  55. });
  56. fail(res);
  57. },
  58. /**
  59. * 图片被点击
  60. */
  61. onImageTap() {
  62. this.setData({
  63. imageReady: false,
  64. ocrResponse: null,
  65. });
  66. },
  67. },
  68. });