print.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // 打印类属性、方法定义
  2. // const Print = function (dom, options) {
  3. // if (!(this instanceof Print)) return new Print(dom, options)
  4. // this.options = this.extend({
  5. // 'noPrint': '.no-print'
  6. // }, options)
  7. // if ((typeof dom) === 'string') {
  8. // this.dom = document.querySelector(dom)
  9. // } else {
  10. // this.dom = dom
  11. // }
  12. // this.init()
  13. // }
  14. // Print.prototype = {
  15. // init: function () {
  16. // var content = this.getStyle() + this.getHtml()
  17. // this.writeIframe(content)
  18. // },
  19. // extend: function (obj, obj2) {
  20. // for (var k in obj2) {
  21. // obj[k] = obj2[k]
  22. // }
  23. // return obj
  24. // },
  25. // getStyle: function () {
  26. // var str = ''
  27. // var styles = document.getElementById('print')
  28. // // for (var i = 0; i < styles.length; i++) {
  29. // str += styles.outerHTML
  30. // // }
  31. // str += '<style>' + (this.options.noPrint ? this.options.noPrint : '.no-print') + '{display:none;}</style>'
  32. // return str
  33. // },
  34. // getHtml: function () {
  35. // var inputs = document.querySelectorAll('input')
  36. // var textareas = document.querySelectorAll('textarea')
  37. // var selects = document.querySelectorAll('select')
  38. // for (var k in inputs) {
  39. // if (inputs[k].type === 'checkbox' || inputs[k].type === 'radio') {
  40. // if (inputs[k].checked === true) {
  41. // inputs[k].setAttribute('checked', 'checked')
  42. // } else {
  43. // inputs[k].removeAttribute('checked')
  44. // }
  45. // } else if (inputs[k].type === 'text') {
  46. // inputs[k].setAttribute('value', inputs[k].value)
  47. // }
  48. // }
  49. // for (var k2 in textareas) {
  50. // if (textareas[k2].type === 'textarea') {
  51. // textareas[k2].innerHTML = textareas[k2].value
  52. // }
  53. // }
  54. // for (var k3 in selects) {
  55. // if (selects[k3].type === 'select-one') {
  56. // var child = selects[k3].children
  57. // for (var i in child) {
  58. // if (child[i].tagName === 'OPTION') {
  59. // if (child[i].selected === true) {
  60. // child[i].setAttribute('selected', 'selected')
  61. // } else {
  62. // child[i].removeAttribute('selected')
  63. // }
  64. // }
  65. // }
  66. // }
  67. // }
  68. // return this.dom.outerHTML
  69. // },
  70. // writeIframe: function (content) {
  71. // // eslint-disable-next-line
  72. // var w, doc, iframe = document.createElement('iframe'),
  73. // f = document.body.appendChild(iframe)
  74. // iframe.id = 'myIframe'
  75. // iframe.style = 'position:absolute;width:0;height:0;top:-10px;left:-10px;'
  76. // w = f.contentWindow || f.contentDocument
  77. // doc = f.contentDocument || f.contentWindow.document
  78. // doc.open()
  79. // doc.write(content)
  80. // doc.close()
  81. // this.toPrint(w)
  82. // setTimeout(function () {
  83. // document.body.removeChild(iframe)
  84. // }, 100)
  85. // },
  86. // toPrint: function (frameWindow) {
  87. // try {
  88. // setTimeout(function () {
  89. // frameWindow.focus()
  90. // try {
  91. // if (!frameWindow.document.execCommand('print', false, null)) {
  92. // frameWindow.print()
  93. // }
  94. // } catch (e) {
  95. // frameWindow.print()
  96. // }
  97. // frameWindow.close()
  98. // }, 10)
  99. // } catch (err) {
  100. // console.log('err', err)
  101. // }
  102. // }
  103. // }
  104. import store from '@/store'
  105. import moment from 'moment'
  106. /**
  107. *
  108. *
  109. * @param {*} order 订单门票打印
  110. * @param {*} ticket 单张门票信息打印,通常用于测试打印,或者已经构造好数据的门票对象
  111. */
  112. const Print = function (order, ticket) {
  113. if (ticket) {
  114. // 必须是数组
  115. store.commit('SET_PRINT_LIST', [ticket])
  116. } else {
  117. let tickets = order.tickets || order.ticketList || []
  118. tickets.forEach((item, index) => {
  119. item.teamId = order.teamId
  120. item.index = index
  121. item.remark = order.remark
  122. item.orderNo = order.orderNo
  123. item.is_otaorder = order.is_otaorder
  124. item.createTime = moment(order.createTime).format('YYYY-MM-DD HH:mm')
  125. item.hidePrice = order.hidePrice
  126. item.buyerName = order.buyerName
  127. item.groupIndividual = order.groupIndividual
  128. item.travelAgencyName = order.travelAgencyName
  129. item.manager_name = order.manager_name
  130. item.invoiceQrcode = order.invoiceQrcode || ''
  131. })
  132. store.commit('SET_PRINT_LIST', tickets)
  133. }
  134. }
  135. const PrintSmallTicket = function (order) {
  136. store.commit('SET_PRINT_ORDER', order)
  137. }
  138. const PrintPlugin = {}
  139. PrintPlugin.install = function (Vue, options) {
  140. // 4. 添加实例方法
  141. Vue.prototype.$print = Print
  142. Vue.prototype.$printSmallTicket = PrintSmallTicket
  143. }
  144. export default PrintPlugin