162 lines
6.2 KiB
C#
162 lines
6.2 KiB
C#
using AlibabaCloud.SDK.Dingtalkoauth2_1_0.Models;
|
|
using AlibabaCloud.SDK.Dingtalkyida_1_0.Models;
|
|
using Infrastructure;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Tea;
|
|
using Tea.Utils;
|
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
|
|
|
namespace ZR.Common
|
|
{
|
|
public class YIDAHelper
|
|
{
|
|
|
|
private static readonly string AppKey = AppSettings.App(new string[] { "dingding", "AppKey" });
|
|
private static readonly string AppSecret = AppSettings.App(new string[] { "dingding", "AppSecret" });
|
|
private static readonly string FormUuid = AppSettings.App(new string[] { "yida", "FormUuid" });
|
|
private static readonly string UserId = AppSettings.App(new string[] { "yida", "UserId" });
|
|
private static readonly string AppType = AppSettings.App(new string[] { "yida", "AppType" });
|
|
private static readonly string SystemToken = AppSettings.App(new string[] { "yida", "SystemToken" });
|
|
|
|
/**
|
|
* 使用 Token 初始化账号Client
|
|
* @return Client
|
|
* @throws Exception
|
|
*/
|
|
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);
|
|
}
|
|
/// <summary>
|
|
/// 获取token
|
|
/// </summary>
|
|
public static int GetToken()
|
|
{
|
|
int result = 0;
|
|
AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client client = CreateClient();
|
|
AlibabaCloud.SDK.Dingtalkoauth2_1_0.Models.GetAccessTokenRequest getAccessTokenRequest = new AlibabaCloud.SDK.Dingtalkoauth2_1_0.Models.GetAccessTokenRequest
|
|
{
|
|
AppKey = AppKey,
|
|
AppSecret = AppSecret,
|
|
};
|
|
try
|
|
{
|
|
GetAccessTokenResponse response = client.GetAccessToken(getAccessTokenRequest);
|
|
if (response != null)
|
|
{
|
|
//token 扔到缓存里面
|
|
CacheHelper.SetCache("YIDA_token", response.Body.AccessToken);
|
|
result = 1;
|
|
return result;
|
|
|
|
}
|
|
}
|
|
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 }
|
|
});
|
|
Console.WriteLine(err.Code + "|" + err.Message);
|
|
if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
|
|
{
|
|
// err 中含有 code 和 message 属性,可帮助开发定位问题
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public static AlibabaCloud.SDK.Dingtalkyida_1_0.Client CreateClient1()
|
|
{
|
|
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config();
|
|
config.Protocol = "https";
|
|
config.RegionId = "central";
|
|
return new AlibabaCloud.SDK.Dingtalkyida_1_0.Client(config);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 向宜搭表单发送数据
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="token"></param>
|
|
/// <param name="t"></param>
|
|
/// <returns></returns>
|
|
public static async Task<int> SendMessage<T>(string token, T t) where T : class
|
|
{
|
|
return await Task.Run(() =>
|
|
{
|
|
int result = 0;
|
|
string formData = JsonConvert.SerializeObject(t);
|
|
AlibabaCloud.SDK.Dingtalkyida_1_0.Client client = CreateClient1();
|
|
AlibabaCloud.SDK.Dingtalkyida_1_0.Models.SaveFormDataHeaders saveFormDataHeaders = new AlibabaCloud.SDK.Dingtalkyida_1_0.Models.SaveFormDataHeaders();
|
|
saveFormDataHeaders.XAcsDingtalkAccessToken = token;
|
|
AlibabaCloud.SDK.Dingtalkyida_1_0.Models.SaveFormDataRequest saveFormDataRequest = new AlibabaCloud.SDK.Dingtalkyida_1_0.Models.SaveFormDataRequest
|
|
{
|
|
SystemToken = SystemToken,
|
|
FormUuid = FormUuid,
|
|
UserId = UserId,
|
|
AppType = AppType,
|
|
FormDataJson = formData,
|
|
};
|
|
try
|
|
{
|
|
SaveFormDataResponse response = client.SaveFormDataWithOptions(saveFormDataRequest, saveFormDataHeaders, new AlibabaCloud.TeaUtil.Models.RuntimeOptions());
|
|
if (response != null)
|
|
{
|
|
Console.WriteLine(response.StatusCode + "|" + response.Body);
|
|
if (response.StatusCode == 200)
|
|
{
|
|
result = 1;
|
|
}
|
|
}
|
|
|
|
}
|
|
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 属性,可帮助开发定位问题
|
|
}
|
|
|
|
|
|
}
|
|
return result;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|