/// <summary>
        /// 订阅(关注)事件
        /// </summary>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage)
        {
            var accessToken = Senparc.Weixin.MP.Containers.AccessTokenContainer.TryGetAccessToken(appId, appSecret);
            var openId      = requestMessage.FromUserName;//获取OpenId
            var userInfo    = Senparc.Weixin.MP.AdvancedAPIs.UserApi.Info(appId, openId, Language.zh_CN);
            var temp        = wx_userService.LoadEntities(u => u.gzh_openid == openId).FirstOrDefault();

            if (temp != null)
            {
                temp.state    = (int)WXUserState.已关注;
                temp.add_time = DateTime.Now;
                wx_userService.EditEntity(temp);
            }
            else
            {
                //新用户
                var user = new user();
                user.pid   = 0;
                user.name  = userInfo.nickname;
                user.sex   = userInfo.sex;
                user.state = (int)User_state.正常;
                user.isbuy = (int)Isbuy.未购买;
                //首次关注赠送积分
                var config_ruleItem = Config_ruleService.LoadEntities(n => n.name == "首次关注" && n.state == (int)State.可用).FirstOrDefault();
                if (config_ruleItem != null)
                {
                    var usr = new user_score_record();
                    usr.id    = Guid.NewGuid();
                    usr.score = config_ruleItem.value != null?Convert.ToDecimal(config_ruleItem.value) : 0;

                    usr.type           = (int)Score_type.收益;
                    usr.state          = (int)User_score_record_state.已完成;
                    usr.source_name    = "首次关注系统赠送";
                    usr.add_time       = DateTime.Now;
                    usr.remark         = ((int)Score_source_remark.系统赠送).ToString();
                    user.total_score  += usr.score;
                    user.usable_score += user.total_score;
                    user.user_score_record.Add(usr);
                }
                var newUser = new wx_user()
                {
                    city             = userInfo.city,
                    country          = userInfo.country,
                    wx_head_protrait = userInfo.headimgurl,
                    nickname         = userInfo.nickname,
                    gzh_openid       = userInfo.openid,
                    province         = userInfo.province,
                    sex      = userInfo.sex,
                    add_time = DateTime.Now,
                    unionid  = userInfo.unionid,
                    state    = (int)WXUserState.已关注,                 //已关注
                    user     = user,
                };
                wx_userService.AddEntity(newUser);
            }
            var responseMessage = base.CreateResponseMessage <ResponseMessageText>();

            responseMessage.Content = userInfo.nickname + ",欢迎您关注我们~";
            return(responseMessage);
        }
Пример #2
0
        /// <summary>
        /// 在调用操作方法前调用。
        /// </summary>
        /// <param name="filterContext">有关当前请求和操作的信息。</param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            IApplicationContext ctx            = ContextRegistry.GetContext();
            Iwx_userService     wx_userService = ctx.GetObject("wx_userService") as Iwx_userService;

            if (UserInfo == null)
            {
                string tokenStr = RequestParams["token"];

                if (string.IsNullOrEmpty(tokenStr))
                {
                    filterContext.Result = Json(SysEnum.未授权登录, "缺少token");
                    return;
                }
                string          openid;
                string          userip;
                RequestCategory RequestCategory;
                try
                {
                    string[] arr = Common.EncryptHelper.Decrypt(tokenStr).Split('|');
                    openid          = arr[0];
                    userip          = arr[1];
                    RequestCategory = (RequestCategory)Enum.Parse(typeof(RequestCategory), arr[2]);
                }
                catch (Exception)
                {
                    filterContext.Result = Json(SysEnum.未授权登录, "token错误");
                    return;
                }
                if (string.IsNullOrEmpty(openid) || string.IsNullOrEmpty(userip))
                {
                    filterContext.Result = Json(SysEnum.未授权登录, "token错误");
                    return;
                }
                //if (!userip.Equals(Request.UserHostAddress))
                //{
                //    filterContext.Result = Json(SysEnum.IP不匹配, "IP地址发生变化,请重新登录");
                //    return;
                //}

                switch (RequestCategory)
                {
                case RequestCategory.微信公众号:
                    UserInfo = wx_userService.LoadEntities(s => s.gzh_openid == openid).FirstOrDefault();
                    break;

                case RequestCategory.微信小程序:
                    UserInfo = wx_userService.LoadEntities(s => s.xcx_openid == openid).FirstOrDefault();
                    break;

                //case RequestCategory.APP:
                //	UserInfo = wx_userService.LoadEntities(s => s.appopenid == openid).FirstOrDefault();
                //	break;
                //case RequestCategory.WEB:
                //	UserInfo = wx_userService.LoadEntities(s => s.webopenid == openid).FirstOrDefault();
                //	break;
                default:
                    filterContext.Result = Json(SysEnum.未授权登录, "token错误");
                    return;
                }

                if (UserInfo == null)
                {
                    filterContext.Result = Json(SysEnum.未授权登录);
                    return;
                }
                else
                {
                    if (UserInfo.state != (int)WXUserState.已关注)                    //数据库中状态未关注
                    {
                        try
                        {
                            var user = Senparc.Weixin.MP.AdvancedAPIs.UserApi.Info(AppId, UserInfo.gzh_openid, Senparc.Weixin.Language.zh_CN); //微信获取用户信息 该接口每日可调用 50000000 次 保险起见用try包裹
                            if (user.subscribe == (int)WXUserState.已关注)                                                                        //是否已关注
                            {
                                UserInfo.wx_head_protrait = user.headimgurl;
                                UserInfo.sex        = user.sex;
                                UserInfo.city       = user.city;
                                UserInfo.country    = user.country;
                                UserInfo.state      = (int)WXUserState.已关注;
                                UserInfo.unionid    = user.unionid;
                                UserInfo.gzh_openid = user.openid;
                                UserInfo.city       = user.city;
                                UserInfo.nickname   = user.nickname;
                                wx_userService.EditEntity(UserInfo);                                //已关注 修改用户状态
                            }
                        }
                        catch (Exception)
                        {
                        }
                    }
                }


                //只允许微信里访问
                //String userAgent = Request.UserAgent;
                //if (userAgent != null && userAgent.IndexOf("MicroMessenger", StringComparison.Ordinal) <= -1)
                //{
                //    HttpContext.Response.Write("请在微信浏览器里访问");
                //    HttpContext.Response.End();

                //}
            }
        }