钉钉接口封装成插件:其中有个参数要求格式是Dictionary<string, string>
然后我的写法是:
public static async Task<AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestCardData> CreateCardData(IServerCommandExecuteContext dataContext, string ResultTo, IEnumerable<kapianshuju> aaa)
{
// 创建一个字典来存储卡片数据内容
var cardDataCardParamMap = new Dictionary<string, string>();
// 先检查 数据是否为空
if (aaa == null || !aaa.Any())
{
await CreateErrorJson(101, "卡片数据内容不能为空");
return null; // 停止执行
}
// 遍历卡片模板内容替换参数列表
foreach (var param in aaa)
{
// 先检查 param.参数名 是否为空
if (string.IsNullOrEmpty(Convert.ToString(param.参数名)))
{
await CreateErrorJson(101, "卡片数据:参数名不能为空");
break; // 停止执行
}
// 将参数名和参数值转换为字符串并添加到字典中
var key = (await dataContext.EvaluateFormulaAsync(param.参数名))?.ToString() ?? string.Empty;
string value = null;
bool shifouhangtou = param.参数值.是否显示行头;
if (param.参数值.参数数据类型 == shujuleibie.Table)
{
//对参数内容值做公式计算
var aaa01 = (await dataContext.EvaluateFormulaAsync(param.参数值.参数内容值));
try
{
var data = aaa01 as List<Dictionary<string, object>>;
// 调用方法处理表格数据
var jsonResult = await ConvertToJson(dataContext, data, param.参数值.表格参数, shifouhangtou);
string value1 = JsonConvert.SerializeObject(jsonResult, Formatting.None);
value = value1.Replace("\\\"", "\"");
}
catch (Exception ex)
{
// 处理 ConvertToJson 方法可能抛出的异常
await CreateErrorJson(101, $"处理表格数据错误:{ex.Message}");
}
}
else
{
value = (await dataContext.EvaluateFormulaAsync(param.参数值.参数内容值))?.ToString() ?? string.Empty;
}
if (!string.IsNullOrEmpty(key) && value != null)
{
cardDataCardParamMap[key] = value;
}
}
var cardData = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest.CreateAndDeliverRequestCardData
{
CardParamMap = cardDataCardParamMap,
};
return cardData;
}
另一个处理钉钉要求的表格类格式数据转换,
public static async Task<Dictionary<string, object>> ConvertToJson(IServerCommandExecuteContext dataContext, List<Dictionary<string, object>> data, List<kapianshujubiaoge> columns, bool shifouhangtou)
{
// 创建一个列表来存储meta对象
List<Dictionary<string, object>> metaList = new List<Dictionary<string, object>>();
foreach (var item in columns)
{
//公式计算
var ccc = (await dataContext.EvaluateFormulaAsync(item.列名))?.ToString() ?? string.Empty;
var ddd = (await dataContext.EvaluateFormulaAsync(item.列宽))?.ToString() ?? string.Empty;
// 创建第一个meta对象
Dictionary<string, object> meta1 = new Dictionary<string, object>
{
{ "aliasName", ccc },
{ "dataType", item.表格类型 },
{ "alias", ccc },
{ "weight", ddd }
};
metaList.Add(meta1);
}
// 创建一个列表来存储data对象
List<Dictionary<string, object>> datalist = new List<Dictionary<string, object>>();
int counter = 1; // 初始化计数器
foreach (var item in data)
{
if (shifouhangtou)
{
item["rank"] = counter;
//item.Add("rank", counter.ToString());
datalist.Add(item);
counter++;
}
else
{
datalist.Add(item);
}
}
// 创建最终结果对象
var result = new Dictionary<string, object>
{
{ "data", datalist },
{ "meta", metaList }
};
return result;
}
而我的代码处理结果是:
设置变量 返回结果 = {"CardParamMap":{"url":"http://baidu.com","biaoti":"测试标题","lastMessage":"的说法是","biaoge":"{\"data\":[{\"科室\":\"总装科\",\"区域\":\"总装\",\"汇报人\":\"杨奉德\",\"rank\":1},{\"科室\":\"涂装\",\"区域\":\"待洗\",\"汇报人\":\"赵明友\",\"rank\":2},{\"科室\":\"涂装\",\"区域\":\"上挂\",\"汇报人\":\"赵明友\",\"rank\":3}],\"meta\":[{\"aliasName\":\"可人\",\"dataType\":\"STRING\",\"alias\":\"可人\",\"weight\":\"20\"},{\"aliasName\":\"区域人\",\"dataType\":\"STRING\",\"alias\":\"区域人\",\"weight\":\"20\"},{\"aliasName\":\"汇报发\",\"dataType\":\"STRING\",\"alias\":\"汇报发\",\"weight\":\"20\"}]}"}}
这明显不正确,
那么正确的格式应该是:
{"CardParamMap":{"url":"http://baidu.com","biaoti":"测试标题","lastMessage":"的说法是","biaoge":{"data":[
{"科室":"总装科","区域":"总装","汇报人":"杨奉德","rank":1},
{"科室":"涂装","区域":"待洗","汇报人":"赵明友","rank":2},
{"科室":"涂装","区域":"上挂","汇报人":"赵明友","rank":3}],
"meta":[
{"aliasName":"可人","dataType":"STRING","alias":"可人","weight":"20"},
{"aliasName":"区域人","dataType":"STRING","alias":"区域人","weight":"20"},
{"aliasName":"汇报发","dataType":"STRING","alias":"汇报发","weight":"20"}
]}}}
biaoge的值是对象值,而不是字符串,不然就会看到末尾处的 }]}"}} 这里 有个引号, 如何组装这个数据呀
|