项目需求
扫描提供的二维码,获得二维码中相关信息。
解决方案
使用uni.scanCode(),调用小程序中的扫码功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| <script> export default { data() { return { } }, onLoad() {
}, methods: { scanCode() { uni.scanCode({ scanType: ["qrCode"], success: (res) => { console.log(res); if (res.result) { const val = res.result; } else { console.log('请重新扫描'); return false; } }, fail: (res) => { console.log('未识别到二维码'); } }) }, parseUrl(url) { let pattern = /(\w+)=([0-9a-zA-Z\u4e00-\u9fa5]+)/ig; let parames = {}; url.replace(pattern, function(a, b, c) { parames[b] = c; }); return parames; }, } } </script>
|
uni.scanCode
| 参数名 |
类型 |
必填 |
说明 |
| onlyFromCamera |
Boolean |
否 |
是否只能从相机扫码 |
| scanType |
Array |
否 |
扫码类型,barCode(一维码)、qrCode(二维码) |
| success |
Function |
否 |
接口调用成功的回调,result:扫码的内容。 |
| fail |
Function |
否 |
接口调用失败的回调函数(识别失败、用户取消等情况下触发) |
| complete |
Function |
否 |
接口调用结束的回调函数(调用成功、失败都会执行) |