コード例 #1
0
ファイル: SetUserPos.cs プロジェクト: oisnull/hwl-api-old
        public override SetUserPosResponseBody ExecuteCore()
        {
            SetUserPosResponseBody res = new SetUserPosResponseBody();

            //{
            //    "UserId": 1,
            //    "UserPos": {
            //        "Country": "中国",
            //        "Province": "上海市",
            //        "City": "上海市",
            //        "District": "闵行区",
            //        "Details": "上海市闵行区浦申路靠近上海闵行区金色阳光世博幼儿园",
            //        "Latitude": 31.0732898712158,
            //        "Longitude": 121.507202148438
            //    }
            //}

            using (HWLEntities db = new HWLEntities())
            {
                //检测是否存在country province city district 没有就添加
                t_country country = db.t_country.Where(c => c.name == this.request.Country).FirstOrDefault();
                if (country == null)
                {
                    country = new t_country()
                    {
                        id   = 0,
                        name = this.request.Country,
                    };
                    db.t_country.Add(country);
                    db.SaveChanges();
                }

                t_province province = db.t_province.Where(p => p.name == this.request.Province).FirstOrDefault();
                if (province == null)
                {
                    province = new t_province()
                    {
                        id         = 0,
                        country_id = country.id,
                        name       = this.request.Province,
                    };
                    db.t_province.Add(province);
                    db.SaveChanges();
                }

                t_city city = db.t_city.Where(c => c.name == this.request.City).FirstOrDefault();
                if (city == null)
                {
                    city = new t_city()
                    {
                        id          = 0,
                        province_id = province.id,
                        name        = this.request.City,
                    };
                    db.t_city.Add(city);
                    db.SaveChanges();
                }

                t_district district = db.t_district.Where(p => p.name == this.request.District).FirstOrDefault();
                if (district == null)
                {
                    district = new t_district()
                    {
                        id      = 0,
                        city_id = city.id,
                        name    = this.request.District,
                    };
                    db.t_district.Add(district);
                    db.SaveChanges();
                }

                //检测用户是否已经存在当前位置信息(条件,用户id,位置id,位置详情)
                t_user_pos upos = db.t_user_pos.Where(u => u.user_id == this.request.UserId &&
                                                      u.country_id == country.id &&
                                                      u.province_id == province.id &&
                                                      u.city_id == city.id &&
                                                      u.district_id == district.id &&
                                                      u.pos_details == this.request.Details
                                                      ).FirstOrDefault();
                if (upos == null)
                {
                    //向用户位置表中加入数据
                    upos = new t_user_pos()
                    {
                        id          = 0,
                        create_date = DateTime.Now,
                        update_date = DateTime.Now,
                        geohash_key = Geohash.Encode(this.request.Latitude, this.request.Longitude),
                        lat         = this.request.Latitude,
                        lon         = this.request.Longitude,
                        pos_details = this.request.Details,
                        user_id     = this.request.UserId,
                        city_id     = city.id,
                        country_id  = country.id,
                        district_id = district.id,
                        province_id = province.id,
                    };
                    db.t_user_pos.Add(upos);
                }
                else
                {
                    upos.update_date = DateTime.Now;
                }

                db.SaveChanges();
                res.Status    = ResultStatus.Success;
                res.UserPosId = upos.id;

                //保存用户的位置到redis
                Redis.UserAction userAction = new Redis.UserAction();
                userAction.SavePos(upos.user_id, upos.lon, upos.lat);

                //获取当前位置附近的组
                Redis.GroupAction groupAction = new Redis.GroupAction();
                res.UserGroupGuid = groupAction.GetNearGroupGuid(upos.lon, upos.lat);
                if (string.IsNullOrEmpty(res.UserGroupGuid))
                {
                    //如果没有组数据,创建一个组
                    res.UserGroupGuid = groupAction.CreateNearGroupPos(upos.lon, upos.lat);
                    //将用户加入到组中
                    groupAction.SaveGroupUser(res.UserGroupGuid, upos.user_id);
                }
                else
                {
                    if (groupAction.ExistsInGroup(res.UserGroupGuid, upos.user_id))
                    {
                        //如果用户当前所在位置与上次所在的位置不一样,则需要将用户从上次的群组里面移出来
                        if (!string.IsNullOrEmpty(this.request.LastGroupGuid) && this.request.LastGroupGuid != res.UserGroupGuid)
                        {
                            groupAction.DeleteGroupUser(this.request.LastGroupGuid, upos.user_id);
                        }

                        return(res);
                    }
                    else
                    {
                        //将用户加入到组中
                        groupAction.SaveGroupUser(res.UserGroupGuid, upos.user_id);
                    }
                }

                //如果用户当前所在位置与上次所在的位置不一样,则需要将用户从上次的群组里面移出来
                if (!string.IsNullOrEmpty(this.request.LastGroupGuid) && this.request.LastGroupGuid != res.UserGroupGuid)
                {
                    groupAction.DeleteGroupUser(this.request.LastGroupGuid, upos.user_id);
                }

                //返回组用户列表
                List <int> userIds = groupAction.GetGroupUserIds(res.UserGroupGuid);
                if (userIds == null || userIds.Count <= 0)
                {
                    return(res);
                }

                res.GroupUserInfos = db.t_user.Where(u => userIds.Contains(u.id))
                                     .Select(u => new UserSecretInfo()
                {
                    UserId    = u.id,
                    UserName  = u.name,
                    UserImage = u.head_image,
                }).ToList();
            }

            return(res);
        }
コード例 #2
0
ファイル: UserLogin.cs プロジェクト: oisnull/hwl-api-old
        public override UserLoginResponseBody ExecuteCore()
        {
            UserLoginResponseBody res = new UserLoginResponseBody();

            using (HWLEntities db = new HWLEntities())
            {
                IQueryable <t_user> query = db.t_user;

                if (!string.IsNullOrEmpty(this.request.Mobile))
                {
                    query = query.Where(u => u.mobile == this.request.Mobile);
                }
                else
                {
                    query = query.Where(u => u.email == this.request.Email);
                }

                t_user user = query.FirstOrDefault();
                if (user == null)
                {
                    throw new Exception("用户不存在");
                }
                if (user.status != UserStatus.Normal)
                {
                    throw new Exception("用户被禁用");
                }
                if (user.password != this.request.Password)
                {
                    throw new Exception("密码错误");                                        //CommonCs.GetMd5Str32(this.request.Password)
                }
                Redis.UserAction userAction = new Redis.UserAction();
                ////获取用户之前是否已经登录过,如果登录过则需要发送消息通知用户在其它位置登录
                //string oldToken = userAction.GetUserToken(user.id);
                //if (!string.IsNullOrEmpty(oldToken))
                //{
                //    //AndroidChatMessage.SendLogoutMessage(user.id, oldToken, "您的帐号已经在其它位置登录,如果不是您本人操作,建议重新登录后立即更换密码!");
                //}

                //清除用户之前登录用过的TOKEN
                userAction.RemoveUserToken(user.id);
                string userToken = UserUtility.BuildToken(user.id);
                bool   succ      = userAction.SaveUserToken(user.id, userToken);
                if (!succ)
                {
                    throw new Exception("用户登录token生成失败");
                }

                //获取地址信息
                var pos = (from country in db.t_country
                           join province in db.t_province on country.id equals province.country_id
                           join city in db.t_city on province.id equals city.province_id
                           join dist in db.t_district on city.id equals dist.city_id
                           where country.id == user.register_country && province.id == user.register_province && city.id == user.register_city && dist.id == user.register_district
                           select new
                {
                    CountryName = country.name,
                    ProvinceName = province.name,
                    CityName = city.name,
                    DistName = dist.name,
                }).FirstOrDefault();

                List <int> posIdList = new List <int>()
                {
                    user.register_country,
                    user.register_province,
                    user.register_city,
                    user.register_district,
                };

                List <string> posList = new List <string>()
                {
                    pos != null?pos.CountryName:string.Empty,
                    pos != null?pos.ProvinceName:string.Empty,
                    pos != null?pos.CityName:string.Empty,
                    pos != null?pos.DistName:string.Empty,
                };

                //输出
                res.UserInfo = new Entity.Extends.UserBaseInfo()
                {
                    Id                = user.id,
                    Symbol            = user.symbol,
                    Email             = user.email,
                    Mobile            = user.mobile,
                    Name              = user.name,
                    Token             = userToken,
                    HeadImage         = user.head_image,
                    CircleBackImage   = user.circle_back_image,
                    UserSex           = user.sex,
                    LifeNotes         = user.life_notes,
                    RegisterPosIdList = posIdList,
                    RegisterPosList   = posList,
                    FriendCount       = db.t_user_friend.Where(f => f.user_id == user.id).Count(),
                    GroupCount        = db.t_group_user.Where(f => f.user_id == user.id).Count()
                };

                //new Redis.ImMessageAction().AddWelcomeImMessage(user.id,, pos != null ? pos.DistName : string.Empty);
            }

            return(res);
        }