Exemplo n.º 1
0
        // GET api/<controller>
        /// <summary>
        /// 线下客户端获取微信登录二维码
        /// </summary>
        /// <returns></returns>
        public JsonResult <string> Get(string lockCode)
        {
            JsonResult <string> result = new JsonResult <string>();

            result.code = 0;
            result.msg  = "OK";
            string uuid      = Utils.GetWeChatUUID();
            string long_url  = WeChateSiteHelper.getCRContent(uuid);
            string cqContent = WeChatAccessTokenAPI.GetShortURL(long_url);

            if (string.IsNullOrEmpty(cqContent))
            {
                cqContent = long_url;
            }
            string fileName = string.Format("{0}.png", uuid);
            string filePath = FileHelper.GetPicFilePath(fileName);

            if (QrCodeHelper.CreateImgCode(cqContent, filePath))
            {
                result.code      = 1;
                result.Result    = FileHelper.GetPicFileURL(fileName);
                result.ResultMsg = uuid;

                ThreadPool.QueueUserWorkItem(new WaitCallback(p => {
                    //图片记录进缓存,定期清理
                    string key = CacheKey.GetQrCodeKey(DateTime.Now);
                    RedisBase.List_Add <string>(key, filePath);
                    RedisBase.List_SetExpire(key, DateTime.Now.AddDays(2));

                    //记录日志
                    WeChatLogin login = new WeChatLogin();
                    login.State       = 0;
                    login.UUID        = uuid;
                    login.LoginData   = DateTime.Now.ToString("yyyy-MM-dd");
                    login.CreateTime  = DateTime.Now;
                    login.LockCode    = lockCode;
                    SaveWeChatLogin(login);
                }), null);
            }


            return(result);
        }
Exemplo n.º 2
0
        public ActionResult jssignature()
        {
            string   url = Request["url"].TryToString();
            TimeSpan ts  = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);

            string timestamp = Convert.ToInt64(ts.TotalSeconds).ToString();
            //随机数
            var noncestr = url.MD5() + Utils.GetRandom(10000, 99999);

            var jsapi_ticket = WeChatAccessTokenAPI.Getjsapi_ticket();

            ClassLoger.Info("jsapi_ticket", jsapi_ticket);
            ClassLoger.Info("url", url);
            if (url.IsNull())
            {
                url = Request.Url.ToString();
            }
            string data      = string.Format("jsapi_ticket={0}&noncestr={1}&timestamp={2}&url={3}", jsapi_ticket, noncestr, timestamp, url);
            string signature = Utils.SHA1(data);

            return(Json(new { timestamp = timestamp, noncestr = noncestr, appid = SystemSet.Serviceappid, signature = signature }, JsonRequestBehavior.AllowGet));
        }
        public async Task <IHttpActionResult> BindingWeChartCQ()
        {
            JsonResult <string> result = new JsonResult <string>();

            result.code = 0;
            result.msg  = "OK";
            await Task.Run(() => {
                string uuid      = Utils.GetWeChatUUID();
                string long_url  = WeChateSiteHelper.getCRContent(uuid);
                string cqContent = WeChatAccessTokenAPI.GetShortURL(long_url);
                if (string.IsNullOrEmpty(cqContent))
                {
                    cqContent = long_url;
                }
                string fileName = string.Format("{0}.png", uuid);
                string filePath = FileHelper.GetPicFilePath(fileName);
                if (QrCodeHelper.CreateImgCode(cqContent, filePath))
                {
                    result.code      = 1;
                    result.Result    = FileHelper.GetPicFileURL(fileName);
                    result.ResultMsg = uuid;

                    //图片记录进缓存,定期清理
                    string key = CacheKey.GetQrCodeKey(DateTime.Now);
                    RedisBase.List_Add <string>(key, filePath);
                    RedisBase.List_SetExpire(key, DateTime.Now.AddDays(2));

                    ApiUserManager userManager = new ApiUserManager(ActionContext);
                    string bindkey             = string.Format("bind_{0}", uuid);
                    RedisBase.Item_Set(bindkey, userManager.User);
                    RedisBase.ExpireEntryAt(bindkey, DateTime.Now.AddHours(1));
                }
            });

            return(Ok(result));
        }
        /// <summary>
        /// 用户确认授权
        /// </summary>
        /// <param name="code"></param>
        /// <param name="state"></param>
        private void getUserinfo(string code, string state)
        {
            try
            {
                //网络授权有有效期,最好用redis缓存
                //获取用户网络授权

                string access_token = string.Empty;
                string openid       = string.Empty;
                if (RedisBase.ContainsKey(code))
                {
                    Access_tokenResult accResult = RedisBase.Item_Get <Access_tokenResult>(code);
                    access_token = accResult.access_token;
                    openid       = accResult.openid;
                }
                else
                {
                    Access_tokenResult accResult = WeChatAccessTokenAPI.GetWeChatAccess_token(code);
                    RedisBase.Item_Set(code, accResult);
                    RedisBase.ExpireEntryAt(code, DateTime.Now.AddSeconds(accResult.expires_in));
                    access_token = accResult.access_token;
                    openid       = accResult.openid;
                }

                //获取用户信息
                WeChatUserInfo userinfo = null;
                string         userkey  = getWeChatUserKey(access_token, openid);
                if (RedisBase.ContainsKey(userkey))
                {
                    userinfo = RedisBase.Item_Get <WeChatUserInfo>(userkey);
                }
                else
                {
                    userinfo = WeChatUserInfoAPI.GetWeChatUserInfo(access_token, openid);
                    if (userinfo != null)
                    {
                        RedisBase.Item_Set(userkey, userinfo);
                        RedisBase.ExpireEntryAt(userkey, DateTime.Now.AddDays(2));
                    }
                }
                WeChatUser wuser = userbll.GetWeChatUserByUnionID(userinfo.unionid);
                if (wuser == null)
                {
                    wuser = new WeChatUser();
                    wuser.PlatformOpenID = userinfo.openid;
                    wuser.UnionID        = userinfo.unionid;
                    userbll.AddWeChatUser(wuser);
                }
                else
                {
                    if (string.IsNullOrEmpty(wuser.PlatformOpenID))
                    {
                        wuser.PlatformOpenID = userinfo.openid;
                        wuser.UnionID        = userinfo.unionid;
                        userbll.UpdateWeChatUser(wuser);
                    }
                }
                UserInfoBll ubll  = new UserInfoBll();
                UserInfo    _user = ubll.GetUserInfoByOpenID(userinfo.unionid);
                // 用户绑定微信
                string bindkey = string.Format("bind_{0}", state);
                string msg     = MqttAgreement.GetWeChatLoginMA(state, true);
                if (RedisBase.ContainsKey(bindkey))
                {
                    //用户之前已经关注过微信公共号,需要把之前微信公共号账户中的信息更新到这个账户中
                    if (_user != null)
                    {
                        _user.Openid = "";
                        ubll.UpdateUserinfo(_user);
                    }
                    UserInfo binduser = RedisBase.Item_Get <UserInfo>(bindkey);
                    binduser.Openid = userinfo.unionid;
                    ubll.UpdateUserinfo(binduser);
                }
                else
                {
                    WeChatLogin login = loginbll.GetWeChatLoginByUUID(state);
                    if (login == null)
                    {
                        login            = new WeChatLogin();
                        login.UUID       = state;
                        login.CreateTime = DateTime.Now;
                        login.LoginData  = DateTime.Now.ToString("yyyy-MM-dd");
                    }
                    login.Headimgurl = userinfo.headimgurl;
                    login.Nickname   = userinfo.nickname;
                    login.Openid     = userinfo.unionid;
                    login.Sex        = userinfo.sex.TryToString();
                    login.State      = 1;
                    login.LoginData  = DateTime.Now.ToString("yyyy-MM-dd");
                    login.CreateTime = DateTime.Now;
                    loginbll.UpdateWeChatLogin(login);

                    if (_user == null)
                    {
                        _user            = new UserInfo();
                        _user.Openid     = userinfo.unionid;
                        _user.CreateTime = DateTime.Now;
                        _user.Headimgurl = userinfo.headimgurl;
                        _user.Nickname   = userinfo.nickname;
                        _user.Sex        = (SexEnum)userinfo.sex;
                        _user.Name       = userinfo.nickname;
                        _user.city       = userinfo.city;
                        _user.province   = userinfo.province;
                        ubll.UpdateUserinfo(_user);
                    }
                    //向客户端推送消息
                    MqttPublishClient.Ins.PublishOneClient(login.LockCode, msg);
                }
                MqttPublishClient.Ins.PublishAllClient(msg);
            }
            catch (Exception ex)
            {
                ClassLoger.Error("WeChatLoginCallBackController.getUserinfo", ex);
            }
        }
Exemplo n.º 5
0
        public ActionResult CallBackRedirect()
        {
            //网络授权有有效期,最好用redis缓存
            //获取用户网络授权
            string code  = Request["code"].TryToString();
            string state = Request["state"].TryToString();

            ClassLoger.Info("WeixinRedirectController.CallBackRedirect", state, code);
            string access_token = string.Empty;
            string openid       = string.Empty;

            if (RedisBase.ContainsKey(code))
            {
                Access_tokenResult accResult = RedisBase.Item_Get <Access_tokenResult>(code);
                access_token = accResult.access_token;
                openid       = accResult.openid;
            }
            else
            {
                Access_tokenResult accResult = WeChatAccessTokenAPI.GetWeChatServiceAccess_token(code);
                RedisBase.Item_Set(code, accResult);
                RedisBase.ExpireEntryAt(code, DateTime.Now.AddSeconds(accResult.expires_in));
                access_token = accResult.access_token;
                openid       = accResult.openid;
            }

            //获取用户信息
            WeChatUserInfo userinfo = null;
            string         userkey  = getWeChatUserKey(access_token, openid);

            if (RedisBase.ContainsKey(userkey))
            {
                userinfo = RedisBase.Item_Get <WeChatUserInfo>(userkey);
            }
            else
            {
                userinfo = WeChatUserInfoAPI.GetWeChatUserInfo(access_token, openid);
                if (userinfo != null)
                {
                    RedisBase.Item_Set(userkey, userinfo);
                    RedisBase.ExpireEntryAt(userkey, DateTime.Now.AddDays(2));
                }
            }
            UserInfo _user = userbll.GetUserInfoByOpenID(userinfo.unionid);

            if (_user == null)
            {
                _user            = new UserInfo();
                _user.Openid     = userinfo.unionid;
                _user.CreateTime = DateTime.Now;
                _user.Headimgurl = userinfo.headimgurl;
                _user.Nickname   = userinfo.nickname;
                _user.Sex        = (SexEnum)userinfo.sex;
                _user.Name       = userinfo.nickname;
                _user.city       = userinfo.city;
                _user.province   = userinfo.province;
                userbll.UpdateUserinfo(_user);
            }
            ClassLoger.Info("CallBackRedirect", userkey);
            RedisSession <UserInfo> redissession = new Models.RedisSession <UserInfo>(HttpContext, true, 120);

            redissession["UserCode"] = _user;
            switch (state)
            {
            //普通用户个人中心
            case "UserCore":
                return(RedirectToAction("Index", "UserCore"));

            case "AdminCore":
            //return RedirectToAction("Index", "AdminCore");
            default:
                string url = HttpUtils.Ins.UrlDecode(state);
                if (RedisBase.ContainsKey(state))
                {
                    url = RedisBase.Item_Get <string>(state);
                }
                return(Redirect(url));
            }
        }