コード例 #1
0
        public User GetCurrentUser()
        {
            //从url参数中获取用户openid
            var current_user_openid = Request["openid"];

            //从session中获取用户openid
            if (current_user_openid == null)
            {
                var current_user_openid_session = System.Web.HttpContext.Current.Session["openid"];
                if (current_user_openid_session != null)
                {
                    current_user_openid = current_user_openid_session.ToString();
                }
            }
            else
            {
                System.Web.HttpContext.Current.Session["openid"] = current_user_openid;
            }

            //重新从微信中获取用户openid
            if (current_user_openid == null)
            {
                WXInterface.ReGetOpenId();
                var current_user_openid_session = System.Web.HttpContext.Current.Session["openid"];
                if (current_user_openid_session != null)
                {
                    current_user_openid = current_user_openid_session.ToString();
                }
            }
            if (current_user_openid == null)
            {
                return(null);
                //   return Content("无法获取用户信息,请确认微信客户端打开链接");
            }
            //if (user_openids != null)
            //{
            //    string content = current_user_openid + "<br /><br />";
            //    foreach (var id in user_openids)
            //    {
            //        content += id + "<br />";
            //    }
            //    return Content(content);
            //}
            //else
            //{

            var user = HttpHelper.ConvertJson <User>("GET", Config.ApiServer + "/users/" + current_user_openid.ToString(), "");

            if (user != null)
            {
                System.Web.HttpContext.Current.Session["current_user"] = user;
            }
            //return Content(Newtonsoft.Json.JsonConvert.SerializeObject(user));
            return(user);
        }
コード例 #2
0
        public ActionResult Index()
        {
            var token = WXInterface.GetAccessToken(Config.AppID, Config.AppSecret).access_token;

            string        state        = Request["state"];
            List <string> user_openids = null;


            var current_user_openid = System.Web.HttpContext.Current.Session["openid"];

            if (state == "1" && current_user_openid == null)
            {
                user_openids = WXInterface.GetUserList(token);
            }
            WXInterface.ReGetOpenId();
            current_user_openid = System.Web.HttpContext.Current.Session["openid"];
            if (current_user_openid == null)
            {
                return(Content("无法获取用户信息,请确认微信客户端打开链接"));
            }
            if (user_openids != null)
            {
                string content = current_user_openid + "<br /><br />";
                foreach (var id in user_openids)
                {
                    content += id + "<br />";
                }
                return(Content(content));
            }
            else
            {
                if (current_user_openid == null)
                {
                    return(View());
                }
                var user = HttpHelper.ConvertJson <User>("GET", Config.ApiServer + "/users/" + current_user_openid.ToString(), "");
                if (user == null)
                {
                    HttpHelper.Post(Config.ApiServer + "/users/", "wxopenid=" + current_user_openid.ToString());
                    user = HttpHelper.ConvertJson <User>(Config.ApiServer + "/users/" + current_user_openid.ToString(), "");
                }
                return(Content(Newtonsoft.Json.JsonConvert.SerializeObject(user)));
            }
        }