print.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. import { disAutoConnect, hiprint, defaultElementTypeProvider } from 'vue-plugin-hiprint'
  2. disAutoConnect()
  3. import panel from '@/utils/panel'
  4. import panel2 from '@/utils/panel2'
  5. import { addPrint, getDtailPrintDis } from '@/api/supply/pickup'
  6. import { getDeliverDetail } from '@/api/supply/deliver'
  7. import { addPrints } from '@/api/supply/pickup'
  8. import { getCompanyList } from '@/api/user'
  9. export default {
  10. data() {
  11. return {
  12. company: '', // 公司名称
  13. clonelData: [], // 克隆数据
  14. outputData: [], // 打印数据
  15. curPaper: {
  16. type: 'A5',
  17. width: 500,
  18. height: 147.6
  19. },
  20. paperTypes: {
  21. A3: {
  22. width: 420,
  23. height: 296.6
  24. },
  25. A4: {
  26. width: 210,
  27. height: 296.6
  28. },
  29. A5: {
  30. width: 210,
  31. height: 147.6
  32. },
  33. B3: {
  34. width: 500,
  35. height: 352.6
  36. },
  37. B4: {
  38. width: 250,
  39. height: 352.6
  40. },
  41. B5: {
  42. width: 250,
  43. height: 175.6
  44. }
  45. },
  46. scaleValue: 1,
  47. scaleMax: 5,
  48. scaleMin: 0.5,
  49. loading: false,
  50. hiprintTemplate: '',
  51. addIds: [],
  52. dataLength: 5,
  53. panelData: '',
  54. moduleType: ''
  55. }
  56. },
  57. computed: {
  58. curPaperType() {
  59. let type = 'other'
  60. const types = this.paperTypes
  61. for (const key in types) {
  62. const item = types[key]
  63. const { width, height } = this.curPaper
  64. if (item.width === width && item.height === height) {
  65. type = key
  66. }
  67. }
  68. return type
  69. }
  70. },
  71. created() {
  72. this.getCompanyLists()
  73. },
  74. methods: {
  75. // 初始化打印模板
  76. initPrint(type) {
  77. hiprint.init({
  78. providers: [new defaultElementTypeProvider()]
  79. })
  80. // 替换配置
  81. hiprint.setConfig({
  82. movingDistance: 2.5,
  83. text: {
  84. supportOptions: [
  85. {
  86. name: 'styler',
  87. hidden: true
  88. },
  89. {
  90. name: 'formatter',
  91. hidden: true
  92. }
  93. ]
  94. }
  95. })
  96. let panelData = panel
  97. if (type === 0) {
  98. panelData = panel2
  99. this.dataLength = 4
  100. }
  101. // eslint-disable-next-line no-undef
  102. hiprint.PrintElementTypeManager.buildByHtml($('.ep-draggable-item'))
  103. this.hiprintTemplate = new hiprint.PrintTemplate({
  104. template: panelData,
  105. settingContainer: '#PrintElementOptionSetting',
  106. paginationContainer: '.hiprint-printPagination'
  107. })
  108. // this.hiprintTemplate.design('#hiprint-printTemplate')
  109. // 获取当前放大比例, 当zoom时传true 才会有
  110. // this.scaleValue = hiprintTemplate.editingPanel.scale || 1;
  111. },
  112. /**
  113. * 获取需要打印数据详情
  114. * @param {Array} ids
  115. */
  116. async getDateil(ids, funcType = 'getDeliverDetail', check = null) {
  117. this.$startLoading()
  118. let loadingLen
  119. // 兼容多个打印数据
  120. ids = ids instanceof Array ? ids : [ids]
  121. console.log(ids)
  122. // 清空之前打印的数据
  123. this.outputData = []
  124. // 筛选id
  125. let formatting = []
  126. // 仓库认证请求接口参数
  127. const params = []
  128. // 获取数据id
  129. for (let i = ids.length; i > 0; i--) {
  130. formatting.push(ids[i - 1].id || ids[i - 1])
  131. if (funcType === 'getDtailPrintDis') {
  132. params.push({
  133. id: ids[i - 1].id,
  134. invoiceId: ids[i - 1].invoiceId
  135. })
  136. this.addIds.push(ids[i - 1].id)
  137. }
  138. }
  139. if (funcType === 'getDtailPrintDis') {
  140. const requestParams = params
  141. try {
  142. const { data } = await getDtailPrintDis(requestParams)
  143. loadingLen = data.length
  144. for (let i = data.length; i > 0; i--) {
  145. const newData = data[i - 1]
  146. await this.setPrintData(newData, funcType, check)
  147. loadingLen--
  148. }
  149. } catch (error) {
  150. this.$endLoading()
  151. console.error('获取打印数据失败')
  152. }
  153. } else {
  154. // id 去重
  155. formatting = [...new Set(formatting)]
  156. loadingLen = formatting.length
  157. for (let i = formatting.length; i > 0; i--) {
  158. const requestParams = {
  159. id: formatting[i - 1]
  160. }
  161. try {
  162. const { data } = await getDeliverDetail(requestParams)
  163. this.setPrintData(data)
  164. } catch (error) {
  165. this.$endLoading()
  166. this.$errorMsg(error)
  167. console.error('获取打印数据失败')
  168. }
  169. loadingLen--
  170. }
  171. }
  172. if (loadingLen == 0) {
  173. return Promise.resolve()
  174. }
  175. },
  176. // 获取公司名称
  177. async getCompanyLists() {
  178. try {
  179. const { data } = await getCompanyList()
  180. this.company = data ? data[0].companyName : ''
  181. } catch (error) {
  182. console.error('获取公司名称失败')
  183. }
  184. },
  185. // 获取当前时间
  186. nowDate() {
  187. var date = new Date()
  188. var seperator1 = '-'
  189. var year = date.getFullYear()
  190. var month = date.getMonth() + 1
  191. var strDate = date.getDate()
  192. if (month >= 1 && month <= 9) {
  193. month = '0' + month
  194. }
  195. if (strDate >= 0 && strDate <= 9) {
  196. strDate = '0' + strDate
  197. }
  198. var currentdate = year + seperator1 + month + seperator1 + strDate
  199. return currentdate
  200. },
  201. // 格式化时间
  202. dateToDayFilter(date) {
  203. if (!date) {
  204. return ''
  205. }
  206. return date.slice(0, 10)
  207. },
  208. // 添加次数
  209. async addPrint(funcType = 'getDeliverDetail') {
  210. let ids = []
  211. for (let i = this.clonelData.length; i > 0; i--) {
  212. const tempData = this.clonelData[i - 1].invoicePickBeans
  213. if (tempData.length) {
  214. for (let e = tempData.length; e > 0; e--) {
  215. const newTempData = tempData[e - 1]
  216. ids.push(newTempData.id)
  217. }
  218. } else {
  219. return this.clonelData[i - 1].invoiceOrderId || this.clonelData[i - 1].id
  220. }
  221. }
  222. let requestParams = {}
  223. try {
  224. if (funcType === 'getDtailPrintDis') {
  225. ids = [...new Set(this.addIds)]
  226. requestParams = {
  227. ids: ids.join(',')
  228. }
  229. funcType = addPrint
  230. } else {
  231. requestParams = {
  232. ids: ids.join(',')
  233. }
  234. funcType = addPrints
  235. }
  236. const promise = await funcType(requestParams)
  237. // 清空当前克隆数据,避免重复添加次数
  238. this.clonelData = []
  239. return promise
  240. } catch (error) {
  241. // console.error('添加打印次数失败')
  242. this.clonelData = []
  243. // this.$errorMsg('添加打印次数失败' + JSON.stringify(requestParams))
  244. return Promise.reject('系统提示:单被商家取消预约,请刷新页面,重新选择打印')
  245. }
  246. },
  247. /**
  248. * 设置纸张大小
  249. * @param type [A3, A4, A5, B3, B4, B5, other]
  250. * @param value {width,height} mm
  251. */
  252. setPaper(type, value) {
  253. try {
  254. if (Object.keys(this.paperTypes).includes(type)) {
  255. this.curPaper = {
  256. type: type,
  257. width: value.width,
  258. height: value.height
  259. }
  260. this.hiprintTemplate.setPaper(value.width, value.height)
  261. } else {
  262. this.curPaper = {
  263. type: 'other',
  264. width: value.width,
  265. height: value.height
  266. }
  267. this.hiprintTemplate.setPaper(value.width, value.height)
  268. }
  269. } catch (error) {
  270. this.$message.error(`操作失败: ${error}`)
  271. }
  272. },
  273. /**
  274. * 方法1
  275. * 自定义模板数据
  276. * @param {object} data
  277. * this.outputData 打印数据
  278. */
  279. setPrintData(data, funcType, check) {
  280. let salesOrderId, invoiceId // 出库单号,发货单号 默认数据中的第一个
  281. salesOrderId = data.invoicePickBeans[0].salesOrderId
  282. invoiceId = data.invoicePickBeans[0].invoiceId
  283. let refUseUnit, refEnginRecordNo
  284. refUseUnit = data.refUseUnit || data.invoicePickBeans[0].refUseUnit || ''
  285. refEnginRecordNo = data.refEnginRecordNo || data.invoicePickBeans[0].refEnginRecordNo || ''
  286. let remark
  287. remark = funcType === 'getDtailPrintDis' ? data.invoicePickBeans[0].remark || '' : data.remark || ''
  288. console.log(data.invoicePickBeans[0], 'oo')
  289. const tuiHuoRen = data.createBy
  290. let address = null
  291. if (data.pickType === 2) {
  292. address = data.invoicePickBeans[0].address || ''
  293. }
  294. // 数量合计
  295. let total = 0
  296. // 初始化打印次数
  297. let printNum = 0
  298. // 避免数据发生改变
  299. this.clonelData.push(JSON.parse(JSON.stringify(data)))
  300. // 需要计算长度和数据裁切
  301. const invoicePickBeans = data.invoicePickBeans
  302. // 获取length向上取整
  303. const len = Math.ceil(invoicePickBeans.length / this.dataLength)
  304. for (let index = 0; index < len; index++) {
  305. const table = []
  306. // length <= 0 则不执行打印
  307. if (invoicePickBeans.length) {
  308. // 取第一个条数据printNum
  309. printNum = invoicePickBeans[0].printNum
  310. const newInvoicePickBeans = invoicePickBeans.splice(0, this.dataLength)
  311. for (let e = newInvoicePickBeans.length; e > 0; e--) {
  312. const tempData = newInvoicePickBeans[e - 1]
  313. total += Math.abs(+tempData.refundableQty)
  314. // 添加表格数据
  315. table.push({
  316. id: tempData.id,
  317. createTime: tempData.id ? this.dateToDayFilter(data.createTime) : '',
  318. enginOrderType:
  319. tempData.orderType == 'HOME' ||
  320. tempData.orderType == 'TRADE' ||
  321. tempData.orderType === 'REQUISITION_TRADE' ||
  322. tempData.orderType === 'REQUISITION_HOME'
  323. ? tempData.enginOrderNo
  324. : tempData.mainOrderId,
  325. materialName: tempData.materialName,
  326. specification: tempData.specification,
  327. refundableQty: tempData.refundableQty,
  328. pjxh1Text: tempData.pjxh1Text
  329. })
  330. }
  331. }
  332. // 添加print输出数据
  333. this.outputData.push({
  334. pageNumber: `${len}-${index + 1}`,
  335. printNum: printNum + 1,
  336. salesId: salesOrderId,
  337. invoiceId: invoiceId,
  338. refUseUnit: refUseUnit,
  339. refEnginRecordNo: refEnginRecordNo,
  340. type: data.type,
  341. tiTui: data.type === 2 ? `退货人` : `提货人`,
  342. takerPhone: data.takerPhone || '',
  343. pickTypeName: data.pickType === null ? '' : data.pickType === 1 ? '商家自提' : '物流配送',
  344. headerRemark: remark,
  345. total_num: data.total_num,
  346. total: total,
  347. company: data.type === 2 ? `${this.company}销售退货单` : `${this.company}销售发货单`,
  348. pickOrderWater: data.pickOrderWater,
  349. customerNumber: data.customerNumber,
  350. takerDa: '',
  351. nowDate: this.nowDate(),
  352. takerName:
  353. data.type === 2
  354. ? `退货人:${check == 0 ? data.pickLogistics || '' : tuiHuoRen || ''}`
  355. : `提货人:${check == 0 ? data.pickLogistics || '' : data.takerName || ''}`,
  356. customerName: data.customerName || '',
  357. correspondName: data.correspondName,
  358. correspondNames: '',
  359. pickCar: data.pickCar || '',
  360. createBy: JSON.parse(localStorage.getItem('supply_user')).nickName,
  361. address,
  362. table
  363. })
  364. }
  365. },
  366. /**
  367. * 方法2
  368. * 自定义打印模板
  369. * @param {object} data
  370. * @returns HtmlDom
  371. */
  372. setPrintDom(data) {
  373. return `
  374. <div style="font-family:'黑体';">
  375. <h1 style="text-align:center">${this.company}其他收款单</h1>
  376. <div>
  377. <div>
  378. <span style="wdith:50%"></span>
  379. <span style="wdith:50%"></span>
  380. </div>
  381. <div>
  382. <span style="wdith:50%"></span>
  383. <span style="wdith:50%"></span>
  384. </div>
  385. </div>
  386. <div >
  387. <table border=".5" cellspacing="0" width="856" height="250"
  388. style="border-color: rgb(0,0,0);
  389. border-collapse: collapse;
  390. border-style: none;
  391. border: 1px solid rgb(0,0,0);
  392. font-weight: normal;
  393. direction: ltr;
  394. padding-bottom: 0pt;
  395. padding-left: 4pt;
  396. padding-right: 4pt;
  397. padding-top: 0pt;
  398. text-decoration: none;
  399. vertical-align: middle;
  400. box-sizing: border-box;
  401. word-wrap: break-word;
  402. word-break: break-all;">
  403. <tr>
  404. <td>单据编号</td>
  405. <td>${data.billNo}</td>
  406. <td>业务日期</td>
  407. <td>${data.theTime}</td>
  408. <td>打印日期</td>
  409. <td>${data.createTime}</td>
  410. </tr>
  411. <tr>
  412. <td>付款单位</td>
  413. <td colspan="3">${data.customerName}</td>
  414. <td>项目费用名称</td>
  415. <td>${data.customerType}</td>
  416. </tr>
  417. <tr>
  418. <td>钱包</td>
  419. <td>${data.walletName}</td>
  420. <td>实收金额</td>
  421. <td style="text-align:right">${numToFixed(data.amount)}</td>
  422. <td colspan="2">${data.amount}</td>
  423. </tr>
  424. <tr>
  425. <td>备注</td>
  426. <td colspan="5">${data.remark}</td>
  427. </tr>
  428. </table>
  429. </div>
  430. <div style="margin:100px 0 0 0">
  431. <div>
  432. <div>
  433. <span style="wdith:50%"></span>
  434. <span style="wdith:50%"></span>
  435. </div>
  436. <div>
  437. <span style="wdith:50%"></span>
  438. <span style="wdith:50%"></span>
  439. </div>
  440. </div>
  441. </div>
  442. </div>
  443. `
  444. },
  445. /**
  446. * 备份方法
  447. * 获取需要打印数据详情
  448. * @param {Array} ids
  449. */
  450. async getDateils(ids, funcType = 'getDeliverDetail') {
  451. this.$startLoading()
  452. let loadingLen
  453. // 兼容多个打印数据
  454. ids = ids instanceof Array ? ids : [ids]
  455. console.log(ids)
  456. // 清空之前打印的数据
  457. this.outputData = []
  458. // 筛选id
  459. let formatting = []
  460. // 仓库认证请求接口参数
  461. const params = []
  462. // 获取数据id
  463. for (let i = ids.length; i > 0; i--) {
  464. formatting.push(ids[i - 1].id || ids[i - 1])
  465. if (funcType === 'getDtailPrintDis') {
  466. params.push({
  467. id: ids[i - 1].id,
  468. invoiceId: ids[i - 1].invoiceId
  469. })
  470. this.addIds.push(ids[i - 1].id)
  471. }
  472. }
  473. if (funcType === 'getDtailPrintDis') {
  474. const requestParams = params
  475. try {
  476. const { data } = await getDtailPrintDis(requestParams)
  477. loadingLen = data.length
  478. for (let i = data.length; i > 0; i--) {
  479. const newData = data[i - 1]
  480. await this.setPrintData(newData)
  481. loadingLen--
  482. }
  483. } catch (error) {
  484. this.$endLoading()
  485. console.error('获取打印数据失败')
  486. }
  487. } else {
  488. // id 去重
  489. formatting = [...new Set(formatting)]
  490. loadingLen = formatting.length
  491. for (let i = formatting.length; i > 0; i--) {
  492. const requestParams = {
  493. id: formatting[i - 1]
  494. }
  495. try {
  496. const { data } = await getDeliverDetail(requestParams)
  497. this.setPrintData(data)
  498. } catch (error) {
  499. this.$endLoading()
  500. this.$errorMsg(error)
  501. console.error('获取打印数据失败')
  502. }
  503. loadingLen--
  504. }
  505. }
  506. if (loadingLen == 0) {
  507. return Promise.resolve()
  508. }
  509. }
  510. }
  511. }