JGPrint.js 11 KB

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