print.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. let types = this.paperTypes;
  61. for (const key in types) {
  62. let item = types[key];
  63. let { 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") {
  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. let 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);
  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. try {
  220. let requestParams = {};
  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. }
  239. },
  240. /**
  241. * 设置纸张大小
  242. * @param type [A3, A4, A5, B3, B4, B5, other]
  243. * @param value {width,height} mm
  244. */
  245. setPaper(type, value) {
  246. try {
  247. if (Object.keys(this.paperTypes).includes(type)) {
  248. this.curPaper = {
  249. type: type,
  250. width: value.width,
  251. height: value.height,
  252. };
  253. this.hiprintTemplate.setPaper(value.width, value.height);
  254. } else {
  255. this.curPaper = {
  256. type: "other",
  257. width: value.width,
  258. height: value.height,
  259. };
  260. this.hiprintTemplate.setPaper(value.width, value.height);
  261. }
  262. } catch (error) {
  263. this.$message.error(`操作失败: ${error}`);
  264. }
  265. },
  266. /**
  267. * 方法1
  268. * 自定义模板数据
  269. * @param {object} data
  270. * this.outputData 打印数据
  271. */
  272. setPrintData(data) {
  273. let salesOrderId, invoiceId; //出库单号,发货单号 默认数据中的第一个
  274. salesOrderId = data.invoicePickBeans[0].salesOrderId;
  275. invoiceId = data.invoicePickBeans[0].invoiceId;
  276. let refUseUnit,refEnginRecordNo
  277. refUseUnit=data.refUseUnit|| data.invoicePickBeans[0].refUseUnit || ''
  278. refEnginRecordNo=data.refEnginRecordNo|| data.invoicePickBeans[0].refEnginRecordNo || ''
  279. let 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. let invoicePickBeans = data.invoicePickBeans;
  288. // 获取length向上取整
  289. let 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
  304. ? this.dateToDayFilter(data.createTime)
  305. : "",
  306. enginOrderType:
  307. tempData.orderType == "HOME" || tempData.orderType == "TRADE" || 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: data.remark,
  330. total_num: data.total_num,
  331. total:total,
  332. company:
  333. data.type === 2
  334. ? `${this.company}销售退货单`
  335. : `${this.company}销售发货单`,
  336. pickOrderWater: data.pickOrderWater,
  337. customerNumber: data.customerNumber,
  338. takerDa: "",
  339. nowDate: this.nowDate(),
  340. takerName:
  341. data.type === 2
  342. ? `退货人:${tuiHuoRen|| ""}`
  343. : `提货人:${data.takerName || ""}`,
  344. customerName: data.customerName || "",
  345. correspondName: data.correspondName,
  346. correspondNames: "",
  347. pickCar: data.pickCar || "",
  348. createBy: JSON.parse(localStorage.getItem("supply_user")).nickName,
  349. table,
  350. });
  351. }
  352. },
  353. /**
  354. * 方法2
  355. * 自定义打印模板
  356. * @param {object} data
  357. * @returns HtmlDom
  358. */
  359. setPrintDom(data) {
  360. return `
  361. <div style="font-family:'黑体';">
  362. <h1 style="text-align:center">${this.company}其他收款单</h1>
  363. <div>
  364. <div>
  365. <span style="wdith:50%"></span>
  366. <span style="wdith:50%"></span>
  367. </div>
  368. <div>
  369. <span style="wdith:50%"></span>
  370. <span style="wdith:50%"></span>
  371. </div>
  372. </div>
  373. <div >
  374. <table border=".5" cellspacing="0" width="856" height="250"
  375. style="border-color: rgb(0,0,0);
  376. border-collapse: collapse;
  377. border-style: none;
  378. border: 1px solid rgb(0,0,0);
  379. font-weight: normal;
  380. direction: ltr;
  381. padding-bottom: 0pt;
  382. padding-left: 4pt;
  383. padding-right: 4pt;
  384. padding-top: 0pt;
  385. text-decoration: none;
  386. vertical-align: middle;
  387. box-sizing: border-box;
  388. word-wrap: break-word;
  389. word-break: break-all;">
  390. <tr>
  391. <td>单据编号</td>
  392. <td>${data.billNo}</td>
  393. <td>业务日期</td>
  394. <td>${data.theTime}</td>
  395. <td>打印日期</td>
  396. <td>${data.createTime}</td>
  397. </tr>
  398. <tr>
  399. <td>付款单位</td>
  400. <td colspan="3">${data.customerName}</td>
  401. <td>项目费用名称</td>
  402. <td>${data.customerType}</td>
  403. </tr>
  404. <tr>
  405. <td>钱包</td>
  406. <td>${data.walletName}</td>
  407. <td>实收金额</td>
  408. <td style="text-align:right">${numToFixed(data.amount)}</td>
  409. <td colspan="2">${data.amount}</td>
  410. </tr>
  411. <tr>
  412. <td>备注</td>
  413. <td colspan="5">${data.remark}</td>
  414. </tr>
  415. </table>
  416. </div>
  417. <div style="margin:100px 0 0 0">
  418. <div>
  419. <div>
  420. <span style="wdith:50%"></span>
  421. <span style="wdith:50%"></span>
  422. </div>
  423. <div>
  424. <span style="wdith:50%"></span>
  425. <span style="wdith:50%"></span>
  426. </div>
  427. </div>
  428. </div>
  429. </div>
  430. `;
  431. },
  432. },
  433. };