recipe_A5.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <script>
  2. import CLodop from '@/libs/print/CLodop';
  3. import {getDevice, getDevices} from '@/tools/print.tool';
  4. import {templateA5} from '@/components/print/template';
  5. import {selectOrderDetail, selectOrderDetail2} from '@/api/prescription/prescriptionAudit';
  6. import {bignumber, chain, multiply} from 'mathjs';
  7. export default {
  8. name: 'print_recipe_a5',
  9. props: {
  10. id: {type: [String, Number], required: true},
  11. selectable: Boolean,
  12. },
  13. data() {
  14. return {
  15. loaded: false,
  16. preview: false,
  17. total: 1,
  18. tag: `print-preview_${Date.now()}`,
  19. paper: '中药处方笺',
  20. paperWidth: 1485,
  21. paperHeight: 2100,
  22. model: null,
  23. device: null,
  24. devices: [],
  25. printing: false,
  26. };
  27. },
  28. computed: {
  29. loading() {
  30. return !this.loaded || !this.preview;
  31. },
  32. deviceId() {
  33. return this.device ? this.device.index : '';
  34. },
  35. },
  36. mounted() {
  37. if (this.selectable) getDevices().then(devices => this.devices = devices);
  38. this.loaded = true;
  39. },
  40. methods: {
  41. async print(preview = false) {
  42. if (preview) this.loaded = false;
  43. const instance = await CLodop();
  44. const model = await this.getModel();
  45. templateA5.call(instance, model, `${this.paper}`);
  46. const device = await this.getDevice();
  47. if (device) {
  48. if (device.papers.includes(this.paper)) instance['SET_PRINT_PAGESIZE'](1, 0, 0, this.paper);
  49. else instance['SET_PRINT_PAGESIZE'](1, this.paperWidth, this.paperHeight, 'CreateCustomPage');
  50. instance['SET_PRINTER_INDEX'](device.index);
  51. instance['SET_PRINT_COPIES'](this.total);
  52. if (preview) {
  53. instance['SET_PREVIEW_WINDOW'](1, 1, 1, 0, 0, `${this.paper}.开始打印`);
  54. instance['SET_SHOW_MODE']('PREVIEW_IN_BROWSE', true);
  55. instance['SET_PRINT_MODE']('AUTO_CLOSE_PREWINDOW', true);
  56. instance['PREVIEW'](this.tag);
  57. } else {
  58. if (instance['PRINT']()) this.complete();
  59. else this.$message.warning(`请检查打印机 (${this.device.name})`);
  60. }
  61. } else if (preview) {
  62. this.devices = await getDevices();
  63. } else {
  64. if (instance['PRINTA']()) this.complete();
  65. else this.$message.warning(`请检查打印机`);
  66. }
  67. this.printing = false;
  68. this.loaded = true;
  69. },
  70. delay() {
  71. setTimeout(() => {this.preview = true;}, 500);
  72. },
  73. complete() {
  74. this.$message.success(`开始打印`);
  75. this.$emit('close', true);
  76. },
  77. async getModel() {
  78. if (this.model) return this.model;
  79. this.model = await selectOrderDetail2({id: this.id});
  80. const count = bignumber(this.model.recipe.count || 0);
  81. let unitWeight = chain(bignumber(0));
  82. for (const medicine of this.model.recipe.medicines) {
  83. unitWeight = unitWeight.add(bignumber(medicine.dosage));
  84. }
  85. this.model.recipe.unitWeight = +unitWeight.valueOf().toFixed(2);
  86. this.model.recipe.totalWeight = +unitWeight.multiply(count).valueOf().toFixed(2);
  87. return this.model;
  88. },
  89. async getDevice() {
  90. if (!this.device) this.device = await getDevice(
  91. `name: Six:${this.paper}`,
  92. `name: Six:${this.paperWidth}*${this.paperHeight}`,
  93. `driver: Canon LBP6030/6040/6018L; paper: ${this.paper}`,
  94. `driver: Canon LBP6030/6040/6018L; form: A5`,
  95. `driver: Canon LBP6030/6040/6018L;`,
  96. {width: this.paperWidth, height: this.paperHeight},
  97. `paper:${this.paper}`,
  98. `form: A5`,
  99. );
  100. console.log(this.device);
  101. return this.device;
  102. },
  103. selected(value) {
  104. this.device = this.devices.find(device => device.index === value);
  105. this.print(true);
  106. },
  107. },
  108. };
  109. </script>
  110. <template>
  111. <div class="print-preview">
  112. <div class="content" v-loading="loading">
  113. <div class="top" :style="{backgroundColor: preview ? '#f0f0f0' : 'transparent'}">
  114. {{ device && device.name }}
  115. </div>
  116. <iframe :id="tag" @load="delay"></iframe>
  117. </div>
  118. <div style="display: flex; align-items: center; justify-content: space-evenly;">
  119. <el-select v-if="devices.length" placeholder="打印机" :value="deviceId" @change="selected">
  120. <el-option v-for="item in devices" :key="item.index" :label="item.name" :value="item.index"></el-option>
  121. </el-select>
  122. <el-button style="width: 100%;" type="primary" :disabled="loading" :loading="printing"
  123. @click="printing = true;print()">打印
  124. </el-button>
  125. </div>
  126. </div>
  127. </template>
  128. <style scoped lang="scss">
  129. .print-preview {
  130. margin: auto;
  131. width: 559px + 100px;
  132. display: flex;
  133. flex-direction: column;
  134. .content {
  135. flex: auto;
  136. display: flex;
  137. flex-direction: column;
  138. }
  139. .top {
  140. height: 40px;
  141. font-size: 12px;
  142. color: rgba(0, 0, 0, 0.5);
  143. }
  144. iframe {
  145. border: none;
  146. width: 100%;
  147. flex: auto;
  148. }
  149. > div {
  150. flex: none;
  151. }
  152. .el-select {
  153. margin-right: 12px;
  154. }
  155. }
  156. </style>