print.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. console.error("获取打印数据失败");
  162. }
  163. loadingLen--;
  164. }
  165. }
  166. if (loadingLen == 0) {
  167. return Promise.resolve();
  168. }
  169. },
  170. // 获取公司名称
  171. async getCompanyLists() {
  172. try {
  173. const { data } = await getCompanyList();
  174. this.company = data ? data[0].companyName : "";
  175. } catch (error) {
  176. console.error("获取公司名称失败");
  177. }
  178. },
  179. // 获取当前时间
  180. nowDate() {
  181. var date = new Date();
  182. var seperator1 = "-";
  183. var year = date.getFullYear();
  184. var month = date.getMonth() + 1;
  185. var strDate = date.getDate();
  186. if (month >= 1 && month <= 9) {
  187. month = "0" + month;
  188. }
  189. if (strDate >= 0 && strDate <= 9) {
  190. strDate = "0" + strDate;
  191. }
  192. var currentdate = year + seperator1 + month + seperator1 + strDate;
  193. return currentdate;
  194. },
  195. // 格式化时间
  196. dateToDayFilter(date) {
  197. if (!date) {
  198. return "";
  199. }
  200. return date.slice(0, 10);
  201. },
  202. // 添加次数
  203. async addPrint(funcType = "getDeliverDetail") {
  204. let ids = [];
  205. for (let i = this.clonelData.length; i > 0; i--) {
  206. const tempData = this.clonelData[i - 1].invoicePickBeans;
  207. if (tempData.length) {
  208. for (let e = tempData.length; e > 0; e--) {
  209. const newTempData = tempData[e - 1];
  210. ids.push(newTempData.id);
  211. }
  212. } else {
  213. return (
  214. this.clonelData[i - 1].invoiceOrderId || this.clonelData[i - 1].id
  215. );
  216. }
  217. }
  218. try {
  219. let requestParams = {};
  220. if (funcType === "getDtailPrintDis") {
  221. ids = [...new Set(this.addIds)];
  222. requestParams = {
  223. ids: ids.join(","),
  224. };
  225. funcType = addPrint;
  226. } else {
  227. requestParams = {
  228. ids: ids.join(","),
  229. };
  230. funcType = addPrints;
  231. }
  232. await funcType(requestParams);
  233. } catch (error) {
  234. console.error("添加打印次数失败");
  235. }
  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) {
  270. let salesOrderId , invoiceId
  271. salesOrderId=data.invoicePickBeans[0].salesOrderId
  272. invoiceId=data.invoicePickBeans[0].invoiceId
  273. // 初始化打印次数
  274. let printNum = 0;
  275. //避免数据发生改变
  276. this.clonelData.push(JSON.parse(JSON.stringify(data)));
  277. // 需要计算长度和数据裁切
  278. let invoicePickBeans = data.invoicePickBeans;
  279. // 获取length向上取整
  280. let len = Math.ceil(invoicePickBeans.length / 5);
  281. for (let index = 0; index < len; index++) {
  282. const table = [];
  283. // length <= 0 则不执行打印
  284. if (invoicePickBeans.length) {
  285. // 取第一个条数据printNum
  286. printNum = invoicePickBeans[0].printNum;
  287. const newInvoicePickBeans = invoicePickBeans.splice(0, 5);
  288. for (let e = newInvoicePickBeans.length; e > 0; e--) {
  289. const tempData = newInvoicePickBeans[e - 1];
  290. //添加表格数据
  291. table.push({
  292. id: tempData.id,
  293. createTime: tempData.id
  294. ? this.dateToDayFilter(data.createTime)
  295. : "",
  296. enginOrderType:
  297. tempData.orderType == "HOME" || tempData.orderType == "TRADE"
  298. ? tempData.enginOrderNo
  299. : tempData.mainOrderId,
  300. materialName: tempData.materialName,
  301. specification: tempData.specification,
  302. refundableQty: tempData.refundableQty,
  303. pjxh1Text: tempData.pjxh1Text,
  304. });
  305. }
  306. }
  307. // 添加print输出数据
  308. this.outputData.push({
  309. pageNumber: `${len}-${index + 1}`,
  310. printNum:printNum + 1,
  311. salesId: salesOrderId,
  312. invoiceId: invoiceId,
  313. type: data.type,
  314. tiTui: data.type === 2 ? `退货人:${data.createBy}` : `提货人`,
  315. takerPhone: data.takerPhone || "",
  316. headerRemark: data.remark,
  317. total_num: data.total_num,
  318. company:
  319. data.type === 2
  320. ? `${this.company}销售退货单`
  321. : `${this.company}销售发货单`,
  322. pickOrderWater: data.pickOrderWater,
  323. customerNumber: data.customerNumber,
  324. takerDa: "",
  325. nowDate: this.nowDate(),
  326. takerName:
  327. data.type === 2
  328. ? `退货人:${data.takerName || ""}`
  329. : `提货人:${data.takerName || ""}`,
  330. customerName: data.customerName || "",
  331. correspondName: data.correspondName,
  332. correspondNames: "",
  333. pickCar: data.pickCar || "",
  334. createBy: JSON.parse(localStorage.getItem("supply_user")).nickName,
  335. table,
  336. });
  337. }
  338. },
  339. /**
  340. * 方法2
  341. * 自定义打印模板
  342. * @param {object} data
  343. * @returns HtmlDom
  344. */
  345. setPrintDom(data) {
  346. return `
  347. <div style="font-family:'黑体';">
  348. <h1 style="text-align:center">${this.company}其他收款单</h1>
  349. <div>
  350. <div>
  351. <span style="wdith:50%"></span>
  352. <span style="wdith:50%"></span>
  353. </div>
  354. <div>
  355. <span style="wdith:50%"></span>
  356. <span style="wdith:50%"></span>
  357. </div>
  358. </div>
  359. <div >
  360. <table border=".5" cellspacing="0" width="856" height="250"
  361. style="border-color: rgb(0,0,0);
  362. border-collapse: collapse;
  363. border-style: none;
  364. border: 1px solid rgb(0,0,0);
  365. font-weight: normal;
  366. direction: ltr;
  367. padding-bottom: 0pt;
  368. padding-left: 4pt;
  369. padding-right: 4pt;
  370. padding-top: 0pt;
  371. text-decoration: none;
  372. vertical-align: middle;
  373. box-sizing: border-box;
  374. word-wrap: break-word;
  375. word-break: break-all;">
  376. <tr>
  377. <td>单据编号</td>
  378. <td>${data.billNo}</td>
  379. <td>业务日期</td>
  380. <td>${data.theTime}</td>
  381. <td>打印日期</td>
  382. <td>${data.createTime}</td>
  383. </tr>
  384. <tr>
  385. <td>付款单位</td>
  386. <td colspan="3">${data.customerName}</td>
  387. <td>项目费用名称</td>
  388. <td>${data.customerType}</td>
  389. </tr>
  390. <tr>
  391. <td>钱包</td>
  392. <td>${data.walletName}</td>
  393. <td>实收金额</td>
  394. <td style="text-align:right">${numToFixed(data.amount)}</td>
  395. <td colspan="2">${data.amount}</td>
  396. </tr>
  397. <tr>
  398. <td>备注</td>
  399. <td colspan="5">${data.remark}</td>
  400. </tr>
  401. </table>
  402. </div>
  403. <div style="margin:100px 0 0 0">
  404. <div>
  405. <div>
  406. <span style="wdith:50%"></span>
  407. <span style="wdith:50%"></span>
  408. </div>
  409. <div>
  410. <span style="wdith:50%"></span>
  411. <span style="wdith:50%"></span>
  412. </div>
  413. </div>
  414. </div>
  415. </div>
  416. `;
  417. },
  418. },
  419. };