index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <template>
  2. <div class="print-template">
  3. <module-view :propConfig="config" ref="view" v-loading="load" @pagination="pagination" @onRefresh="getList" @resert="queryList" @search="queryList"
  4. @modalHandle="modalHandle" @clickHandle="clickHandle" @detail="detail"/>
  5. </div>
  6. </template>
  7. <script lang="ts">
  8. import { Component, Prop, Vue, Watch } from "vue-property-decorator";
  9. import {query,add,update,del} from '@/api/printTemplate'
  10. // import byMonacoEditor from '@/benyun/components/byMonacoEditor/byMonacoEditor.vue'
  11. import {preview} from '@/api/print'
  12. import EditInput from "./components/editInput.vue";
  13. @Component({components:{}})
  14. export default class PrintTemplate extends Vue {
  15. load=false;
  16. isSearch=false
  17. timeNum = 0;
  18. config:any={
  19. attr:{
  20. calculateH:true
  21. },
  22. search:{
  23. attr:{
  24. size:'small'
  25. },
  26. columns:[
  27. [{
  28. span:8,
  29. label:'模板名称',
  30. prop:'templateName',
  31. component:'by-input',
  32. compConfig:{
  33. attr:{
  34. clearable:true
  35. }
  36. }
  37. },{
  38. span:8,
  39. label:'模板引擎',
  40. prop:'templateEngine',
  41. component:'by-select',
  42. compConfig:{
  43. attr:{
  44. clearable:true,
  45. data:[{
  46. label:'freemarker',
  47. value:'freemarker'
  48. },{
  49. label:'thymeleaf',
  50. value:'thymeleaf'
  51. }]
  52. }
  53. }
  54. }]
  55. ]
  56. },
  57. tool:{
  58. tools:{
  59. search:true,
  60. refresh:true,
  61. add:true,
  62. delete:true
  63. }
  64. },
  65. table:{
  66. attr:{
  67. size:'mini',
  68. seq:true,
  69. checkbox:true
  70. },
  71. columns:[{
  72. title:'模板名称',
  73. isDetail:true,
  74. field:'templateName'
  75. },{
  76. title:'模板引擎',
  77. field:'templateEngine',
  78. width:130,
  79. },{
  80. title:'打印次数限制',
  81. field:'printLimit',
  82. width:130
  83. },{
  84. title:'创建时间',
  85. field:'createTime',
  86. width:140
  87. },{
  88. title:'操作',
  89. action:true,
  90. width:100,
  91. plugins:[{
  92. name:'删除',
  93. event:{
  94. click:(item:any) => {
  95. this.singleDel(item);
  96. }
  97. }
  98. }]
  99. }]
  100. },
  101. modal:{
  102. tool:{
  103. tools:{
  104. return:true,
  105. add:true
  106. },
  107. customTools:[{
  108. name: '预览',
  109. icon: 'el-icon-view',
  110. event:{
  111. click:() => {
  112. this.preView()
  113. }
  114. }
  115. }]
  116. },
  117. form:{
  118. attr:{
  119. size:'small',
  120. width:'1200px',
  121. rules:{
  122. templateName:[
  123. { required: true, message: '模版名称不能为空!', trigger: 'blur' }
  124. ],
  125. templateEngine:[
  126. { required: true, message: '请选择模板引擎!', trigger: 'change' }
  127. ],
  128. templateHtml:[
  129. { required: true, message: '模板内容不能为空!', trigger: 'change' }
  130. ],
  131. dataExample:[
  132. {validator: (rule:any, value:any, callback:Function) => {
  133. try {
  134. if (value && typeof (JSON.parse(value)) === 'number') {
  135. callback(new Error('输入项非JSON,请检查!'))
  136. }else{
  137. callback()
  138. }
  139. } catch (e) {
  140. callback(new Error('输入项非JSON,请检查!'))
  141. }
  142. }, trigger: 'blur' }
  143. ],
  144. printLimit:[
  145. {required: true, message: '打印次数限制不能为空!', trigger: 'blur'}
  146. ]
  147. }
  148. },
  149. columns:[
  150. [{
  151. label:'模版名称',
  152. prop:'templateName',
  153. component:'by-input',
  154. compConfig:{
  155. attr:{
  156. clearable:true
  157. }
  158. }
  159. }],
  160. [{
  161. label:'模板引擎',
  162. prop:'templateEngine',
  163. component:'by-select',
  164. compConfig:{
  165. attr:{
  166. clearable:true,
  167. data:[{
  168. label:'freemarker',
  169. value:'freemarker'
  170. },{
  171. label:'thymeleaf',
  172. value:'thymeleaf'
  173. }]
  174. }
  175. }
  176. },{
  177. label:'打印次数限制',
  178. labelWidth:'120px',
  179. prop:'printLimit',
  180. component:'by-input',
  181. compConfig:{
  182. attr:{
  183. clearable:true,
  184. type:'integer'
  185. }
  186. }
  187. }],
  188. [{
  189. label:'模板内容',
  190. prop:'templateHtml',
  191. component:EditInput,
  192. }],
  193. [{
  194. label:'示例数据',
  195. prop:'dataExample',
  196. component:'by-input',
  197. compConfig:{
  198. attr:{
  199. defaultValue:'{}',
  200. type:'textarea',
  201. rows:8
  202. }
  203. }
  204. }]
  205. ]
  206. }
  207. }
  208. }
  209. mounted(){
  210. // this.$nextTick(()=>{
  211. this.getList()
  212. // })
  213. }
  214. //预览
  215. preView(){
  216. let data = (this.$refs.view as any).getFormValue();
  217. if(!data.templateEngine){
  218. this.$message('模版引擎不能为空!')
  219. return
  220. }
  221. if(!data.templateHtml){
  222. this.$message('模版内容不能为空!')
  223. return
  224. }
  225. if(!data.dataExample){
  226. this.$message('示例数据不能为空!')
  227. return
  228. }
  229. this.load=true;
  230. const that:any = this;
  231. preview({
  232. templateEngine:data.templateEngine,
  233. templateHtml:data.templateHtml,
  234. templateData:data.dataExample,
  235. outType:'PDF'
  236. }).then((res:any) => {
  237. this.load=false;
  238. if(res.type === 'application/json'){
  239. const reader:any = new FileReader();
  240. reader.onload = function (){
  241. const d:any = JSON.parse(reader.result);
  242. if(d && d.msg){
  243. that.$message({
  244. message:d.msg,
  245. type:'error'
  246. })
  247. }
  248. }
  249. reader.readAsText(res);
  250. return
  251. }
  252. const blob = new Blob([res],{
  253. type: "application/pdf;chartset=UTF-8"
  254. })
  255. const qrUrl:any =(window as any).URL.createObjectURL(blob);
  256. (window as any).open(qrUrl);
  257. // const myWindow =(window as any).open(qrUrl);
  258. // myWindow.print();
  259. }).catch((err:any)=>{
  260. this.load=false;
  261. })
  262. }
  263. //单个删除
  264. singleDel(item:any){
  265. let data=[item.id];
  266. this.$confirm('此操作将永久删除“'+item.templateName+'”, 是否继续?', '提示', {
  267. confirmButtonText: '确定',
  268. cancelButtonText: '取消',
  269. type: 'warning'
  270. }).then(() => {
  271. this.del(data);
  272. }).catch(() => {});
  273. }
  274. //多个删除
  275. multipleDel(){
  276. let data = (this.$refs.view as any).getSelectData();
  277. if(data.length == 0){
  278. this.$message('请选择需要删除的数据!')
  279. return
  280. }
  281. let ids:Array<any>=[];
  282. let names='';
  283. for(const item of data){
  284. ids.push(item.id);
  285. if(names){
  286. names = names + ',' + item.templateName
  287. }else{
  288. names = item.templateName;
  289. }
  290. }
  291. this.$confirm('此操作将永久删除“'+names+'”, 是否继续?', '提示', {
  292. confirmButtonText: '确定',
  293. cancelButtonText: '取消',
  294. type: 'warning'
  295. }).then(() => {
  296. this.del(ids);
  297. }).catch(() => {});
  298. }
  299. //删除
  300. del(data:Array<any>){
  301. this.load = true;
  302. del({ids:data}).then(()=>{
  303. this.load = false;
  304. this.$message({
  305. message:'删除成功!',
  306. type:'success'
  307. })
  308. this.pagination();
  309. }).catch(()=>{
  310. this.load = false;
  311. })
  312. }
  313. //分页
  314. pagination(){
  315. if(this.isSearch){
  316. this.queryList();
  317. }else{
  318. this.getList()
  319. }
  320. }
  321. //列表请求(只有分页,不包含搜素条件)
  322. getList(){
  323. if(!this.$refs.view){
  324. if(this.timeNum > 5){
  325. return
  326. }
  327. setTimeout(()=>{
  328. this.getList()
  329. },500)
  330. this.timeNum ++;
  331. return
  332. }
  333. this.isSearch = false;
  334. let data = (this.$refs.view as any).getPage();
  335. delete data.total;
  336. this.requestList(data);
  337. }
  338. //列表请求(包含分页和搜素条件)
  339. queryList(){
  340. this.isSearch = true;
  341. let data = (this.$refs.view as any).getQuery();
  342. delete data.total;
  343. this.requestList(data);
  344. }
  345. requestList(data:any){
  346. this.load = true;
  347. query(data).then((res:any) => {
  348. this.load = false;
  349. (this.$refs.view as any).setTableValue(res.data.records);
  350. let page = {
  351. pageNo: res.data.current, //当前页
  352. pageSize: res.data.size, //每页条数
  353. total: res.data.total //总条数
  354. };
  355. (this.$refs.view as any).setPage(page)
  356. }).catch(()=>{
  357. this.load = false;
  358. })
  359. }
  360. modalHandle(n:string){
  361. if(n == 'onAdd'){
  362. (this.$refs.view as any).getFormValidate(this.onAdd)
  363. }
  364. if(n == 'onSave'){
  365. (this.$refs.view as any).getFormValidate(this.onSave)
  366. }
  367. }
  368. //新增
  369. onAdd(){
  370. let value = (this.$refs.view as any).getFormValue();
  371. if(value){
  372. this.load = true;
  373. add(value).then((res:any)=>{
  374. this.load = false;
  375. this.$message({
  376. message:res.msg,
  377. type:'success'
  378. });
  379. (this.$refs.view as any).closeModal();
  380. this.getList();
  381. }).catch(()=>{
  382. this.load = false;
  383. })
  384. }
  385. }
  386. //保存
  387. onSave(){
  388. let data = (this.$refs.view as any).getFormValue();
  389. this.load = true;
  390. update(data).then((res:any) => {
  391. this.$message({
  392. message:res.msg,
  393. type:'success'
  394. });
  395. this.load = false;
  396. (this.$refs.view as any).closeModal();
  397. this.pagination();
  398. }).catch(()=>{
  399. this.load = false;
  400. })
  401. }
  402. clickHandle(n:string){
  403. if(n == 'onAdd'){
  404. this.config.modal.tool.tools.add = true;
  405. delete this.config.modal.tool.tools.save;
  406. (this.$refs.view as any).initFormTool();
  407. }
  408. if(n == 'onDelete'){
  409. this.multipleDel()
  410. }
  411. }
  412. //点击详情
  413. detail(){
  414. this.config.modal.tool.tools.save = true;
  415. delete this.config.modal.tool.tools.add;
  416. (this.$refs.view as any).initFormTool();
  417. }
  418. }
  419. </script>
  420. <style lang="scss" scoped>
  421. .print-template{
  422. height: 100%;
  423. width: 100%;
  424. overflow-y: hidden;
  425. }
  426. </style>