scanCode.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * 摄像头扫码
  3. *
  4. * 需求:
  5. * - 侧键扫码继续走输入框,这里只处理摄像头
  6. * - 仿照 ZL-MES-UNIAPP /pages/rollingScan 使用 uni.scanCode 打开摄像头扫码
  7. *
  8. * 做法:
  9. * - 摄像头走 uni.scanCode
  10. * - 原有支付宝 mPaaS 原生插件(Mpaas-Scan-Module)方法已注释保留
  11. */
  12. function isAppPlus() {
  13. return typeof plus !== "undefined";
  14. }
  15. function isCancelError(err) {
  16. if (!err) return false;
  17. if (err.cancelled) return true;
  18. if (
  19. err.code === 8 ||
  20. err.code === "8" ||
  21. err.code === 10 ||
  22. err.code === "10"
  23. )
  24. return true;
  25. const msg = String(
  26. err.errMsg || err.message || err.resp_message || err || "",
  27. );
  28. return /cancel|取消/i.test(msg);
  29. }
  30. export function normalizeScanText(raw) {
  31. if (raw == null) return "";
  32. let text = String(raw);
  33. text = text.replace(/^/, "");
  34. text = text.replace(/\u0000/g, "");
  35. text = text.replace(/^[\r\n\u0003\u0004]+/, "");
  36. text = text.replace(/[\r\n\u0003\u0004]+$/, "");
  37. return text.trim();
  38. }
  39. // ========== 以下为原有支付宝 mPaaS 原生插件扫码,已注释保留 ==========
  40. /*
  41. function scanByMpaas() {
  42. return new Promise((resolve, reject) => {
  43. try {
  44. const mpaasScanModule = uni.requireNativePlugin("Mpaas-Scan-Module");
  45. if (!mpaasScanModule || typeof mpaasScanModule.mpaasScan !== "function") {
  46. reject(new Error("Mpaas-Scan-Module unavailable"));
  47. return;
  48. }
  49. mpaasScanModule.mpaasScan(
  50. {
  51. scanType: ["qrCode", "barCode"],
  52. hideAlbum: false,
  53. },
  54. (ret) => {
  55. if (!ret) {
  56. reject(new Error("mpaas empty response"));
  57. return;
  58. }
  59. if (ret.resp_code === 1000) {
  60. resolve(normalizeScanText(ret.resp_result));
  61. return;
  62. }
  63. reject({
  64. code: ret.resp_code,
  65. message: ret.resp_message,
  66. cancelled: ret.resp_code === 10,
  67. });
  68. },
  69. );
  70. } catch (err) {
  71. reject(err);
  72. }
  73. });
  74. }
  75. */
  76. export function scanByCamera() {
  77. return new Promise((resolve) => {
  78. const done = (text) => {
  79. const value = normalizeScanText(text);
  80. if (!value) {
  81. uni.showToast({
  82. title: "扫码内容为空",
  83. icon: "none",
  84. });
  85. resolve("");
  86. return;
  87. }
  88. resolve(value);
  89. };
  90. const cancel = () => resolve("");
  91. const failToast = () => {
  92. uni.showToast({
  93. title: "扫码失败",
  94. icon: "none",
  95. });
  96. resolve("");
  97. };
  98. if (!isAppPlus()) {
  99. failToast();
  100. return;
  101. }
  102. // 仿照 ZL-MES-UNIAPP /pages/rollingScan 的 uni.scanCode 用法
  103. uni.scanCode({
  104. // 是否只能从相机扫码,不允许从相册选择图片
  105. onlyFromCamera: false,
  106. // 扫码识别类型
  107. scanType: ["qrCode", "barCode"],
  108. // 是否启用自动放大
  109. autoZoom: true,
  110. // 是否隐藏相册
  111. hideAlbum: false,
  112. success: (res) => {
  113. done(res && res.result);
  114. },
  115. fail: (err) => {
  116. if (isCancelError(err)) {
  117. cancel();
  118. return;
  119. }
  120. failToast();
  121. },
  122. });
  123. });
  124. }
  125. export function scanByCameraCompat(_options, callback) {
  126. scanByCamera().then((text) => {
  127. if (typeof callback !== "function") return;
  128. if (!text) {
  129. callback({
  130. resp_code: 10,
  131. resp_message: "用户取消",
  132. resp_result: "",
  133. });
  134. return;
  135. }
  136. callback({
  137. resp_code: 1000,
  138. resp_message: "成功",
  139. resp_result: text,
  140. });
  141. });
  142. }