uni-app 一个项目中两个文件同时使用barcode插件 只有A文件可以正常使用 B文件不可以使用
uni-app 一个项目中两个文件同时使用barcode插件 只有A文件可以正常使用 B文件不可以使用
| 开发环境 | 版本号 | 项目创建方式 |
|---|---|---|
| Windows | Windows 11 | HBuilderX |
产品分类:uniapp/App
PC开发环境操作系统:Windows
HBuilderX类型:正式
HBuilderX版本号:3.2.12
手机系统:Android
手机系统版本号:Android 11
手机厂商:华为
手机机型:p30
页面类型:nvue
打包方式:云端
项目创建方式:HBuilderX
示例代码:
A文件:
<template>
<view class="content">
<barcode style="height:300upx;" id='1' class="barcode" autostart="true" ref="barcode" background="rgb(0,0,0)"
frameColor="#1C86EE" scanbarColor="#1C86EE" :filters="fil" @marked="success1" @error="fail1"></barcode>
<view style="display: flex;">
<view class="scan_box">
<picker @change="bindPickerChange" :value="index1" :range="array" range-key="name">
<view style="flex-direction: row">
<image :src="top_img" style="width:120rpx;" mode="widthFix"></image>
<image :src="top_imgs" style="width:100rpx;" mode="widthFix"></image>
</view>
</picker>
</view>
<input type="text" :value="inputvalue" placeholder="请扫描快递单号 支持PDA扫描"
style="font-size:28rpx; width:500rpx;height: 100rpx;text-align: center; background-color: #FFFFFF;margin-top: -110rpx; margin-left: 230rpx;"
@input="bindin" @confirm="huiche" />
</view>
<text style="color:#797979;font-size: 27rpx;margin-top: 15rpx;margin-left: 30rpx;">今日我已接收的包裹:{{dataList.length}}</text>
<view class="scan_box" style="margin-top:15rpx;height:120rpx;" v-for="(item,index) in dataList" :key="index">
<block>
<image src="../../static/shunfeng.png"
style="width:100rpx;height:100rpx;margin-left:10rpx;margin-top:10rpx;"></image>
<view style="margin-left: 120rpx;margin-top: -90rpx;font-size: 28rpx;color:#797979;">
<text style="font-size: 26rpx;width: 500rpx;margin-top:-8px;margin-left: -40px ;">快递单号:{{item.parcelinspection_mailno}}</text>
<text style="color: #FFFFFF;font-size: 26rpx;background-color: #BFBF00;text-align: center;border-radius: 10rpx;color:#FFFFFF;font-size: 22upx;width: 100rpx;margin-left: 220px;margin-top: -25px;" v-if="item.parcel_packagestatus==6">已接收</text>
<text style="color: #FFFFFF;font-size: 26rpx;background-color: #4B7902;text-align: center;border-radius: 10rpx;color:#FFFFFF;font-size: 22upx;width: 100rpx;margin-left: 220px;margin-top: -25px;" v-if="item.parcel_packagestatus==4">已拆检</text>
<view><text style="font-size: 26rpx;">接收信息:{{item.parcelinspection_receiver}} {{item.parcelinspection_receivingtime}}</text></view>
</view>
</block>
</view>
<view class="no-more-data" v-if="loading" style="width:100%"><text style="font-size: 28rpx;text-align: center;color:#A0A0A0">加载中...</text></view>
<template v-else>
<view class="nodata" v-if="dataList.length==0" style="width:100%"><text style="font-size: 28rpx;text-align: center;color:#A0A0A0">暂无相关数据</text></view>
<view class="no-more-data" v-else-if="loaded" style="width:100%"><text style="font-size: 28rpx;text-align: center;color:#A0A0A0">没有更多数据了</text></view>
</template>
</view>
</template>
<script>
var main, receiver, filter;
export default {
data() {
return {
array: [
{ name: '顺丰' },
{ name: '京东' },
{ name: '圆通快递' },
{ name: '中通快递' },
{ name: '百世汇通' },
{ name: '韵达快递' },
{ name: '申通快递' },
{ name: 'EMS标准快递' }
],
top_img: '../../static/shunfeng.png',
top_imgs: '../../static/hengxian.png',
dataList: [],
inputvalue: "",
pageSize: 20,
currentPage: 1,
loading: false,
loaded: false,
saomiao: false
}
},
methods: {
initScan() {
console.log("==========1==============")
var that = this;
main = plus.android.runtimeMainActivity(); //获取activity
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
filter = new IntentFilter();
filter.addAction("scan.rcv.message"); // 换你的广播动作
receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
onReceive: function(context, intent) {
plus.android.importClass(intent);
var codeStr = ""
var code = null
if (uni.getStorageSync("model").indexOf("i6310A") != -1) {
console.log("========isSpecialPDA===========", intent.getStringExtra('barocode'))
codeStr = intent.getStringExtra('barocode');
console.log('codeStr====:', codeStr);
} else {
code = intent.getByteArrayExtra('barocode');
codeStr = String.fromCharCode(...code);
console.log('codeStr:', codeStr);
}
console.log("==111===", codeStr)
that.inputvalue = codeStr
that.jieshou();
console.log("=========123======")
}
});
},
startScan() {
main.registerReceiver(receiver, filter);
},
closeScan() {
main.unregisterReceiver(receiver);
},
success1(e) {
console.log("success1:" + JSON.stringify(e));
console.log("=================:" + e.detail.message);
this.inputvalue = e.detail.message
this.toStart()
this.jieshou();
},
fail1(e) {
console.log("fail1:" + JSON.stringify(e));
},
toStart: function() {
this.$refs.barcode.start({
conserve: true,
filename: '_doc/barcode/'
});
},
tocancel: function() {
this.$refs.barcode.cancel();
},
toFlash: function() {
this.$refs.barcode.setFlash(true);
},
toscan: function() {
console.log("scan:");
const barcodeModule = uni.requireNativePlugin('barcodeScan');
barcodeModule.scan("/static/barcode1.png", (e) => {
console.log("scan_error:" + JSON.stringify(e));
});
},
bindPickerChange: function(e) {
console.log(e, "ppp")
if (e.detail.value == 0) {
this.top_img = '../../static/shunfeng.png'
}
if (e.detail.value == 1) {
this.top_img = '../../static/jd.png'
}
if (e.detail.value == 2) {
this.top_img = '../../static/yt.png'
}
if (e.detail.value == 3) {
this.top_img = '../../static/zt.png'
}
if (e.detail.value == 4) {
this.top_img = '../../static/bs.png'
}
if (e.detail.value == 5) {
this.top_img = '../../static/yd.png'
}
if (e.detail.value == 6) {
this.top_img = '../../static/st.png'
}
if (e.detail.value == 7) {
this.top_img = '../../static/yz.png'
}
},
bindin(e) {
this.inputvalue = e.detail.value
},
huiche() {
this.jieshou()
},
jieshou() {
var that = this
uni.request({
url: 'https://baoguoguanli.yaguyun.com/api/Package/parcelinspection_list',
data: {
number: that.inputvalue,
},
header: {
Authorization: "Bearer " + uni.getStorageSync('AuthToken')
},
method: "POST",
success: function(res) {
var data = res.data
console.log("接收列表", data)
uni.showToast({
title: data.msg,
icon: "none"
})
that.getdatalist(1)
},
fail: function(err) {
uni.hideLoading()
if (err.msg) {
uni.showToast({
duration: 2000,
title: data.msg,
icon: "none"
})
}
}
})
},
getdatalist(page) {
var that = this
let params = {}
if (page == 1) {
that.dataList = []
that.loaded = false
}
that.loading = true
uni.request({
url: 'https://baoguoguanli.yaguyun.com/api/Package/getpackagelist',
data: {
page: page || 1,
limit: that.pageSize,
},
header: {
Authorization: "Bearer " + uni.getStorageSync('AuthToken')
},
method: "POST",
success: function(res) {
console.log(res, "kkkkkkk")
var data = res.data
console.log(data, "kkkkkkk")
that.loading = false
uni.stopPullDownRefresh()
if (page == 1) {
that.dataList = data.data
console.log("接收列表", that.dataList)
for (var i = 0; i < that.dataList.length; i++) {
that.dataList[i].parcelinspection_receivingtime = that.dataList[i]
.parcelinspection_receivingtime.replace("T", " ").substring(0, that.dataList[i]
.parcelinspection_receivingtime.lastIndexOf('.'))
}
that.currentPage = page
} else {
that.dataList = [...that.dataList, ...data.data]
for (var i = 0; i < that.dataList.length; i++) {
that.dataList[i].parcelinspection_receivingtime = that.dataList[i]
.parcelinspection_receivingtime.replace("T", " ").substring(0, that.dataList[i]
.parcelinspection_receivingtime.lastIndexOf('.'))
}
if (data.data.length > 0) {
that.currentPage = page
}
}
that.loaded = that.dataList.length == data.count
console.log("包裹列表", that.dataList)
},
fail: function(err) {
that.loading = false
uni.stopPullDownRefresh()
if (err.msg) {
uni.showToast({
duration: 2000,
title: data.msg,
icon: "none"
})
}
}
})
}
},
onLoad() {
console.log(uni.getStorageSync('AuthToken'), "pppppppppppppppp")
if (!uni.getStorageSync('AuthToken')) {
uni.showToast({
title: "您还没有登陆,正在跳转登陆页...请稍后",
icon: 'none',
duration: 2000
});
setTimeout(function() {
uni.reLaunch({
url: '/pages/login/login'
})
}, 2000);
return;
}
},
onShow() {
this.toStart()
this.getdatalist(1)
this.initScan();
this.startScan();
},
onHide: function() {
this.closeScan();
},
onReachBottom() {
if (!this.loading && !this.loaded) {
this.getdatalist(this.currentPage + 1)
}
},
onPullDownRefresh() {
if (!this.loading) {
this.getdatalist(1)
}
}
}
</script>
<style>
/* .content{
width:750rpx;
height: 100vh;
background-color: #f2f2f2;
} */
.title {
width: 750rpx;
height: 70rpx;
line-height: 70rpx;
background-color: #02A7F0;
color: #FFFFFF;
}
.barcode {
width: 700rpx;
height: 560rpx;
background-color: #555555;
margin-top: 30rpx;
margin-left: 24rpx;
}
.scan_box {
background-color: #FFFFFF;
margin-top: 30rpx;
margin-left: 24rpx;
}
</style>
B文件:
<template>
<view class="content">
<barcode style="height:300upx;" id='1' class="barcode" autostart="true" ref="barcode" background="rgb(0,0,0)"
frameColor="#1C86EE" scanbarColor="#1C86EE" :filters="fil" @marked="success1" @error="fail1"></barcode>
<view style="display: flex;">
<view class="scan_box" style="height:60px">
<view>
<image :src="top_img" style="width:120rpx;" mode="widthFix"></image>
</view>
</view>
<input type="text" :value="inputvalue" placeholder="请扫描快递单号 支持PDA扫描"
style="font-size:28rpx; width:500rpx;height: 100rpx;text-align: center; background-color: #FFFFFF;margin-top: -110rpx; margin-left: 130rpx;"
@input="bindin" @confirm="huiche" />
</view>
<view class="scan_box" style="margin-top:15rpx;height:120rpx;"
v-if="chaijianlist.parcelinspection_guid != undefined">
<block>
<image src="../../static/shunfeng.png"
style="width:100rpx;height:100rpx;margin-left:10rpx;margin-top:10rpx;"></image>
<view style="margin-left: 120rpx;margin-top: -90rpx;font-size: 28rpx;color:#797979;">
<text style="font-size: 26rpx;width: 500rpx;margin-top:-8px;margin-left: -33px ;">快递单号:{{chaijianlist.parcel_mailno}}</text>
<text
style="color: #FFFFFF;font-size: 26rpx;background-color: #BFBF00;text-align: center;border-radius: 10rpx;color:#FFFFFF;font-size: 22upx;width: 100rpx;margin-left: 220px;margin-top: -25px;">当前拆检</text>
<view style="display:flex;flex-direction: row;width:450upx; height:50upx;">
<text style="font-size: 26rpx;">接收信息:{{chaijianlist.parcelinspection_staff_guid}}
</text>
<text style="font-size: 26rpx; margin-left: -60upx; margin-top: -30upx;">{{chaijianlist.parcelinspection_receivingtime}}</text>
</view>
</view>
</block>
</view>
<view class="scan_boxs">
<view style="margin-left:30rpx;font-size: 28rpx;font-weight: bold;color:#555555;margin-top: 10rpx;">
<text style="font-size: 28rpx;width:100px">已拆检订单列表</text>
</view>
<text
style="background-color: #F59A23;text-align: center;border-radius:50%;width: 50rpx;height:50rpx;line-height:50rpx;color:#FFFFFF;font-size: 22upx;margin-top: -32rpx;margin-left: 640rpx;border-radius:50rpx">{{PackagereceivingOrderInfos.length}}</text>
<view
style="flex-direction:row;height: 40upx;margin:30rpx 0 0 30rpx;font-size: 28rpx;color:#797979;margin-top: 10px;" v-for="(item,index) in PackagereceivingOrderInfos" :key='index'>
<view style="width:200rpx">
<text style="margin-left:10upx;font-size: 24rpx;">{{item.PackagereceivingOrderInfo_OrderNumber}}</text>
</view>
<view style="width:100rpx;">
<text style="font-size: 26rpx;">{{chaijianlist.parcelinspection_staff_guid}}</text>
</view>
<view style="width:335rpx;">
<text style="font-size: 26rpx;">{{item.PackagereceivingOrderInfo_orderassociation_receivemodeltime}}</text>
</view>
<view @click="shanchu(item.PackagereceivingOrderInfo_guid)" style="margin-left:-10px">
<image src="../../static/del.png" style="width: 45rpx;height:40rpx;"></image>
</view>
</view>
</view>
</view>
</template>
<script>
var main, receiver, filter;
export default {
data() {
return {
inputvalue: "",
PackagereceivingOrderInfo: [],
PackagereceivingOrderInfos: [],
chaijianlist: {},
parcel_guid: "",
parcel_guid1: "",
saomiao: false
}
},
methods: {
initScan() {
console.log("==========1==============")
var that = this;
main = plus.android.runtimeMainActivity(); //获取activity
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
filter = new IntentFilter();
filter.addAction("scan.rcv.message"); // 换你的广播动作
receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
onReceive: function(context, intent) {
plus.android.importClass(intent);
var codeStr = ""
var code = null
if (uni.getStorageSync("model").indexOf("i6310A") != -1) {
console.log("========isSpecialPDA===========", intent.getStringExtra('barocode'))
codeStr = intent.getStringExtra('barocode');
console.log('codeStr====:', codeStr);
} else {
code = intent.getByteArrayExtra('barocode');
codeStr = String.fromCharCode(...code);
console.log('codeStr:', codeStr);
}
console.log("==111===", codeStr)
that.inputvalue = codeStr
that.chaijian();
console.log("=========123======")
}
});
},
startScan() {
main.registerReceiver(receiver, filter);
},
closeScan() {
main.unregisterReceiver(receiver);
},
success1(e) {
console.log("success1:" + JSON.stringify(e));
console.log("=================:" + e.detail.message);
this.inputvalue = e.detail.message
this.toStart()
if (this.inputvalue.length > 14) {
this.chaijian()
this.dangqianchaijian()
} else {
this.bindscan1()
}
},
fail1(e) {
console.log("fail1:" + JSON.stringify(e));
},
toStart: function() {
this.$refs.barcode.start({
conserve: true,
filename: '_doc/barcode/'
});
},
tocancel: function() {
this.$refs.barcode.cancel();
},
toFlash: function() {
this.$refs.barcode.setFlash(true);
},
toscan: function() {
console.log("scan:");
const barcodeModule = uni.requireNativePlugin('barcodeScan');
barcodeModule.scan("/static/barcode1.png", (e) => {
console.log("scan_error:" + JSON.stringify(e));
});
},
bindin(e) {
this.inputvalue = e.detail.value
},
chaijian() {
var that = this
uni.request({
url: 'https://baoguoguanli.yaguyun.com/api/Package/disassemblyinspectionlistnew',
data: {
number: this.inputvalue,
},
header: {
Authorization: "Bearer " + uni.getStorageSync('AuthToken')
},
method: "POST",
success: function(res) {
var data = res.data
uni.showToast({
title: "拆检成功",
icon: "none"
})
that.dangqianchaijian()
},
fail: function(err) {
uni.stopPullDownRefresh()
if (err.msg) {
uni.showToast({
duration: 2000,
title: data.msg,
icon: "none"
})
}
}
})
},
huiche() {
if (this.inputvalue.length > 14) {
this.chaijian()
this.dangqianchaijian()
} else {
this.bindscan1()
}
},
dangqianchaijian() {
uni.showLoading({
title: "加载中..."
})
var that = this
uni.request({
url: 'https://baoguoguanli.yaguyun.com/api/Package/getdisassemblyinspectionlist',
data: {
number: that.inputvalue,
status: 4,
},
header: {
Authorization: "Bearer " + uni.getStorageSync('AuthToken')
},
method: "POST",
success: function(res) {
var data = res.data
uni.showToast({
title: data.msg,
icon: "none"
})
if (!data.data.parcelinspection_guid) {
return
}
that.chaijianlist = data.data
uni.setStorageSync("staff_guid", that.chaijianlist.parcelinspection_staff_guid)
that.parcel_guid1 = that.chaijianlist.parcel_guid
console.log(33, that.chaijianlist);
that.chaijianlist.parcelinspection_receivingtime = that.chaijianlist
.parcelinspection_receivingtime.replace("T", " ").substring(0, that.chaijianlist
.parcelinspection_receivingtime.lastIndexOf('.'))
uni.hideLoading()
that.getddlist();
},
fail: function(err) {
console.log("当前拆检信息err", err)
uni.stopPullDownRefresh()
if (err.msg) {
uni.showToast({
title: err.msg,
icon: "none"
})
}
}
})
},
getddlist() {
uni.showLoading({
title: "加载中..."
})
var that = this
console.log(222, that.parcel_guid1);
uni.setStorageSync("that.parcel_guid1", that.parcel_guid1)
uni.request({
url: 'https://baoguoguanli.yaguyun.com/api/Package/GetPackagereceivingOrderInfo',
data: {
parcel_guid: that.parcel_guid1,
},
header: {
Authorization: "Bearer " + uni.getStorageSync('AuthToken')
},
method: "GET",
success: function(res) {
var data = res.data
that.PackagereceivingOrderInfos = data.data
console.log("订单列表", data)
for (var i = 0; i < that.PackagereceivingOrderInfos.length; i++) {
that.PackagereceivingOrderInfos[i].PackagereceivingOrderInfo_orderassociation_receivemodeltime = that.PackagereceivingOrderInfos[i].PackagereceivingOrderInfo_orderassociation_receivemodeltime.replace("T", " ").substring(0, that.PackagereceivingOrderInfos[i]
.PackagereceivingOrderInfo_orderassociation_receivemodeltime.lastIndexOf('.'))
}
uni.hideLoading()
},
fail: function(err) {
if (err.msg) {
uni.showToast({
duration: 2000,
title: data.msg,
icon: "none"
})
}
}
})
},
bindscan1() {
var that = this
console.log(2222, that.parcel_guid1)
if (that.parcel_guid1 == "") {
uni.showToast({
title: "请先拆检包裹",
icon: "none"
})
return
}
uni.request({
url: 'https://baoguoguanli.yaguyun.com/api/Package/AddPackagereceivingOrderInfo',
data: {
parcel_guid: that.parcel_guid1,
orderNumber: that.inputvalue
},
header: {
Authorization: "Bearer " + uni.getStorageSync('AuthToken')
},
method: "GET",
success: function(res) {
var data = res.data
console.log("添加", data)
if (data.code == 0) {
that.inputvalue = '';
that.parcel_guid1 = data.data[0].PackagereceivingOrderInfo_parcel_guid;
console.log(11, that.parcel_guid1);
uni.showToast({
title: "添加成功",
icon: "none"
})
that.getddlist()
} else {
uni.showToast({
title: "扫描系统里没有订单",
icon: "none"
})
}
},
fail: function(err) {
console.log(err);
if (err.msg) {
uni.showToast({
title: "该订单已经存再运单号为:" + err.msg + " 的包裹了",
icon: "none"
})
}
}
})
},
bindipt(e) {
this.inputvalue = e.target.value
console.log("inputvalue", this.inputvalue)
},
shanchu(guid) {
let that = this;
uni.showModal({
title: '提示',
content: '是否确定删除',
success: function(res) {
if (res.confirm) {
uni.request({
url: 'https://baoguoguanli.yaguyun.com/api/Package/DeletePackagereceivingOrderInfo',
data: {
guid: guid,
},
header: {
Authorization: "Bearer " + uni.getStorageSync('AuthToken')
},
method: "GET",
success: function(res) {
var data = res.data
if (data.code == 0) {
for (let i = 0; i < that.PackagereceivingOrderInfo.length; i++) {
if (that.PackagereceivingOrderInfo[i]
.PackagereceivingOrderInfo_guid == guid) {
that.PackagereceivingOrderInfo.splice(i, 1);
break;
}
}
uni.showToast({
title: "删除成功",
icon: "none"
})
that.getddlist()
}
},
fail: function(err) {
console.log(err);
if (err.msg) {
uni.showToast({
duration: 2000,
title: data.msg,
icon: "none"
})
}
}
})
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
}
},
onLoad() {},
onHide: function() {
this.closeScan();
},
onshow() {
this.toStart()
this.initScan();
this.startScan();
}
}
</script>
<style>
/* .content {
width: 750rpx;
height: 100vh;
background-color: #f2f2f2;
} */
.barcode {
width: 700rpx;
height: 560rpx;
background-color: #555555;
margin-top: 30rpx;
margin-left: 24rpx;
}
.scan_box {
background-color: #FFFFFF;
margin-top: 30rpx;
margin-left: 24rpx;
}
.scan_boxs {
background-color: #FFFFFF;
margin-top: 20rpx;
margin-left: 24rpx;
}
.list_box {
display: flex;
margin-left: 20rpx;
}
</style>
更多关于uni-app 一个项目中两个文件同时使用barcode插件 只有A文件可以正常使用 B文件不可以使用的实战教程也可以访问 https://www.itying.com/category-93-b0.html
更多关于uni-app 一个项目中两个文件同时使用barcode插件 只有A文件可以正常使用 B文件不可以使用的实战教程也可以访问 https://www.itying.com/category-93-b0.html
这个问题通常是由于barcode组件在多个页面同时使用时,组件实例冲突导致的。从你的代码分析,B文件存在几个关键问题:
-
生命周期钩子函数名错误:B文件中使用了
onshow(小写),正确的应该是onShow(大写S)。这导致B页面的barcode组件初始化代码没有执行。 -
组件引用冲突:两个页面都使用了相同的
ref="barcode",当页面切换时可能会产生冲突。建议为每个页面的barcode组件使用不同的ref名称。 -
广播接收器冲突:两个页面都注册了相同的广播接收器
scan.rcv.message,可能会相互干扰。
解决方案:
- 修正生命周期函数:
// B文件修改
onShow() { // 注意大写S
this.toStart()
this.initScan();
this.startScan();
}
- 使用不同的ref名称:
<!-- B文件修改 -->
<barcode ref="barcode2" ...></barcode>
// 相应的方法也要修改
toStart: function() {
this.$refs.barcode2.start({ // 使用新的ref名称
conserve: true,
filename: '_doc/barcode/'
});
}

