/// <summary> /// 全网发布时处理文本发送 /// </summary> /// <param name="requestMessage"></param> /// <returns></returns> public static string OnTextRequest(RequestMessageText requestMessage, PostModel postModel) { if (requestMessage.Content == "TESTCOMPONENT_MSG_TYPE_TEXT") { StringBuilder sb = new StringBuilder(); sb.Append("<xml>"); sb.Append($"<ToUserName><![CDATA[{requestMessage.FromUserName}]]></ToUserName>"); sb.Append($"<FromUserName><![CDATA[{requestMessage.ToUserName}]]></FromUserName>"); sb.Append($"<CreateTime>{DateTimeHelper.GetTimeStamp(true)}</CreateTime>"); sb.Append($"<MsgType><![CDATA[text]]></MsgType>"); sb.Append($"<Content><![CDATA[TESTCOMPONENT_MSG_TYPE_TEXT_callback]]></Content>"); sb.Append("</xml>"); var timeStamp = DateTime.Now.Ticks.ToString(); var nonce = DateTime.Now.Ticks.ToString(); //必须要加密 WXBizMsgCrypt msgCrype = new WXBizMsgCrypt(postModel.Token, postModel.EncodingAESKey, postModel.AppId); string finalResponseXml = null; msgCrype.EncryptMsg(sb.ToString().Replace("\r\n", "\n")/* 替换\r\n是为了处理iphone设备上换行bug */, timeStamp, nonce, ref finalResponseXml);//TODO:这里官方的方法已经把EncryptResponseMessage对应的XML输出出来了 return finalResponseXml; } if (requestMessage.Content.StartsWith("QUERY_AUTH_CODE:")) { string openTicket = OpenPlatConfigBLL.SingleModel.GetComponentVerifyTicket(); string query_auth_code = requestMessage.Content.Replace("QUERY_AUTH_CODE:", ""); try { OpenPlatConfig currentmodel = OpenPlatConfigBLL.SingleModel.getCurrentModel(); QueryAuthResult oauthResult = WxRequest.QueryAuth(currentmodel.component_access_token, currentmodel.component_Appid, query_auth_code); //调用客服接口 string content = query_auth_code + "_from_api"; //Task.Run(() => { // Thread.Sleep(1000); // WxRequest.SendText(oauthResult.authorization_info.authorizer_access_token, requestMessage.FromUserName, content); //}); WxRequest.SendText(oauthResult.authorization_info.authorizer_access_token, requestMessage.FromUserName, content); return ""; } catch (Exception ex) { log4net.LogHelper.WriteError(typeof(WXRequestCommandBLL), ex); } } return "success"; }
/// <summary> /// 使用授权码换取公众号的授权信息 /// </summary> /// <param name="componentAppId">服务开发方的appid</param> /// <param name="componentAccessToken">服务开发方的access_token</param> /// <param name="authorizationCode">授权code,会在授权成功时返回给第三方平台,详见第三方平台授权流程说明</param> /// <param name="timeOut">代理请求超时时间(毫秒)</param> /// <returns></returns> public static QueryAuthResult QueryAuth(string componentAccessToken, string componentAppId, string authorizationCode) { string url = string.Format(_queryAuthUrl, componentAccessToken.AsUrlData()); var data = new { component_appid = componentAppId, authorization_code = authorizationCode }; string dataJson = JsonConvert.SerializeObject(data); string result = HttpHelper.DoPostJson(url, dataJson); QueryAuthResult model = GetResultModel <QueryAuthResult>(result); return(model); }
/// <summary> /// 保存微信信息 /// </summary> /// <param name="pwai"></param> /// <param name="paui"></param> /// <param name="clientId"></param> /// <returns></returns> public static bool SetWechatApp(QueryAuthResult pwai, GetAuthorizerInfoResult paui, string clientId) { #region 保存到微信信息表里 //var clientId = "eb17cc2569c74ab7888b1f403972d37d";//测试用 var loggingSessionInfo = Default.GetBSLoggingSession(clientId, "open"); var waiBll = new WApplicationInterfaceBLL(loggingSessionInfo); var waiEntitys = new WApplicationInterfaceEntity[] { }; var waiEntity = new WApplicationInterfaceEntity(); //去除之前授权的记录 //查询参数 List <IWhereCondition> complexCondition = new List <IWhereCondition> { }; complexCondition.Add(new EqualsCondition() { FieldName = "AppID", Value = pwai.authorization_info.authorizer_appid }); complexCondition.Add(new DirectCondition(" CustomerId != '" + clientId + "'")); complexCondition.Add(new EqualsCondition() { FieldName = "OpenOAuthAppid", Value = WebConfigurationManager.AppSettings["Component_Appid"] }); var tempList = waiBll.PagedQuery(complexCondition.ToArray(), null, 10, 1); if (tempList.Entities.Length > 0) { foreach (var item in tempList.Entities) { waiBll.Delete(item); } } var prevWaiEntitys = waiBll.QueryByEntity(new WApplicationInterfaceEntity { AppID = pwai.authorization_info.authorizer_appid, CustomerId = clientId, IsDelete = 0, OpenOAuthAppid = WebConfigurationManager.AppSettings["Component_Appid"] }, null); waiEntitys = waiBll.QueryByEntity(new WApplicationInterfaceEntity { AppID = pwai.authorization_info.authorizer_appid, CustomerId = clientId, IsDelete = 0 }, null); if (waiEntitys != null && waiEntitys.Length > 0) { waiEntity = waiEntitys.FirstOrDefault(); waiEntity.URL = Config.URL; waiEntity.Token = "jitmarketing";//老代码写死token//Config.ServerToken; //waiEntity.Token = "zmindclouds";//老代码写死token//Config.ServerToken, //waiEntity.AppID = "wx691c2f2bbac04b4b"; //waiEntity.AppSecret = "0c79e1fa963cd80cc0be99b20a18faeb"; //waiEntity.PrevEncodingAESKey = "F3Rd5xvdCqUsL5FFwhS1vSIqTRFoNpRcWhyGrOjQhAK"; //waiEntity.CurrentEncodingAESKey = "F3Rd5xvdCqUsL5FFwhS1vSIqTRFoNpRcWhyGrOjQhAK"; waiEntity.ServerIP = Config.ServerIP; waiEntity.AuthUrl = Config.AuthUrl; waiEntity.RequestToken = pwai != null ? pwai.authorization_info.authorizer_access_token : string.Empty; //waiEntity.RefreshToken = pwai != null ? pwai.authorization_info.authorizer_refresh_token : string.Empty,//刷新token waiEntity.ExpirationTime = DateTime.Now.AddSeconds(7000);//默认7200,提前200秒过期 waiEntity.IsMoreCS = 1; waiEntity.OpenOAuthAppid = WebConfigurationManager.AppSettings["Component_Appid"]; waiBll.Update(waiEntity); //提交管理平台 waiBll.setCposApMapping(waiEntity); } else { if (pwai != null && paui != null) { } var weixinTypeId = "3";//服务号 switch (paui.authorizer_info.service_type_info.id.ToString()) { case "0": weixinTypeId = "1"; //订阅号 break; case "1": weixinTypeId = "2"; //订阅号 break; } waiEntity = new WApplicationInterfaceEntity() { ApplicationId = Guid.NewGuid().ToString("N"), WeiXinName = paui != null ? paui.authorizer_info.nick_name : string.Empty, WeiXinID = paui != null ? paui.authorizer_info.user_name : string.Empty, URL = Config.URL, Token = "jitmarketing", //老代码写死token//Config.ServerToken, AppID = pwai != null ? pwai.authorization_info.authorizer_appid : string.Empty, AppSecret = string.Empty, //空 //Token = "zmindclouds",//老代码写死token//Config.ServerToken, //AppID = "wx691c2f2bbac04b4b", //AppSecret = "0c79e1fa963cd80cc0be99b20a18faeb",//空 //PrevEncodingAESKey = "F3Rd5xvdCqUsL5FFwhS1vSIqTRFoNpRcWhyGrOjQhAK", //CurrentEncodingAESKey = "F3Rd5xvdCqUsL5FFwhS1vSIqTRFoNpRcWhyGrOjQhAK", ServerIP = Config.ServerIP, WeiXinTypeId = weixinTypeId,// paui != null ? paui.authorizer_info.service_type_info.id.ToString() : "3", AuthUrl = Config.AuthUrl, RequestToken = pwai != null ? pwai.authorization_info.authorizer_access_token : string.Empty, //RefreshToken = pwai != null ? pwai.authorization_info.authorizer_refresh_token : string.Empty,//刷新token ExpirationTime = DateTime.Now.AddSeconds(7000), //默认7200,提前200秒过期 IsHeight = 1, //1=高级帐号 IsMoreCS = 1, OpenOAuthAppid = WebConfigurationManager.AppSettings["Component_Appid"], // component_AppId; CustomerId = clientId }; waiBll.Create(waiEntity); //提交微信初级菜单 waiBll.setCreateWXMenu(waiEntity); //提交管理平台 waiBll.setCposApMapping(waiEntity); } #endregion return(true); }