index.blade.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. @extends("layouts.app")
  2. @section("title","打印")
  3. @section("content")
  4. <span id="nav2">
  5. @component('maintenance.menu')@endcomponent
  6. @component('maintenance.expressPrinting.menu')@endcomponent
  7. @component('maintenance.expressPrinting.print.menu')@endcomponent
  8. </span>
  9. <div class="container" id="print">
  10. <div class="row">
  11. <div class="container">
  12. <div class="input-group">
  13. <label for="scaleNumber" class="col-form-label-sm">缩放比例</label>
  14. <input id="scaleNumber" type="number" v-model="scaleNumber" class="form-control-sm">
  15. <button class="btn btn-success" @click="printImage">打印</button>
  16. </div>
  17. </div>
  18. </div>
  19. <div class="row p-0 m-0" id="img-div">
  20. </div>
  21. <div class="container m-0 p-0" id="print-div" :style="getByWidth(template)" >
  22. <div class="position-relative border-1 row p-0 m-0 bg-white border print-template" v-for="(item,i) in items" :style="getBgStyle(template)">
  23. <div class="position-absolute" v-for="(part,index) in parts" :style="getStyle(part)">
  24. <svg v-if="isSvg(part)" :style="getSvgStyle(part)" :id="'svg'+i" class="StripeCode">
  25. @{{ getContent(part,item) }}
  26. </svg>
  27. <span v-else>@{{ getContent(part,item) }}</span>
  28. </div>
  29. </div>
  30. </div>
  31. </div>
  32. @endsection
  33. @section("lastScript")
  34. <script type="text/javascript" src="{{mix('js/utilities/barcode.js')}}"></script>
  35. <script type="text/javascript" src="{{mix('js/utilities/html2canvas.js')}}"></script>
  36. <script>
  37. let vue = new Vue({
  38. el:"#print",
  39. data:{
  40. template:{!! $template !!},
  41. items:{!! $items !!},
  42. parts:[],
  43. index:0,
  44. scaleNumber:1,
  45. },
  46. created() {
  47. this.parts = JSON.parse(this.template.value);
  48. },
  49. mounted() {
  50. $.each(this.items,(index,item)=>{
  51. window.setBarcode('123456',"#svg"+index,2,50,true);
  52. $("#svg"+index).find("svg");
  53. });
  54. },
  55. methods:{
  56. isSvg(part){
  57. return part.type==="StripeCode";
  58. },
  59. printingTemplate(template,items){
  60. let _this = this;
  61. $.each(items,function(i,item){
  62. _this.paddingPrintDiv(template,item);
  63. });
  64. },
  65. getBgStyle(template){
  66. if (this.parts === []) this.parts = JSON.parse(template.value);
  67. let style = {};
  68. this.parts.forEach((item)=>{
  69. if(item.type==="bg"){
  70. style = {
  71. width:item.width+'px',
  72. height:item.height+'px'
  73. }
  74. }
  75. });
  76. return style;
  77. },
  78. getByWidth(){
  79. if (this.parts === []) this.parts = JSON.parse(template.value);
  80. let style = {};
  81. this.parts.forEach((item)=>{
  82. if(item.type==="bg"){
  83. style = {
  84. width:item.width+'px',
  85. transform: "scale(" +this.scaleNumber +")",
  86. transformOrigin: "left top",
  87. }
  88. }
  89. });
  90. return style;
  91. },
  92. getSvgStyle(item){
  93. return {
  94. width:item.width+'px',
  95. height:item.height+'px',
  96. };
  97. },
  98. getStyle(item){
  99. let style = JSON.parse(JSON.stringify(item));
  100. style.display = 'flex';
  101. style['border-style'] = style.borderStyle;
  102. style['border-width'] = style.borderWidth+'px';
  103. style['font-size'] = style.fontSize+'px';
  104. style['font-weight'] = style.fontWeight;
  105. style['justify-content'] = style.justifyContent;
  106. style['align-items'] = style.alignItems;
  107. style.width+='px';
  108. style.height+='px';
  109. style.left+='px';
  110. style.top+='px';
  111. style['white-space'] = 'pre'; // 字体换行
  112. return style;
  113. },
  114. getContent(part,item){ // 组件内容
  115. return part.text;
  116. if(part.text.indexOf("$")===-1)return part.text;
  117. if(part.text.indexOf("+$")!==-1){
  118. let arr = part.text.split("+$");
  119. return arr[0]+item[ arr[1]];
  120. }
  121. if(part.text.indexOf("$")!==-1){
  122. let arr = part.text.split("$");
  123. return arr[0]+item[arr[1]];
  124. }
  125. },
  126. printImage(){
  127. let _this = this;
  128. window.canvasImg("#print-div","#img-div",this.scaleNumber,function(canvas){
  129. let data = canvas.toBlob(function (blob){
  130. let formData =new FormData();
  131. formData.append("blob",blob);
  132. _this.printData(formData);
  133. },"image/png");
  134. });
  135. },
  136. printData(data){
  137. window.axios.post("{{url("apiLocal/maintenance/expressPrinting/part/printTemplate")}}",data).then(res=>{
  138. tempTip.showSuccess(res);
  139. }).catch(err=>{
  140. tempTip.showSuccess(err);
  141. });
  142. }
  143. }
  144. })
  145. </script>
  146. @endsection