Ejemplo n.º 1
0
 public static Project SaveWechat_UserCurrentProjectID(string OpenId, int ProjectID)
 {
     using (SqlHelper helper = new SqlHelper())
     {
         try
         {
             helper.BeginTransaction();
             var data = GetWechat_UserByUserOpenid(OpenId, helper);
             if (data == null)
             {
                 data = new Wechat_User();
                 data.OpenId = OpenId;
                 data.SubscribeTime = DateTime.Now;
                 data.SubScribe = 1;
             }
             data.CurrentProjectID = ProjectID;
             var project = Project.GetProject(ProjectID, helper);
             data.Save(helper);
             helper.Commit();
             return project;
         }
         catch (Exception)
         {
             helper.Rollback();
             return null;
         }
     }
 }
Ejemplo n.º 2
0
        private Wechat_User CreateWechatUser(HttpContext context, out string result)
        {
            result = string.Empty;
            string openID             = context.Request.Form["OpenID"];
            string city               = context.Request.Form["City"];
            string country            = context.Request.Form["Country"];
            string nickName           = context.Request.Form["NickName"];
            string headImgUrl         = context.Request.Form["HeadImgUrl"];
            string province           = context.Request.Form["Province"];
            string language           = context.Request.Form["Language"];
            string sex                = context.Request.Form["Sex"];
            string subScribe          = context.Request.Form["SubScribe"];
            string subscribeTimeStamp = context.Request.Form["SubscribeTime"];
            //todo:绑定微信粉丝来源
            string qrScene = context.Request.Form["QrScene"];
            string ticket  = context.Request.Form["Ticket"];


            DateTime subscribeTime = DateTime.Now;

            if (!string.IsNullOrEmpty(subscribeTimeStamp))
            {
                subscribeTime = new DateTime(1970, 1, 1).AddSeconds(Convert.ToInt32(subscribeTimeStamp));
            }

            if (string.IsNullOrEmpty(openID))
            {
                LogHelper.WriteError(LogModule, "添加微信用户失败。缺少参数:OpenID", null);
                return(null);
            }
            if (string.IsNullOrEmpty(nickName))
            {
                LogHelper.WriteError(LogModule, "添加微信用户失败。缺少参数:NickName", null);
                return(null);
            }


            var user = Wechat_User.GetWechat_UserByUserOpenID(openID);

            if (user == null)
            {
                user = new Foresight.DataAccess.Wechat_User()
                {
                    OpenId = openID,
                };
            }
            if (!string.IsNullOrEmpty(qrScene))
            {
                int qrSceneID = 0;
                int.TryParse(qrScene, out qrSceneID);
                if (qrSceneID > 0)
                {
                    var scene = Wechat_QrScene.GetWechat_QrScene(qrSceneID);
                    if (scene != null)
                    {
                        if (user.FromQrID <= 0)
                        {
                            user.FromQrID = scene.ID;
                        }
                        result = SaveWechatProject(openID, scene.ProjectID);
                    }
                }
            }
            user.SubScribe = int.Parse(subScribe);
            if (user.SubScribe != 0)
            {
                user.City            = city;
                user.Country         = country;
                user.NickName        = nickName;
                user.HeadImgUrl      = headImgUrl;
                user.Province        = province;
                user.Sex             = int.Parse(sex);
                user.SubscribeTime   = subscribeTime;
                user.UnSubscribeTime = DateTime.MinValue;
                user.Language        = language;
            }
            else
            {
                if (user.UnSubscribeTime == DateTime.MinValue)
                {
                    user.UnSubscribeTime = DateTime.Now;
                }
            }
            if (user.FirstSubScribeTime == DateTime.MinValue)
            {
                user.FirstSubScribeTime = user.SubscribeTime;
            }
            user.Save();

            return(user);
        }
Ejemplo n.º 3
0
        private void getuserinfo(HttpContext context)
        {
            string OpenID = GetOpenID(context);

            if (string.IsNullOrEmpty(OpenID))
            {
                WebUtil.WriteJsonError(context, ErrorCode.InvalideRequest, "获取用户OpenID失败");
                return;
            }
            ErrInfo err         = new ErrInfo();
            string  accesstoken = WeixinHelper.GetAccessToken(null);
            var     wechatuser  = WeixinHelper.GetUserInfo(OpenID, accesstoken, out err);

            if (wechatuser == null)
            {
                if (err.ErrCode == 40001)//invalid credential
                {
                    accesstoken = WeixinHelper.GetAccessToken(accesstoken);
                    wechatuser  = WeixinHelper.GetUserInfo(OpenID, accesstoken, out err);
                }
            }
            if (wechatuser == null)
            {
                WebUtil.WriteJsonError(context, ErrorCode.InvalideRequest, err.ErrMsg);
                return;
            }
            Foresight.DataAccess.Wechat_User user = Foresight.DataAccess.Wechat_User.GetWechat_UserByUserOpenID(OpenID);
            if (user == null)
            {
                user = new Foresight.DataAccess.Wechat_User()
                {
                    OpenId = OpenID,
                };
            }
            if (!string.IsNullOrEmpty(wechatuser.NickName))
            {
                user.NickName = wechatuser.NickName;
            }

            if (!string.IsNullOrEmpty(wechatuser.HeadImgUrl))
            {
                user.HeadImgUrl = wechatuser.HeadImgUrl.Replace("\\", "");
            }

            if (!string.IsNullOrEmpty(wechatuser.Sex))
            {
                user.Sex = int.Parse(wechatuser.Sex);
            }

            if (!string.IsNullOrEmpty(wechatuser.City))
            {
                user.City = wechatuser.City;
            }

            if (!string.IsNullOrEmpty(wechatuser.Country))
            {
                user.Country = wechatuser.Country;
            }

            if (!string.IsNullOrEmpty(wechatuser.Province))
            {
                user.Province = wechatuser.Province;
            }

            if (!string.IsNullOrEmpty(wechatuser.Language))
            {
                user.Language = wechatuser.Language;
            }

            if (!string.IsNullOrEmpty(wechatuser.SubScribe))
            {
                user.SubScribe = int.Parse(wechatuser.SubScribe);
            }

            if (!string.IsNullOrEmpty(wechatuser.SubscribeTime))
            {
                user.SubscribeTime = ConvertDate(wechatuser.SubscribeTime);
            }
            if (user.SubScribe != 0)
            {
                user.UnSubscribeTime = DateTime.MinValue;
            }
            else
            {
                if (user.UnSubscribeTime == DateTime.MinValue)
                {
                    user.UnSubscribeTime = DateTime.Now;
                }
            }
            if (user.FirstSubScribeTime == DateTime.MinValue)
            {
                user.FirstSubScribeTime = user.SubscribeTime;
            }
            user.Save();
            WebUtil.WriteJsonResult(context, new { status = true, HeadImgUrl = user.HeadImgUrl, NickName = user.NickName });
        }