/// <summary> /// 退订 /// 实际上用户无法收到非订阅账号的消息,所以这里可以随便写。 /// unsubscribe事件的意义在于及时删除网站应用中已经记录的OpenID绑定,消除冗余数据。并且关注用户流失的情况。 /// </summary> /// <returns></returns> public override IResponseMessageBase OnEvent_UnsubscribeRequest(RequestMessageEvent_Unsubscribe requestMessage) { var responseMessage = base.CreateResponseMessage <ResponseMessageText>(); responseMessage.Content = "有空再来"; //记录取消关注用户到数据 WxUserService wxUserService = new WxUserService(); wxUserService.Unsubscribe(requestMessage.ToUserName, requestMessage.FromUserName, responseMessage.CreateTime.DateTime); return(responseMessage); }
public IHttpActionResult GetAuthBase(string code, string topic = "", string invite = "") { string ip = string.Empty; string region = string.Empty; string openid = string.Empty; string access_token = string.Empty; try { ip = Network.GetClientIP(); region = ipSearcher.BinarySearch(ip)?.Region; } catch { region = string.Empty; } userService = new WxUserService(); try { string[] keypair = Cache.GetCache <string[]>(code); if (keypair == null) { var http = new HttpHelper(); string oauthId = ConfigurationManager.AppSettings["oauthId"]; string oauthSecret = ConfigurationManager.AppSettings["oauthSecret"]; string apiUrl = $"https://api.weixin.qq.com/sns/oauth2/access_token?appid={oauthId}&secret={oauthSecret}&code={code}&grant_type=authorization_code"; var result = http.Post(apiUrl, string.Empty, Encoding.UTF8, Encoding.UTF8).ToJson <dynamic>(); openid = result.openid; access_token = result.access_token; if (region.Length > 100) { region = string.Empty; } userService.SaveOpenIdAndInvitation(openid, topic, invite, ip, region); Cache.WriteCache <string[]>(code, new string[] { openid, access_token }, DateTime.Now.AddHours(8)); } else { openid = keypair[0]; access_token = keypair[1]; } return(Ok(new { openid, access_token })); } catch (Exception ex) { log.Error(ex); return(InternalServerError(ex)); } }
public ActionResult GetWxAppUser() { int pageIndex = Request["page"] != null?int.Parse(Request["page"]) : 1; int pageSize = Request["rows"] != null?int.Parse(Request["rows"]) : 15; string ScName = Request["ScName"] != null ? Request["ScName"].ToString() : string.Empty; bool ispd = Request["ScB"] == null ? false : Convert.ToBoolean(Request["ScB"]); int totalCount = 0; IQueryable <WxUser> wu = WxUserService.LoadPageEntities(pageIndex, pageSize, out totalCount, x => x.Del != null, x => x.AddTime, false).DefaultIfEmpty(); if (ScName == string.Empty) { if (!ispd) { wu = wu.Where(x => x.UserInfoID == null).DefaultIfEmpty(); } else { wu = wu.Where(x => x.UserInfoID != null).DefaultIfEmpty(); } } if (ScName != string.Empty) { wu = wu.Where(x => x.wxName.Contains(ScName)).DefaultIfEmpty(); } if (wu.ToList()[0] == null) { return(Json(new { rows = "", total = totalCount }, JsonRequestBehavior.AllowGet)); } else { var temp = from a in wu select new { a.ID, a.wxName, a.AddTime, a.WxImg, a.Wxgender, a.Wxprovince, a.Wxcity, UserInfo = a.UserInfo == null ? "" : a.UserInfo.UName }; return(Json(new { rows = temp, total = totalCount }, JsonRequestBehavior.AllowGet)); } }
/// <summary> /// 订阅(关注)事件 /// </summary> /// <returns></returns> public override IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage) { var responseMessage = ResponseMessageBase.CreateFromRequestMessage <ResponseMessageText>(requestMessage); responseMessage.Content = GetWelcomeInfo(); if (!string.IsNullOrEmpty(requestMessage.EventKey)) { responseMessage.Content += "\r\n============\r\n场景值:" + requestMessage.EventKey; } //推送消息 //下载文档 if (requestMessage.EventKey.StartsWith("qrscene_")) { var sceneId = long.Parse(requestMessage.EventKey.Replace("qrscene_", "")); //var configHelper = new ConfigHelper(new HttpContextWrapper(HttpContext.Current)); var codeRecord = ConfigHelper.CodeCollection.Values.FirstOrDefault(z => z.QrCodeTicket != null && z.QrCodeId == sceneId); if (codeRecord != null) { if (codeRecord.AllowDownload) { Task.Factory.StartNew(() => AdvancedAPIs.CustomApi.SendTextAsync(null, OpenId, "下载已经开始,如需下载其他版本,请刷新页面后重新扫一扫。")); } else { //确认可以下载 codeRecord.AllowDownload = true; Task.Factory.StartNew(() => AdvancedAPIs.CustomApi.SendTextAsync(null, OpenId, GetDownloadInfo(codeRecord))); } } } //记录关注用户到数据 WxUserService wxUserService = new WxUserService(); wxUserService.Subscribe(requestMessage.ToUserName, requestMessage.FromUserName, responseMessage.CreateTime.DateTime); return(responseMessage); }
public IHttpActionResult GetUserInfo(string code) { string ip = string.Empty; string region = string.Empty; try { ip = Network.GetClientIP(); region = ipSearcher.BinarySearch(ip)?.Region; } catch { region = string.Empty; } try { var keypair = Cache.GetCache <string[]>(code); if (keypair == null) { throw new ArgumentException("code 已过期"); } var http = new HttpHelper(); string apiUrl = $"https://api.weixin.qq.com/sns/userinfo?access_token={keypair[1]}&openid={keypair[0]}&lang=zh_CN"; var userInfo = http.Get(apiUrl, Encoding.UTF8).ToJson <WxUser>(); if (region.Length > 100) { region = string.Empty; } userInfo.ip_address = ip; userInfo.ip_region = region; if (!string.IsNullOrEmpty(userInfo.openid)) { userService = new WxUserService(); userService.SaveWechatUser(userInfo); } return(Ok(userInfo)); } catch (Exception ex) { log.Error(ex); return(InternalServerError(ex)); } }
public IHttpActionResult GetRank([FromBody] RankPost rankBody) { try { userService = new WxUserService(); if (string.IsNullOrEmpty(rankBody.tid)) { throw new ArgumentNullException("tid"); } List <TopicRank> rank = Cache.GetCache <List <TopicRank> >("topicRank"); if (rank == null) { rank = userService.GetTopicInviteRank(rankBody.tid, rankBody.uid); Cache.WriteCache("topicRank", rank, DateTime.Now.AddMinutes(5)); } return(Ok(rank)); } catch (Exception ex) { log.Error(ex); return(InternalServerError(ex)); } }
//绑定微信和用户 解除微信与用户的绑定 public ActionResult WxuserBandUserinfo() { int Uid = Convert.ToInt32(Request["Uid"]); long Wid = Convert.ToInt32(Request["Wid"]); bool IsJieC = Request["Jiechu"] == "null" ? false : true; var iwu = WxUserService.LoadEntities(x => x.ID == Wid).FirstOrDefault(); if (IsJieC) { iwu.UserInfoID = null; } else { iwu.UserInfoID = Uid; } string ret = "ok"; if (!WxUserService.EditEntity(iwu)) { ret = "no"; } return(Json(ret, JsonRequestBehavior.AllowGet)); }
public UserController() { _wxUserService = new WxUserService(); }