Skip to content

Commit

Permalink
Merge pull request handsomeliuyang#466 from xiaoyan428820/release-3.6…
Browse files Browse the repository at this point in the history
….8-app

feat: 新增支持一维码二维码扫码能力
  • Loading branch information
tangcq-code authored Dec 8, 2023
2 parents fc3940f + 8f08756 commit 7db4d5b
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 9 deletions.
53 changes: 49 additions & 4 deletions examples/mini-program-example/src/pages/api/device/scan/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'
import Taro from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
import ButtonList from '@/components/buttonList'
import { TestConsole } from '@/util/util'
import './index.scss'

/**
Expand All @@ -14,14 +15,58 @@ export default class Index extends React.Component {
list: [
{
id: 'scanCode',
func: null,
inputData: {
onlyFromCamera: false,
scanType: ['qrCode']
},
func: (apiIndex, data) => {
TestConsole.consoleTest('Taro.scanCode')
Taro.scanCode({
...data,
success: (res) => {
TestConsole.consoleSuccess.call(this, res, apiIndex)
this.setState({
scanResult: res
})
},
fail: (res) => {
TestConsole.consoleFail.call(this, res, apiIndex)
},
complete: (res) => {
TestConsole.consoleComplete.call(this, res, apiIndex)
},
}).then((res) => {
TestConsole.consoleResult.call(this, res, apiIndex)
}).catch((err) => {
TestConsole.consoleDebug('scanCode', err)
})
},
},
],
scanResult: {}
}
render() {
const { list } = this.state
render () {
const { list, scanResult } = this.state
return (
<View className='api-page'>
<View>
<View>
<Text>扫码结果</Text>
{Object.keys(scanResult).length === 0 && (
<View>
<Text>未扫描到内容</Text>
</View>
)}
{Object.keys(scanResult).length !== 0 && (
<View>
<View>
<Text>{'扫码的字符集: ' + scanResult?.charSet}</Text><br />
<Text>{'编码原始数据: ' + scanResult?.rawData}</Text><br />
<Text>{'所扫码的内容: ' + scanResult?.result}</Text><br />
<Text>{'所扫码的类型: ' + scanResult?.scanType}</Text><br />
</View>
</View>
)}
</View>
<ButtonList buttonList={list} />
</View>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export default {
navigationBarTitleText: '位置',
navigationStyle: 'custom'
}
3 changes: 3 additions & 0 deletions packages/taro-h5/src/api/location/chooseLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { MethodHandler } from '../../utils/handler'

let container: HTMLDivElement | null = null
function createLocationChooser (handler, mapOpt: Taro.chooseLocation.Option['mapOpts'] = {}) {
// @ts-ignore
const systemBarHeight = window.systemBarHeight ? window.systemBarHeight : 0
const { key = LOCATION_APIKEY, referer = 'myapp', ...opts } = mapOpt
const query = {
key,
Expand All @@ -17,6 +19,7 @@ function createLocationChooser (handler, mapOpt: Taro.chooseLocation.Option['map
if (!container) {
const html = `
<div class='taro_choose_location'>
<div style='height:${systemBarHeight}px; width:100%; background-color:transparent;'></div>
<div class='taro_choose_location_bar'>
<div class='taro_choose_location_back'></div>
<p class='taro_choose_location_title'>位置</p>
Expand Down
5 changes: 4 additions & 1 deletion packages/taro-h5/src/api/location/openLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import { MethodHandler } from '../../utils/handler'

let container: HTMLDivElement | null = null
function createContainer (options: Taro.openLocation.Option) {
// @ts-ignore
const systemBarHeight = window.systemBarHeight ? window.systemBarHeight : 0
const key = LOCATION_APIKEY
const { longitude, latitude, name, address } = options
if (!container) {
const html = `
<div class='taro_choose_location'>
<div style='height:${systemBarHeight}px; width:100%; background-color:transparent;'></div>
<div class='taro_choose_location_bar'>
<div class='taro_choose_location_back'></div>
<p class='taro_choose_location_title'>${name}</p>
<p class='taro_choose_location_title'>位置</p>
</div>
<iframe id='map-iframe' class='taro_choose_location_frame' frameborder='0' src="https://apis.map.qq.com/tools/poimarker?key=${key}&referer=myapp&type=0&marker=coord:${latitude},${longitude};title:${name};addr:${address}" />
</div>
Expand Down
42 changes: 39 additions & 3 deletions packages/taro-mpharmony/src/api/device/scan.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
import Taro from '@tarojs/api'
import { shouldBeObject } from 'src/utils'
import { MethodHandler } from 'src/utils/handler'

/**
* 调起客户端扫码界面,扫码成功后返回对应的结果(harmony平台暂不支持)
* 调起客户端扫码界面,扫码成功后返回对应的结果
*
* @canNotUse scanCode
* @canUse scanCode
* @__object [onlyFromCamera, scanType[barCode, qrCode, datamatrix, pdf417]]
* @__success [charSet, rawData, result, scanType[QR_CODE, AZTEC, CODABAR, CODE_39, CODE_93, CODE_128,\
* DATA_MATRIX, EAN_8, EAN_13, ITF, PDF_417, UPC_A, UPC_E]]
*/
export { scanCode } from '@tarojs/taro-h5'
export const scanCode: typeof Taro.scanCode = (options) => {
const name = 'scanCode'
const isValid = shouldBeObject(options).flag || typeof options === 'undefined'
if (!isValid) {
const res = { errMsg: `${name}:fail invalid params` }
return Promise.reject(res)
}
const {
onlyFromCamera = false,
scanType = [],
success,
fail,
complete
} = options || {}
const handle = new MethodHandler({ name, success, fail, complete })

return new Promise((resolve, reject) => {
// @ts-ignore
native.scanCode({
onlyFromCamera,
scanType,
success: (res: any) => {
handle.success(res, { resolve, reject })
},
fail: (res: any) => {
handle.fail(res, { resolve, reject })
},
})
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -1755,7 +1755,37 @@
"filePath": true
}
},
"scanCode": false,
"scanCode": {
"object": {
"onlyFromCamera": true,
"scanType": {
"barCode": true,
"qrCode": true,
"datamatrix": true,
"pdf417": true
}
},
"success": {
"charSet": true,
"rawData": true,
"result": true,
"scanType": {
"QR_CODE": true,
"AZTEC": true,
"CODABAR": true,
"CODE_39": true,
"CODE_93": true,
"CODE_128": true,
"DATA_MATRIX": true,
"EAN_8": true,
"EAN_13": true,
"ITF": true,
"PDF_417": true,
"UPC_A": true,
"UPC_E": true
}
}
},
"seekBackgroundAudio": false,
"sendHCEMessage": false,
"sendSms": false,
Expand Down

0 comments on commit 7db4d5b

Please sign in to comment.