/// <summary> /// 获取令牌 /// </summary> private AccessToken GetToken() { var sendDataToWeChat = new SendDataToWeChat(); var tmp = sendDataToWeChat.GetPage("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxafebc80a58de29b8&secret=35e2abe7503bffa6a95049684dfd5e0e"); var accessToken = JsonConvert.DeserializeObject<AccessToken>(tmp); return accessToken; }
private void SendMsg(string openId, string replyContent) { var jsonWx = "{\"touser\":\"" + openId + "\",\"msgtype\":\"text\",\"text\":{\"content\":\"" + replyContent + "\"}}"; var sendDataToWeChat = new SendDataToWeChat(); WebServ.WebService1 service1 = new WebService1(); var token = service1.GetToken(); var back = sendDataToWeChat.GetPage("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + token, jsonWx); var backData = JsonConvert.DeserializeObject<WeChatBackData>(back); if (backData.errcode == 0) { Success = true; Log.Debug("发送成功,返回:" + back); } else { Log.Debug("发送失败,返回:" + back); } }
/// <summary> /// 发送图文信息 /// </summary> /// <param name="userid">用户id</param> /// <param name="key">菜单id</param> /// <param name="name">菜单名称</param> /// <param name="type">业务类型</param> /// <param name="count">查询数据数量</param> public void SendNewsMessage(string userid, string key, string name, string type, string count) { var hashTable = new Hashtable(); var accessToken = new AccessToken(); var sendDataToUser = new SendDataToWeChat(); var infoReleaseDal = new InfoReleaseDal(); var listnews = new List<object>(); try { //获取发布信息数据 var dataDt = infoReleaseDal.QueryInfo(new InfoRelease() { BusinessType = key, FlagRelease = type }, count); var listDt = from ldt in dataDt.AsEnumerable() select new { Id = ldt.Field<string>("Id"), Title = ldt.Field<string>("Title"), MessageDescription = ldt.Field<string>("MessageDescription") }; if (dataDt.Rows.Count > 1) { var name2 = HttpContext.Current.Server.UrlEncode(name); listnews.Add(new { title = name + "更多列表...", description = "", url = _ip1 + "/views/messagelist/messagelist.htm?key=" + key + "&name=" + name2 + "&flag=1", picurl = _ip1 + "/UploadImages/" + key + ".jpg" }); } foreach (var ldt in listDt) { if (dataDt.Rows.Count > 1) { listnews.Add(new { title = ldt.Title, description = ldt.MessageDescription, url = _ip1 + "/views/messagelist/messagedetail.htm?id=" + ldt.Id }); } else { listnews.Add(new { title = ldt.Title, description = ldt.MessageDescription, url = _ip1 + "/views/messagelist/messagedetail.htm?id=" + ldt.Id, picurl = _ip1 + "/UploadImages/" + key + ".jpg" }); } } hashTable["touser"] = userid; hashTable["msgtype"] = "news"; hashTable["news"] = new { articles = listnews }; var json = _jss.Serialize(hashTable); var token = accessToken.GetExistAccessToken(); var back = sendDataToUser.GetPage("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + token, json); Log.Debug("调试信息:" + back); } catch (Exception ex) { Log.Debug("错误信息:" + ex.Message); } }
/// <summary> /// 回复消息 /// </summary> /// <param name="context"></param> private void ReplyMsg1(HttpContext context) { var hashtable = new Hashtable(); var msgId = context.Request["msgId"] ?? ""; var openId = context.Request["openId"] ?? ""; var replyContent = context.Request["replyContent"] ?? ""; try { var jsonWx = "{\"touser\":\"" + openId + "\",\"msgtype\":\"text\",\"text\":{\"content\":\"" + replyContent + "\"}}"; var accessToken = new AccessToken(); var sendDataToWeChat = new SendDataToWeChat(); var token = accessToken.GetExistAccessToken(); var back = sendDataToWeChat.GetPage("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + token, jsonWx); hashtable = new Hashtable(); hashtable["data"] = back; hashtable["error"] = false; ReplyMsgBll bll = new ReplyMsgBll(); bll.Add(new Model.common.ReplyMsg() { ReceiveMsgId = msgId, AcceptName = openId, TextMsg = replyContent, IsSend = 2, }); var json = _jss.Serialize(hashtable); context.Response.Write(json); } catch (Exception e) { hashtable["error"] = true; var json = _jss.Serialize(hashtable); context.Response.Write(json); Log.Error(e); } }
/// <summary> /// 发送图文信息 /// </summary> /// <param name="token">token</param> /// <param name="userid">微信号</param> /// <param name="articles"></param> public void SendNewsMessage(string token, string userid, List<Articles> articles) { var hashTable = new Hashtable(); var sendDataToUser = new SendDataToWeChat(); try { hashTable["touser"] = userid; hashTable["msgtype"] = "news"; hashTable["news"] = new { articles }; var json = _jss.Serialize(hashTable); var back = sendDataToUser.GetPage("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + token, json); Log.Debug("调试信息:" + back); } catch (Exception ex) { Log.Debug("错误信息:" + ex.Message); } }
/// <summary> /// 发送文本信息 /// </summary> /// <param name="userid"></param> /// <param name="msg">消息文字</param> public void SendTxtMessage(string userid, string msg) { var hashTable = new Hashtable(); var accessToken = new AccessToken(); var sendDataToUser = new SendDataToWeChat(); try { hashTable["touser"] = userid; hashTable["msgtype"] = "text"; hashTable["text"] = new { content = msg }; var json = _jss.Serialize(hashTable); var token = accessToken.GetExistAccessToken(); var back = sendDataToUser.GetPage("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + token, json); Log.Debug("调试信息:" + back); } catch (Exception ex) { Log.Debug("错误信息:" + ex.Message); } }
/// <summary> /// 发送图文信息 /// </summary> /// <param name="context"></param> public void SendNewsMessage(HttpContext context) { var hashTable = new Hashtable(); var key = context.Request.Params["key"]; var type = context.Request.Params["type"]; var count = context.Request.Params["count"]; var accessToken = new AccessToken(); var sendDataToUser = new SendDataToWeChat(); var userInfoDal = new UserInfoDal(); var infoReleaseDal = new InfoReleaseDal(); var listnews = new List<object>(); try { //获取发布信息数据 var dataDt = infoReleaseDal.QueryInfo(new InfoRelease() { BusinessType = key, FlagRelease = type }, "2");//todo:替换count var listDt = from ldt in dataDt.AsEnumerable() select new { Title = ldt.Field<string>("Title"), MessageDescription = ldt.Field<string>("MessageDescription") }; foreach (var ldt in listDt) { listnews.Add(new { title = ldt.Title, description = ldt.MessageDescription, url = "http://218.22.27.236/views/messagelist/messagelist.htm?key=" + key + "&name=停电信息列表", picurl = "http://218.22.27.236/tl/UploadImages/topleft.jpg" }); } //获取用户数据 var data = userInfoDal.QueryAll(); var list = from da in data.AsEnumerable() select new { openid = da.Field<string>("openid"), }; foreach (var li in list) { hashTable["touser"] = li.openid; hashTable["msgtype"] = "news"; hashTable["news"] = new { articles = listnews }; var json = _jss.Serialize(hashTable); var token = accessToken.GetExistAccessToken(); var back = sendDataToUser.GetPage("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + token, json); Log.Debug("调试信息:" + back); } } catch (Exception ex) { Log.Debug("错误信息:" + ex.Message); } }
/// <summary> /// 发送菜单 /// </summary> /// <param name="httpContext"></param> public void SendMenu(HttpContext httpContext) { var hashTable = new Hashtable(); var accessToken = new AccessToken(); var hashTableBack = new Hashtable(); var sendDataToWeChat = new SendDataToWeChat(); var customMenuDal = new CustomMenuDal(); var data = customMenuDal.QueryByFirstMenu();//查询一级菜单 var tmp = from da in data.AsEnumerable() select new { id = da.Field<string>("id"), pid = da.Field<string>("pid"), type = da.Field<string>("type"), name = da.Field<string>("name"), key = da.Field<string>("nkey"), url = da.Field<string>("url") }; var list = new List<object>(); foreach (var info in tmp.ToList()) { // 算法: // 首先构造一个List<object>,然后,通过一级菜单数据查询二级菜单, // 如果有二级菜单就构造一个MainMenuParameter()类,将它加到List<object>中 // 如果没有则构造一个SubMenuParameter类,将它加到List<object>中 var dataSub = customMenuDal.QueryBySecondMenu(info.id);//查询二级菜单 if (dataSub.Rows.Count != 0) { var mainMenuParameter = new MainMenuParameter(); var tmpSub = from da in dataSub.AsEnumerable() select new { id = da.Field<string>("id"), pid = da.Field<string>("pid"), type = da.Field<string>("type"), name = da.Field<string>("name"), key = da.Field<string>("nkey"), url = da.Field<string>("url") }; var subList = new List<object>(); foreach (var tm in tmpSub) { switch (tm.type) { case "click": { var menuClick = new MenuClick { name = tm.name, key = tm.key, type = tm.type }; subList.Add(menuClick); } break; case "view": { var menuView = new MenuView { name = tm.name, url = tm.url, type = tm.type }; subList.Add(menuView); } break; } }; mainMenuParameter.name = info.name; mainMenuParameter.sub_button = subList.ToList(); list.Add(mainMenuParameter); } else { switch (info.type) { case "click": { var menuClick = new MenuClick { name = info.name, key = info.key, type = info.type }; list.Add(menuClick); } break; case "view": { var menuView = new MenuView { name = info.name, url = info.url, type = info.type }; list.Add(menuView); } break; } } } hashTable["button"] = list.ToList(); var json = _jss.Serialize(hashTable); json = json.Replace("\\u0026", "&"); var token = accessToken.GetExistAccessToken(); var back = sendDataToWeChat.GetPage(" https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + token, json); hashTableBack["isSuccess"] = true; hashTableBack["data"] = back; hashTableBack["jsMethod"] = "ajax_SendMenu"; var jsonBack = _jss.Serialize(hashTableBack); httpContext.Response.Write(jsonBack); }
private void Send(HttpContext context) { var userid = context.Request["userid"] ?? ""; var Id = context.Request["Id"] ?? ""; var Title = context.Request["title"] ?? ""; var messageDescription = context.Request["MessageDescription"] ?? ""; var key = context.Request["key"] ?? ""; if (string.IsNullOrEmpty(userid) || string.IsNullOrEmpty(Id) || string.IsNullOrEmpty(Title) || string.IsNullOrEmpty(messageDescription)) { context.Response.Write("{\"error\":true,\"msg\":\"信息不完整。\"}"); return; } var hashTable = new Hashtable(); var listnews = new List<object>(); var accessToken = new AccessToken(); var sendDataToUser = new SendDataToWeChat(); try { listnews.Add(new { title = Title, description = messageDescription, url = "http://218.22.27.236/views/messagelist/messagedetail_fxgz.htm?id=" + Id + "&wxid=" + userid + "&state=2", picurl = "http://218.22.27.236/tl/UploadImages/" + key + ".jpg" }); hashTable["touser"] = userid; hashTable["msgtype"] = "news"; hashTable["news"] = new { articles = listnews }; var json = _jss.Serialize(hashTable); json = json.Replace("\\u0026", "&"); var token = accessToken.GetExistAccessToken(); var back = sendDataToUser.GetPage("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + token, json); var backData = JsonConvert.DeserializeObject<WeChatBackData>(back); if (backData.errcode != 0) { context.Response.Write("{\"error\":true,\"msg\":\"" + backData.errmsg + "\"}"); } else { context.Response.Write("{\"error\":false}"); } var infoReleaseDetailsModel = new InfoReleaseDetailsModel(); if (backData.errcode == 0 && backData.errmsg == "ok") { infoReleaseDetailsModel.State = "2";//待确认 } var infoReleaseDetailsDal = new InfoReleaseDetailsDal(); infoReleaseDetailsModel.Id = Id; infoReleaseDetailsDal.Modify(infoReleaseDetailsModel); Log.Debug("调试信息:" + back); } catch (Exception e) { context.Response.Write("{\"error\":true,\"msg\":\"" + e.Message + "\"}"); Log.Error(e); } }
/// <summary> /// 发送文本信息 /// </summary> /// <param name="context"></param> public void SendTxtMessage(HttpContext context) { var hashTable = new Hashtable(); var accessToken = new AccessToken(); var sendDataToUser = new SendDataToWeChat(); var userInfoDal = new UserInfoDal(); try { var data = userInfoDal.QueryAll(); var list = from da in data.AsEnumerable() select new { openid = da.Field<string>("openid"), }; foreach (var li in list) { hashTable["touser"] = li.openid; hashTable["msgtype"] = "text"; hashTable["text"] = new { content = "你好张威,这是你的测试内容" }; var json = _jss.Serialize(hashTable); var token = accessToken.GetExistAccessToken(); var back = sendDataToUser.GetPage("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + token, json); Log.Debug("调试信息:" + back); } } catch (Exception ex) { Log.Debug("错误信息:" + ex.Message); } }
/// <summary> /// 修改信息发布详细表里状态 /// </summary> /// <param name="context"></param> public void ModifyInfoReleaseDetails(HttpContext context) { var hashtable = new Hashtable(); var infoReleaseDetailsDal = new InfoReleaseDetailsDal(); var accessToken = new AccessToken(); var sendDataToUser = new SendDataToWeChat(); var wxid = context.Request.Params["wxid"]; var id = context.Request.Params["id"]; try { //修改信息详细表内容为已确认 infoReleaseDetailsDal.ModifyStateByWxid(new InfoReleaseDetailsModel() { OpenId = wxid, InfoReleaseId = id }); hashtable["touser"] = wxid; hashtable["msgtype"] = "text"; hashtable["text"] = new { content = "尊敬的客户,请按以下标准导语进行语音确认,“某某单位某某某已签收并确认某月某日高压用户风险告知通知书”。" }; var json = _jss.Serialize(hashtable); var token = accessToken.GetExistAccessToken(); var back = sendDataToUser.GetPage("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + token, json); var backData = JsonConvert.DeserializeObject<WeChatBackData>(back); if (backData.errcode == 0 && backData.errmsg == "ok") { hashtable["isSuccess"] = true; hashtable["jsMethod"] = "ajax_ModifyInfoReleaseDetails"; } else { hashtable["isSuccess"] = false; hashtable["msg"] = "发送文本消息失败"; } json = _jss.Serialize(hashtable); context.Response.Write(json); } catch (Exception e) { Log.Debug("方法名:ModifyInfoReleaseDetails,,错误原因:" + e.Message); } }
/// <summary> /// 推送分组信息 /// </summary> public void UploadGroup(string token) { var dataone = userdal.QueryUserToGroupByOne(); if (dataone.Rows.Count > 0) { for (int i = 0; i < dataone.Rows.Count; i++) { string openid = dataone.Rows[i]["openid"].ToString(); string groupid = dataone.Rows[i]["groupid"].ToString(); //查询目标分组的父id var groupPid = userdal.QueryPidById(groupid); var groupname = userdal.QueryNameById(groupid); if (groupPid == "-1") { string replyContent = ""; if (groupname.Contains("高压")) replyContent = "高压客户,您的资料已审核通过!"; else if (groupname.Contains("未分组")) replyContent = ""; else replyContent = "您的客户资料已审核通过!"; try { //分组时,先发送分组信息到微信服务器,返回成功再更新本地数据库 var jsonWx = "{\"openid\":\"" + openid + "\",\"to_groupid\":\"" + groupid + "\"}"; var accessToken = new AccessToken(); var sendDataToWeChat = new SendDataToWeChat(); //var token = accessToken.GetExistAccessToken(); var back = sendDataToWeChat.GetPage("https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=" + token, jsonWx); var backData = JsonConvert.DeserializeObject<GroupBackData>(back); if (backData.errcode != "0") { Log.Debug("给" + openid + "用户分到" + groupid + "_" + groupname + "组失败!"); } else { userdal.ModifyUploadWechat(openid); //分组成功后,给用户发送消息 var jsonSend = "{\"touser\":\"" + openid + "\",\"msgtype\":\"text\",\"text\":{\"content\":\"" + replyContent + "\"}}"; var backU = sendDataToWeChat.GetPage("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + token, jsonSend); var backUData = JsonConvert.DeserializeObject<GroupBackData>(back); if (backUData.errcode != "0") Log.Debug("给" + openid + "用户发送消息失败!"); } } catch (Exception e) { Log.Debug("方法名:UploadGroup,描述:分组信息推送方法出错,错误原因:" + e.Message); Log.Debug("给" + openid + "用户分到" + groupid + "_" + groupname + "组失败!"); } } else if (groupPid != "-1") { string replyContent = ""; if (groupname.Contains("高压")) replyContent = "高压客户,您的资料已审核通过!"; else if (groupname.Contains("未分组")) replyContent = ""; else replyContent = "您的客户资料已审核通过!"; try { //分组时,先发送分组信息到微信服务器,返回成功再更新本地数据库 var jsonWx = "{\"openid\":\"" + openid + "\",\"to_groupid\":\"" + groupPid + "\"}"; var accessToken = new AccessToken(); var sendDataToWeChat = new SendDataToWeChat(); //var token = accessToken.GetExistAccessToken(); var back = sendDataToWeChat.GetPage("https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=" + token, jsonWx); var backData = JsonConvert.DeserializeObject<GroupBackData>(back); if (backData.errcode != "0") { Log.Debug("给" + openid + "用户分到" + groupid + "_" + groupname + "组失败!"); } else { userdal.ModifyUploadWechat(openid); //分组成功后,给用户发送消息 var jsonSend = "{\"touser\":\"" + openid + "\",\"msgtype\":\"text\",\"text\":{\"content\":\"" + replyContent + "\"}}"; var backU = sendDataToWeChat.GetPage("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + token, jsonSend); var backUData = JsonConvert.DeserializeObject<GroupBackData>(back); if (backUData.errcode != "0") Log.Debug("给" + openid + "用户发送消息失败!"); } } catch (Exception e) { Log.Debug("方法名:UploadGroup,描述:分组信息推送方法出错,错误原因:" + e.Message); Log.Debug("给" + openid + "用户分到" + groupid + "_" + groupname + "组失败!"); } } } } }
/// <summary> /// 编辑分组 /// </summary> /// <param name="httpContext"></param> public void ModifyFz(HttpContext context) { var id = context.Request["id"]; var name = context.Request["name"]; Model.common.UserGroup usergroup = new Model.common.UserGroup() { id = id, name = name }; var pid = userdal.QueryPidById(id); try { if (pid == "-1") { var jsonWx = "{\"group\":{\"id\":\"" + id + "\",\"name\":\"" + name + "\"}}"; var accessToken = new AccessToken(); var sendDataToWeChat = new SendDataToWeChat(); var token = accessToken.GetExistAccessToken(); var back = sendDataToWeChat.GetPage("https://api.weixin.qq.com/cgi-bin/groups/update?access_token=" + token, jsonWx); var backData = JsonConvert.DeserializeObject<GroupBackData>(back); if (backData.errcode != "0") { context.Response.Write("{\"success\":true,\"msg\":\"保存失败!\"}"); } else { usersInfodal.ModifyFz(usergroup); context.Response.Write("{\"success\":true,\"msg\":\"保存成功!\"}"); } } else if (pid != "-1") { usersInfodal.ModifyFz(usergroup); context.Response.Write("{\"success\":true,\"msg\":\"保存成功!\"}"); } } catch (Exception e) { Log.Debug("方法名:ModifyFz,描述:编辑分组方法出错,错误原因:" + e.Message); context.Response.Write("{\"success\":false,\"msg\":\"保存失败!\"}"); } }
/// <summary> /// 获取微信服务器传来的组id /// </summary> /// <param name="context"></param> public void GetId(HttpContext context) { string name = context.Request["name"]; var jsonWx = "{\"group\":{\"name\":\"" + name + "\"}}"; var accessToken = new AccessToken(); var sendDataToWeChat = new SendDataToWeChat(); var token = accessToken.GetExistAccessToken(); var back = sendDataToWeChat.GetPage("https://api.weixin.qq.com/cgi-bin/groups/create?access_token=" + token, jsonWx); //var backData = JsonConvert.DeserializeObject<GroupBackData>(back); context.Response.Write(back); }
/// <summary> /// 添加用户到信息发布表 /// </summary> /// <param name="context"></param> public void AddPersonToInfoRelease(HttpContext context) { var hashTable = new Hashtable(); var weChatData = new Hashtable(); var infoReleaseDal = new InfoReleaseDal(); var infoReleaseDetailsDal = new InfoReleaseDetailsDal(); var accessToken = new AccessToken(); var sendDataToUser = new SendDataToWeChat(); var id = context.Request.Params["id"]; var ppid = context.Request.Params["ppid"]; var key = context.Request.Params["key"]; var title = context.Request.Params["title"]; var ms = context.Request.Params["ms"]; try { infoReleaseDal.Modify(new InfoRelease() { Id = id, FlagRelease = "1", SendPerson = ppid }); var tmp = JsonConvert.DeserializeObject<List<Tmp>>(ppid); if (tmp.Count > 0) { infoReleaseDetailsDal.Del(new InfoReleaseDetailsModel() { InfoReleaseId = id }); foreach (var t in tmp) { var weChatDataList = new List<object>(); weChatDataList.Add(new { title, description = ms, url = "http://218.22.27.236/views/messagelist/messagedetail_fxgz.htm?id=" + id + "&wxid=" + t.wxid, picurl = "http://218.22.27.236/tl/UploadImages/" + key + ".jpg" }); weChatData["touser"] = t.wxid; weChatData["msgtype"] = "news"; weChatData["news"] = new { articles = weChatDataList }; var json = _jss.Serialize(weChatData); json = json.Replace("\\u0026", "&"); var token = accessToken.GetExistAccessToken(); var back = sendDataToUser.GetPage("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + token, json); var backData = JsonConvert.DeserializeObject<WeChatBackData>(back); var infoReleaseDetailsModel = new InfoReleaseDetailsModel(); if (backData.errcode == 0 && backData.errmsg == "ok") { infoReleaseDetailsModel.State = "2";//待确认 } else { infoReleaseDetailsModel.State = "1";//待推送 } infoReleaseDetailsModel.Id = Guid.NewGuid().ToString(); infoReleaseDetailsModel.OpenId = t.wxid; infoReleaseDetailsModel.NickName = t.nickname; infoReleaseDetailsModel.RNumber = t.ywh; infoReleaseDetailsModel.InfoReleaseId = id; infoReleaseDetailsDal.Add(infoReleaseDetailsModel); } } hashTable["isSuccess"] = true; hashTable["jsMethod"] = "ajax_AddPersonToInfoRelease"; var json2 = _jss.Serialize(hashTable); context.Response.Write(json2); } catch (Exception e) { Log.Debug("出错信息:" + e.Message); } }