import msg from './message.js' import http from './http.js' import store from '../../store/index.js' import storage from './storage.js' import DateFormat from '../../js_sdk/xfl-DateFormat/DateFormat.js' export default { getUserList(_type) { console.log(123123123) //type 1领料列表 return storage.getStorageSync('userList') }, // 返回 back(pageNum = 1) { uni.navigateBack({delta: pageNum}) storage.setStorage('outTime', new Date().getTime()) }, // 打开新窗口 open(url) { uni.navigateTo({ url: url }); storage.setStorage('outTime', new Date().getTime()) }, // 打开新窗口 关闭当前窗口 openNew(url) { uni.redirectTo({ url: url }); storage.setStorage('outTime', new Date().getTime()) }, // 打开新窗口 关闭其他窗口 openReLaunch(url) { uni.reLaunch({ url: url }); storage.setStorage('outTime', new Date().getTime()) }, // 全局消息弹窗 async openMsg() { // if(store.getters.isAlerting) { // let timer = setInterval(() => { // if(!store.getters.isAlerting) { // clearInterval(timer) // this.openMsg() // } // }, 500) // return false // } else { // store.commit('msg/SET_ALERT', true) // } // 消息已读后移除当前弹窗列表 const msgRead = (id) => { store.commit('msg/REMOVE_ALERT_ID', id) http.msgRead({ id }).then(res => { console.log(res.msg, '消息已读utils'); }) } let alertList = [] let roleKey = storage.getStorageSync('userInfo').roleKey await http.getStatus().then(response => { console.log('-----获取消息-----'); console.log(response) let res = response.result alertList = res.alertList alertList.reverse() }) for (let i in alertList) { let alert = alertList[i] // 如果打开列表种已有改消息则不再弹窗 if(store.getters.alertList.indexOf(alert.id) != -1) { console.log('-----------消息已存在-----------',alert.id); console.log(store.getters.alertList); continue; }else { store.commit('msg/SAVE_ALERT_ID', alert.id) // 特殊处理data对象 里面包括一些订单号信息 try { if(alert.data && ((typeof alert.data) == 'string')) { alert.data = JSON.parse(alert.data) } } catch(err){ msg.showToast({title: '消息格式错误!'}) } if(alert.code == 0 || alert.code == 1) { console.log('弹出新订单----'); // 只有四名角色在手机端收到新订单消息后才进行弹窗提醒 if(['directInkPrinter', 'paperPrinter', 'heatPrinter', 'autoCut'].includes(roleKey)) { await new Promise((resolve, reject) => { setTimeout(() => { resolve() }, 1000) }).then(res => { this.open(`/pages/popup/popup?info=${ JSON.stringify(alert) }`) }) } } // 放置 拿取 提示消息 else if(alert.code == 4 || alert.code == 15) { // console.log('放置 拿取 提示消息') await new Promise((resolve, reject) => { setTimeout(() => { resolve() }, 1000) }).then(res => { this.open(`/pages/popup/popup?info=${ JSON.stringify(alert) }`) }) } // 放置成功 拿取成功 提示消息 else if(alert.code == 6 || alert.code == 13) { console.log('放置成功 拿取成功 提示消息--------------'); await new Promise((resolve, reject) => { setTimeout(() => { resolve() }, 1000) }).then(res => { // this.open(`/pages/popup/popup?info=${ JSON.stringify(alert) }`) // 如果是放置成功类型的消息 则保存dispatchNo 用于和放置类型消息的dispatchNo做对比 store.commit('msg/SET_HIDDENT_ID', alert.data.dispatchNo) uni.$emit('printer-list-reflash') msgRead(alert.id) }) } else if([3, 5, 20, 21, 23].includes(alert.code)) { console.log('其他异常消息--------------'); await new Promise((resolve, reject) => { setTimeout(() => { resolve() }, 1000) }).then(res => { this.open(`/pages/popup/popup?info=${ JSON.stringify(alert) }`) }) } // 拍照员新订单 16帐篷拍照 17其他普通拍照 else if([16, 17].includes(alert.code)) { await new Promise((resolve, reject) => { setTimeout(() => { resolve() }, 1000) }).then(res => { uni.$emit('photographer-working-order', { orderIDStr: alert.data.OrderIDStr, id: alert.id }) // 帐篷拍照每次直接已读 需要的时候点击拍照再获取数据 if(alert.code === 16) { msgRead(alert.id) } }) } else if([31].includes(alert.code)) { await new Promise((resolve, reject) => { setTimeout(() => { resolve() }, 1000) }).then(res => { uni.$emit('printer-list-reflash') msgRead(alert.id) }) } else { console.log('其他消息-----'); // msg.showToast({title: '未知消息类型!'}) } } } // store.commit('msg/SET_ALERT', false) }, // 获取设备信息 getSystemInfo() { let info = {} uni.getSystemInfo({ success(res) { console.log(res); info = res }, fail() { msg.showToast({title: '获取设备信息失败!'}) } }) return info }, // 表单验证 formValidate(el) { return new Promise((resolve, reject) => { el.validate(valid => { if (valid) { resolve() } else { reject() } }) }) }, // uniapp获取本地图片地址 getUniAppImgPath(count = 1) { // #ifdef APP-PLUS return new Promise((resolve,reject) => { uni.chooseImage({ count: count, //默认9 sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有 original 原图 sourceType: ['album','camera'], //从相册选择 success: (res) => { count == 1? resolve(res.tempFilePaths[0]): resolve(res.tempFilePaths) }, fail: (e) => { msg.hideLoading() } }); }) // #endif // #ifndef APP-PLUS msg.showToast({title: '仅支持APP端'}) // #endif }, // 压缩图片 compressImage(path) { return new Promise((resolve,reject) => { uni.compressImage({ src: path, quality: 50, width: '60%', height: '60%', success: res => { resolve(res.tempFilePath) } }) }) }, // 获取文件base64文件编码 async uploadFile(path) { let handlePath = path // 解决分辨率过高错误 if(storage.getStorageSync('mobileInfo').platform == 'ios') { await this.compressImage(path).then(res => { handlePath = res }) } return new Promise((resolve,reject) => { plus.io.resolveLocalFileSystemURL(handlePath, (entry) => { entry.file(file => { console.log(file.size, 'size--'); let reader = new plus.io.FileReader(); reader.onloadend = ( e ) => { resolve(e.target.result) }; reader.readAsDataURL( file ); }) }, (e) => { msg.hideLoading() msg.showToast({title: '读取文件失败:' + e}) }) }) }, // 扫描二维码 uniScanCode() { // #ifdef APP-PLUS return new Promise((resolve, reject) => { uni.scanCode({ success: function (res) { resolve(res) }, fail: function (err) { reject(err) } }); }) // #endif // #ifndef APP-PLUS msg.showToast({title: '仅APP端支持扫码功能!'}) // #endif }, // 扫码 scanCodeLogin() { this.uniScanCode().then(res => { let resultCode = res.result if(resultCode.indexOf('deLogin:') !== -1){ http.deviceLogin({ mac: resultCode.replace('deLogin:','') }).then(res => { if(res.code == 0) { msg.showToast({title: '扫码成功!', success: 'success'}) }else { msg.showToast({title: res.msg}) } }) }else if(resultCode.indexOf('station:') !== -1) { // this.$emit('bindStation', resultCode.replace('station:', '')) this.$on('bind http.bindStation({id: resultCode.replace('station:', '')}).then(res => { console.log(res) if(res.code == 0) { msg.showToast({title: '绑定成功!'}) }else { msg.showToast({title: res.msg}) } }) }else if(resultCode.indexOf('qrLogin:') !== -1) { let code = res.result.replace('qrLogin:','') http.scanCodeLogin({ connectID: code.trim() }).then(res => { res.code === 0? msg.showToast({title: '扫码成功!', success: 'success'}): msg.showToast({title: res.msg || '扫码失败!'}) }) }else if(resultCode.indexOf('orReject:') !== -1) { let code = res.result.replace('orReject:','').split(':') this.open(`/pages/order/order-supplement?orderId=${ code[0] }`) } else { return msg.showToast({title: '二维码信息有误'}) } }) }, /** * 处理获取的消息列表 将列表信息归类通过一个字段 最后解析成一个对象 * @param {Object} data 目标数据 * @param {Array} handleData 将要归类处理的数组 * @param {String} filed 归类字段 * @param {String} type 特殊处理 如果已时间分类 **/ handleData(data, handleData, filed, type) { if(handleData && handleData.length > 0) { let key = filed let obj = handleData.reduce((last,item)=>{ if(type == 'date') { key = new DateFormat(new Date(item[filed])).toString('yyyy-mm-dd') }else { key = item[filed] } if(last[key]){ last[key].push(item) }else{ last[key]=[item] } return last },{}) for (let key1 in data) { for (let key2 in obj) { if(key1 == key2) { obj[key2].push(...data[key1]) } } } data = { ...data,...obj } return data } }, // 返回两个时间的相差分钟数 getTimeDifference(time1, time2) { if(time1 && time2) { return parseInt(((new Date(time2).getTime() - new Date(time1).getTime())/1000/60)) + '分钟' } else { return '' } }, // 设置用户在线 setOnline() { // 指定的角色才有在离线功能 let roleKey = storage.getStorageSync('userInfo').roleKey if(['directInkPrinter', 'paperPrinter', 'heatPrinter', 'autoCut', 'photoGrapher'].includes(roleKey)){ http.updatePhoneOnline({onLineState: 1}).then(res => { console.log(res.msg, '在线') // console.log(res) if(res.code !== 0) { this.setOutline() setTimeout(()=> { this.setOnline() },3000) } }) } }, setOutline() { let roleKey = storage.getStorageSync('userInfo').roleKey if(['directInkPrinter', 'paperPrinter', 'heatPrinter', 'autoCut', 'photoGrapher'].includes(roleKey)){ http.updatePhoneOnline({onLineState: 0}).then(res => { console.log(res, '离线') if(res.code !== 0) { // msg.showToast({title: '设备在离线切换失败!'}) } }) } }, // 对象转码 object2Code(obj) { return encodeURIComponent(JSON.stringify(obj)) }, code2Object(code) { return JSON.parse(decodeURIComponent(code)) }, utf8ByteToUnicodeStr(utf8Bytes) { var unicodeStr =''; for (var pos = 0; pos < utf8Bytes.length;){ var flag= utf8Bytes[pos]; var unicode = 0 ; if ((flag >>>7) === 0 ) { unicodeStr+= String.fromCharCode(utf8Bytes[pos]); pos += 1; } else if ((flag &0xFC) === 0xFC ){ unicode = (utf8Bytes[pos] & 0x3) << 30; unicode |= (utf8Bytes[pos+1] & 0x3F) << 24; unicode |= (utf8Bytes[pos+2] & 0x3F) << 18; unicode |= (utf8Bytes[pos+3] & 0x3F) << 12; unicode |= (utf8Bytes[pos+4] & 0x3F) << 6; unicode |= (utf8Bytes[pos+5] & 0x3F); unicodeStr+= String.fromCodePoint(unicode) ; pos += 6; }else if ((flag &0xF8) === 0xF8 ){ unicode = (utf8Bytes[pos] & 0x7) << 24; unicode |= (utf8Bytes[pos+1] & 0x3F) << 18; unicode |= (utf8Bytes[pos+2] & 0x3F) << 12; unicode |= (utf8Bytes[pos+3] & 0x3F) << 6; unicode |= (utf8Bytes[pos+4] & 0x3F); unicodeStr+= String.fromCodePoint(unicode) ; pos += 5; } else if ((flag &0xF0) === 0xF0 ){ unicode = (utf8Bytes[pos] & 0xF) << 18; unicode |= (utf8Bytes[pos+1] & 0x3F) << 12; unicode |= (utf8Bytes[pos+2] & 0x3F) << 6; unicode |= (utf8Bytes[pos+3] & 0x3F); unicodeStr+= String.fromCodePoint(unicode) ; pos += 4; } else if ((flag &0xE0) === 0xE0 ){ unicode = (utf8Bytes[pos] & 0x1F) << 12;; unicode |= (utf8Bytes[pos+1] & 0x3F) << 6; unicode |= (utf8Bytes[pos+2] & 0x3F); unicodeStr+= String.fromCharCode(unicode) ; pos += 3; } else if ((flag &0xC0) === 0xC0 ){ //110 unicode = (utf8Bytes[pos] & 0x3F) << 6; unicode |= (utf8Bytes[pos+1] & 0x3F); unicodeStr+= String.fromCharCode(unicode) ; pos += 2; } else{ unicodeStr+= String.fromCharCode(utf8Bytes[pos]); pos += 1; } } return unicodeStr; }, /* 检测增量更新 */ updateVersion () { http.DownloadApkFile().then(res => { console.log(res) if(res.code === 0) { // if(res.result.updateApkType === 0 && res.result.ossUrl) { plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => { console.log(res.result.version, wgtinfo.version) let version = storage.getStorageSync('version') if (res.result.version !== wgtinfo.version) { uni.showLoading({ title: '新版本下载中,请稍后', mask:true }); let downloadTask = uni.downloadFile({ url: res.result.ossUrl, //下载地址,必传 success: (downloadResult) => { console.log('下载',downloadResult) if (downloadResult.statusCode === 200) { plus.runtime.install(downloadResult .tempFilePath, { //安装api force: true }, function() { plus.runtime.quit(); // console.log('install success...'); // plus.nativeUI.showWaiting('更新完成'); // storage.setStorageSync('version', res.result.version) // setTimeout(() => { // plus.nativeUI // .closeWaiting(); // plus.runtime // .restart(); //重启app // }, 1500) }, function(e) { console.error('install fail...'); }); } }, fail: (item => { uni.hideLoading(); }) }) // downloadTask.onProgressUpdate(item => { // if (item.progress < 100) { // plus.nativeUI.showWaiting('正在下载' + item.progress + // '%'); // } else if (item.progress >= 100) { // plus.nativeUI.closeWaiting(); // } // }) }else{ } }) // } else { // this.checkVersion(res.result.version, res.result.ossUrl) // } } }) }, // 检查更新 checkVersion(version, ossUrl) { if(version !== plus.runtime.version) { uni.showModal({ title: '新版本', content: '是否下载更新版本', cancelText: '取消', confirmText: '下载', success: (res) => { if (res.confirm) { this.androidUpdate(ossUrl) } } }) } else { storage.setStorageSync('version', version) } }, // 安卓更新 androidUpdate(url) { uni.showLoading({ title: '下载中...' }) uni.downloadFile({//执行下载 url: url, //下载地址 success: downloadResult => {//下载成功 uni.hideLoading(); if (downloadResult.statusCode == 200) { plus.runtime.install(//安装 downloadResult.tempFilePath, { force: true }, function(res) { plus.runtime.quit(); // plus.runtime.restart(); } ); } }, fail: err => { msg.showToast({ title: err }) } }) } }