JGPrint.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import confirm from 'ant-design-vue/es/modal/confirm'
  2. import { Modal } from 'ant-design-vue'
  3. import notification from 'ant-design-vue/es/notification'
  4. import store from '@/store'
  5. import { getLodop } from '@/libs/LodopFuncs'
  6. import moment from 'moment'
  7. // 打印页签,支持批量打印
  8. export const JGPrintTag = function (width, height, data) {
  9. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  10. if (!LODOP) {
  11. confirm({
  12. title: '提示?',
  13. content: h => <div>打印控件未安装,请先下载并安装。安装完成后,刷新页面即可打印。</div>,
  14. okText: '立即下载',
  15. okType: 'danger',
  16. cancelText: '暂不打印',
  17. zIndex: 100000,
  18. onOk () {
  19. var agent = navigator.userAgent.toLowerCase();
  20. if (agent.indexOf("win32") >= 0 || agent.indexOf("wow32") >= 0) {
  21. window.open('https://iscm.360arrow.com/electron/CLodop.exe')
  22. }else if (agent.indexOf("win64") >= 0 || agent.indexOf("wow64") >= 0) {
  23. window.open('https://iscm.360arrow.com/electron/CLodop.exe')
  24. }
  25. }
  26. })
  27. return
  28. }
  29. LODOP.PRINT_INITA("0mm","0mm",width,height,"标签");
  30. LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD', 1) // 隐藏底图上有模拟走纸板的条纹线
  31. LODOP.SET_PRINT_MODE('POS_BASEON_PAPER', true) // 可使输出以纸张边缘为基点
  32. const top = data.dealerName.length > 12 ? '5mm' : '7mm';
  33. const top1 = data.dealerName.length > 12 ? '17mm' : '15mm';
  34. const top2 = data.dealerName.length > 12 ? '29mm' : '27mm';
  35. // 经销商名称
  36. LODOP.SET_PRINT_STYLE("TextNeatRow",true);
  37. LODOP.SET_PRINT_STYLE("FontSize",11);
  38. LODOP.ADD_PRINT_TEXT(top,"5mm","50mm","10mm",data.dealerName);
  39. LODOP.SET_PRINT_STYLEA(0,"Alignment",2);
  40. LODOP.SET_PRINT_STYLEA(0,"Horient",2);
  41. // 货位号
  42. LODOP.SET_PRINT_STYLE("TextNeatRow",true);
  43. LODOP.SET_PRINT_STYLE("FontSize",32);
  44. LODOP.ADD_PRINT_TEXT(top1,"50%","28mm","15mm",data.shelfPlaceCode);
  45. // 产品编码
  46. LODOP.SET_PRINT_STYLE("TextNeatRow",true);
  47. LODOP.SET_PRINT_STYLE("FontSize",11);
  48. LODOP.ADD_PRINT_TEXT(top2,"50%","30mm","15mm",data.productCode);
  49. // 二维码
  50. LODOP.ADD_PRINT_BARCODE(top1,'7mm',90,90,"QRCode",data.qrCodeContent)
  51. LODOP.SET_PRINT_STYLE("QRCodeVersion",5)
  52. // LODOP.SET_PRINT_STYLE("QRCodeErrorLevel",'H')
  53. LODOP.SET_PRINT_COPIES(data.printQty)// 指定份数
  54. LODOP.SET_PRINT_PAGESIZE(1, width, height)
  55. // LODOP.PREVIEW()
  56. LODOP.PRINT()
  57. }
  58. export const demoGetBASE64 = function (dataArray) {
  59. var digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
  60. var strData = ''
  61. for (var i = 0, ii = dataArray.length; i < ii; i += 3) {
  62. if (isNaN(dataArray[i])) break
  63. var b1 = dataArray[i] & 0xFF; var b2 = dataArray[i + 1] & 0xFF; var b3 = dataArray[i + 2] & 0xFF
  64. var d1 = b1 >> 2; var d2 = ((b1 & 3) << 4) | (b2 >> 4)
  65. var d3 = i + 1 < ii ? ((b2 & 0xF) << 2) | (b3 >> 6) : 64
  66. var d4 = i + 2 < ii ? (b3 & 0x3F) : 64
  67. strData += digits.substring(d1, d1 + 1) + digits.substring(d2, d2 + 1) + digits.substring(d3, d3 + 1) + digits.substring(d4, d4 + 1)
  68. }
  69. return strData
  70. }
  71. // 导出下载excel
  72. export const downloadExcel = function (data, fileName) {
  73. if (!data) { return }
  74. const a = moment().format('YYYYMMDDHHmmss')
  75. const fname = fileName + a
  76. const blob = new Blob([data], { type: 'application/vnd.ms-excel' })
  77. if (window.navigator && window.navigator.msSaveOrOpenBlob) {
  78. navigator.msSaveBlob(blob, fname + '.xlsx')
  79. } else {
  80. const link = document.createElement('a')
  81. link.style.display = 'none'
  82. var href = URL.createObjectURL(blob)
  83. link.href = href
  84. link.setAttribute('download', fname + '.xlsx')
  85. document.body.appendChild(link)
  86. link.click()
  87. document.body.removeChild(link)
  88. window.URL.revokeObjectURL(href) // 释放掉blob对象
  89. }
  90. }
  91. /*
  92. *printerType: 打印机类型
  93. *type: 打印预览,打印,导出
  94. *url: 数据请求接口
  95. *params: 请求参数
  96. *fileName: 导出文件名称
  97. *callback: 回调函数
  98. */
  99. export const hdPrint = function (printerType, type, url, params, fileName, callback) {
  100. // 打印时需要传打印机类型
  101. if (type !== 'export') {
  102. params.type = printerType
  103. }
  104. url(params).then(res => {
  105. console.log(res, type, printerType)
  106. if (res.type == 'application/json') {
  107. var reader = new FileReader()
  108. reader.addEventListener('loadend', function () {
  109. const obj = JSON.parse(reader.result)
  110. notification.error({
  111. message: '提示',
  112. description: obj.message
  113. })
  114. })
  115. reader.readAsText(res)
  116. } else {
  117. if (type == 'export') { // 导出
  118. downloadExcel(res, fileName)
  119. } else { // 打印
  120. const taskName = fileName+'-'+(params.sn||params.checkWarehouseSn||params.settleClientSn)+'-'
  121. jGPrint(res, type, printerType, taskName)
  122. }
  123. }
  124. callback()
  125. })
  126. }
  127. // pdf blob 转 base64
  128. export const blobToBaseByPdf = function(data,callback){
  129. const reader = new FileReader()
  130. reader.readAsDataURL(new Blob([data], { type: 'application/pdf' }))
  131. reader.addEventListener('load', () => {
  132. callback(reader.result)
  133. })
  134. }
  135. // lodop 打印预览
  136. export const lodopPrintView = function(data){
  137. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  138. LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD', 1) // 隐藏底图上有模拟走纸板的条纹线
  139. // LODOP.SET_PRINT_MODE('POS_BASEON_PAPER', true) // 可使输出以纸张边缘为基点
  140. // 预览
  141. const dlen = data.length
  142. LODOP.PRINT_INIT('')
  143. if(dlen > 1){
  144. LODOP.SET_SHOW_MODE("HIDE_PBUTTIN_PREVIEW",1);//隐藏打印按钮
  145. }
  146. LODOP.SET_PRINTER_INDEX(store.state.app.printDefNeedle)
  147. LODOP.ADD_PRINT_PDF(0, 0, '100%', '100%', data[0])
  148. LODOP.SET_PRINT_PAGESIZE(3,2090,dlen>1?0:50,"");
  149. LODOP.PREVIEW()
  150. }
  151. // 获取打印机列表
  152. export const getPrintList = function(){
  153. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  154. const printCount = LODOP.GET_PRINTER_COUNT()
  155. const printList = []
  156. for(let i=0;i<printCount;i++){
  157. const pname = LODOP.GET_PRINTER_NAME(i)
  158. if(pname.indexOf('Microsoft')<0&&pname.indexOf('OneNote')<0&&pname.indexOf('Fax')<0&&pname.indexOf('导出')<0){
  159. printList.push({name:pname,value:i})
  160. }
  161. }
  162. // store.state.app.printDefNeedle = LODOP.GET_PRINTER_NAME(-1)
  163. return printList
  164. }
  165. // 获取打印机状态
  166. export const getStatusValue = function (ValueType,ValueIndex,callback){
  167. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  168. if (LODOP.CVERSION) {
  169. LODOP.On_Return=function(TaskID,Value){
  170. callback(Value)
  171. };
  172. }
  173. var strResult=LODOP.GET_VALUE(ValueType,ValueIndex);
  174. if (!LODOP.CVERSION) return callback(strResult); else return callback('');
  175. }
  176. // pdf 直接打印
  177. export const pdfPrint = function (data,index){
  178. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  179. LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD', 1) // 隐藏底图上有模拟走纸板的条纹线
  180. // LODOP.SET_PRINT_MODE('POS_BASEON_PAPER', true) // 可使输出以纸张边缘为基点
  181. // 开始打印
  182. const dlen = data.length
  183. // 打印进入队列开始
  184. store.commit('SET_printLoading',true)
  185. LODOP.PRINT_INIT(store.state.app.printTaskName + (dlen>1 ? ('第'+(index+1)+'页') : ''))
  186. //这个语句设置网络共享打印机连接不通时是否提示一下
  187. LODOP.SET_PRINT_MODE("TRYLINKPRINTER_NOALERT",true);
  188. //执行该语句之后,PRINT指令不再返回那个所谓“打印成功”,才能获取到打印状态
  189. LODOP.SET_PRINT_MODE("CATCH_PRINT_STATUS",true);
  190. //TaskID:任务id,Value:job代码
  191. LODOP.On_Return=function(TaskID,Value){
  192. console.log(TaskID,Value)
  193. store.commit('SET_printTaskID',Value)
  194. if(index+1 < dlen){
  195. pdfPrint(data,index+1)
  196. }else{
  197. // 打印进入队列结束
  198. store.commit('SET_printLoading',false)
  199. }
  200. };
  201. console.log(store.state.app.printDefNeedle)
  202. // 指定打印机
  203. LODOP.SET_PRINTER_INDEX(store.state.app.printDefNeedle)
  204. LODOP.ADD_PRINT_PDF(0, 0, '100%', '100%', data[index])
  205. LODOP.SET_PRINT_PAGESIZE(3, 2090, (index==dlen-1)?50:0, '')
  206. LODOP.PRINT()
  207. }
  208. // 判断当前单据是否已存在打印任务列表中
  209. export const hasExitTaskByName = function(noCallback){
  210. const printTaskID = store.state.app.printTaskID
  211. const printLoading = store.state.app.printLoading
  212. if(printLoading){
  213. Modal.info({
  214. title: '提示',
  215. content: '打印机正在打印中...',
  216. okText: '知道了',
  217. okType: 'danger',
  218. centered: true,
  219. zIndex: 100000
  220. })
  221. }else{
  222. noCallback()
  223. // if(printTaskID){
  224. // getStatusValue('PRINT_STATUS_BUSY',printTaskID,function(isExist){
  225. // console.log(isExist)
  226. // if(isExist.length){
  227. // if(isExist != '0'){
  228. // confirm({
  229. // title: '提示',
  230. // content: h => <div style='font-size:14px;'><div>打印机正在打印中或有未完成的打印任务</div><div>是否继续添加打印任务?</div></div>,
  231. // okText: '继续打印',
  232. // okType: 'danger',
  233. // cancelText: '取消',
  234. // centered: true,
  235. // zIndex: 100000,
  236. // onOk () {
  237. // noCallback()
  238. // }
  239. // })
  240. // }else{
  241. // noCallback()
  242. // }
  243. // }
  244. // })
  245. // }else{
  246. // noCallback()
  247. // }
  248. }
  249. }
  250. // 打印控件
  251. export const jGPrint = function (data, type, printerType, taskName) {
  252. console.log(data,taskName)
  253. if (!data) {
  254. return
  255. }
  256. // 针式打印
  257. if (printerType == 'NEEDLE') {
  258. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  259. if (!LODOP) {
  260. confirm({
  261. title: '提示?',
  262. content: h => <div>打印控件未安装,请先下载并安装。安装完成后,刷新页面即可打印。</div>,
  263. okText: '立即下载',
  264. okType: 'danger',
  265. cancelText: '暂不打印',
  266. onOk () {
  267. var agent = navigator.userAgent.toLowerCase();
  268. if (agent.indexOf("win32") >= 0 || agent.indexOf("wow32") >= 0) {
  269. window.open('https://iscm.360arrow.com/electron/CLodop.exe')
  270. }else if (agent.indexOf("win64") >= 0 || agent.indexOf("wow64") >= 0) {
  271. window.open('https://iscm.360arrow.com/electron/CLodop.exe')
  272. }
  273. }
  274. })
  275. return
  276. }
  277. // pdf 打印预览
  278. if (type == 'preview') {
  279. store.commit('SET_pdfPrintList', data.data)
  280. store.commit('SET_showPdfPrint', data.data)
  281. store.commit('SET_printTaskName', taskName)
  282. } else if (type == 'print') { // pdf 打印
  283. // 选择打印机
  284. const printDefNeedle = store.state.app.printDefNeedle
  285. const hasPrint = getPrintList().find(item => item.name == printDefNeedle)
  286. console.log(printDefNeedle,hasPrint)
  287. // 已经设置默认打印机
  288. if(hasPrint){
  289. hasExitTaskByName(function () {
  290. pdfPrint(data.data, 0)
  291. })
  292. }else{
  293. // 没有设置默认打印机时,请选择打印机
  294. store.commit('SET_pdfPrintList', data.data)
  295. store.commit('SET_printTaskName', taskName)
  296. store.commit('SET_printUseing', 1)
  297. store.commit('SET_showSelectPrint', true)
  298. }
  299. }
  300. } else {
  301. // 喷墨打印
  302. const url = window.URL.createObjectURL(new Blob([data], { type: 'application/pdf' }))
  303. document.getElementById('print').innerHTML = '<iframe id="printfsqd" name="printfsqd" src="' + url + '" hidden></iframe>'
  304. if (type == 'preview') { // 预览
  305. window.open(url)
  306. } else if (type == 'print') { // 打印
  307. window.frames['printfsqd'].focus()
  308. window.frames['printfsqd'].print()
  309. }
  310. }
  311. }