import { BARCODE_CAIGOU, BARCODE_CAIGOU_KEY, BARCODE_FINISHEDGOOD, BARCODE_FINISHEDGOOD_KEY, BARCODE_PRODUCE, BARCODE_PRODUCE_KEY, BARCODE_PROCESS, BARCODE_PROCESS_KEY, BARCODE_JOBCENTER, BARCODE_JOBCENTER_KEY, BARCODE_TARE, BARCODE_TARE_KEY, BARCODE_OUTSOURCE } from "@/common/utils/barCode.js" import {MSG_MAX_SCAN_NUM,MAX_SCAN_NUM} from "@/common/utils/tipMessage.js" import {formateScanData,onErrorBarCodeType} from "@/common/utils/common.js" import {mapGetters} from "vuex" //获取module const barcodeModel = uni.requireNativePlugin("iData-BarcodePlugin-BarcodeModule") const globalEvent = uni.requireNativePlugin('globalEvent'); const BRAND_PRO = "iData" // pda品牌 const BigNumber = require('bignumber.js'); // 扫描物品的逻辑 export const scanProductMixin={ data () { return { list:[], // 显示数据列表 // itemnoClassList:[], // 每一类物品号所拥有的物品列表 barCodeTypeList:[BARCODE_CAIGOU_KEY,BARCODE_FINISHEDGOOD_KEY], // 扫描的二维码类型 uniqueType:{}, // 不允许重复的类型 barcode:null, // 扫到的条形码 } }, computed:{ // 已扫描的物品列表 itemcheckedList () { let arr=this.list.map(item=>{ return item.itemid }) return arr }, // 计算去重物品号的数据 itemnoClassList () { let arr=[] const arr2=this.list.slice(0) this.list.forEach(v=>{ const i=arr.findIndex(v2=>{ return v.itemno===v2.itemno }) if (i<0){ const obj={...v} // 避免污染this.list arr.push(obj) }else{ arr[i].itemnum+=v.itemnum } }) return arr } }, onLoad (e) { uni.$on('onSubmitIventory',(e)=>{ this.clearList() }) }, onUnload () { uni.$off('onSubmitIventory',()=>{}) }, onBackPress (e) { if (this.list.length>0 && e.from ==='backbutton') { this.confirmLeave() return true } }, methods:{ // 离开提示 confirmLeave () { uni.showModal({ title:"重要提示", content:"扫描的物品未确认,系统将不会保存数据,离开将清空,确认离开?", success:(res)=>{ if (res.confirm) { this.clearList() uni.navigateBack({ delta:1 }) } } }) }, // 清空数据 clearList () { this.list=[] }, // 找索引 _findIndex (array,key,item) { const index=array.findIndex((v)=>{ return v[key]===item }) return index }, // 删除物品 onDelete (index) { uni.showModal({ title:"确定删除该条物品吗?", success:res=>{ if (res.confirm) { this.list.splice(index,1) // 删除列表 } } }) }, // 扫描入库,value:条形码,obj :其他数据 /* 物品不能重复有两种情况 */ onScan ({value},obj={}) { // 如果数组长度过长,进行阻断提示 if (this.list.length===MAX_SCAN_NUM) { uni.showToast({ title:MSG_MAX_SCAN_NUM, icon:"none" }) throw new Error('mixins.js:scanProductMixin{}:onScan():扫码的物品数量过多') return false; } this.barcode=value let {barCodeType,itemno,itemnum,itemcode,itemid}=formateScanData(value) itemnum=Number(itemnum) const other=this.scanCustomer() let otherData=[] if (typeof other ==="boolean") { if (!other) { throw new Error ('mixins.js:scanCustomer():return false') return false } } if (typeof other==="object") { if (!other.ok) { throw new Error ('mixins.js:scanCustomer():return false') return false }; if (typeof other.data !="undefined") { otherData=other.data } } /* 情况一:通过扫码得到数量,需要(条形码类型+物品号+识别码)这三样组成的物品身份 不能重复 */ if (obj.uniqueItemid) { this.uniqueType={ uniqueItemid:true } if (this.itemcheckedList.indexOf(itemid) >=0) { return uni.showToast({ title:"该物品已扫描,不可重复扫描", icon:"none", duration:2000 }) }else{ // 没有重复进行数据录入,判断是否需要进行数据合并 const obj={ WPH:itemno, itemno, itemnum, itemid, itemcode, } const objitem= other.ok ? Object.assign({},obj,otherData[0]) : obj this.list.push(objitem) } } /* 手动录入数量 */ if (obj.uniqueItemno) { this.uniqueType={ uniqueItemno:true } const itemNoIndex=this._findIndex(this.list,'itemno',itemno) if (itemNoIndex>=0) { return uni.showToast({ title:"该物品已扫描,请不要重复扫描", icon:"none", duration:2000 }) }else{ this.list.push({ itemno, WPH:itemno }) } } }, // 自定义一个扫码回调,方便在页面里面进行一些替换覆盖 scanCustomer () { return true }, // 自定义一个提交前确认 beforeSubmitConfirmCustomer () { return true }, onSubmit (urlkey,params='&') { const other=this.beforeSubmitConfirmCustomer() let customerList=[] let isCustomer=false if (typeof other ==="boolean") { if (!other) { throw new Error ('mixins.js:beforeSubmitConfirmCustomer():return false') return false } } if (typeof other==="object") { if (!other.ok) { throw new Error ('mixins.js:beforeSubmitConfirmCustomer():return false') return false }; if (typeof other.data !="undefined") { customerList=other.data isCustomer=true } } if (!isCustomer) { if(this.list.length===0){ return uni.showToast({ title:"请至少扫描添加一条物品", icon:"none" }) } } uni.showModal({ title:"确认保存吗?", success:(res)=>{ if (res.confirm) { uni.showToast({ title:"保存成功" }) let list=[] // 如果用户自定义格式话的数组,传递到下一张页面得数据自己定义 if (isCustomer) { list=JSON.stringify(customerList) }else{ // 扫描输入数量,需要传递物品类列表,物品类是我们经过合并之后的数据 if (this.uniqueType.uniqueItemid) { list= JSON.stringify(this.itemnoClassList) } // 手动输入数量,直接把列表传递过去,因为已经控制了物品号不能重复 if (this.uniqueType.uniqueItemno) { list=JSON.stringify(this.list) } } const url=`/pages/${urlkey}/${urlkey}?list=${list}${params}` uni.navigateTo({ url:url }) } } }) }, } } // 清点单合并数据 export const inventoryEditMixin={ methods:{ checkList () { let arr=[] this.realList.forEach((item,index,array)=>{ this.scanList.forEach((item2,index2,array2)=>{ // 两个数据源都有的项 if (item.itemno===item2.itemno) { const obj={...item,...item2} obj.difAmount=item.amount-item2.itemnum obj.highlight=obj.difAmount == 0 ? false : true array[index]=obj }else { // 扫码里有,后台没有 let i=array.findIndex(v=>{ return v.itemno===item2.itemno }) if (i<0) { const obj=item2 obj.amount="--" obj.difAmount=0-obj.itemnum obj.highlight=obj.difAmount == 0 ? false : true array.push(obj) } // 后台有,扫码里没有找到,修改后台数据 let i2=array2.findIndex(v=>{ return v.itemno===item.itemno }) if (i2<0) { const obj=item obj.itemnum="--" obj.difAmount=obj.amount-0 obj.highlight=obj.difAmount == 0 ? false : true array[index]=obj } } }) }) this.list.body=this.realList.slice(0) }, } } export const onScanMixin={ data () { return { sourcename:"", // 数据集合名称 tapIndex:-1, // 点击的索引 tapKeyname:"", // 点击的键名 barcodekey:"", // 条形码关键字 scaning:false, //扫码中 barcode:null, // 条形码 } }, computed:{ ...mapGetters(['deviceBrand']), }, methods:{ // 关闭扫码 onCloseScanPage () { this.scaning=false }, // 开启监听 addListenerFn () { const _this=this globalEvent.addEventListener('iDataBarcodeEvent', function(e) { if (e.barcode) { /*扫完码就关闭扫码,下次需要扫码,再次点击初始化扫码, 给数据赋值 */ _this.barcode=e.barcode _this.scaning=false _this.onFormatCode() barcodeModel.closeScan(); } }); }, // 卸载监听 removeListenerFn () { // 去除监听,关闭扫码 globalEvent.removeEventListener('iDataBarcodeEvent', () => {}) barcodeModel.closeScan(); }, // 格式化点击事件数据 formatTapEvent (e) { const dataset=e.currentTarget.dataset const index=this.tapIndex=dataset.index const keyname=this.tapKeyname=dataset.keyname const barcodekey=this.barcodekey=dataset.barcodekey const sourcename=this.sourcename=dataset.sourcename return { index, keyname, barcodekey, sourcename } }, //扫码 onScan(e) { this.formatTapEvent(e) if (this.deviceBrand === BRAND_PRO) { // iData /** 初始化扫码 */ barcodeModel.initScan(); this.scaning=true } else { // uni-app uni.scanCode({ scanType:['barCode'], success:(res)=>{ // console.log(res) this.barcode=res.result this.onFormatCode() }, fail:err=>{ console.log(err) uni.showToast({ title:err, icon:"none" }) } }) } }, // 清空input数据 onClearValue(e) { this.formatTapEvent(e) this.myClearValue() }, // 清除数据的回调 myClearValue () { this.setBarcode() this.barcode=null }, // 处理扫到的码,value:条形码 onFormatCode () { const value=this.barcode let info=formateScanData(value) info.value=value let str='' //显示的值 // 如果有条码类型再进入程序 let {ok,msg}=onErrorBarCodeType(this.barcodekey,value) if (!ok) { uni.showToast({ title:msg, icon:"none", duration:1500 }) info={} }else{ uni.showToast({ title:"扫码成功" }) // 根据不同的条形码类型赋值 switch (info.barCodeType) { case BARCODE_CAIGOU: case BARCODE_FINISHEDGOOD: str=info.itemno break; case BARCODE_PRODUCE: case BARCODE_PROCESS: case BARCODE_JOBCENTER: str=info.value break; case BARCODE_TARE: str=info.tare break; case BARCODE_OUTSOURCE: str=info.value break; } this.setBarcode(str) } }, // 设置数据 setBarcode (value=null) { const ok=this.myScanCallback() if(!ok)return const index=this.tapIndex const keyname=this.tapKeyname const sourcename=this.sourcename if (sourcename) { if (index >=0) { this.$set(this[sourcename][index],keyname,value) }else{ this.$set(this[sourcename],keyname,value) } }else{ this[keyname]=value } }, myScanCallback () { return true } } }