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.

79 lines
2.8 KiB
C#

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;
namespace DSWeb.Areas.MvcShipping.Helper
{
public class WorkWeixinSendHelper
{
private static ILog logger = LogManager.GetLogger("WorkWeixinSendHelper");
/// <summary>
/// 推送企业微信通知消息
/// </summary>
/// <param name="userId">企业微信中的用户ID</param>
/// <param name="url">点击跳转的url</param>
/// <returns></returns>
public static bool SendNotifyToUser(string userId, string url)
{
if (!MemoryCache.Default.Contains(WechatWorkTokenJob.WorkWeixinAccessTokenStoreKey))
{
return false;
}
string token = MemoryCache.Default[WechatWorkTokenJob.WorkWeixinAccessTokenStoreKey].ToString();
RunBillContext runBillContext = new RunBillContext();
var pAgentid = runBillContext.ParamSets.FirstOrDefault(p => p.PARAMNAME == "WorkWeixinAgentid");
if (pAgentid == null || string.IsNullOrWhiteSpace(pAgentid.PARAMVALUE))
{
logger.Error("微信企业号Agentid参数配置错误");
throw new Exception("微信企业号Agentid参数配置错误");
}
var pServerUrl = runBillContext.ParamSets.FirstOrDefault(p => p.PARAMNAME == "WorkWeixinServerUrl");
if (pServerUrl == null || string.IsNullOrWhiteSpace(pAgentid.PARAMVALUE))
{
logger.Error("微信企业号ServerUrl参数配置错误");
throw new Exception("微信企业号ServerUrl参数配置错误");
}
var jsonPara = new
{
touser = userId,
msgtype = "textcard",
agentid = pAgentid.PARAMVALUE,
textcard = new
{
title = "跑单通知",
description = "<div class=\"gray\">收到新的跑单任务</div> <div class=\"normal\">请点击查看</div>",
url = pServerUrl.PARAMVALUE + url,
btntxt = "详情"
}
};
string resp = WebRequestHelper.DoPost($"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={token}", JsonConvert.SerializeObject(jsonPara));
logger.Debug("发送微信企业号回复:" + resp);
var jsonObj = JsonConvert.DeserializeAnonymousType(resp, new
{
errcode = 0,
errmsg = "",
invaliduser = ""
});
return jsonObj.errcode == 0;
}
}
}