/// <summary> /// 订阅(关注)事件 如果用户还未关注公众号,则用户可以关注公众号,关注后微信会将带场景值关注事件推送给开发者 /// </summary> /// <returns></returns> public override IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage) { var responseMessage = ResponseMessageBase.CreateFromRequestMessage <ResponseMessageText>(requestMessage); responseMessage.Content = "欢迎您加入五福商城大家庭"; //新关注用户的openId var openId = requestMessage.FromUserName; var userInfo = new UserOAuthService().UserInfo(openId); var parentOpenId = ""; //如果是场景关注 if (!string.IsNullOrEmpty(requestMessage.EventKey) && requestMessage.EventKey.StartsWith("qrscene_")) { parentOpenId = requestMessage.EventKey.Replace("qrscene_", "");//推荐人的openid if (openId != parentOpenId) { //给推荐人发送推荐成功消息 string notifyMsg = string.Format(@"用户【{0}】,通过您的二维码加入了商城,加入时间:{1}", userInfo.nickname, DateTime.Now.ToString()); var customService = new CustomService(); customService.SendText(parentOpenId, notifyMsg); } } //将新关注的用户添加到数据库保存 Task.Factory.StartNew(() => { //用户信息保存到数据库 var userGenerator = new UserGenerator(); userGenerator.CreateUser(new Models.UserInfo { OpenId = openId, UnionId = userInfo.unionid, NickName = userInfo.nickname, Province = userInfo.province, City = userInfo.city, County = userInfo.country, Gender = userInfo.sex.ToString(), Portrait = userInfo.headimgurl, ParentOpenId = parentOpenId }); }); return(responseMessage); }
/// <summary> /// 通过二维码扫描 扫描事件 如果用户已经关注公众号,则微信会将带场景值扫描事件推送给开发者 /// </summary> /// <param name="requestMessage"></param> /// <returns></returns> public override IResponseMessageBase OnEvent_ScanRequest(RequestMessageEvent_Scan requestMessage) { var responseMessage = CreateResponseMessage <ResponseMessageText>(); responseMessage.Content = "欢迎您加入五福商城大家庭"; if (!string.IsNullOrEmpty(requestMessage.EventKey)) { //新用户openId var openId = requestMessage.FromUserName; var userInfo = new Services.impl.UserOAuthService().UserInfo(openId); var parentOpenId = requestMessage.EventKey;//推荐人的openid //这里要锁定用户推荐关系 if (openId != parentOpenId) { Task.Factory.StartNew(() => { var userGenerator = new UserGenerator(); userGenerator.CreateUser(new Models.UserInfo { OpenId = openId, UnionId = userInfo.unionid, NickName = userInfo.nickname, Province = userInfo.province, City = userInfo.city, County = userInfo.country, Gender = userInfo.sex.ToString(), Portrait = userInfo.headimgurl, ParentOpenId = parentOpenId }); //给推荐人发送推荐成功消息 string notifyMsg = string.Format(@"用户【{0}】,通过您的二维码加入了商城,加入时间:{1}", userInfo.nickname, DateTime.Now.ToString()); new CustomService().SendText(parentOpenId, notifyMsg); }); } } return(responseMessage); }