print.js 15 KB

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