(一)、JavaScript调用DLL代码

需要验证登录的场景下调用:

LoadServiceClass.LoadServiceInterfaceByDLLSetting(DLLSetting, ActionName, NeedUpdateShowMsgDIVID, ShowMsgDIVID, LoadingMsg, AjaxPostDataObject, SuccessCallBack, ErrorCallBack, CallBackObject);

不需要验证登录的场景下调用:

LoadServiceClass.LoadServiceInterfaceByDLLSettingNoLogin(DLLSetting, ActionName, NeedUpdateShowMsgDIVID, ShowMsgDIVID, LoadingMsg, AjaxPostDataObject, SuccessCallBack, ErrorCallBack, CallBackObject);

DLLSetting:调用的插件,格式:DLL文件名.dll|类名|方法名;必须放在CRM/BIN目录下

ActionName:本次调用的名称或描述

NeedUpdateShowMsgDIVID:是否要将结果显示在界面上

ShowMsgDIVID:如果需要显示结果,则显示在哪个层上面,层的ID

LoadingMsg:正在加载的提示文字

AjaxPostDataObject:POST方式传递给后面的字典

SuccessCallBack:成功的回调,如:function(返回值) {  };返回值里面包括4个属性:IsSuccess:是否成功;Result:如果成功,返回的结果;Script:返回要执行的JavaScript;ErrorMsg:如果失败,返回错误的原因

ErrorCallBack:失败后的回调,如:function(返回值) {  };返回值里面包括4个属性:IsSuccess:是否成功;Result:如果成功,返回的结果;Script:返回要执行的JavaScript;ErrorMsg:如果失败,返回错误的原因

CallBackObject:回调对象字典,会和SuccessCallBack或ErrorCallBack的返回值对象进行合并

示例:

//显示返回结果
LoadServiceClass.LoadServiceInterfaceByDLLSetting("测试插件.dll|标准项目插件.测试类|测试方法", "测试动作名称", true, "DisplayResultDIV", GetLoadingMsg("正在加载..."), { name: "王", gender: "女" }, function (返回值)
{
    //成功后的回调
}, Common_Error, { ActionName: "测试动作名称" });

//不显示返回结果
LoadServiceClass.LoadServiceInterfaceByDLLSetting("测试插件.dll|标准项目插件.测试类|测试方法", "测试动作名称", false, "", "", { name: "王", gender: "女" }, function (返回值)
{
    //成功后的回调
    var Config = eval("(" + 返回值.Result + ")");

}, function (返回值)
{
    Element.SetHtml($("#DisplayResultDIV"), "加载数据失败,原因:" + 返回值.ErrorMsg);
}, { ActionName: "测试动作名称" });

(二)、DLL插件源代码

//-----------------------------------------------------------------------
// <copyright file="测试类.cs" company="企管宝">
//  CustomizedWCFUI
// </copyright>
// <author></author>
// <createdate>2019-12-01</createdate>
// <revisionhistory>
// </revisionhistory>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;
using PublicFunctions;
using PublicFunctions.Service;
using PublicFunctions.Button;
using PublicFunctions.Form;
using PublicFunctions.Grid;
using PublicFunctions.PageTemplate;

namespace 标准项目插件
{
    public class 测试类 : ServiceFactoryBase
    {
        #region 函数说明:构造函数
        /// <summary>
        /// 构造函数,必须保留
        /// </summary>
        /// <param name="ServiceData"></param>
        public 测试类(ServiceDataModel ServiceData)
            : base(ServiceData)
        { }
        #endregion

        /// <summary>
        /// 测试方法
        /// </summary>
        /// <returns></returns>
        public AjaxResultModel 测试方法()
        {
            AjaxResultModel AjaxResultObject = new AjaxResultModel();

            //获取JS前端传入的参数
            string name = PublicFunctions.PublicFunctions.GetDataFromDictionary("name", ServiceData.RequestFormDictionary);
            string gender = PublicFunctions.PublicFunctions.GetDataFromDictionary("gender", ServiceData.RequestFormDictionary);

            //生成StrWhere
            List<string> StrWhereList = new List<string>();
            if (!Check.IsNull(name))
            {
                StrWhereList.Add("CustomerName like '%" + ReplaceRestorStr.Replace(name) + "%'");
            }

            if (!Check.IsNull(gender))
            {
                StrWhereList.Add("Gender='" + gender + "'");
            }
            string StrWhere = (StrWhereList.Count > 0 ? " where " : " ") + string.Join(" and ", StrWhereList);

            //连接数据库
            using (PublicFunctions.DBHelper.RawDBHelper.SqlDataFactory context = new PublicFunctions.DBHelper.RawDBHelper.SqlDataFactory(PublicSetting.RawDBHelper_CRM_ConnectionStringKey))
            {
                string ErrorMsg = "";
                DataTable dt = new DataTable();
                if (context.GetDataAll(ref dt, "select * from CustomerInfo  " + StrWhere, ref ErrorMsg))
                {
                    //还原数据
                    PublicFunctions.Form.FormEngine.RestoreFormDataByFormKey("CRM.CustomerInfo.1", ref dt);

                    //输出结果(把数据表序列化为JSON)
                    AjaxResultObject.Result = PublicFunctions.JSONClass.DataTableToJSON(dt);
                }
            }
            AjaxResultObject.IsSuccess = true;
            return AjaxResultObject;
        }
    }
}