一定从上到下的流程操作
原生插件通用使用流程:
- 购买插件,选择该插件绑定的项目。
- 在HBuilderX里找到项目,在manifest的app原生插件配置中勾选模块,如需要填写参数则参考插件作者的文档添加。
- 根据插件作者的提供的文档开发代码,在代码中引用插件,调用插件功能。
- 打包自定义基座,选择插件,得到自定义基座,然后运行时选择自定义基座,进行log输出测试。
- 开发完毕后正式云打包
manifest.json
App.vue
<script>
import * as utils from "@/common/util.js"
// 同步获取设备信息
let info = uni.getSystemInfoSync();
if (!info.model) {
info.model = ''
} else if (info.model == 'T1Pro') {
uni.setStorageSync("phoneModel", info.model)
}
console.log('设备型号:', info, info.model);
let phoneModel = uni.getStorageSync('phoneModel') ? uni.getStorageSync('phoneModel') : false
// #ifdef APP-PLUS
const modal = uni.requireNativePlugin('modal');
if (phoneModel) {
//获取module
var barcodeModel = uni.requireNativePlugin("iData-BarcodePlugin-BarcodeModule")
//setScanKeyEnable(boolean enable, UniJSCallback callback)
//enable为bool类型,true为启用扫描键扫码功能,false为禁用扫描键扫码功能,默认为true
//当设置为false后,可以自行监听扫描按键,实现自定义功能
barcodeModel.setScanKeyEnable(false, (ret) => {
console.log(10, 'setScanKeyEnable', ret)
// modal.toast({
// message: ret,
// duration: 1.5
// });
});
// //页面监听event事件,建议在页面onLoad方法里调用
// var globalEvent = uni.requireNativePlugin('globalEvent');
// globalEvent.addEventListener('iDataBarcodeEvent', function(e) {
// console.log(20, '扫码结果', JSON.stringify(e))
// // modal.toast({
// // //条码结果示例:{"barcode","条码数据"}
// // message: "收到条码:" + JSON.stringify(e),
// // duration: 1.5
// // });
// utils.toGetScanCode(e.barcode)
// });
}
// #endif
export default {
onLoad() {
// #ifdef APP-PLUS
if (phoneModel) {
// globalEvent.addEventListener('iDataBarcodeEvent', function(e) {
// console.log(60, "addEventListener", ret)
// // modal.toast({
// // message: "1111111收到条码:" + JSON.stringify(e),
// // duration: 1.5
// // });
// //连续扫描如果间隔时间比较短,会出现toast不提示的情况,数据可以正常接收到,建议查看控制台输出
// console.log(157, JSON.stringify(e));
// });
barcodeModel.initScan((ret) => {
console.log(60, "initScan", ret)
// modal.toast({
// message: ret,
// duration: 1.5
// });
});
}
// #endif
},
onUnload() {
// #ifdef APP-PLUS
if (phoneModel) {
barcodeModel.closeScan((ret) => {
console.log(60, "closeScan", ret)
// modal.toast({
// message: ret,
// duration: 1.5
// });
});
globalEvent.removeEventListener('iDataBarcodeEvent');
}
// #endif
},
}
index.vue
<script>
let phoneModel = uni.getStorageSync('phoneModel') ? uni.getStorageSync('phoneModel') : false
// #ifdef APP-PLUS
const modal = uni.requireNativePlugin('modal');
if (phoneModel) {
//获取module
var barcodeModel = uni.requireNativePlugin("iData-BarcodePlugin-BarcodeModule")
var globalEvent = uni.requireNativePlugin('globalEvent');
}
// #endif
import person from '@/pages/user/person/person.vue'
import workbench from '@/pages/workbench/workbench.vue'
// import apps from '@/pages/apps/apps.vue'
import UpdateProcess from '@/pages/common/updateProcess.vue'
export default {
components: {
person,
workbench,
UpdateProcess
// apps
},
data() {
return {
PageCur: 'workbench',
// 设备型号
phoneModel: uni.getStorageSync('phoneModel') ? uni.getStorageSync('phoneModel') : false,
updateApp: false,
}
},
onLoad(option) {
if (option && option.PageCur) {
this.PageCur = option.PageCur
}
uni.setStorageSync("scanIndex", 'Index')
// #ifdef APP-PLUS
if (phoneModel) {
let that = this
globalEvent.addEventListener('iDataBarcodeEvent', function(e) {
console.log(151, "addEventListener", JSON.stringify(e))
// modal.toast({
// message: "1111111收到条码:" + JSON.stringify(e),
// duration: 1.5
// });
//连续扫描如果间隔时间比较短,会出现toast不提示的情况,数据可以正常接收到,建议查看控制台输出
that.$utils.toGetScanCode(e.barcode)
});
barcodeModel.initScan((ret) => {
console.log(151, "initScan", ret)
// modal.toast({
// message: ret,
// duration: 1.5
// });
});
this.setLockScanKeyFunc()
this.continueScanFunc()
}
// #endif
// #ifdef APP-PLUS
setTimeout(() => {
this.updateApp = true
}, 1000);
// #endif
},
// onUnload() {
// // #ifdef APP-PLUS
// if (phoneModel) {
// barcodeModel.closeScan((ret) => {
// console.log(151, "closeScan", ret)
// // modal.toast({
// // message: ret,
// // duration: 1.5
// // });
// });
// globalEvent.removeEventListener('iDataBarcodeEvent');
// }
// // #endif
// },
methods: {
scanStartFunc() {
// #ifdef APP-PLUS
if (phoneModel) {
barcodeModel.scanStart((ret) => {
console.log(151, "scanStart", ret)
// modal.toast({
// message: ret,
// duration: 1.5
// });
});
}
// #endif
},
// #ifdef APP-PLUS
closeUpdateProcess(bool) {
this.updateApp = bool
},
// #endif
setLockScanKeyFunc() {
// #ifdef APP-PLUS
if (phoneModel) {
//启用扫码按键扫码功能,启用后按扫描键会自动触发扫码功能
barcodeModel.setScanKeyEnable(true, (ret) => {
console.log(151, "setScanKeyEnable", ret)
// modal.toast({
// message: ret,
// duration: 1.5
// });
});
}
// #endif
},
setUnLockScanKeyFunc() {
// #ifdef APP-PLUS
if (phoneModel) {
//禁用扫码按键扫码功能,禁用后按扫描键不会自动触发扫码功能,支持自行监听按键触发功能
barcodeModel.setScanKeyEnable(false, (ret) => {
console.log(151, "setScanKeyEnable", ret)
// modal.toast({
// message: ret,
// duration: 1.5
// });
});
}
// #endif
},
// 连续扫描开启
continueScanFunc(enable) {
// #ifdef APP-PLUS
if (phoneModel) {
// 连续扫描设置:先设置扫描间隔时间,然后开启扫描开关
if (enable) {
//设置连续扫描间隔时间
barcodeModel.intervalSet("500",
(ret) => {
console.log(151, "intervalSet", ret)
// modal.toast({
// message: ret,
// duration: 1.5
// });
});
}
//设置连续扫描
barcodeModel.continueScan(enable,
(ret) => {
console.log(151, "continueScan", ret)
// modal.toast({
// message: ret,
// duration: 1.5
// });
});
}
// #endif
},
NavChange: function(e) {
this.PageCur = e.currentTarget.dataset.cur
}
}
}
</script>