public JsonResult <UserInfo> Get(string uuid)
        {
            JsonResult <UserInfo> result = new JsonResult <UserInfo>();

            result.code = 0;
            result.msg  = "OK";
            WeChatLogin loginList = logBll.GetWeChatLoginByUUID(uuid);

            if (loginList != null)
            {
                UserInfo userinfo = userinfoBll.GetUserInfoByOpenID(loginList.Openid);
                if (userinfo == null)
                {
                    userinfo            = new UserInfo();
                    userinfo.Headimgurl = loginList.Headimgurl;
                    userinfo.Nickname   = loginList.Nickname;
                    userinfo.Openid     = loginList.Openid;
                    userinfo.Sex        = (SexEnum)loginList.Sex.TryToInt(0);
                    //userinfo.UserName = loginList.Nickname;
                    userinfo.ID = userinfoBll.UpdateUserinfo(userinfo);
                }
                ApiUserManager userManager = new ApiUserManager(ActionContext);
                if (userinfo.UserName.IsNull())
                {
                    userinfo.UserName = userinfo.Nickname;
                }
                result.code      = 1;
                result.Result    = userinfo;
                result.ResultMsg = userManager.GetUserToken(userinfo);
            }
            return(result);
        }
Example #2
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);
        }
        public bool UpdateWeChatLogin(WeChatLogin _weChatLogin)
        {
            bool flag = true;

            try
            {
                ISession     session     = NHibernateSessionFactory.getSession();
                ITransaction transaction = session.BeginTransaction();
                session.SaveOrUpdate(_weChatLogin);
                transaction.Commit();
                session.Close();
            }
            catch (Exception ex)
            {
                flag = false;
                ClassLoger.Error("WeChatLoginDal.AddWeChatLogin", ex);
            }
            return(flag);
        }
        /// <summary>
        /// 用户拒绝授权给客户端发送通知
        /// </summary>
        /// <param name="state"></param>
        private void SaveNoUserinfo(string state)
        {
            try
            {
                WeChatLogin login = loginbll.GetWeChatLoginByUUID(state);
                login.State      = -1;
                login.UUID       = state;
                login.LoginData  = DateTime.Now.ToString("yyyy-MM-dd");
                login.CreateTime = DateTime.Now;
                loginbll.UpdateWeChatLogin(login);

                //向客户端推送消息
                string msg = MqttAgreement.GetWeChatLoginMA(state, false);
                MqttPublishClient.Ins.PublishOneClient(login.LockCode, msg);
                MqttPublishClient.Ins.PublishAllClient(msg);
            }
            catch (Exception ex)
            {
                ClassLoger.Error("WeChatLoginCallBackController.getUserinfo", ex);
            }
        }
        /// <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);
            }
        }
Example #6
0
 private async void SaveWeChatLogin(WeChatLogin login)
 {
     await loginbll.AddWeChatLogin(login);
 }
 public async Task <bool> UpdateWeChatLogin(WeChatLogin _weChatLogin)
 {
     return(WeChatLoginDal.Ins.UpdateWeChatLogin(_weChatLogin));
 }
 public async Task <bool> AddWeChatLogin(WeChatLogin _weChatLogin)
 {
     return(WeChatLoginDal.Ins.AddWeChatLogin(_weChatLogin));
 }