JGPrint.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.defaultPrint)
  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. return printList
  145. }
  146. // 获取打印机状态
  147. export const getStatusValue = function (ValueType,ValueIndex,callback){
  148. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  149. if (LODOP.CVERSION) {
  150. LODOP.On_Return=function(TaskID,Value){
  151. callback(Value)
  152. };
  153. }
  154. var strResult=LODOP.GET_VALUE(ValueType,ValueIndex);
  155. if (!LODOP.CVERSION) return callback(strResult); else return callback('');
  156. }
  157. // pdf 直接打印
  158. export const pdfPrint = function (data,index){
  159. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  160. LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD', 1) // 隐藏底图上有模拟走纸板的条纹线
  161. // LODOP.SET_PRINT_MODE('POS_BASEON_PAPER', true) // 可使输出以纸张边缘为基点
  162. // 开始打印
  163. const dlen = data.length
  164. // 打印进入队列开始
  165. store.commit('SET_printLoading',true)
  166. LODOP.PRINT_INIT(store.state.app.printTaskName + (dlen>1 ? ('第'+(index+1)+'页') : ''))
  167. //这个语句设置网络共享打印机连接不通时是否提示一下
  168. LODOP.SET_PRINT_MODE("TRYLINKPRINTER_NOALERT",true);
  169. //执行该语句之后,PRINT指令不再返回那个所谓“打印成功”,才能获取到打印状态
  170. LODOP.SET_PRINT_MODE("CATCH_PRINT_STATUS",true);
  171. //TaskID:任务id,Value:job代码
  172. LODOP.On_Return=function(TaskID,Value){
  173. console.log(TaskID,Value)
  174. store.commit('SET_printTaskID',Value)
  175. if(index+1 < dlen){
  176. pdfPrint(data,index+1)
  177. }else{
  178. // 打印进入队列结束
  179. store.commit('SET_printLoading',false)
  180. }
  181. };
  182. // 指定打印机
  183. LODOP.SET_PRINTER_INDEX(store.state.app.defaultPrint)
  184. LODOP.ADD_PRINT_PDF(0, 0, '100%', '100%', data[index])
  185. LODOP.SET_PRINT_PAGESIZE(3, 2090, (index==dlen-1)?50:0, '')
  186. LODOP.PRINT()
  187. }
  188. // 判断当前单据是否已存在打印任务列表中
  189. export const hasExitTaskByName = function(noCallback){
  190. const printTaskID = store.state.app.printTaskID
  191. const printLoading = store.state.app.printLoading
  192. if(printLoading){
  193. Modal.info({
  194. title: '提示',
  195. content: '打印机正在打印中...',
  196. okText: '知道了',
  197. okType: 'danger',
  198. centered: true,
  199. zIndex: 100000
  200. })
  201. }else{
  202. if(printTaskID){
  203. getStatusValue('PRINT_STATUS_BUSY',printTaskID,function(isExist){
  204. console.log(isExist)
  205. if(isExist.length){
  206. if(isExist != '0'){
  207. confirm({
  208. title: '提示',
  209. content: h => <div style='font-size:14px;'><div>打印机正在打印中...</div><div>或者有未完成的打印任务</div><div>是否继续添加打印任务?</div></div>,
  210. okText: '继续打印',
  211. okType: 'danger',
  212. cancelText: '取消',
  213. centered: true,
  214. zIndex: 100000,
  215. onOk () {
  216. noCallback()
  217. }
  218. })
  219. }else{
  220. noCallback()
  221. }
  222. }
  223. })
  224. }else{
  225. noCallback()
  226. }
  227. }
  228. }
  229. // 打印控件
  230. export const jGPrint = function (data, type, printerType, taskName) {
  231. if (!data) {
  232. return
  233. }
  234. // 针式打印
  235. if (printerType == 'NEEDLE') {
  236. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  237. if (!LODOP) {
  238. confirm({
  239. title: '提示?',
  240. content: h => <div>打印控件未安装,请先下载并安装。安装完成后,刷新页面即可打印。</div>,
  241. okText: '立即下载',
  242. okType: 'danger',
  243. cancelText: '暂不打印',
  244. onOk () {
  245. var agent = navigator.userAgent.toLowerCase();
  246. if (agent.indexOf("win32") >= 0 || agent.indexOf("wow32") >= 0) {
  247. window.open('https://iscm.360arrow.com/electron/CLodop.exe')
  248. }else if (agent.indexOf("win64") >= 0 || agent.indexOf("wow64") >= 0) {
  249. window.open('https://iscm.360arrow.com/electron/CLodop.exe')
  250. }
  251. }
  252. })
  253. return
  254. }
  255. // pdf 打印预览
  256. if (type == 'preview') {
  257. store.commit('SET_pdfPrintList', data.data)
  258. store.commit('SET_showPdfPrint', data.data)
  259. store.commit('SET_printTaskName', taskName)
  260. } else if (type == 'print') { // pdf 打印
  261. hasExitTaskByName(function(){
  262. // 选择打印机
  263. store.commit('SET_pdfPrintList', data.data)
  264. store.commit('SET_showSelectPrint', true)
  265. store.commit('SET_printTaskName', taskName)
  266. })
  267. }
  268. } else {
  269. // 喷墨打印
  270. const url = window.URL.createObjectURL(new Blob([data], { type: 'application/pdf' }))
  271. document.getElementById('print').innerHTML = '<iframe id="printfsqd" name="printfsqd" src="' + url + '" hidden></iframe>'
  272. if (type == 'preview') { // 预览
  273. window.open(url)
  274. } else if (type == 'print') { // 打印
  275. window.frames['printfsqd'].focus()
  276. window.frames['printfsqd'].print()
  277. }
  278. }
  279. }