multi-side-behavior.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. const SIDE_ENUM = require('../../constant/side.js');
  2. const { RESULT_ENUM } = require('../../constant/result');
  3. const mapOcrToDisplay = require('../../utils/mapOcrToDisplay');
  4. const ocrFinishBehavior = require('./ocr-finish-behavior');
  5. const modifyResultBehavior = require('./modify-result-behavior');
  6. const uploadBehavior = require('./upload-behavior');
  7. const initBehavior = require('./init-behavior');
  8. module.exports = Behavior({
  9. behaviors: [initBehavior, ocrFinishBehavior, modifyResultBehavior, uploadBehavior],
  10. data: {
  11. frontSidePath: '',
  12. backSidePath: '',
  13. frontState: '',
  14. backState: '',
  15. ocrResponse: null,
  16. },
  17. methods: {
  18. /**
  19. * 用户选择好了照片(拍摄或者从相册选择),送检
  20. */
  21. async onImageReady(e) {
  22. // 关闭摄像头,保持文件路径
  23. const { side, filePath, isAuto } = e.detail;
  24. if (!isAuto) {
  25. switch (side) {
  26. case SIDE_ENUM.FRONT:
  27. await this.setData({
  28. showCamera: false,
  29. frontSidePath: filePath,
  30. frontState: 'waiting',
  31. });
  32. break;
  33. case SIDE_ENUM.BACK:
  34. this.setData({
  35. showCamera: false,
  36. backSidePath: filePath,
  37. backState: 'waiting',
  38. });
  39. break;
  40. };
  41. }
  42. this.upload(e);
  43. },
  44. /**
  45. * ocr成功callback
  46. */
  47. onOcrSuccess(detail, res) {
  48. const { ocrType } = wx.clientInfo;
  49. const previousResult = this.data.ocrResponse;
  50. const previousOriginResult = this.data.originResult;
  51. const { side, filePath } = detail;
  52. const newResponse = mapOcrToDisplay(res);
  53. Object.keys(newResponse).forEach((item) => {
  54. newResponse[item].side = side;
  55. });
  56. const newOrigin = { ...previousOriginResult };
  57. const { optionalResultKey } = RESULT_ENUM[ocrType];
  58. optionalResultKey.forEach((item) => {
  59. if (!newOrigin[item]) {
  60. newOrigin[item] = {};
  61. }
  62. newOrigin[item][side] = res[item];
  63. });
  64. if (!newOrigin.RequestId) {
  65. newOrigin.RequestId = {};
  66. }
  67. newOrigin.RequestId[side] = res.RequestId;
  68. if (side === SIDE_ENUM.FRONT) {
  69. this.setData({
  70. showCamera: false,
  71. frontSidePath: filePath,
  72. frontState: 'success_no_circle',
  73. });
  74. } else {
  75. this.setData({
  76. showCamera: false,
  77. backSidePath: filePath,
  78. backState: 'success_no_circle',
  79. });
  80. }
  81. this.setData({
  82. ocrResponse: {
  83. ...previousResult,
  84. ...newResponse,
  85. },
  86. originResult: newOrigin,
  87. });
  88. },
  89. /**
  90. * ocr失败callback
  91. */
  92. onOcrFail(detail, res) {
  93. const { fail } = wx.clientInfo;
  94. const { Message } = res.Error;
  95. const { side } = detail;
  96. if (side === SIDE_ENUM.FRONT) {
  97. this.setData({ frontState: 'warn' });
  98. } else {
  99. this.setData({ backState: 'warn' });
  100. }
  101. wx.showToast({
  102. title: `${Message},请点击图片重新尝试`,
  103. icon: 'none',
  104. duration: 3000,
  105. });
  106. fail(res);
  107. },
  108. },
  109. });