活字格插件开发笔记

 次点击
27 分钟阅读

项目文档加载事件

实现SupportUpgrade接口,会有一个Upgrade函数,这个函数会在打开文档时被调用。

public class MyCellType : CellType, ISupportUpgrade
    {
        [Browsable(false)]
        public List<Command> CommandList { get; set; }

        [CustomCommandObject]
        public object CommandObject { get; set; }

        //Upgrade函数在文档打开时被调用
        public void Upgrade(ISupportUpgradeContext context)
        {
            if (CommandList != null && CommandList.Count > 0)
            {
                //如果存在旧属性,则将命令迁移到新属性中
                CommandObject = new CustomCommandObject()
                {
                    Commands = CommandList
                };
                CommandList = null;
            }
        }
    }

同步执行子命令

此函数可同步执行 CommandList。

 this.CommandExecutor.insertCommandsToCurrentExecutingList(
                this.CommandParam.CommandList,
                this.runTimePageName,
                initParams);

检查当前页面是否加载完毕

//获取当前页面是否加载完毕
var state = Forguncy.PageBuilder.pageIsLoading

获取插件资源文件夹

提供插件 guid 获取插件资源文件夹地址

//提供插件 guid 获取插件资源文件夹地址
Forguncy.Helper.SpecialPath.getPluginRootPath("250c8482-3fac-4fb3-b84b-e346bdc0bcc2");

关闭弹窗并传值

可关闭当前弹窗,并回传值

 // 关闭 Popup 命令会导致弹出框失去宽高,所以这里强制设置宽高,防止闪烁
dialog.bodySize = new Forguncy.Numerics.Vector2(ui.dialog.outerWidth(), ui.dialog.outerHeight());
var closePopup = new Forguncy.ClosePopupCommand();
closePopup.CommandParam = {
  PassValueBack: commandSettings.PassValueBack,
  PassQueryCondition: commandSettings.PassQueryCondition,
  ReloadData: commandSettings.ReloadData
};
closePopup.CommandExecutingInfo.runTimePageName = "p";
closePopup.execute();

获取附件文件夹

//获取附件文件夹路径
var serverFileService = dataContext.ServiceProvider.GetService(typeof(IServerFileService)) as IServerFileService;
var uploadFolder = serverFileService.GetUploadFolder();

数据校验

//校验页面数据
var validate = Forguncy.ValidationMananger.validateAllCells();
//0:成功  1:失败

//校验表格数据
var listviewValidate = Forguncy.ListviewBase.validateAllListviewsData();
//true:成功   false:失败
  

获取后台的 Email 配置信息

获取活字格后台配置的 email 服务器信息

//需要引入ServerDesignerCommon.dll

//获取后台Email配置
        public static async Task<EmailSettings> GetForguncyMailSettingAsync(IServerCommandExecuteContext dataContext) {
            EmailSettings emailSettings = new EmailSettings();//构建一个新的邮箱配置实例 
//通过POST请求调用后台API获取邮箱配置;
            HttpClient http = dataContext.ServiceProvider.GetRequiredService<HttpClient>();
            using HttpResponseMessage respose = await http.PostAsync(dataContext.UserInfos.UserServiceURL + "/UserService/GetServiceEmailSettings", null);
            var res = await respose.Content.ReadAsStringAsync();
            emailSettings = JsonConvert.DeserializeObject<EmailSettings>(res);
            emailSettings.SSO = emailSettings.EnableSSL ? SecureSocketOptions.Auto : SecureSocketOptions.None;
            emailSettings.HostName = emailSettings.HostName.Replace("smtp", "imap");
            emailSettings.HostPort = 993;
            return emailSettings;
        }

//定义一个和获取结果一样的类
 public class EmailSettings
    {
        public EmailSettings(string _HostName = "", int _HostPort = 993, string _UserName = "", string _Password = "", bool _EnableSSL = true, SecureSocketOptions _SSO = SecureSocketOptions.Auto, string _Folder = "INBOX", string _UploadFolder = null, bool _AutoRead = true)
        {
            HostName = _HostName;
            HostPort = _HostPort;
            UserName = _UserName;
            Password = _Password;
            EnableSSL = _EnableSSL;
            SSO = _SSO;
            Folder = _Folder;
            UploadFolder = _UploadFolder;
            AutoRead = _AutoRead;
        }

        public string TestEmailAddress { get; set; }
        public string TestEmail { get; set; }
        public bool IsSetPassword { get; set; }
        public string SubjectPrefix { get; set; }
        public string FromName { get; set; }
        public string HostName { get; set; }
        public string FromAddress { get; set; }
        public int HostPort { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public bool EnableSSL { get; set; }
        public SecureSocketOptions SSO { get; set; }
        public string Folder { get; set; }
        public string UploadFolder { get; set; }
        public bool AutoRead { get; set; }
    }

中断执行命令

该命令会终端当前命令列表。

this.stopExecuteCommandList();

克隆对象列表参数

如果不进行克隆,直接遍历,会导致修改的是命令对象,影响是始终获取的是上一次参数

//深克隆一个列表
List<object> _Headers = Headers.Select(i => i?.Clone()).ToList();
//再进行遍历
foreach (HeaderObj header in _Headers.Cast<HeaderObj>())
 {
     var name = Convert.ToString(await dataContext.EvaluateFormulaAsync(header.Name));
     var value = Convert.ToString(await dataContext.EvaluateFormulaAsync(header.Value));
     headers.Add(name, value);

 }

获取单元格位置 DOM

var cellLoaction = this.CommandParam.CellLoaction;
const cellLoaction = this.getCellLocation(cellLoaction);
const cell = Forguncy.Page.getCellByLocation(cellLoaction);
element = "#" + cell._pageCell.cellType.CellElement.runtimeContainer[0].id;

强制生成 DOM

IForceGenerateCell接口后,可以给空白单元格强制生成 DOM 接受结果

 [DisplayName("加数")]
        [FormulaProperty]
        public object FirstValue { get; set; }

        [DisplayName("被加数")]
        [FormulaProperty]
        public object LastValue { get; set; }

        [FormulaProperty(OnlySupportNormalCell = true)]
        [DisplayName("结果保存至单元格")]
        public object Result { get; set; } = "结果";

        public IEnumerable<GenerateCellInfo> GetForceGenerateCells()
        {
            if(Result!= null)
            {
                if (Result is IFormulaReferObject formulaReferObject)
                {
                    var cellInfo = formulaReferObject.GetGenerateCellInfo();
                    if (cellInfo != null)
                    {
                        yield return cellInfo;
                    }
                }
            }
        }
    }

构建 dataContext 及 HttpContext

对于异步或脱离上下文环境的代码中,可以手动构建一个 Context 来确保鉴权和命令的调用

using Microsoft.AspNetCore.Http;

//构建一个HttpContext,并将原始HttpContext里的User和Method复制过来,目的是为了确保新构建的HttpContext可以被正确鉴权
var httpContext = new DefaultHttpContext();
httpContext.User = dataContext.Context.User;
httpContext.Request.Method = dataContext.Context.Request.Method;

//构建一个新的IServerCommandExecuteContext
var context = Activator.CreateInstance(dataContext.GetType(), null, httpContext, null) as IServerCommandExecuteContext;

//接着可以用context来调用活字格提供的方法

扁平化对象类型编辑器

[ObjectProperty(ObjType=typeof(config_list_row_gap))]
public config_list_row_gap gap {get;set;}

获取图片地址

var appService = context.ServiceProvider.GetService(typeof(IApplicationInformationService)) as IApplicationInformationService;
appService.ApplicationAttachmentStorageInfo.GetFilesFromAttachmentValue(dbValue);

© 本文著作权归作者所有,未经许可不得转载使用。