util.js 15 KB

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