using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Dysmsapi.Model.V20170525;
using Com.Alipay;
using dccdc.BLL;
using dccdc.Common;
using dccdc.Models;
using dccdc.Models.WeiXin;
using MessagingToolkit.QRCode.Codec;
using MessagingToolkit.QRCode.Codec.Data;
using MiniExcelLibs;
using NetSDKCS;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using ZWL.Common;
namespace dccdc.Controllers
{
public class WeiXinController : Controller
{
#region zzq
//const string appID = "wxb79cf945835c07e3";
//const string appsecret = "e7ff8220aaae27b4dc88862bdf0a0087";
#endregion
#region wwp
//wwp微信测试号信息
//private const string appID = "wxd87b28ea53d51416";
//private const string appsecret = "2a83e3bb4fed7f230e2cf0da90665834";
#endregion
///
/// ytgjjc
///
//const string Token = "3502b42365dff46b61c38f89524ebcf8";
//const string EncodingAESKey = "ha1Wb7md8RGwc6hziCONKVwtvIbT5sMS3O4CTKUlzcf";
//const string MyId = "gh_7a3845c111a0";
[AllowAnonymous]
public ActionResult about()
{
return View();
}
[AllowAnonymous]
// GET: WeiXin
[HttpGet]
public string Index()
{
string echostr = Request.QueryString["echostr"];
return CheckSignature() ? echostr : "error";
}
internal void sendtxmsgdx(jkztxsz sz, ProfessionalExamRegisterModel dtx)
{
var commonbll = new BLL.Common();
string yddx = commonbll.getParm_Value("qyyddx", "false", "是否启用移动短信");
if (yddx == "false")
{
string AccessKeyID = commonbll.getParm_Value("AccessKeyID", "LTAIWq3410bmP7hi", "阿里AccessKeyID");
string AccessKeySecret = commonbll.getParm_Value("AccessKeySecret", "F953Ru3uZN2ZbS5741zvO9OvHrthNV", "阿里AccessKeySecret");
string qm = commonbll.getParm_Value("aldxqm", "三才网络", "短信签名");
String product = "Dysmsapi";//短信API产品名称
String domain = "dysmsapi.aliyuncs.com";//短信API产品域名
String accessKeyId = AccessKeyID;//你的accessKeyId
String accessKeySecret = AccessKeySecret;//你的accessKeySecret
IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.AddEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
try
{
//必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为20个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
request.PhoneNumbers = dtx.mobile;
//必填:短信签名-可在短信控制台中找到
request.SignName = qm;
//必填:短信模板-可在短信控制台中找到
request.TemplateCode = commonbll.getParm_Value("aldxmb_cycb", "SMS_205400964", "短信模板从业人员催办"); //"SMS_205400964";
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
request.TemplateParam = Newtonsoft.Json.JsonConvert.SerializeObject(new { name = dtx.person_name, time = dtx.register_date });
//可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
//request.OutId = "21212121211";
//请求失败这里会抛ClientException异常
SendSmsResponse sendSmsResponse = acsClient.GetAcsResponse(request);
//System.Console.WriteLine(sendSmsResponse.Message);
log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType).Info(sendSmsResponse.Message);
}
catch (ServerException e)
{
log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType).Info(e.Message + e.RequestId);
//System.Console.WriteLine("Hello World!");
//return Json(new { State = 0, Message = e.Message});
}
catch (ClientException e)
{
log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType).Info(e.Message + e.RequestId);
//return Json(new { State = 0, Message = e.Message});
}
}
else
{
string ydhttp = commonbll.getParm_Value("ydhttp", "http://112.35.1.155:1992/sms/tmpsubmit", "移动短信地址");
var req = new Models.duanxin.msend();
req.ecName = commonbll.getParm_Value("yddxqymc", "德州市德城区疾病预防控制中心", "企业名称");
req.apId = commonbll.getParm_Value("yddxapId", "jkoa", "接口账号用户名");
req.mobiles = dtx.mobile;
req.sign = commonbll.getParm_Value("yddxsign", "d37CYmrbG", "签名编码");
req.templateId = "cb6620fa80254683a9bd41c22b55603f";
req.addSerial = "";
string[] parms = new string[2];
parms[0] = dtx.person_name;
parms[1] = dtx.register_date;
req.parms = Newtonsoft.Json.JsonConvert.SerializeObject(parms);
var md5 = MD5.Create();
//secretKey
var bs = md5.ComputeHash(Encoding.UTF8.GetBytes(req.ecName + req.apId + commonbll.getParm_Value("yddxsecretKey", "a7551898", "短信接口密码") + req.templateId + req.mobiles + req.parms + req.sign));
var sb = new StringBuilder();
foreach (byte b in bs)
{
sb.Append(b.ToString("x2"));
}
req.mac = sb.ToString().ToLower();
WebClient wc = new WebClient();
string upstr = Newtonsoft.Json.JsonConvert.SerializeObject(req);
upstr = upstr.Replace("parms", "params");
//new {params }
//upstr.Insert(upstr.Length-1,",")
byte[] resp = wc.UploadData(ydhttp, System.Text.Encoding.UTF8.GetBytes(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(upstr))));
var nres = Newtonsoft.Json.JsonConvert.DeserializeObject(System.Text.Encoding.UTF8.GetString(resp));
log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType).Info(System.Text.Encoding.UTF8.GetString(resp));
//return Json(new { State = 1, Message = yzm.id.ToString() });
//return Json(new { State = 1, Message = System.Text.Encoding.UTF8.GetString(resp) });
}
}
[AllowAnonymous]
[HttpPost]
public string Index(FormCollection fc)
{
string postString;
using (Stream stream = Request.InputStream)
{
Byte[] postBytes = new Byte[stream.Length];
stream.Read(postBytes, 0, (Int32)stream.Length);
postString = Encoding.UTF8.GetString(postBytes);
}
log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType).Info(postString);
string signature = Request.QueryString["signature"];
string timestamp = Request.QueryString["timestamp"];
string nonce = Request.QueryString["nonce"];
string msg_signature = Request.QueryString["msg_signature"];
string msg = postString;
string retmsg = string.Empty;
WXBizMsgCrypt wxbiz = new WXBizMsgCrypt(Common.Global.Token, Common.Global.EncodingAESKey, Common.Global.AppId);
log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType).Info(Common.Global.Token);
log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType).Info(Common.Global.EncodingAESKey);
log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType).Info(Common.Global.AppId);
//string msg = postString;
int code = wxbiz.DecryptMsg(msg_signature, timestamp, nonce, postString, ref msg);
log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType).Info(code);
log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType).Info(msg);
try
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(msg);
XmlNode root = doc.FirstChild;
var xmlElement = root["MsgType"];
if (xmlElement != null)
{
string msgType = xmlElement.InnerText;
log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType).Info(msgType);
switch (msgType.ToLower())
{
case "event":
retmsg = ExecEvent(doc);
break;
case "text":
//retmsg = ExceText(doc);
break;
}
}
}
catch (Exception ex)
{
log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType).Info(ex.Message);
}
if (string.IsNullOrEmpty(retmsg))
return retmsg;
else
{
log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType).Info(retmsg);
//int t_code = wxbiz.EncryptMsg(retmsg, timestamp, nonce, ref msg);
/*log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType).Info(msg);
if (t_code == 0)
return msg;
else
{
log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType).Info(t_code.ToString());
return "";
}*/
return retmsg;
}
//return "";
}
#region 校验开发者
private bool CheckSignature()
{
string signature = Request.QueryString["signature"];
string timestamp = Request.QueryString["timestamp"];
string nonce = Request.QueryString["nonce"];
string[] arrTmp = { Common.Global.Token, timestamp, nonce };
//string[] arrTmp = { Token, timestamp, nonce };
Array.Sort(arrTmp);//字典排序
string tmpStr = string.Join("", arrTmp);
var sha1 = System.Security.Cryptography.SHA1.Create();
tmpStr = BitConverter.ToString(sha1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(tmpStr))).Replace("-", ""); //对该字符串进行sha1加密
tmpStr = tmpStr.ToLower();//对字符串中的字母部分进行小写转换,非字母字符不作处理
//WriteLog(tmpStr);//计入日志
if (tmpStr == signature)//开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。开发者通过检验signature对请求进行校验,若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,否则接入失败
{
return true;
}
return false;
}
#endregion
#region 菜单维护
///
/// 微信菜单管理
///
///
public ActionResult Menu()
{
return View();
}
///
/// 微信菜单类型
///
///
[HttpPost]
public JsonResult getCDLX()
{
return Json(
new[]
{
new { id="zu",name="菜单组" },
new { id="click",name="点击推事件" },
new { id="view",name="跳转URL" }
}
);
}
///
/// 获取菜单列表
///
///
[HttpPost]
public string getMeun()
{
// string sql = "select * from menu";
var dt = new BLL.weixinBll().getMenu();
return Newtonsoft.Json.JsonConvert.SerializeObject(dt);
}
///
/// 获取上级菜单
///
///
[HttpPost]
public string getSJCD()
{
var dt = new BLL.weixinBll().getSJCD();
var dr = new Models.WeiXin.menu
{
id = 0,
name = "顶级菜单"
};
dt.Insert(0, dr);
return Newtonsoft.Json.JsonConvert.SerializeObject(dt);
}
///
/// 删除菜单
///
/// 菜单ID
///
[HttpPost]
public JsonResult delCD(int id)
{
return Json(new BLL.weixinBll().delCd(id));
}
///
/// 保存菜单
///
/// 菜单实体
///
[HttpPost]
public JsonResult saveCD(Models.WeiXin.menu m)
{
return Json(new BLL.weixinBll().saveCD(m));
}
#endregion
#region 发送菜单
///
/// 提交微信菜单
///
///
[HttpPost]
public string CreateWXMenu()
{
List