util.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. import msg from './message.js'
  2. import http from './http.js'
  3. import store from '../../store/index.js'
  4. import storage from './storage.js'
  5. import DateFormat from '../../js_sdk/xfl-DateFormat/DateFormat.js'
  6. export default {
  7. // 返回
  8. back(pageNum = 1) {
  9. uni.navigateBack({delta: pageNum})
  10. storage.setStorage('outTime', new Date().getTime())
  11. },
  12. // 打开新窗口
  13. open(url) {
  14. uni.navigateTo({
  15. url: url
  16. });
  17. storage.setStorage('outTime', new Date().getTime())
  18. },
  19. // 打开新窗口 关闭当前窗口
  20. openNew(url) {
  21. uni.redirectTo({
  22. url: url
  23. });
  24. storage.setStorage('outTime', new Date().getTime())
  25. },
  26. // 打开新窗口 关闭其他窗口
  27. openReLaunch(url) {
  28. uni.reLaunch({
  29. url: url
  30. });
  31. storage.setStorage('outTime', new Date().getTime())
  32. },
  33. // 全局消息弹窗
  34. async openMsg() {
  35. // if(store.getters.isAlerting) {
  36. // let timer = setInterval(() => {
  37. // if(!store.getters.isAlerting) {
  38. // clearInterval(timer)
  39. // this.openMsg()
  40. // }
  41. // }, 500)
  42. // return false
  43. // } else {
  44. // store.commit('msg/SET_ALERT', true)
  45. // }
  46. // 消息已读后移除当前弹窗列表
  47. const msgRead = (id) => {
  48. store.commit('msg/REMOVE_ALERT_ID', id)
  49. http.msgRead({ id }).then(res => {
  50. console.log(res.msg, '消息已读utils');
  51. })
  52. }
  53. let alertList = []
  54. let roleKey = storage.getStorageSync('userInfo').roleKey
  55. await http.getStatus().then(response => {
  56. console.log('-----获取消息-----');
  57. console.log(response)
  58. let res = response.result
  59. alertList = res.alertList
  60. alertList.reverse()
  61. })
  62. for (let i in alertList) {
  63. let alert = alertList[i]
  64. // 如果打开列表种已有改消息则不再弹窗
  65. if(store.getters.alertList.indexOf(alert.id) != -1) {
  66. console.log('-----------消息已存在-----------',alert.id);
  67. console.log(store.getters.alertList);
  68. continue;
  69. }else {
  70. store.commit('msg/SAVE_ALERT_ID', alert.id)
  71. // 特殊处理data对象 里面包括一些订单号信息
  72. try {
  73. if(alert.data && ((typeof alert.data) == 'string')) {
  74. alert.data = JSON.parse(alert.data)
  75. }
  76. }
  77. catch(err){
  78. msg.showToast({title: '消息格式错误!'})
  79. }
  80. if(alert.code == 0 || alert.code == 1) {
  81. console.log('弹出新订单----');
  82. // 只有四名角色在手机端收到新订单消息后才进行弹窗提醒
  83. if(['directInkPrinter', 'paperPrinter', 'heatPrinter', 'autoCut'].includes(roleKey)) {
  84. await new Promise((resolve, reject) => {
  85. setTimeout(() => {
  86. resolve()
  87. }, 1000)
  88. }).then(res => {
  89. this.open(`/pages/popup/popup?info=${ JSON.stringify(alert) }`)
  90. })
  91. }
  92. }
  93. // 放置 拿取 提示消息
  94. else if(alert.code == 4 || alert.code == 15) {
  95. // console.log('放置 拿取 提示消息')
  96. await new Promise((resolve, reject) => {
  97. setTimeout(() => {
  98. resolve()
  99. }, 1000)
  100. }).then(res => {
  101. this.open(`/pages/popup/popup?info=${ JSON.stringify(alert) }`)
  102. })
  103. }
  104. // 放置成功 拿取成功 提示消息
  105. else if(alert.code == 6 || alert.code == 13) {
  106. console.log('放置成功 拿取成功 提示消息--------------');
  107. await new Promise((resolve, reject) => {
  108. setTimeout(() => {
  109. resolve()
  110. }, 1000)
  111. }).then(res => {
  112. // this.open(`/pages/popup/popup?info=${ JSON.stringify(alert) }`)
  113. // 如果是放置成功类型的消息 则保存dispatchNo 用于和放置类型消息的dispatchNo做对比
  114. store.commit('msg/SET_HIDDENT_ID', alert.data.dispatchNo)
  115. uni.$emit('printer-list-reflash')
  116. msgRead(alert.id)
  117. })
  118. }
  119. else if([3, 5, 20, 21, 23].includes(alert.code)) {
  120. console.log('其他异常消息--------------');
  121. await new Promise((resolve, reject) => {
  122. setTimeout(() => {
  123. resolve()
  124. }, 1000)
  125. }).then(res => {
  126. this.open(`/pages/popup/popup?info=${ JSON.stringify(alert) }`)
  127. })
  128. }
  129. // 拍照员新订单 16帐篷拍照 17其他普通拍照
  130. else if([16, 17].includes(alert.code)) {
  131. await new Promise((resolve, reject) => {
  132. setTimeout(() => {
  133. resolve()
  134. }, 1000)
  135. }).then(res => {
  136. uni.$emit('photographer-working-order', {
  137. orderIDStr: alert.data.OrderIDStr,
  138. id: alert.id
  139. })
  140. // 帐篷拍照每次直接已读 需要的时候点击拍照再获取数据
  141. if(alert.code === 16) {
  142. msgRead(alert.id)
  143. }
  144. })
  145. } else if([31].includes(alert.code)) {
  146. await new Promise((resolve, reject) => {
  147. setTimeout(() => {
  148. resolve()
  149. }, 1000)
  150. }).then(res => {
  151. uni.$emit('printer-list-reflash')
  152. msgRead(alert.id)
  153. })
  154. } else {
  155. console.log('其他消息-----');
  156. // msg.showToast({title: '未知消息类型!'})
  157. }
  158. }
  159. }
  160. // store.commit('msg/SET_ALERT', false)
  161. },
  162. // 获取设备信息
  163. getSystemInfo() {
  164. let info = {}
  165. uni.getSystemInfo({
  166. success(res) {
  167. console.log(res);
  168. info = res
  169. },
  170. fail() {
  171. msg.showToast({title: '获取设备信息失败!'})
  172. }
  173. })
  174. return info
  175. },
  176. // 表单验证
  177. formValidate(el) {
  178. return new Promise((resolve, reject) => {
  179. el.validate(valid => {
  180. if (valid) {
  181. resolve()
  182. } else {
  183. reject()
  184. }
  185. })
  186. })
  187. },
  188. // uniapp获取本地图片地址
  189. getUniAppImgPath(count = 1) {
  190. // #ifdef APP-PLUS
  191. return new Promise((resolve,reject) => {
  192. uni.chooseImage({
  193. count: count, //默认9
  194. sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有 original 原图
  195. sourceType: ['album','camera'], //从相册选择
  196. success: (res) => {
  197. count == 1? resolve(res.tempFilePaths[0]): resolve(res.tempFilePaths)
  198. },
  199. fail: (e) => {
  200. msg.hideLoading()
  201. }
  202. });
  203. })
  204. // #endif
  205. // #ifndef APP-PLUS
  206. msg.showToast({title: '仅支持APP端'})
  207. // #endif
  208. },
  209. // 压缩图片
  210. compressImage(path) {
  211. return new Promise((resolve,reject) => {
  212. uni.compressImage({
  213. src: path,
  214. quality: 50,
  215. width: '60%',
  216. height: '60%',
  217. success: res => {
  218. resolve(res.tempFilePath)
  219. }
  220. })
  221. })
  222. },
  223. // 获取文件base64文件编码
  224. async uploadFile(path) {
  225. let handlePath = path
  226. // 解决分辨率过高错误
  227. if(storage.getStorageSync('mobileInfo').platform == 'ios') {
  228. await this.compressImage(path).then(res => {
  229. handlePath = res
  230. })
  231. }
  232. return new Promise((resolve,reject) => {
  233. plus.io.resolveLocalFileSystemURL(handlePath,
  234. (entry) => {
  235. entry.file(file => {
  236. console.log(file.size, 'size--');
  237. let reader = new plus.io.FileReader();
  238. reader.onloadend = ( e ) => {
  239. resolve(e.target.result)
  240. };
  241. reader.readAsDataURL( file );
  242. })
  243. },
  244. (e) => {
  245. msg.hideLoading()
  246. msg.showToast({title: '读取文件失败:' + e})
  247. })
  248. })
  249. },
  250. // 扫描二维码
  251. uniScanCode() {
  252. // #ifdef APP-PLUS
  253. return new Promise((resolve, reject) => {
  254. uni.scanCode({
  255. success: function (res) {
  256. resolve(res)
  257. },
  258. fail: function (err) {
  259. reject(err)
  260. }
  261. });
  262. })
  263. // #endif
  264. // #ifndef APP-PLUS
  265. msg.showToast({title: '仅APP端支持扫码功能!'})
  266. // #endif
  267. },
  268. // 扫码
  269. scanCodeLogin() {
  270. this.uniScanCode().then(res => {
  271. let resultCode = res.result
  272. if(resultCode.indexOf('deLogin:') !== -1){
  273. http.deviceLogin({
  274. mac: resultCode.replace('deLogin:','')
  275. }).then(res => {
  276. if(res.code == 0) {
  277. msg.showToast({title: '扫码成功!', success: 'success'})
  278. }else {
  279. msg.showToast({title: res.msg})
  280. }
  281. })
  282. }else if(resultCode.indexOf('station:') !== -1) {
  283. // this.$emit('bindStation', resultCode.replace('station:', '')) this.$on('bind
  284. http.bindStation({id: resultCode.replace('station:', '')}).then(res => {
  285. console.log(res)
  286. if(res.code == 0) {
  287. msg.showToast({title: '绑定成功!'})
  288. }else {
  289. msg.showToast({title: res.msg})
  290. }
  291. })
  292. }else if(resultCode.indexOf('qrLogin:') !== -1) {
  293. let code = res.result.replace('qrLogin:','')
  294. http.scanCodeLogin({
  295. connectID: code.trim()
  296. }).then(res => {
  297. res.code === 0?
  298. msg.showToast({title: '扫码成功!', success: 'success'}):
  299. msg.showToast({title: res.msg || '扫码失败!'})
  300. })
  301. }else if(resultCode.indexOf('orReject:') !== -1) {
  302. let code = res.result.replace('orReject:','').split(':')
  303. this.open(`/pages/order/order-supplement?orderId=${ code[0] }`)
  304. } else {
  305. return msg.showToast({title: '二维码信息有误'})
  306. }
  307. })
  308. },
  309. /**
  310. * 处理获取的消息列表 将列表信息归类通过一个字段 最后解析成一个对象
  311. * @param {Object} data 目标数据
  312. * @param {Array} handleData 将要归类处理的数组
  313. * @param {String} filed 归类字段
  314. * @param {String} type 特殊处理 如果已时间分类
  315. **/
  316. handleData(data, handleData, filed, type) {
  317. if(handleData && handleData.length > 0) {
  318. let key = filed
  319. let obj = handleData.reduce((last,item)=>{
  320. if(type == 'date') {
  321. key = new DateFormat(new Date(item[filed])).toString('yyyy-mm-dd')
  322. }else {
  323. key = item[filed]
  324. }
  325. if(last[key]){
  326. last[key].push(item)
  327. }else{
  328. last[key]=[item]
  329. }
  330. return last
  331. },{})
  332. for (let key1 in data) {
  333. for (let key2 in obj) {
  334. if(key1 == key2) {
  335. obj[key2].push(...data[key1])
  336. }
  337. }
  338. }
  339. data = { ...data,...obj }
  340. return data
  341. }
  342. },
  343. // 返回两个时间的相差分钟数
  344. getTimeDifference(time1, time2) {
  345. if(time1 && time2) {
  346. return parseInt(((new Date(time2).getTime() - new Date(time1).getTime())/1000/60)) + '分钟'
  347. } else {
  348. return ''
  349. }
  350. },
  351. // 设置用户在线
  352. setOnline() {
  353. // 指定的角色才有在离线功能
  354. let roleKey = storage.getStorageSync('userInfo').roleKey
  355. if(['directInkPrinter', 'paperPrinter', 'heatPrinter', 'autoCut', 'photoGrapher'].includes(roleKey)){
  356. http.updatePhoneOnline({onLineState: 1}).then(res => {
  357. console.log(res.msg, '在线')
  358. // console.log(res)
  359. if(res.code !== 0) {
  360. this.setOutline()
  361. setTimeout(()=> {
  362. this.setOnline()
  363. },3000)
  364. }
  365. })
  366. }
  367. },
  368. setOutline() {
  369. let roleKey = storage.getStorageSync('userInfo').roleKey
  370. if(['directInkPrinter', 'paperPrinter', 'heatPrinter', 'autoCut', 'photoGrapher'].includes(roleKey)){
  371. http.updatePhoneOnline({onLineState: 0}).then(res => {
  372. console.log(res, '离线')
  373. if(res.code !== 0) {
  374. // msg.showToast({title: '设备在离线切换失败!'})
  375. }
  376. })
  377. }
  378. },
  379. // 对象转码
  380. object2Code(obj) {
  381. return encodeURIComponent(JSON.stringify(obj))
  382. },
  383. code2Object(code) {
  384. return JSON.parse(decodeURIComponent(code))
  385. },
  386. utf8ByteToUnicodeStr(utf8Bytes) {
  387. var unicodeStr ='';
  388. for (var pos = 0; pos < utf8Bytes.length;){
  389. var flag= utf8Bytes[pos];
  390. var unicode = 0 ;
  391. if ((flag >>>7) === 0 ) {
  392. unicodeStr+= String.fromCharCode(utf8Bytes[pos]);
  393. pos += 1;
  394. } else if ((flag &0xFC) === 0xFC ){
  395. unicode = (utf8Bytes[pos] & 0x3) << 30;
  396. unicode |= (utf8Bytes[pos+1] & 0x3F) << 24;
  397. unicode |= (utf8Bytes[pos+2] & 0x3F) << 18;
  398. unicode |= (utf8Bytes[pos+3] & 0x3F) << 12;
  399. unicode |= (utf8Bytes[pos+4] & 0x3F) << 6;
  400. unicode |= (utf8Bytes[pos+5] & 0x3F);
  401. unicodeStr+= String.fromCodePoint(unicode) ;
  402. pos += 6;
  403. }else if ((flag &0xF8) === 0xF8 ){
  404. unicode = (utf8Bytes[pos] & 0x7) << 24;
  405. unicode |= (utf8Bytes[pos+1] & 0x3F) << 18;
  406. unicode |= (utf8Bytes[pos+2] & 0x3F) << 12;
  407. unicode |= (utf8Bytes[pos+3] & 0x3F) << 6;
  408. unicode |= (utf8Bytes[pos+4] & 0x3F);
  409. unicodeStr+= String.fromCodePoint(unicode) ;
  410. pos += 5;
  411. } else if ((flag &0xF0) === 0xF0 ){
  412. unicode = (utf8Bytes[pos] & 0xF) << 18;
  413. unicode |= (utf8Bytes[pos+1] & 0x3F) << 12;
  414. unicode |= (utf8Bytes[pos+2] & 0x3F) << 6;
  415. unicode |= (utf8Bytes[pos+3] & 0x3F);
  416. unicodeStr+= String.fromCodePoint(unicode) ;
  417. pos += 4;
  418. } else if ((flag &0xE0) === 0xE0 ){
  419. unicode = (utf8Bytes[pos] & 0x1F) << 12;;
  420. unicode |= (utf8Bytes[pos+1] & 0x3F) << 6;
  421. unicode |= (utf8Bytes[pos+2] & 0x3F);
  422. unicodeStr+= String.fromCharCode(unicode) ;
  423. pos += 3;
  424. } else if ((flag &0xC0) === 0xC0 ){ //110
  425. unicode = (utf8Bytes[pos] & 0x3F) << 6;
  426. unicode |= (utf8Bytes[pos+1] & 0x3F);
  427. unicodeStr+= String.fromCharCode(unicode) ;
  428. pos += 2;
  429. } else{
  430. unicodeStr+= String.fromCharCode(utf8Bytes[pos]);
  431. pos += 1;
  432. }
  433. }
  434. return unicodeStr;
  435. },
  436. /* 检测增量更新 */
  437. updateVersion () {
  438. http.DownloadApkFile().then(res => {
  439. console.log(res)
  440. if(res.code === 0) {
  441. // if(res.result.updateApkType === 0 && res.result.ossUrl) {
  442. plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
  443. console.log(res.result.version, wgtinfo.version)
  444. let version = storage.getStorageSync('version')
  445. if (res.result.version != wgtinfo.version) {
  446. uni.showLoading({
  447. title: '新版本下载中,请稍后',
  448. mask:true
  449. });
  450. let downloadTask = uni.downloadFile({
  451. url: res.result.ossUrl, //下载地址,必传
  452. success: (downloadResult) => {
  453. console.log('下载',downloadResult)
  454. if (downloadResult.statusCode === 200) {
  455. plus.runtime.install(downloadResult
  456. .tempFilePath, { //安装api
  457. force: true
  458. },
  459. function() {
  460. plus.runtime.quit();
  461. // console.log('install success...');
  462. // plus.nativeUI.showWaiting('更新完成');
  463. // storage.setStorageSync('version', res.result.version)
  464. // setTimeout(() => {
  465. // plus.nativeUI
  466. // .closeWaiting();
  467. // plus.runtime
  468. // .restart(); //重启app
  469. // }, 1500)
  470. },
  471. function(e) {
  472. console.error('install fail...');
  473. });
  474. }
  475. },
  476. fail: (item => {
  477. uni.hideLoading();
  478. })
  479. })
  480. // downloadTask.onProgressUpdate(item => {
  481. // if (item.progress < 100) {
  482. // plus.nativeUI.showWaiting('正在下载' + item.progress +
  483. // '%');
  484. // } else if (item.progress >= 100) {
  485. // plus.nativeUI.closeWaiting();
  486. // }
  487. // })
  488. }else{
  489. }
  490. })
  491. // } else {
  492. // this.checkVersion(res.result.version, res.result.ossUrl)
  493. // }
  494. }
  495. })
  496. },
  497. // 检查更新
  498. checkVersion(version, ossUrl) {
  499. if(version !== plus.runtime.version) {
  500. uni.showModal({
  501. title: '新版本',
  502. content: '是否下载更新版本',
  503. cancelText: '取消',
  504. confirmText: '下载',
  505. success: (res) => {
  506. if (res.confirm) {
  507. this.androidUpdate(ossUrl)
  508. }
  509. }
  510. })
  511. } else {
  512. storage.setStorageSync('version', version)
  513. }
  514. },
  515. // 安卓更新
  516. androidUpdate(url) {
  517. uni.showLoading({
  518. title: '下载中...'
  519. })
  520. uni.downloadFile({//执行下载
  521. url: url, //下载地址
  522. success: downloadResult => {//下载成功
  523. uni.hideLoading();
  524. if (downloadResult.statusCode == 200) {
  525. plus.runtime.install(//安装
  526. downloadResult.tempFilePath, {
  527. force: true
  528. },
  529. function(res) {
  530. plus.runtime.quit();
  531. // plus.runtime.restart();
  532. }
  533. );
  534. }
  535. },
  536. fail: err => {
  537. msg.showToast({ title: err })
  538. }
  539. })
  540. }
  541. }