You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/DSWeb/Areas/MvcShipping/Helper/DingTalkHelper.cs

349 lines
10 KiB
C#

using AlibabaCloud.SDK.Dingtalkh5package_1_0.Models;
using DSWeb.Areas.CommMng.DAL;
using DSWeb.Areas.Dispatch.Helper;
using DSWeb.Areas.MvcShipping.DB;
using DSWeb.Areas.MvcShipping.Job;
using DSWeb.Areas.MvcShipping.Models.MsOpSeaeBaoXian;
using DSWeb.Common.DB;
using HcUtility.Comm;
using java.rmi.server;
using java.util;
using log4net;
using Microsoft.Office.Interop.Excel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Runtime.Caching;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;
using Tea;
namespace DSWeb.Areas.MvcShipping.Helper
{
public static class DingTalkHelper
{
public static readonly string DingTalkAccessTokenStoreKey = "DingTalkToken";
private static ILog logger = LogManager.GetLogger("DingTalkHelper");
/// <summary>
/// 发送工作通知
/// https://open-dev.dingtalk.com/apiExplorer#/?devType=org&api=dingtalk.oapi.message.corpconversation.asyncsend_v2
/// </summary>
/// <param name="uid"></param>
/// <param name="msg"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static bool SendMessage(string uid, string msg)
{
DingTalkContext dingTalkContext = new DingTalkContext();
var pAgentid = dingTalkContext.ParamSets.FirstOrDefault(p => p.PARAMNAME == "DingTalkAgentid");
if (pAgentid == null || string.IsNullOrWhiteSpace(pAgentid.PARAMVALUE))
{
throw new Exception("钉钉Agentid参数配置错误");
}
var user = dingTalkContext.Users.FirstOrDefault(u => u.GID == uid);
if (user != null && !string.IsNullOrWhiteSpace(user.DingTalkAccount))
{
var token = MemoryCache.Default[DingTalkTokenJob.DingTalkAccessTokenStoreKey].ToString();
var json = new
{
agent_id = pAgentid.PARAMVALUE,
userid_list = user.DingTalkAccount,
msg = new
{
msgtype = "text",
text = new
{
content = msg
}
}
};
string resp = WebRequestHelper.DoPost($"https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token={token}", JsonConvert.SerializeObject(json));
logger.Debug(resp);
var jsonObj = JsonConvert.DeserializeAnonymousType(resp, new
{
errcode = 0,
errmsg = "",
task_id = ""
});
if (jsonObj.errcode == 0)
{
return true;
}
}
return false;
}
public static DBResult Send_WMSIN(string uid, string GID)
{
var result = new DBResult();
DingTalk_SendAuditWork dh = new DingTalk_SendAuditWork(uid);
if (dh.cansend(GID))
{
dh.MakeSendMessage(GID);
result = dh.DoSend();
}
else {
result.SetErrorInfo("该业务已发送");
}
return result;
}
}
public class DingTalkSendHelper {
public DingTalkSendHelper() { }
public string url { get; set; }
public string uid { get; set; }
public object SendObject { get; set; }
public SysParamSet pAgentid { get; set; }
public DB.User user { get; set; } = null;
public object jsonObj { get; set; }
public string json { get; set; }
public string token { get; set; }
public Dictionary<string, string> HeaderDic { get; set; }
private static ILog logger = LogManager.GetLogger("DingTalkHelper");
public DingTalkContext dingTalkContext { get; set; } = new DingTalkContext();
DBResult result { get; set; } = new DBResult();
public DBResult getUser()
{
user = dingTalkContext.Users.FirstOrDefault(u => u.GID == uid);
if (user == null || string.IsNullOrWhiteSpace(user.DingTalkAccount))
{
result.SetErrorInfo("用户没有维护钉钉userid");
return result;
}
result.OK();
return result;
}
public DBResult GetAgentid() {
result = new DBResult();
pAgentid = dingTalkContext.ParamSets.FirstOrDefault(p => p.PARAMNAME == "DingTalkAgentid");
if (pAgentid == null || string.IsNullOrWhiteSpace(pAgentid.PARAMVALUE))
{
result.SetErrorInfo("钉钉Agentid参数配置错误");
return result;
}
result.OK();
return result;
}
public DBResult DoSend() {
var result = new DBResult();
string resp = WebRequestHelper.DoPost(url, JsonConvert.SerializeObject(jsonObj));
logger.Debug(resp);
result.setMessage(true,"发送成功", jsonObj);
return result;
}
public DBResult DoSend_Header()
{
var result = new DBResult();
string resp = WebRequestHelper.DoPost_Header(url, HeaderDic, jsonObj);
logger.Debug(resp);
result.setMessage(true, "发送成功", resp);
return result;
}
public static object JsonToObject(string jsonString, object obj)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
return serializer.ReadObject(mStream);
}
}
public class DingTalk_SendWorkMessage : DingTalkSendHelper {
public DingTalk_SendWorkMessage(string uid) {
this.uid = uid;
}
public DBResult MakeSendMessage(string msg) {
var result = new DBResult();
result = GetAgentid();
if (!result.Success) return result;
result = getUser();
if (!result.Success) return result;
jsonObj = new
{
agent_id = pAgentid.PARAMVALUE,
userid_list = user.DingTalkAccount,
msg = new
{
msgtype = "text",
text = new
{
content = msg
}
}
};
token = MemoryCache.Default[DingTalkTokenJob.DingTalkAccessTokenStoreKey].ToString();
url = $"https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token={token}";
result.OK();
return result;
}
}
public class DingTalk_SendAuditWork : DingTalkSendHelper
{
public string BSNO { get; set; }
public DingTalk_SendAuditWork(string uid)
{
this.uid = uid;
}
public class DingTalkFormField {
public string name { get; set; }
public string value { get; set; }
public DingTalkFormField(string name, string value)
{
this.name = name;
this.value = value;
}
}
public class DingTalkForm_approvers
{
public string actionType { get; set; }
public List<string> userIds { get; set; }
public DingTalkForm_approvers(string actionType, List<string> userIds)
{
this.actionType = actionType;
this.userIds = userIds;
}
}
public class DingTalk_OAAuditBody {
public int deptId { get; set; }
public List<DingTalkForm_approvers> approvers { get; set; } = new List<DingTalkForm_approvers>();
public long microappAgentId { get; set; }
public string originatorUserId { get; set; }
public string processCode { get; set; }
public List<DingTalkFormField> formComponentValues { get; set; } = new List<DingTalkFormField>();
}
public bool cansend(string GID) {
//看是否已发过
var logiclog = BasicDataRefDAL.GetLogicInfo(GID, "钉钉审批");
if (logiclog.Count > 0)
{
return false;
}
return true;
}
public DBResult MakeSendMessage(string GID)
{
var result = new DBResult();
BSNO = GID;
var processCode = dingTalkContext.ParamSets.FirstOrDefault(p => p.PARAMNAME == "DingTalkprocessCode_wmsin");
DingTalk_OAAuditBody body = new DingTalk_OAAuditBody();
body.deptId = 809181118;
body.approvers.Add(new DingTalkForm_approvers("OR", new List<string> { "manager4178" }));
body.microappAgentId = 2298782307;
body.originatorUserId = "manager4178";
body.processCode = "PROC-36B08555-463A-478B-A3CA-9A9FDBF01272";
body.formComponentValues = new List<DingTalkFormField>
{
//new DingTalkFormField("提单号", "123123")
//增加字段内容
};
jsonObj = body;
token = MemoryCache.Default[DingTalkTokenJob.DingTalkAccessTokenStoreKey].ToString();
url = $"https://api.dingtalk.com/v1.0/workflow/processInstances";
HeaderDic = new Dictionary<string, string>
{
{ "x-acs-dingtalk-access-token", token }
};
result.OK();
return result;
}
public class DingTalk_SendAuditResult {
public string instanceId { get; set; }
}
public DBResult DoSend() {
var result = base.DoSend_Header();
if (result.Success) {
try
{
var jo = new DingTalk_SendAuditResult();
jo = (DingTalk_SendAuditResult)JsonToObject(result.Data.ToString(), jo);
var dic = new Dictionary<string, string>();
dic.Add("instanceId", jo.instanceId);
BasicDataRefDAL.SaveLogicInfo(BSNO, "钉钉审批", dic);
}catch(Exception ex)
{
}
}
return result;
}
}
}