print.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import { disAutoConnect, hiprint, defaultElementTypeProvider } from 'vue-plugin-hiprint'
  2. disAutoConnect();
  3. import { Loading } from 'element-ui';
  4. import panel from '@/utils/panel'
  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. };
  53. },
  54. computed: {
  55. curPaperType() {
  56. let type = 'other'
  57. let types = this.paperTypes
  58. for (const key in types) {
  59. let item = types[key]
  60. let { width, height } = this.curPaper
  61. if (item.width === width && item.height === height) {
  62. type = key
  63. }
  64. }
  65. return type
  66. }
  67. },
  68. created() {
  69. this.getCompanyLists()
  70. },
  71. methods: {
  72. // 初始化打印模板
  73. initPrint() {
  74. hiprint.init({
  75. providers: [new defaultElementTypeProvider()]
  76. });
  77. // 替换配置
  78. hiprint.setConfig({
  79. movingDistance: 2.5,
  80. text: {
  81. supportOptions: [
  82. {
  83. name: 'styler',
  84. hidden: true
  85. },
  86. {
  87. name: 'formatter',
  88. hidden: true
  89. },
  90. ]
  91. }
  92. })
  93. // eslint-disable-next-line no-undef
  94. hiprint.PrintElementTypeManager.buildByHtml($('.ep-draggable-item'));
  95. this.hiprintTemplate = new hiprint.PrintTemplate({
  96. template: panel,
  97. settingContainer: '#PrintElementOptionSetting',
  98. paginationContainer: '.hiprint-printPagination'
  99. });
  100. this.hiprintTemplate.design('#hiprint-printTemplate');
  101. // 获取当前放大比例, 当zoom时传true 才会有
  102. // this.scaleValue = hiprintTemplate.editingPanel.scale || 1;
  103. },
  104. /**
  105. * 获取需要打印数据详情
  106. * @param {Array} ids
  107. */
  108. async getDateil(ids, funcType = 'getDeliverDetail') {
  109. // 兼容多个打印数据
  110. ids = ids instanceof Array ? ids : [ids]
  111. console.log(ids);
  112. // 清空之前打印的数据
  113. this.outputData = [];
  114. // 筛选id
  115. let formatting = [];
  116. // 仓库认证请求接口参数
  117. let params = []
  118. // 获取数据id
  119. for (let i = ids.length; i > 0; i--) {
  120. formatting.push(ids[i - 1].id || ids[i - 1]);
  121. if (funcType === 'getDtailPrintDis') {
  122. params.push(
  123. {
  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. for (let i = data.length; i > 0; i--) {
  135. setTimeout(async () => {
  136. const newData = data[i - 1];
  137. await this.mySetData(newData)
  138. }, 0);
  139. }
  140. } catch (error) {
  141. console.error('获取打印数据失败')
  142. }
  143. } else {
  144. // id 去重
  145. formatting = [...new Set(formatting)];
  146. for (let i = formatting.length; i > 0; i--) {
  147. // 延迟请求
  148. setTimeout(async () => {
  149. const requestParams = { id: formatting[i - 1] }
  150. try {
  151. const { data } = await getDeliverDetail(requestParams);
  152. this.mySetData(data)
  153. } catch (error) {
  154. console.error('获取打印数据失败')
  155. }
  156. }, 0);
  157. }
  158. }
  159. },
  160. mySetData(data) {
  161. // 初始化打印次数
  162. let printNum = 0
  163. //避免数据发生改变
  164. this.clonelData.push(JSON.parse(JSON.stringify(data)))
  165. // 需要计算长度和数据裁切
  166. let invoicePickBeans = data.invoicePickBeans;
  167. // 获取length向上取整
  168. let len = Math.ceil(invoicePickBeans.length / 5);
  169. for (let index = 0; index < len; index++) {
  170. const table = [];
  171. // length <= 0 则不执行打印
  172. if (invoicePickBeans.length) {
  173. // 取第一个条数据printNum
  174. printNum = invoicePickBeans[0].printNum
  175. const newInvoicePickBeans = invoicePickBeans.splice(0, 5);
  176. for (let e = newInvoicePickBeans.length; e > 0; e--) {
  177. const tempData = newInvoicePickBeans[e - 1];
  178. //添加表格数据
  179. table.push({
  180. salesId: tempData.salesOrderId,
  181. invoiceId: tempData.invoiceId,
  182. id: tempData.id,
  183. createTime: tempData.id
  184. ? this.dateToDayFilter(tempData.createTime)
  185. : '',
  186. enginOrderType:
  187. tempData.orderType == "HOME" || tempData.orderType == "TRADE"
  188. ? tempData.enginOrderNo
  189. : tempData.mainOrderId,
  190. materialName: tempData.materialName,
  191. specification: tempData.specification,
  192. refundableQty: tempData.refundableQty,
  193. pjxh1Text: tempData.pjxh1Text,
  194. });
  195. }
  196. }
  197. // 添加print输出数据
  198. this.outputData.push({
  199. pageNumber: `${len}-${index + 1}`,
  200. printNum,
  201. type: data.type,
  202. tiTui: data.type === 2 ? `退货人` : `提货人`,
  203. takerPhone: data.takerPhone || '',
  204. headerRemark: data.remark,
  205. total_num: data.total_num,
  206. company:
  207. data.type === 2 ? `${this.company}销售退货单` : `${this.company}销售发货单`,
  208. pickOrderWater: data.pickOrderWater,
  209. customerNumber: data.customerNumber,
  210. takerDa: '',
  211. nowDate: this.nowDate(),
  212. takerName:
  213. data.type === 2
  214. ? `退货人:${data.takerName || ''}`
  215. : `提货人:${data.takerName || ''}`,
  216. customerName: data.customerName || '',
  217. correspondName: data.correspondName,
  218. correspondNames: '',
  219. pickCar: data.pickCar || '',
  220. createBy: JSON.parse(localStorage.getItem("supply_user")).nickName,
  221. table,
  222. });
  223. }
  224. },
  225. // 获取公司名称
  226. async getCompanyLists() {
  227. try {
  228. const { data } = await getCompanyList();
  229. this.company = data ? data[0].companyName : '';
  230. } catch (error) {
  231. console.error('获取公司名称失败')
  232. }
  233. },
  234. // 获取当前时间
  235. nowDate() {
  236. var date = new Date();
  237. var seperator1 = "-";
  238. var year = date.getFullYear();
  239. var month = date.getMonth() + 1;
  240. var strDate = date.getDate();
  241. if (month >= 1 && month <= 9) {
  242. month = "0" + month;
  243. }
  244. if (strDate >= 0 && strDate <= 9) {
  245. strDate = "0" + strDate;
  246. }
  247. var currentdate = year + seperator1 + month + seperator1 + strDate;
  248. console.log(currentdate);
  249. return currentdate;
  250. },
  251. // 格式化时间
  252. dateToDayFilter(date) {
  253. if (!date) {
  254. return '';
  255. }
  256. return date.slice(0, 10);
  257. },
  258. // 添加次数
  259. async addPrint(funcType = 'getDeliverDetail') {
  260. let ids = []
  261. for (let i = this.clonelData.length; i > 0; i--) {
  262. const tempData = this.clonelData[i - 1].invoicePickBeans;
  263. if (tempData.length) {
  264. for (let e = tempData.length; e > 0; e--) {
  265. const newTempData = tempData[e - 1];
  266. ids.push(newTempData.id)
  267. }
  268. } else {
  269. return this.clonelData[i - 1].invoiceOrderId || this.clonelData[i - 1].id
  270. }
  271. }
  272. try {
  273. let requestParams = {}
  274. if (funcType === 'getDtailPrintDis') {
  275. ids = [...new Set(this.addIds)]
  276. requestParams = { ids: ids.join(',') }
  277. funcType = addPrint
  278. } else {
  279. requestParams = { ids: ids.join(',') }
  280. funcType = addPrints
  281. }
  282. await funcType(requestParams)
  283. } catch (error) {
  284. console.error('添加打印次数失败');
  285. }
  286. },
  287. /**
  288. * 设置纸张大小
  289. * @param type [A3, A4, A5, B3, B4, B5, other]
  290. * @param value {width,height} mm
  291. */
  292. setPaper(type, value) {
  293. try {
  294. if (Object.keys(this.paperTypes).includes(type)) {
  295. this.curPaper = { type: type, width: value.width, height: value.height }
  296. this.hiprintTemplate.setPaper(value.width, value.height)
  297. } else {
  298. this.curPaper = { type: 'other', width: value.width, height: value.height }
  299. this.hiprintTemplate.setPaper(value.width, value.height)
  300. }
  301. } catch (error) {
  302. this.$message.error(`操作失败: ${error}`)
  303. }
  304. },
  305. },
  306. };