APP VO 对象
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class NearUserVo {
private Long userId;
private String avatar;
private String nickname;
}
===========================================================
APP 端 显示定位距离 controller
/**
* 搜附近
*
* @param gender
* @param distance
* @return
*/
@GetMapping("search")
public ResponseEntity<List<NearUserVo>> queryNearUser(@RequestParam(value = "gender", required = false) String gender,
@RequestParam(value = "distance", defaultValue = "2000") String distance) {
try {
List<NearUserVo> list = this.tanHuaService.queryNearUser(gender, distance);
return ResponseEntity.ok(list);
} catch (Exception e) {
e.printStackTrace();
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
=======================================================
APP 端 显示定位距离 service
public List<NearUserVo> queryNearUser(String gender, String distance) {
//查询当前用户的位置
User user = UserThreadLocal.get();
UserLocationVo userLocationVo = this.userLocationApi.queryByUserId(user.getId());
if(ObjectUtil.isEmpty(userLocationVo)){
return ListUtil.empty();
}
PageInfo<UserLocationVo> pageInfo = this.userLocationApi.queryUserFromLocation(userLocationVo.getLongitude(),
userLocationVo.getLatitude(),
Convert.toDouble(distance),
1,
50
);
List<UserLocationVo> records = pageInfo.getRecords();
if(CollUtil.isEmpty(records)){
return ListUtil.empty();
}
//构造筛选条件
List<Object> userIdList = CollUtil.getFieldValues(records, "userId");
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.in("user_id", userIdList);
if(StrUtil.equalsIgnoreCase(gender, "man")){
queryWrapper.eq("sex", SexEnum.MAN);
}else if(StrUtil.equalsIgnoreCase(gender, "woman")){
queryWrapper.eq("sex", SexEnum.WOMAN);
}
List<UserInfo> userInfoList = this.userInfoService.queryUserInfoList(queryWrapper);
List<NearUserVo> result = new ArrayList<>();
for (UserLocationVo locationVo : records) {
//排除自己
if(ObjectUtil.equals(locationVo.getUserId(), user.getId())){
continue;
}
for (UserInfo userInfo : userInfoList) {
if(ObjectUtil.equals(locationVo.getUserId(), userInfo.getUserId())){
NearUserVo nearUserVo = new NearUserVo();
nearUserVo.setUserId(userInfo.getUserId());
nearUserVo.setAvatar(userInfo.getLogo());
nearUserVo.setNickname(userInfo.getNickName());
result.add(nearUserVo);
break;
}
}
}
return result;
}