Esempio n. 1
0
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="loginInfo">用户登录信息</param>
        /// <param name="token">用户token</param>
        /// <returns></returns>
        public WxLoginResponseInfo Login(LoginInfo loginInfo, out string token)
        {
            WxLoginResponseInfo result = null;

            try
            {
                ////检查手机号是否注册过
                //if (!userDbDal.IsCustomerNameExists(loginInfo.CustomerName, out userId))
                //{
                //    ThrowResponseContextException(ErrCode.AccountNotExist);
                //}

                //验证登录
                string weiXinSessionKeyUrl           = ConfigurationManager.AppSettings["WeiXinSessionKeyUrl"];
                string weiXinAppId                   = ConfigurationManager.AppSettings["WeiXinAppId"];
                string weiXinSecret                  = ConfigurationManager.AppSettings["WeiXinSecret"];
                Dictionary <string, string> paramDic = new Dictionary <string, string>();
                paramDic.Add("appid", weiXinAppId);
                paramDic.Add("secret", weiXinSecret);
                paramDic.Add("js_code", loginInfo.code);
                paramDic.Add("grant_type", "authorization_code");
                var response = HttpHelper.Get(weiXinSessionKeyUrl, paramDic);
                if (string.IsNullOrEmpty(response) || response.Contains("errcode"))
                {
                    ThrowResponseContextException(ErrCode.TokenPastDue);
                }
                WxLoginInfo        info     = JsonHelper.JsonToObject <WxLoginInfo>(response);
                TokenOpearteResult tokenRes = TokenSrv.GetWXToken(info);
                if (!tokenRes.isok)
                {
                    token = string.Empty;
                    ThrowResponseContextException(ErrCode.TokenPastDue);
                }
                else
                {
                    token  = tokenRes.token;
                    result = new WxLoginResponseInfo
                    {
                        thrdsession = tokenRes.token
                    };

                    Task.Run(() => AddWxUserInfo(loginInfo, info.openid));
                }
            }
            catch (Exception ex)
            {
                LogHelper.Exception(ex);
                throw;
            }
            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="token">token</param>
        /// <returns></returns>
        public TokenOpearteResult TokenLogOff(string token)
        {
            var    customerId   = RedisEntity.HashGet(LoginCustomerListKey, token);
            string userHashId   = string.Format(RedisKeyConstants.CustomerHashId, customerId);
            string customerName = RedisEntity.HashGet(userHashId, nameof(CustomerDetail.CustomerName));

            if (!string.IsNullOrEmpty(customerName))
            {
                RedisEntity.HashRemove(LoginCustomerNameListKey, customerName);
            }
            RedisEntity.HashRemove(LoginCustomerListKey, token);
            TokenOpearteResult result = new TokenOpearteResult
            {
                isok   = true,
                result = "退出成功"
            };

            return(result);
        }
Esempio n. 3
0
        /// <summary>
        /// 获取新的用户token
        /// </summary>
        /// <param name="val">用户信息</param>
        /// <returns></returns>
        public TokenOpearteResult GetNewToken(CustomerInfo val)
        {
            string customerName = val.CustomerName;
            var    oldtoken     = RedisEntity.HashGet(LoginCustomerNameListKey, customerName);

            //被挤掉的用户token
            if (!string.IsNullOrEmpty(oldtoken))
            {
                var customerId = RedisEntity.HashGet(LoginCustomerListKey, oldtoken);
                CustomerRedisDal cusRedisDal  = new CustomerRedisDal();
                CustomerDetail   oldLoginInfo = cusRedisDal.GetCustomerDetail(long.Parse(customerId));
                //不是同一台设备
                if (oldLoginInfo != null)
                {
                    //添加到被挤掉的用户
                    string crowKey = CrowdedTokenKey + ":" + oldtoken;
                    RedisEntity.ItemSet <string>(crowKey, customerName);
                    RedisEntity.ItemSetExpire(crowKey, DateTime.Now.AddDays(1));
                }
                RedisEntity.HashRemove(LoginCustomerNameListKey, customerName);
                RedisEntity.HashRemove(LoginCustomerListKey, oldtoken);
                //记录删除的token列表
                LogHelper.Debug("移除旧的token:" + oldtoken + "|" + customerName);
            }
            string token = Guid.NewGuid().ToString();

            RedisEntity.HashSet(LoginCustomerListKey, token, val.CustomerId.ToString());
            RedisEntity.HashSet(LoginCustomerNameListKey, customerName, token);
            TokenOpearteResult result = new TokenOpearteResult
            {
                isok  = true,
                token = token
            };

            return(result);
        }
Esempio n. 4
0
        /// <summary>
        /// 获取新的用户token
        /// </summary>
        /// <param name="val">用户信息</param>
        /// <returns></returns>
        public TokenOpearteResult GetWXToken(WxLoginInfo val)
        {
            TimeSpan Expire = TimeSpan.FromDays(3);
            //string customerName = val.CustomerName;
            //var oldtoken = RedisEntity.HashGet(LoginCustomerNameListKey, customerName);
            ////被挤掉的用户token
            //if (!string.IsNullOrEmpty(oldtoken))
            //{
            //    var customerId = RedisEntity.HashGet(LoginCustomerListKey, oldtoken);
            //    CustomerRedisDal cusRedisDal = new CustomerRedisDal();
            //    CustomerDetail oldLoginInfo = cusRedisDal.GetCustomerDetail(long.Parse(customerId));
            //    //不是同一台设备
            //    if (oldLoginInfo != null)
            //    {
            //        //添加到被挤掉的用户
            //        string crowKey = CrowdedTokenKey + ":" + oldtoken;
            //        RedisEntity.ItemSet<string>(crowKey, customerName);
            //        RedisEntity.ItemSetExpire(crowKey, DateTime.Now.AddDays(1));
            //    }
            //    RedisEntity.HashRemove(LoginCustomerNameListKey, customerName);
            //    RedisEntity.HashRemove(LoginCustomerListKey, oldtoken);
            //    //记录删除的token列表
            //    LogHelper.Debug("移除旧的token:" + oldtoken + "|" + customerName);
            //}
            string token = Guid.NewGuid().ToString();

            RedisEntity.HashSet(WXLoginListKey, token, val.openid + ";" + val.session_key);
            RedisEntity.HashSetExpire(token, Expire);
            TokenOpearteResult result = new TokenOpearteResult
            {
                isok  = true,
                token = token
            };

            return(result);
        }