+71
-25
@@ -270,34 +270,81 @@ function onScan() {
|
||||
// #endif
|
||||
|
||||
// #ifdef APP-PLUS
|
||||
// 使用 5+ API 直接调用原生扫码
|
||||
const barcode = plus.barcode.create('barcode', [plus.barcode.CODE128, plus.barcode.CODE39, plus.barcode.EAN13, plus.barcode.QR], {
|
||||
top: '0',
|
||||
left: '0',
|
||||
width: '100%',
|
||||
height: '100%',
|
||||
position: 'static',
|
||||
frameColor: '#007AFF',
|
||||
scanbarColor: '#007AFF'
|
||||
})
|
||||
barcode.onmarked = (type, result) => {
|
||||
console.log('扫码结果:', type, result)
|
||||
barcode.close()
|
||||
keyword.value = result
|
||||
doSearch()
|
||||
// App 端使用 5+ API 检查并申请相机权限
|
||||
const osName = plus.os.name
|
||||
if (osName === 'Android') {
|
||||
const main = plus.android.runtimeMainActivity()
|
||||
const ContextCompat = plus.android.importClass('androidx.core.content.ContextCompat')
|
||||
const Manifest = plus.android.importClass('android.Manifest$permission')
|
||||
const permission = Manifest.CAMERA
|
||||
const result = ContextCompat.checkSelfPermission(main, permission)
|
||||
const PackageManager = plus.android.importClass('android.content.pm.PackageManager')
|
||||
if (result !== PackageManager.PERMISSION_GRANTED) {
|
||||
// 没有权限,申请权限
|
||||
plus.android.requestPermissions(['android.permission.CAMERA'], (e) => {
|
||||
if (e.deniedAlways.length > 0) {
|
||||
// 永久拒绝,引导去设置
|
||||
uni.showModal({
|
||||
title: '需要相机权限',
|
||||
content: '请在系统设置中开启相机权限后再使用扫码功能',
|
||||
confirmText: '去设置',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
if (plus.os.name === 'Android') {
|
||||
const Intent = plus.android.importClass('android.content.Intent')
|
||||
const Settings = plus.android.importClass('android.provider.Settings')
|
||||
const intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
|
||||
const Uri = plus.android.importClass('android.net.Uri')
|
||||
intent.setData(Uri.fromParts('package', main.getPackageName(), null))
|
||||
main.startActivity(intent)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
} else if (e.granted.length > 0) {
|
||||
// 授权成功,调用扫码
|
||||
callScan()
|
||||
}
|
||||
}, (e) => {
|
||||
console.error('申请权限失败', e)
|
||||
callScan()
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
barcode.onerror = (err) => {
|
||||
console.error('扫码错误:', err)
|
||||
barcode.close()
|
||||
uni.showToast({ title: '扫码失败', icon: 'none' })
|
||||
}
|
||||
// 创建并显示扫码页面
|
||||
const page = plus.webview.currentWebview()
|
||||
page.append(barcode)
|
||||
// iOS 或已有权限,直接调用扫码
|
||||
callScan()
|
||||
// #endif
|
||||
|
||||
// #ifdef MP-WEIXIN
|
||||
uni.getSetting({
|
||||
success: (res) => {
|
||||
if (res.authSetting['scope.camera'] === false) {
|
||||
uni.showModal({
|
||||
title: '需要相机权限',
|
||||
content: '扫码功能需要相机权限,请在设置中开启',
|
||||
success: (modalRes) => {
|
||||
if (modalRes.confirm) {
|
||||
uni.openSetting()
|
||||
}
|
||||
}
|
||||
})
|
||||
} else {
|
||||
callScan()
|
||||
}
|
||||
},
|
||||
fail: () => {
|
||||
callScan()
|
||||
}
|
||||
})
|
||||
// #endif
|
||||
}
|
||||
|
||||
// 调用扫码
|
||||
function callScan() {
|
||||
uni.scanCode({
|
||||
onlyFromCamera: false,
|
||||
scanType: ['barCode', 'qrCode'],
|
||||
success: (res) => {
|
||||
console.log('扫码结果:', res)
|
||||
keyword.value = res.result
|
||||
@@ -306,10 +353,9 @@ function onScan() {
|
||||
fail: (err) => {
|
||||
console.error('扫码失败', err)
|
||||
if (err.errMsg && err.errMsg.includes('cancel')) return
|
||||
uni.showToast({ title: '扫码失败', icon: 'none' })
|
||||
uni.showToast({ title: '扫码失败: ' + (err.errMsg || '未知错误'), icon: 'none' })
|
||||
}
|
||||
})
|
||||
// #endif
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user