提交 cc59d526 编写于 作者: D DCloud_LXH

feat(App): scanCode

上级 de1bf858
......@@ -33,6 +33,7 @@ export * from './protocols/device/bluetooth'
export * from './protocols/device/ibeacon'
export * from './protocols/device/brightness'
export * from './protocols/device/soterAuthentication'
export * from './protocols/device/scanCode'
export * from './protocols/storage/storage'
......
export const API_SCAN_CODE = 'scanCode'
export type API_TYPE_SCAN_CODE = typeof uni.scanCode
export const ScanCodeProtocol: ApiProtocol<API_TYPE_SCAN_CODE> = {
onlyFromCamera: Boolean,
scanType: Array,
autoDecodeCharSet: Boolean,
}
import { showPage } from '../page.js'
import {
defineAsyncApi,
API_SCAN_CODE,
API_TYPE_SCAN_CODE,
ScanCodeProtocol,
} from '@dcloudio/uni-api'
import { useI18n, initI18nScanCodeMsgsOnce } from '@dcloudio/uni-core'
function getStatusBarStyle() {
let style = plus.navigator.getStatusBarStyle()
if (
style === 'UIStatusBarStyleBlackTranslucent' ||
style === 'UIStatusBarStyleBlackOpaque' ||
style === 'null'
) {
style = 'light'
} else if (style === 'UIStatusBarStyleDefault') {
style = 'dark'
}
return style
}
export const scanCode = defineAsyncApi<API_TYPE_SCAN_CODE>(
API_SCAN_CODE,
(options, { resolve, reject }) => {
initI18nScanCodeMsgsOnce()
const { t } = useI18n()
const statusBarStyle = getStatusBarStyle()
const isDark = statusBarStyle !== 'light'
let result: (UniApp.ScanCodeSuccessRes & { message?: string }) | undefined
let success = false
const page = showPage({
url: '__uniappscan',
data: Object.assign({}, options, {
messages: {
fail: t('uni.scanCode.fail'),
'flash.on': t('uni.scanCode.flash.on'),
'flash.off': t('uni.scanCode.flash.off'),
},
}),
style: {
// @ts-ignore
animationType: options.animationType || 'pop-in',
titleNView: {
autoBackButton: true,
// @ts-ignore
type: 'float',
// @ts-ignore
titleText: options.titleText || t('uni.scanCode.title'),
titleColor: '#ffffff',
backgroundColor: 'rgba(0,0,0,0)',
buttons: !options.onlyFromCamera
? [
{
// @ts-ignore
text: options.albumText || t('uni.scanCode.album'),
fontSize: '17px',
width: '60px',
onclick: () => {
page.sendMessage({
type: 'gallery',
})
},
},
]
: [],
},
popGesture: 'close',
background: '#000000',
backButtonAutoControl: 'close',
},
onMessage({
event,
detail,
}: {
event: string
detail: UniApp.ScanCodeSuccessRes
}) {
result = detail
success = event === 'marked'
},
onClose() {
if (isDark) {
plus.navigator.setStatusBarStyle('dark')
}
result
? success
? (delete result.message, resolve(result))
: reject(result.message)
: reject('cancel')
},
})
if (isDark) {
plus.navigator.setStatusBarStyle('light')
page.webview.addEventListener('popGesture', ({ type, result }) => {
if (type === 'start') {
plus.navigator.setStatusBarStyle('dark')
} else if (type === 'end' && !result) {
plus.navigator.setStatusBarStyle('light')
}
})
}
},
ScanCodeProtocol
)
......@@ -12,6 +12,7 @@ export * from './device/addPhoneContact'
export * from './device/clipboard'
export * from './device/network'
export * from './device/soterAuthentication'
export * from './device/scanCode'
export * from './media/getImageInfo'
export * from './media/getVideoInfo'
......
......@@ -922,10 +922,10 @@
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==
"@dcloudio/types@^2.3.5":
version "2.3.5"
resolved "https://registry.nlark.com/@dcloudio/types/download/@dcloudio/types-2.3.5.tgz#01d1f4880dd6289b34e35792227bda28dd75493e"
integrity sha1-AdH0iA3WKJs041eSInvaKN11ST4=
"@dcloudio/types@^2.3.6":
version "2.3.6"
resolved "https://registry.nlark.com/@dcloudio/types/download/@dcloudio/types-2.3.6.tgz?cache=0&sync_timestamp=1627374542897&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40dcloudio%2Ftypes%2Fdownload%2F%40dcloudio%2Ftypes-2.3.6.tgz#31f6d1cdf5ff152a37892f2fdce34b9f87f01808"
integrity sha1-MfbRzfX/FSo3iS8v3ONLn4fwGAg=
"@eslint/eslintrc@^0.4.3":
version "0.4.3"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册