diff --git a/package.json b/package.json index 495e1c224d532f070d49a77eeec35945a12f17f8..699341d4ed185d322898ae2dde9e9f48f34e8bb8 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ }, "devDependencies": { "@babel/preset-env": "^7.14.7", - "@dcloudio/types": "^2.3.5", + "@dcloudio/types": "^2.3.6", "@jest/types": "^27.0.2", "@microsoft/api-extractor": "^7.13.2", "@rollup/plugin-alias": "^3.1.1", diff --git a/packages/uni-api/src/index.ts b/packages/uni-api/src/index.ts index 4a466df54fa161a52ff8337a6305cc307083a26c..ebe82224dc88568ea10cdde67f1015a5b71c1cab 100644 --- a/packages/uni-api/src/index.ts +++ b/packages/uni-api/src/index.ts @@ -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' diff --git a/packages/uni-api/src/protocols/device/scanCode.ts b/packages/uni-api/src/protocols/device/scanCode.ts new file mode 100644 index 0000000000000000000000000000000000000000..b69518a5fdf1e4fe890a8281c59b210838e46d2f --- /dev/null +++ b/packages/uni-api/src/protocols/device/scanCode.ts @@ -0,0 +1,8 @@ +export const API_SCAN_CODE = 'scanCode' +export type API_TYPE_SCAN_CODE = typeof uni.scanCode + +export const ScanCodeProtocol: ApiProtocol = { + onlyFromCamera: Boolean, + scanType: Array, + autoDecodeCharSet: Boolean, +} diff --git a/packages/uni-app-plus/src/service/api/device/scanCode.ts b/packages/uni-app-plus/src/service/api/device/scanCode.ts new file mode 100644 index 0000000000000000000000000000000000000000..1ae15bbd97610349baac136ed2086047d9ee67b8 --- /dev/null +++ b/packages/uni-app-plus/src/service/api/device/scanCode.ts @@ -0,0 +1,110 @@ +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_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 +) diff --git a/packages/uni-app-plus/src/service/api/index.ts b/packages/uni-app-plus/src/service/api/index.ts index ee517c48236c7baf92a1c1477dd7e36e871e7a94..07e08233957869c601380c363260b552d01f0b49 100644 --- a/packages/uni-app-plus/src/service/api/index.ts +++ b/packages/uni-app-plus/src/service/api/index.ts @@ -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' diff --git a/yarn.lock b/yarn.lock index 59ff606f46fcd0566a61daca8aca9c31a53cadb3..39285d5ad3d3c7fd6d90b167c99f80b95a3e66ca 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"