钉钉开发 - 服务端API Asp.Net Core 发起审批实例
程序员文章站
2022-04-04 21:09:55
...
官方文档:https://ding-doc.dingtalk.com/doc#/serverapi2/cmct1a
我使用的接口:
/processinstance/create
发起审批实例
-- 我遇到的主要问题是
{"errcode":820001,"errmsg":"发起审批实例失败,错误原因为【系统错误:审批表单缺少必填信息:设备类型】","request_id":"5ljo99cw3ujm"}
这个问题关键就是审批表单中我用了“明细”,官方demo中这个明细特别处理了,C#代码我给出来了。
// 明细
OapiProcessinstanceCreateRequest.FormComponentValueVo vo4 = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
vo4.setName("明细示例");
vo4.setValue(JSON.toJSONString(Arrays.asList(Arrays.asList(ItemName1, ItemName2, ItemName3))));
基础东西看官方demo或者其他博客写的。 我这列出关键代码
此代码对应的自定义审批单:
/// <summary>
/// 发起审批实例 - 设备及附件转场申请
/// </summary>
/// <returns></returns>
public OapiProcessinstanceCreateResponse Create()
{
if (_unitOfWorkManager.Current.GetTenantId().HasValue)
{
_currentTenant = _tenantManager.FindById(_unitOfWorkManager.Current.GetTenantId().Value);
_currentUser = _userManager.GetUserById(_userManager.AbpSession.UserId.Value);
}
try
{
IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/create");
OapiProcessinstanceCreateRequest req = new OapiProcessinstanceCreateRequest();
//SmartworkBpmsProcessinstanceCreateRequest req = new SmartworkBpmsProcessinstanceCreateRequest();
// 设备及附件转场申请
req.AgentId = 348597220; //微应用的AgentId,你申请的那个
req.ProcessCode = "PROC-201910E5-64DF-4CA3-A036-CC75E664490A"; // 自定义审批单,编辑那个审批单的时候在Url找!
req.OriginatorUserId = _currentUser.DingTalkUserId;
req.DeptId = -1;
req.Approvers = "111019246426142261";
//_currentUser.DingTalkUserId;
req.CcList = _currentUser.DingTalkUserId;
req.CcPosition = "START";
// 用于整个表单组件的
List<FormComponentValueVoDomain> formComponentValues = new List<FormComponentValueVoDomain>();
FormComponentValueVoDomain vo1 = new FormComponentValueVoDomain();
vo1.Name = "转场类型";
vo1.Value = "";
formComponentValues.Add(vo1);
FormComponentValueVoDomain vo2 = new FormComponentValueVoDomain();
vo2.Name = "转出至";
vo2.Value = "";
formComponentValues.Add(vo2);
FormComponentValueVoDomain vo3 = new FormComponentValueVoDomain();
vo3.Name = "备注";
vo3.Value = "";
formComponentValues.Add(vo3);
// 用于明细列表的 -- 转场设备列表yy
List<List<FormComponentValueVoDomain>> list = new List<List<FormComponentValueVoDomain>>();
List<FormComponentValueVoDomain> formDetaiList = new List<FormComponentValueVoDomain>();
// 明细列表 结构: [ [ { '':'' } ]]
// 转场设备列表
FormComponentValueVoDomain vo4 = new FormComponentValueVoDomain();
vo4.Name = "deviceID";
vo4.Value = "id";
formDetaiList.Add(vo4);
FormComponentValueVoDomain vo5 = new FormComponentValueVoDomain();
vo5.Name = "设备类型";
vo5.Value = "设备类型一号";
formDetaiList.Add(vo5);
FormComponentValueVoDomain vo6 = new FormComponentValueVoDomain();
vo6.Name = "设备编号";
vo6.Value = "T001";
formDetaiList.Add(vo6);
FormComponentValueVoDomain vo7 = new FormComponentValueVoDomain();
vo7.Name = "设备型号";
vo7.Value = "T-001";
formDetaiList.Add(vo7);
FormComponentValueVoDomain vo8 = new FormComponentValueVoDomain();
vo8.Name = "所在仓库";
vo8.Value = "德州";
formDetaiList.Add(vo8);
FormComponentValueVoDomain vo9 = new FormComponentValueVoDomain();
vo9.Name = "臂长";
vo9.Value = "臂长一米";
formDetaiList.Add(vo9);
FormComponentValueVoDomain vo10 = new FormComponentValueVoDomain();
vo10.Name = "是否收到";
vo10.Value = "收到";
formDetaiList.Add(vo10);
list.Add(formDetaiList);
FormComponentValueVoDomain vo11 = new FormComponentValueVoDomain();
vo11.Name = "转场设备列表";
vo11.Value = JsonConvert.SerializeObject(list);
formComponentValues.Add(vo11);
// 转场附件列表
List<List<FormComponentValueVoDomain>> list2 = new List<List<FormComponentValueVoDomain>>();
List<FormComponentValueVoDomain> formDetaiList2 = new List<FormComponentValueVoDomain>();
FormComponentValueVoDomain vo12 = new FormComponentValueVoDomain();
vo12.Name = "accessoryID";
vo12.Value = "idaccessoryID1";
formDetaiList2.Add(vo12);
FormComponentValueVoDomain vo13 = new FormComponentValueVoDomain();
vo13.Name = "附件类型";
vo13.Value = "附件类型1";
formDetaiList2.Add(vo13);
FormComponentValueVoDomain vo14 = new FormComponentValueVoDomain();
vo14.Name = "附件规格";
vo14.Value = "附件规格一";
formDetaiList2.Add(vo14);
FormComponentValueVoDomain vo15 = new FormComponentValueVoDomain();
vo15.Name = "附件规格";
vo15.Value = "附件规格一";
formDetaiList2.Add(vo15);
FormComponentValueVoDomain vo16 = new FormComponentValueVoDomain();
vo16.Name = "生产厂家";
vo16.Value = "生产厂家一号";
formDetaiList2.Add(vo16);
FormComponentValueVoDomain vo17 = new FormComponentValueVoDomain();
vo17.Name = "所在仓库";
vo17.Value = "所在仓库一号仓库";
formDetaiList2.Add(vo17);
FormComponentValueVoDomain vo18 = new FormComponentValueVoDomain();
vo18.Name = "附件数量";
vo18.Value = "120";
formDetaiList2.Add(vo18);
FormComponentValueVoDomain vo19 = new FormComponentValueVoDomain();
vo19.Name = "收到数量";
vo19.Value = "100";
formDetaiList2.Add(vo19);
list2.Add(formDetaiList2);
FormComponentValueVoDomain vo20 = new FormComponentValueVoDomain();
vo20.Name = "转场附件列表";
vo20.Value = JsonConvert.SerializeObject(list2);
formComponentValues.Add(vo20);
req.FormComponentValues_ = formComponentValues;
OapiProcessinstanceCreateResponse rsp = client.Execute(req, AccessToken);
return rsp;
}
catch (Exception ex)
{
throw new UserFriendlyException(ex.Message);
}
}
/// <summary>
/// AccessToken
/// </summary>
public string AccessToken
{
get
{
string apiUrl = "https://oapi.dingtalk.com/gettoken";
IDingTalkClient client = new DefaultDingTalkClient(apiUrl);
OapiGettokenRequest req = new OapiGettokenRequest();
req.SetHttpMethod("GET");
req.Appkey = _currentTenant.DT_AppKey;
req.Appsecret = _currentTenant.DT_AppSecret;
OapiGettokenResponse rsp = client.Execute(req);
if (rsp.IsError)
{
throw new UserFriendlyException($"钉钉gettoken报错:{rsp.ErrCode}{rsp.ErrMsg}");
}
return rsp.AccessToken;
}
}
上一篇: .net framework有什么用
下一篇: php网站数据库怎么建