Cms.Core/Cms.External.WebApi/Controllers/WeworkController.cs

123 lines
4.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Cms.External.WebApi.Dtos;
using Cms.External.WebApi.Services.Impl;
using DG.Core;
using DG.Tool;
using Microsoft.AspNetCore.Mvc;
using Serilog;
using System.ComponentModel.DataAnnotations;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Web;
using static Cms.External.WebApi.Controllers.WeworkController;
namespace Cms.External.WebApi.Controllers
{
public class WeworkController : BaseController
{
private readonly IHttpClient _httpClient;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IWeworkAgentService _weworkAgentService;
public WeworkController(IHttpClient httpClient,
IHttpContextAccessor httpContextAccessor,
IWeworkAgentService weworkAgentService)
{
_httpClient = httpClient;
_httpContextAccessor = httpContextAccessor;
_weworkAgentService = weworkAgentService;
}
/// <summary>
/// 发送企微消息
/// </summary>
/// <param name="weworkSendeMsg"></param>
/// <returns></returns>
/// <exception cref="ApiException"></exception>
[HttpPost("SendMessage")]
public async Task<string> SendMessage([FromBody] WeworkSendeMsg weworkSendeMsg)
{
//CheckSign(weworkSendeMsg);
var clientid = "UPWEBSITE";
var key = "1622a92d";
var param = new
{
Account = "dn.uc",
Password = "dn.uc.password",
Time = SignHelper.GetTimeStamp()
};
var weworkSendUrl = InitConfiguration.GetSection("WeworkSendUrl").Value;
var response = await _httpClient.PostSecurityAsync<WeworkResponse<object?>>(weworkSendUrl, param, weworkSendeMsg, clientid, key);
if (response.ErrCode != 0)
{
var msg = $"请求企微推送消息接口报错:{response.RrrMessage}, ex{JsonSerializer.Serialize(response)}";
Log.Error(msg);
throw new ApiException(msg);
}
return "发送成功!";
}
[HttpGet("AgentConfig")]
public async Task<string> GetAgentConfig([FromQuery] WorkReports model)
{
var request = _httpContextAccessor.HttpContext.Request;
var url = "http://sys.hc.dn8188.com/Work/GetJsApiSignature2.html?appid=" + model.appid + "&agentid=" + model.agentid + "&url=" + HttpUtility.UrlEncode(model.url);
var result = await _httpClient.GetAsync(url);
return result;
}
[HttpGet("Agents")]
public async Task<List<WeworkAgentDto>> GetWeworkAgents()
{
CheckSign();
return await _weworkAgentService.GetWeworkAgents();
}
public class WorkReports
{
public string appid { get; set; }
public string userid { get; set; }
public int agentid { get; set; }
public int deptid { get; set; }
public int eid { get; set; }
public string url { get; set; }
}
public class WeworkSendeMsg
{
/// <summary>
/// Appid
/// </summary>
[Required]
[JsonPropertyName("appid")]
public string? Appid { get; set; }
/// <summary>
/// Agentid
/// </summary>
[Required]
[JsonPropertyName("agentid")]
public int Agentid { get; set; }
/// <summary>
/// 发送数据
/// </summary>
[Required]
[JsonPropertyName("data")]
public string? Data { get; set; }
}
public class WeworkResponse<T>
{
[JsonPropertyName("errcode")]
public int? ErrCode { get; set; }
[JsonPropertyName("errmsg")]
public string? RrrMessage { get; set; }
[JsonPropertyName("data")]
public T? Data { get; set; }
}
}
}