|
|
|
|
using AlibabaCloud.SDK.Dingtalkh5package_1_0.Models;
|
|
|
|
|
using DSWeb.Areas.Dispatch.Helper;
|
|
|
|
|
using DSWeb.Areas.MvcShipping.DB;
|
|
|
|
|
using DSWeb.Areas.MvcShipping.Job;
|
|
|
|
|
using log4net;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Configuration;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.Caching;
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
|
|
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 class DingTalkAuditHelper {
|
|
|
|
|
public static AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client CreateClient()
|
|
|
|
|
{
|
|
|
|
|
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config();
|
|
|
|
|
config.Protocol = "https";
|
|
|
|
|
config.RegionId = "central";
|
|
|
|
|
return new AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client(config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void GetToken() {
|
|
|
|
|
AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client client = CreateClient();
|
|
|
|
|
AlibabaCloud.SDK.Dingtalkoauth2_1_0.Models.GetSsoAccessTokenRequest getSsoAccessTokenRequest = new AlibabaCloud.SDK.Dingtalkoauth2_1_0.Models.GetSsoAccessTokenRequest
|
|
|
|
|
{
|
|
|
|
|
Corpid = "ding7f22510fcf025d5424f2f5cc6abecb85",
|
|
|
|
|
SsoSecret = "LPHtv90gkqmq-fzQ3i36vUNaOtSS39SmtdpsUgQaQbJStHoaWey2na9Pu6_SmCCp",
|
|
|
|
|
};
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
client.GetSsoAccessToken(getSsoAccessTokenRequest);
|
|
|
|
|
var _rstr=client.ToString();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (TeaException err)
|
|
|
|
|
{
|
|
|
|
|
if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
|
|
|
|
|
{
|
|
|
|
|
// err 中含有 code 和 message 属性,可帮助开发定位问题
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception _err)
|
|
|
|
|
{
|
|
|
|
|
TeaException err = new TeaException(new Dictionary<string, object>
|
|
|
|
|
{
|
|
|
|
|
{ "message", _err.Message }
|
|
|
|
|
});
|
|
|
|
|
if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
|
|
|
|
|
{
|
|
|
|
|
// err 中含有 code 和 message 属性,可帮助开发定位问题
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|