print.js 16 KB

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