| | |
| | | package com.stylefeng.guns.modular.system.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.mapper.EntityWrapper; |
| | | import com.baomidou.mybatisplus.service.impl.ServiceImpl; |
| | | import com.stylefeng.guns.core.util.DateUtil; |
| | | import com.stylefeng.guns.core.util.JwtTokenUtil; |
| | | import com.stylefeng.guns.core.util.ToolUtil; |
| | | import com.stylefeng.guns.modular.system.dao.AppUserMapper; |
| | | import com.stylefeng.guns.modular.system.dto.Host; |
| | | import com.stylefeng.guns.modular.system.dto.Medium; |
| | | import com.stylefeng.guns.modular.system.model.*; |
| | | import com.stylefeng.guns.modular.system.dto.AppUserQuery; |
| | | import com.stylefeng.guns.modular.system.dto.TeamQuery; |
| | | import com.stylefeng.guns.modular.system.model.AppUser; |
| | | import com.stylefeng.guns.modular.system.service.IAppUserService; |
| | | import com.stylefeng.guns.modular.system.service.ICollectionHouseResourceService; |
| | | import com.stylefeng.guns.modular.system.service.IHousingDemandService; |
| | | import com.stylefeng.guns.modular.system.service.IRegionService; |
| | | import com.stylefeng.guns.modular.system.util.RedisUtil; |
| | | import com.stylefeng.guns.modular.system.util.ResultUtil; |
| | | import com.stylefeng.guns.modular.system.util.UUIDUtil; |
| | | import com.stylefeng.guns.modular.system.util.weChat.WXCore; |
| | | import com.stylefeng.guns.modular.system.util.weChat.WeChatUtil; |
| | | import com.stylefeng.guns.modular.system.warpper.req.*; |
| | | import com.stylefeng.guns.modular.system.warpper.res.*; |
| | | import com.stylefeng.guns.modular.system.vo.InviteUserVO; |
| | | import com.stylefeng.guns.modular.system.vo.TeamDetailVO; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.StringUtils; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | import sun.rmi.runtime.Log; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @author zhibing.pu |
| | | * @Date 2023/11/7 11:07 |
| | | * <p> |
| | | * 用户表 服务实现类 |
| | | * </p> |
| | | * |
| | | * @author 无关风月 |
| | | * @since 2024-02-06 |
| | | */ |
| | | @Service |
| | | public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements IAppUserService { |
| | | |
| | | @Autowired |
| | | private WeChatUtil weChatUtil; |
| | | @Autowired |
| | | private RedisUtil redisUtil; |
| | | @Autowired |
| | | private IRegionService regionService; |
| | | @Autowired |
| | | private IAppUserService appUserService; |
| | | @Autowired |
| | | private HouseResourceService houseResourceService; |
| | | @Autowired |
| | | private CollectionHouseResourceServiceImpl collectionHouseResourceService; |
| | | |
| | | |
| | | |
| | | |
| | | private AppUserMapper appUserMapper; |
| | | /** |
| | | * 通过token获取用户信息 |
| | | * @return |
| | |
| | | String authorization = request.getHeader("Authorization"); |
| | | // todo 这里注释了一段代码 |
| | | // && authorization.contains("Bearer") |
| | | if(ToolUtil.isNotEmpty(authorization) && authorization.contains("Bearer") ){ |
| | | if(ToolUtil.isNotEmpty(authorization) && authorization.contains("Bearer ") ){ |
| | | String token = authorization.substring(7); |
| | | //通过token获取用户id |
| | | Integer appUserId = getAppUserIdFromToken(token); |
| | |
| | | return null; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 小程序登录 |
| | | * @return |
| | | */ |
| | | @Override |
| | | public ResultUtil<AppletLoginRes> appletLogin(RegisterAccountReq req) { |
| | | try { |
| | | //调用微信获取用户小程序openid |
| | | Map<String, Object> map = weChatUtil.code2Session(req.getJscode()); |
| | | if(null == map){ |
| | | return ResultUtil.error("获取微信身份信息失败"); |
| | | } |
| | | String openid = map.get("openid").toString(); |
| | | AppUser appUser = this.selectOne(new EntityWrapper<AppUser>() |
| | | .eq("wechat_openid", openid) |
| | | .eq("audit_status", 2) |
| | | .eq("status", 1)); |
| | | AppletLoginRes appletLoginRes = new AppletLoginRes(); |
| | | if(null != appUser){ |
| | | String token = JwtTokenUtil.generateToken(appUser.getPhone()); |
| | | appletLoginRes.setToken(token); |
| | | appletLoginRes.setUserType(appUser.getUserType()); |
| | | }else{ |
| | | System.err.println("新增"); |
| | | String sessionKey = map.get("sessionKey").toString(); |
| | | //解密手机号 |
| | | String phone = WXCore.decrypt(req.getEncryptedPhoneData(), sessionKey, req.getPhone_iv()); |
| | | appUser = this.selectOne(new EntityWrapper<AppUser>() |
| | | .eq("phone", phone) |
| | | .eq("audit_status", 2) |
| | | .eq("status", 1)); |
| | | if(null == appUser){ |
| | | JSONObject jsonObject = JSONObject.parseObject(phone); |
| | | String phone1 = jsonObject.getString("phoneNumber"); |
| | | System.err.println("看看json"); |
| | | System.err.println(jsonObject); |
| | | System.err.println(phone1); |
| | | //注册新账号 |
| | | addNewAppUser(openid, phone1); |
| | | }else{ |
| | | appUser.setWechatOpenid(openid); |
| | | this.updateById(appUser); |
| | | } |
| | | } |
| | | AppUser appUser1 = this.selectOne(new EntityWrapper<AppUser>() |
| | | .eq("wechat_openid", openid) |
| | | .eq("audit_status", 2) |
| | | .eq("status", 1)); |
| | | //生成token |
| | | String token = JwtTokenUtil.generateToken(appUser1.getPhone()); |
| | | appletLoginRes.setToken(token); |
| | | appletLoginRes.setUserType(appUser1.getUserType()); |
| | | System.err.println("看看TOKEN:"+token); |
| | | //存入缓存中 |
| | | addTokenToRedis(token, appUser1.getId()); |
| | | return ResultUtil.success(appletLoginRes); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | return ResultUtil.runErr(); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 小程序注册账号 |
| | | * @param req |
| | | * @return |
| | | */ |
| | | @Override |
| | | public ResultUtil<AppletLoginRes> registerAccount(RegisterAccountReq req) { |
| | | try { |
| | | //调用微信获取用户小程序openid |
| | | Map<String, Object> map = weChatUtil.code2Session(req.getJscode()); |
| | | if(null == map){ |
| | | return ResultUtil.error("获取微信身份信息失败"); |
| | | } |
| | | System.err.println("看看返回结果map"); |
| | | System.err.println(map); |
| | | String openid = map.get("openid").toString(); |
| | | AppUser appUser = this.selectOne(new EntityWrapper<AppUser>() |
| | | .eq("wechat_openid", openid) |
| | | .eq("audit_status", 2) |
| | | .eq("status", 1)); |
| | | AppletLoginRes appletLoginRes = new AppletLoginRes(); |
| | | if(null == appUser){ |
| | | String sessionKey = map.get("sessionKey").toString(); |
| | | //解密手机号 |
| | | String phone = WXCore.decrypt(req.getEncryptedPhoneData(), sessionKey, req.getPhone_iv()); |
| | | appUser = this.selectOne(new EntityWrapper<AppUser>() |
| | | .eq("phone", phone) |
| | | .eq("audit_status", 2) |
| | | .eq("status", 1)); |
| | | if(null == appUser){ |
| | | JSONObject jsonObject = JSONObject.parseObject(phone); |
| | | String phone1 = jsonObject.getString("phoneNumber"); |
| | | System.err.println("看看json"); |
| | | System.err.println(jsonObject); |
| | | System.err.println(phone1); |
| | | //注册新账号 |
| | | addNewAppUser(openid, phone1); |
| | | }else{ |
| | | appUser.setWechatOpenid(openid); |
| | | this.updateById(appUser); |
| | | } |
| | | } |
| | | AppUser appUser1 = this.selectOne(new EntityWrapper<AppUser>() |
| | | .eq("wechat_openid", openid) |
| | | .eq("audit_status", 2) |
| | | .eq("status", 1)); |
| | | //生成token |
| | | String token = JwtTokenUtil.generateToken(appUser1.getPhone()); |
| | | appletLoginRes.setToken(token); |
| | | appletLoginRes.setUserType(appUser1.getUserType()); |
| | | //存入缓存中 |
| | | addTokenToRedis(token, appUser1.getId()); |
| | | return ResultUtil.success(appletLoginRes); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | return ResultUtil.runErr(); |
| | | } |
| | | public List<InviteUserVO> inviteUserList(AppUserQuery query) { |
| | | return appUserMapper.inviteUserList(query); |
| | | } |
| | | |
| | | @Override |
| | | public SearchIntermediaryRes searchIntermediaryList(SearchIntermediaryReq req) { |
| | | req.setPageNum((req.getPageNum() - 1) * req.getPageSize()); |
| | | //区域 |
| | | List<Integer> districtIds = new ArrayList<>(); |
| | | List<Integer> cityIds = new ArrayList<>(); |
| | | if (req.getDistrict() != null &&(!req.getDistrict().equals("")) ){ |
| | | // 一级id |
| | | Integer integer = Integer.valueOf(req.getDistrict()); |
| | | districtIds.add(integer); |
| | | if (req.getArea()!=null && (!req.getArea().equals(""))){ |
| | | String[] split = req.getArea().split(","); |
| | | for (String s : split) { |
| | | cityIds.add(Integer.valueOf(s)); |
| | | } |
| | | } |
| | | } |
| | | SearchIntermediaryRes searchIntermediaryRes = new SearchIntermediaryRes(); |
| | | List<SearchIntermediaryListRes> res =this.baseMapper.searchIntermediaryList(req,districtIds,cityIds); |
| | | searchIntermediaryRes.setList(res); |
| | | searchIntermediaryRes.setTotal(res.size()); |
| | | return searchIntermediaryRes; |
| | | public List<TeamDetailVO> teamList(TeamQuery query) { |
| | | return appUserMapper.teamList(query); |
| | | } |
| | | |
| | | @Override |
| | | public ResultUtil<AppUser> userInfo() { |
| | | // 获取当前登录用户 |
| | | AppUser appUser = appUserService.getAppUser(); |
| | | return ResultUtil.success(appUser); |
| | | public List<AppUser> selectList1(String name, String phone) { |
| | | return appUserMapper.selectList1(name,phone); |
| | | |
| | | } |
| | | @Autowired |
| | | private IHousingDemandService housingDemandService; |
| | | @Override |
| | | public ResultUtil<CollectRes> collect() { |
| | | // 查询发布的求房源信息 |
| | | SearchHousingDemandReq req = new SearchHousingDemandReq(); |
| | | SearchHousingDemandRes searchHousingDemandRes = housingDemandService.searchHousingDemand(req); |
| | | List<SearchHousingDemandListRes> list1 = searchHousingDemandRes.getList(); |
| | | List<SearchHousingDemandListRes> collect1 = list1.stream().filter(t -> t.getAppuserId() == appUserService.getAppUser().getId()). |
| | | collect(Collectors.toList()); |
| | | // 拿到收藏的房源ids |
| | | List<Integer> ids = new ArrayList<>(); |
| | | CollectRes searchIntermediaryRes = new CollectRes(); |
| | | searchIntermediaryRes.setList1(collect1); |
| | | List<CollectListRes> list = houseResourceService.collect(ids); |
| | | long count1 = collect1.stream().filter(t -> t.getType() == 1).count(); |
| | | long count = list.stream().filter(t -> t.getType() == 1).count(); |
| | | // 草稿数量 |
| | | searchIntermediaryRes.setDraft(Integer.parseInt(String.valueOf(count))+Integer.parseInt(String.valueOf(count1))); |
| | | // 上架房源数 |
| | | int size = list.stream().filter(t -> t.getStatus() == 1 && t.getType()==2).collect(Collectors.toList()).size(); |
| | | searchIntermediaryRes.setOn(size); |
| | | // 下架房源数 |
| | | int size1 = list.stream().filter(t -> t.getStatus() == 0 && t.getType()==2).collect(Collectors.toList()).size(); |
| | | searchIntermediaryRes.setOff(size1); |
| | | for (CollectListRes collectListRes : list) { |
| | | // 查询这个房源的收藏 |
| | | int collect = collectionHouseResourceService.selectList(new EntityWrapper<CollectionHouseResource>() |
| | | .eq("house_resource_id", collectListRes.getId())).size(); |
| | | collectListRes.setCollect(collect); |
| | | // 根据区id 查询下级 |
| | | Region city = regionService.selectById(collectListRes.getCityId()); |
| | | Region dis = regionService.selectById(collectListRes.getDistrictId()); |
| | | Region region = regionService.selectOne(new EntityWrapper<Region>().eq("parent_id",dis.getId())); |
| | | StringBuilder stringBuilder = new StringBuilder(""); |
| | | stringBuilder.append(dis.getName()); |
| | | if (region!=null){ |
| | | stringBuilder.append("/"+region.getName()); |
| | | Region region1 = regionService.selectOne(new EntityWrapper<Region>().eq("parent_id",region.getId())); |
| | | if (region!=null){ |
| | | stringBuilder.append(region1.getName()); |
| | | Region region2 = regionService.selectOne(new EntityWrapper<Region>().eq("parent_id",region1.getId())); |
| | | if (region2!=null){ |
| | | stringBuilder.append("/"+region2.getName()); |
| | | } |
| | | } |
| | | } |
| | | collectListRes.setAddress(city.getName()+">"+stringBuilder.toString()); |
| | | } |
| | | searchIntermediaryRes.setList(list); |
| | | searchIntermediaryRes.setTotal(list.size()); |
| | | return ResultUtil.success(searchIntermediaryRes); |
| | | } |
| | | |
| | | @Override |
| | | public ResultUtil<CollectRes> release(UserInfoQuery query) { |
| | | query.setPageNum((query.getPageNum() - 1) * query.getPageSize()); |
| | | // 房源ids |
| | | List<Integer> ids = collectionHouseResourceService.selectList(new EntityWrapper<CollectionHouseResource>() |
| | | .eq("app_user_id", appUserService.getAppUser().getId())) |
| | | .stream().map(CollectionHouseResource::getHouseResourceId).collect(Collectors.toList()); |
| | | List<Integer> collect = houseResourceService.selectList(new EntityWrapper<HouseResource>() |
| | | .eq("is_delete", 0) |
| | | .eq("status", 1) |
| | | .eq("auth_status", 2) |
| | | .in("id", ids)).stream().map(HouseResource::getId).collect(Collectors.toList()); |
| | | List<CollectListRes> list = houseResourceService.release(query,collect); |
| | | for (CollectListRes collectListRes : list) { |
| | | // 根据区id 查询下级 |
| | | Region city = regionService.selectById(collectListRes.getCityId()); |
| | | Region dis = regionService.selectById(collectListRes.getDistrictId()); |
| | | Region region = regionService.selectOne(new EntityWrapper<Region>().eq("parent_id",dis.getId())); |
| | | StringBuilder stringBuilder = new StringBuilder(""); |
| | | stringBuilder.append(dis.getName()); |
| | | if (region!=null){ |
| | | stringBuilder.append("/"+region.getName()); |
| | | Region region1 = regionService.selectOne(new EntityWrapper<Region>().eq("parent_id",region.getId())); |
| | | if (region!=null){ |
| | | stringBuilder.append(region1.getName()); |
| | | Region region2 = regionService.selectOne(new EntityWrapper<Region>().eq("parent_id",region1.getId())); |
| | | if (region2!=null){ |
| | | stringBuilder.append("/"+region2.getName()); |
| | | } |
| | | } |
| | | } |
| | | collectListRes.setAddress(city.getName()+">"+stringBuilder.toString()); |
| | | } |
| | | CollectRes searchIntermediaryRes = new CollectRes(); |
| | | searchIntermediaryRes.setList(list); |
| | | searchIntermediaryRes.setTotal(list.size()); |
| | | return ResultUtil.success(); |
| | | } |
| | | |
| | | // @Autowired |
| | | // private HouseResourceService houseResourceService; |
| | | @Override |
| | | public List<Host> listHost(String nickname, Integer userType, Integer status, String phone) { |
| | | List<Host> hosts = this.baseMapper.listHost(nickname, userType, status, phone); |
| | | // for (Host h : hosts) { |
| | | // HouseResource houseResource = houseResourceService.selectOne(new EntityWrapper<HouseResource>().eq("insert_user_id", h.getId()).last("order by insert_time desc limit 1")); |
| | | // HousingDemand housingDemand = housingDemandService.selectOne(new EntityWrapper<HousingDemand>().eq("insert_user_id", h.getId()).last("order by insert_time desc limit 1")); |
| | | // if (houseResource==null&&housingDemand!=null){ |
| | | // h.setWechatQRCode(housingDemand.getWechatQrCode()); |
| | | // h.setWatchApp(housingDemand.getWatchApp()); |
| | | // continue; |
| | | // } |
| | | // if (houseResource!=null&&housingDemand==null){ |
| | | // h.setWechatQRCode(houseResource.getWechatQRCode()); |
| | | // h.setWatchApp(houseResource.getWatchApp()); |
| | | // continue; |
| | | // } |
| | | // if (houseResource==null&&housingDemand==null){ |
| | | // continue; |
| | | // } |
| | | // Date insertTime = houseResource.getInsertTime(); |
| | | // Date insertTime1 = housingDemand.getInsertTime(); |
| | | // if (insertTime.after(insertTime1)) { |
| | | // h.setWechatQRCode(houseResource.getWechatQRCode()); |
| | | // h.setWatchApp(houseResource.getWatchApp()); |
| | | // } else { |
| | | // h.setWechatQRCode(housingDemand.getWechatQrCode()); |
| | | // h.setWatchApp(housingDemand.getWatchApp()); |
| | | // } |
| | | // } |
| | | |
| | | return hosts; |
| | | } |
| | | |
| | | @Override |
| | | public List<Medium> listMedium(String nickname, Integer userType, Integer status, String phone) { |
| | | return this.baseMapper.listMedium(nickname,userType,status,phone); |
| | | } |
| | | |
| | | @Override |
| | | public List<Medium> listAuMedium(String nickname, Integer userType, Integer status, String phone) { |
| | | return this.baseMapper.listAuMedium(nickname,userType,status,phone); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 添加新用户到数据库 |
| | | * @param openid 微信openid |
| | | * @param phone 手机号 |
| | | */ |
| | | private void addNewAppUser(String openid, String phone){ |
| | | AppUser appUser = new AppUser(); |
| | | appUser.setCode(UUIDUtil.getNumberRandom(16)); |
| | | appUser.setUserType(1); |
| | | appUser.setPhone(phone); |
| | | appUser.setWechatOpenid(openid); |
| | | appUser.setAuditStatus(2); |
| | | appUser.setStatus(1); |
| | | appUser.setInsertTime(new Date()); |
| | | this.insert(appUser); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 将用户标识存入缓存中用于后期接口的身份校验 |
| | | * @param token |
| | | * @param id |
| | | */ |
| | | private void addTokenToRedis(String token, Integer id){ |
| | | String key = token; |
| | | int length = token.length(); |
| | | if(length > 16){ |
| | | key = token.substring(4, 12); |
| | | } |
| | | //30天有效期 |
| | | redisUtil.setStrValue(key, id.toString(), 2592000); |
| | | } |
| | | |
| | | |
| | |
| | | private Integer getAppUserIdFromToken(String token){ |
| | | String key = token; |
| | | int length = token.length(); |
| | | if(length > 16){ |
| | | key = token.substring(4, 12); |
| | | if(length > 32){ |
| | | key = token.substring(token.length() - 32); |
| | | } |
| | | String value = redisUtil.getValue(key); |
| | | if(ToolUtil.isEmpty(value)){ |