套打.二开案例.获取套打页面的json数据
【场景】获取套打页面的json数据,自行实现渲染器渲染,不依赖平台的预览、打印端,确保效果一致
【案例】提供不同版本下获取json数据的接口
**注意,此接口外层不要并发调用,最好可通过执行计划排队串行执行,避免引起服务器运行内存上涨(页面存于服务器运行内存中)**
```chsarp
using Kingdee.BOS;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.NotePrint;
using Kingdee.BOS.JSON;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Web.Core;
using Kingdee.BOS.Web.Printing;
using System;
using System.Collections.Generic;
namespace DynamicFormPlugIn.NotePrint
{
[Kingdee.BOS.Util.HotUpdate]
public class GenerateXamlPlugIn : AbstractDynamicFormPlugIn
{
public override void ButtonClick(ButtonClickEventArgs e)
{
if (string.Equals(e.Key, "F_BHR_ButtonGenerate", StringComparison.OrdinalIgnoreCase))
{
/*
* 表单标识、套打模板、单据内码
*/
string formId = "PUR_PurchaseOrder";
string templateId = "734cffd8-26ef-4131-b0c2-1d26b29d90c0";
string billId = "100001";
//版本1
var jsonObj = CreatePageJson(formId, billId, templateId);
SaveJson("1", jsonObj);
//版本2
var jsonObj2 = CreatePageJsonV2(formId, billId, templateId);
SaveJson("2", jsonObj2);
//版本3
var jsonObj3 = CreatePageJsonV3(formId, billId, templateId);
SaveJson("3", jsonObj3);
}
}
private void SaveJson(string fileName, JSONObject jsonObj)
{
string filePath = string.Format("D:\\Test\\{0}.json", fileName);
System.IO.File.WriteAllText(filePath, jsonObj.ToJSONString());
}
#region 方案一 模拟前端请求获取数据,使用版本 全版本基本通用; 缺陷:不支持分页
private JSONObject CreatePageJson(string formId, string billId, string templateId)
{
IDynamicFormView dynamicFormView = CreateView(this.View.Context, formId, billId); //此行为代码模拟打开凭证界面
return ExportTargetBill(dynamicFormView, formId, billId, templateId);
}
/// <summary>
/// 模拟前端请求生成json数据
/// </summary>
/// <param name="dynamicFormView"></param>
/// <param name="formId"></param>
/// <param name="billId"></param>
/// <param name="templateId"></param>
/// <returns></returns>
private JSONObject ExportTargetBill(IDynamicFormView dynamicFormView, string formId, string billId, string templateId)
{
Kingdee.BOS.Core.Import.IImportView importView = dynamicFormView as Kingdee.BOS.Core.Import.IImportView;
IDynamicFormViewService viewService = dynamicFormView as IDynamicFormViewService;
if (importView == null || viewService == null)
return null;
try
{
importView.AddViewSession();
List<PrintJob> printJobList = GeneratePrintJob(new List<string>() { billId }, templateId, formId);
string printJobsKey = Guid.NewGuid().ToString();
dynamicFormView.Session[printJobsKey] = printJobList;
NotePrintService notePrintService = new NotePrintService();
notePrintService.SetContext(CommonSession.GetCurrent(View.Context.UserToken).SessionManager, View.Context);
//分页的逻辑,如果有多页这里需要手动模拟前端分页,在页少的情况下,这里不需要模拟,一次就获取完
var result = notePrintService.GetReport(dynamicFormView.PageId, printJobsKey, 0, -1, "preview");
return result;
}
finally
{
importView.RemoveViewSession();
}
}
#endregion
#region 方案二 在服务端视图的Session中获取页面,使用版本 21年8月份补丁;
private JSONObject CreatePageJsonV2(string formId, string billId, string templateId)
{
IDynamicFormView dynamicFormView = CreateView(this.View.Context, formId, billId); //此行为代码模拟打开凭证界面
return ExportTargetBillV2(dynamicFormView, formId, billId, templateId);
}
/// <summary>
/// 直接使用后端生成接口
/// </summary>
/// <param name="dynamicFormView"></param>
/// <param name="formId"></param>
/// <param name="billId"></param>
/// <param name="templateId"></param>
/// <returns></returns>
private JSONObject ExportTargetBillV2(IDynamicFormView dynamicFormView, string formId, string billId, string templateId)
{
Kingdee.BOS.Core.Import.IImportView importView = dynamicFormView as Kingdee.BOS.Core.Import.IImportView;
IDynamicFormViewService viewService = dynamicFormView as IDynamicFormViewService;
if (importView == null || viewService == null)
return null;
try
{
importView.AddViewSession();
List<PrintJob> printJobList = GeneratePrintJob(new List<string>() { billId }, templateId, formId);
NotePrintParameter param = new NotePrintParameter(Guid.NewGuid().ToString(), dynamicFormView);
param.PrintJob = printJobList;
NotePrintService notePrintService = new NotePrintService();
notePrintService.SetContext(CommonSession.GetCurrent(View.Context.UserToken).SessionManager, View.Context);
//直接得到全部json数据,
var result = notePrintService.GenerateJsonPage(param);
return result;
}
finally
{
importView.RemoveViewSession();
}
}
#endregion
#region 方案三,使用App层接口获取页面,使用版本 21年8月份补丁;该功能是由平台反射调用;优点,支持自定义数据包套打,下一个帖子讲解, 缺陷:反射性能最慢
private JSONObject CreatePageJsonV3(string formId, string billId, string templateId)
{
Kingdee.BOS.Core.NotePrint.PageGenerateParam param = new Kingdee.BOS.Core.NotePrint.PageGenerateParam()
{
FormId = formId
};
List<Kingdee.BOS.Core.NotePrint.PageItem> itemList = new List<Kingdee.BOS.Core.NotePrint.PageItem>();
itemList.Add(PageItem.CreatePageItem(templateId, billId));
param.ItemList = itemList;
IDynamicFormView dynamicFormView = CreateView(this.View.Context, formId, billId); //此行为代码模拟打开凭证界面
return ExportTargetBill(dynamicFormView, formId, billId, templateId);
}
#endregion
/// <summary>
/// 生成打印任务
/// </summary>
/// <param name="billIdList"></param>
/// <param name="templateId"></param>
/// <param name="formId"></param>
/// <returns></returns>
private List<PrintJob> GeneratePrintJob(List<string> billIdList, string templateId, string formId)
{
List<PrintJob> result = new List<PrintJob>();
PrintJob printJob = new Kingdee.BOS.Core.NotePrint.PrintJob();
printJob.FormId = formId;
printJob.PrintJobItems = new List<Kingdee.BOS.Core.NotePrint.PrintJobItem>();
foreach (var billId in billIdList)
{
printJob.PrintJobItems.Add(new PrintJobItem(billId, templateId));
}
result.Add(printJob);
return result;
}
#region 创建服务端视图(引入视图)
/// <summary>
/// 创建表单详情界面开窗参数
/// </summary>
/// <param name="ctx"></param>
/// <param name="metadata"></param>
/// <param name="pkValue"></param>
/// <returns></returns>
private BillOpenParameter CreateOpenParameter(Context ctx, FormMetadata metadata, string pkValue)
{
var form = metadata.BusinessInfo.GetForm();
BillOpenParameter openPara = new BillOpenParameter(form.Id, metadata.GetLayoutInfo().Id);
openPara.Context = ctx;
openPara.PageId = Guid.NewGuid().ToString();
openPara.Status = OperationStatus.VIEW;
openPara.CreateFrom = CreateFrom.Default;
openPara.DefaultBillTypeId = string.Empty;
openPara.PkValue = pkValue;
openPara.FormMetaData = metadata;
openPara.SetCustomParameter(Kingdee.BOS.Core.FormConst.PlugIns, form.CreateFormPlugIns());
openPara.ServiceName = form.FormServiceName;
return openPara;
}
/// <summary>
/// 构造单据详情界面 视图模型引入视图
/// </summary>
/// <param name="ctx"></param>
/// <param name="formId"></param>
/// <param name="pkValue"></param>
/// <returns></returns>
private IDynamicFormView CreateView(Context ctx, string formId, string pkValue)
{
FormMetadata metadata = MetaDataServiceHelper.Load(ctx, formId) as FormMetadata;
if (metadata == null)
return null;
var openParameter = CreateOpenParameter(ctx, metadata, pkValue);
var provider = metadata.BusinessInfo.GetForm().GetFormServiceProvider();
string importViewClass = "Kingdee.BOS.Web.Import.ImportBillView,Kingdee.BOS.Web";
Type type = Type.GetType(importViewClass);
IDynamicFormViewService billView = (IDynamicFormViewService)Activator.CreateInstance(type);
billView.Initialize(openParameter, provider);
billView.LoadData();
return (IDynamicFormView)billView;
}
#endregion
}
}
```
【效果】点击按钮后,使用不同版本的api得到json数据
![Image_20221214114505.webp](/download/010018500934abd14950a43e77fb749a97df.webp)
套打.二开案例.获取套打页面的json数据
【场景】获取套打页面的json数据,自行实现渲染器渲染,不依赖平台的预览、打印端,确保效果一致【案例】提供不同版本下获取json数据的接口...
点击下载文档
本文2024-09-16 18:34:33发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-23040.html
您需要登录后才可以发表评论, 登录登录 或者 注册
最新文档
热门文章