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");
///
/// 推送企业微信通知消息
///
/// 企业微信中的用户ID
/// 点击跳转的url
///
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 = "
收到新的跑单任务
请点击查看
",
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;
}
}
}