JeecgBoot3.6.1 中打印功能实现

JeecgBoot3.6.1中实现打印功能

前言

在项目开发中我们可能会遇到打印得需求,基于JeecgBoot框架做了如下案例

一、前端

1、vue页面代码:List.vue

<template>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--本身的列表代码省略-->	
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="print"> 重打指引单</a-button>
      </template>
    </BasicTable>
  </div>
</template>

<script lang="ts" name="triage-hisOpWaitQueue" setup>
  import { ref } from 'vue';
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns, searchFormSchema } from './List.data';
  import { list, selectTeacInfo } from './List.api';
  import { useUserStore } from '/@/store/modules/user';
  import { useMessage } from '/@/hooks/web/useMessage';
  import printJS from 'print-js';

  const checkedKeys = ref<Array<string | number>>([]);
  const userStore = useUserStore();
  const { createWarningModal } = useMessage();
  // 定义一个响应式 ref 作为表格数据源
  const dataSource = ref([]);
  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      title: '',
      dataSource,
      api: list,
      columns,
      canResize: false,
      formConfig: {
        //labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToNumber: [],
        fieldMapToTime: [],
      },
      actionColumn: {
        width: 150,
        fixed: 'right',
      },
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;
  /**
   * 打印公共方法
   * 
   */
  async function print() {
    const printInfo = await selectTeacInfo({
      queryType: '1',
      queryValue: '213123213213123',
    });
    console.log(JSON.stringify(printInfo));
    if (printInfo.state == 0) {
      createWarningModal({ title: '温馨提示', content: '数据为空' });
    } else {
      const data = {
        标题: printInfo.teaVO.title + '(学生明细)',
        教师名称: printInfo.teaVO.teaName,
        教师年龄: printInfo.teaVO.teaAge,
        教师性别: printInfo.teaVO.teaSex,
      };

      // 根据stuList生成二维数组
      let htmlContent2 = '';
      const data2D = [];
        for (const item of printInfo.stuList) {
          data2D.push([
            item.teaName || '-', // 学生名称
            item.teaAge, // 学生年龄
            item.describe || '-', // 学生描述(如果不存在特殊提示,则用破折号填充)
          ]);
        }

        // 构建HTML内容
        htmlContent2 =
          '<tr><td colspan="3" style="text-align: center;font-weight: bold;">学生明细</td></tr>' +
          // 添加列标题行
          '<tr style="text-align: center;font-weight: bold;"><td>学生名称</td><td>学生年龄</td><td>学生描述</td></tr>';

      let htmlContent = '<table>';
      for (const key in data) {
        htmlContent += '<tr><td>' + key + '</td><td colspan="3">' + data[key] + '</td></tr>';
      }

      // 遍历每一行
      for (const row of data2D) {
        htmlContent2 += '<tr>';

        // 遍历每一列
        for (const item of row) {
          htmlContent2 += `<td style="width: 15ch; word-wrap: break-word; overflow-wrap: break-word;">${item}</td>`;
        }

        htmlContent2 += '</tr>';
      }
      htmlContent += htmlContent2;
      htmlContent += '</table>';
      htmlContent = htmlContent.replace('Document', 'Guide Bill');
      printJS({
        printable: htmlContent,
        type: 'raw-html',
        header: 'Guide Bill',
        style:
          '.printable { font-family: Arial; } .description { font-weight: bold; } table { border-collapse: collapse; width: 100%; } td { border: 1px solid black; padding: 5px; }',
      });
    }
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
</script>

<style scoped>
  .btn {
    margin-right: 4px;
  }
</style>

2、List.api.ts

import { defHttp } from '/@/utils/http/axios';
import { useMessage } from '/@/hooks/web/useMessage';

const { createConfirm } = useMessage();

enum Api {
  selectTeacInfo= '/teacher/teacher/selectTeacInfo',
}

/**
 * 查询打印信息
 * @param params
 * @returns
 */
export const selectTeacInfo = (params) => defHttp.get({ url: Api.selectTeacInfo, params });

3、后端返回数据结构

{
	"stuList": [{
		"id": 1,
		"stuName": '张三',
		"stuAge": 15
		"describe": "优秀",
	}, {
		"id": 2,
		"stuName": '李四',
		"stuAge": 15
		"describe": "有进步空间",
	}],
	"teaVO": {
		"title": '数据列表',
		"teaAge": 26,
		"teaSex": 1,
	},
	"state": 1,
}

二、后端

@ApiOperation(value = "根据教师查询打印信息", notes = "根据教师查询打印信息")
@GetMapping(value = "/selectTeacInfo")
public Result<Map> selectTeacInfo(String identificationValue){
	Map map = new HashMap();
	if(StringUtils.isBlank(identificationValue)){
	    return Result.error("identificationValue参数为空");
	}else{
		// 判断是否有值
		int state = 1;
		List<Student> studentList = null;// 查询所有得学生记录
		QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(Teacher::getId,identificationValue);
		Teacher teacher = teacherService.getOne(queryWrapper);
		if (teacher!=null){
			teacher.setTitle("数据列表");// 此字段数据库中不存在,仅为显示服务
			QueryWrapper<Student> queryWrapper1 = new QueryWrapper<>();
			queryWrapper1.lambda().eq(Student::getClassId,teacher.getId());
			// 查询学生
			studentList = teacherService.list(queryWrapper1);
		}else{
			state = 0;
		}
		
		map.put("stuList",studentList);
		map.put("teaVo",teacher);
		map.put("state",state);
		return Result.ok(map);
	    }else{
		return Result.error("数据不存在");
	    }
	}
}

执行结果:

在这里插入图片描述

总结

道阻且长,一起加油哦!!!

构建一个包含CentOS 7、zsh(Z shell)和nvm(Node Version Manager)的Docker镜像,主要步骤如下: 1. **创建Dockerfile**:首先,创建一个Dockerfile文件,这将作为构建Docker镜像的蓝图。 2. **选择基础镜像**:从官方的CentOS 7镜像开始,这是我们的起点。 3. **安装必要的软件包**:更新基础镜像,安装nvm和zsh所需的依赖。 4. **安装nvm**:下载并安装nvm,这是一个可以管理多个Node.js版本的工具。 5. **安装zsh**:安装zsh以及可能需要的zsh主题或插件,比如oh-my-zsh。 6. **设置默认shell**:设置zsh为登录shell。 7. **清理临时文件**:构建镜像时清理不必要的文件和缓存,保持镜像小巧。 下面是一个简化的Dockerfile示例: ```Dockerfile FROM centos:7 # 更新系统 RUN yum -y update # 安装依赖 RUN yum -y install curl git wget zsh # 安装nvm RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash ENV NVM_DIR /root/.nvm # 安装 Node.js 和 npm # 使用 .nvmrc 文件中的版本或者指定版本号 RUN . $NVM_DIR/nvm.sh && nvm install node # 安装 zsh 插件管理器 oh-my-zsh RUN sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" # 设置 zsh 为默认 shell ENV SHELL /bin/zsh CMD ["/bin/zsh"] ``` 在实际应用中,你可能还需要添加`.nvmrc`文件来指定需要安装的Node.js版本,并且可能需要添加额外的步骤来配置zsh的环境。 构建镜像时,可以在Dockerfile所在的目录下运行以下命令: ```bash docker build -t centos7-zsh-nvm . ``` 这将启动构建过程,并在成功完成后生成一个名为`centos7-zsh-nvm`的镜像。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值