protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.tdCustomerAccount.Value))
            {
                this.tdResponse.Text = "客服帐号不能为空";
                return;
            }
            if (string.IsNullOrEmpty(this.tdOpenID.Value))
            {
                this.tdResponse.Text = "用户OpenID不能为空";
                return;
            }
            var result = MessageHandler.CreateCustomerService(WeixinHelper.GetAccessToken(null), this.tdCustomerAccount.Value, this.tdOpenID.Value);

            this.tdResponse.Text = result.Body;
        }
Example #2
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 });
        }