diff --git a/epmet-auth/src/main/java/com/epmet/constant/SsoConstant.java b/epmet-auth/src/main/java/com/epmet/constant/SsoConstant.java new file mode 100644 index 0000000000..88b4f56389 --- /dev/null +++ b/epmet-auth/src/main/java/com/epmet/constant/SsoConstant.java @@ -0,0 +1,21 @@ +package com.epmet.constant; + +/** + * @Author zxc + * @DateTime 2021/1/19 上午10:26 + */ +public interface SsoConstant { + + /** + * 酒城e通appId + */ + String WINE_CITY_E_OPEN_APP_ID = ""; + + String USER_INFO_IS_NULL = "【jcetApiService.getUserInfoByTicket(formDTO.getTicket()】结果为空......"; + + String INSERT_UPDATE_USER_FAILURE = "新增或更新user_weChat失败......"; + + String USER_ID_IS_NULL = "userId为空,生成token失败......"; + String CUSTOMER_ID_IS_NULL = "customerId为空,缓存放置token失败......"; + +} diff --git a/epmet-auth/src/main/java/com/epmet/controller/SsoController.java b/epmet-auth/src/main/java/com/epmet/controller/SsoController.java index 117a891f2c..3251b5a06c 100644 --- a/epmet-auth/src/main/java/com/epmet/controller/SsoController.java +++ b/epmet-auth/src/main/java/com/epmet/controller/SsoController.java @@ -1,5 +1,13 @@ package com.epmet.controller; +import com.epmet.commons.tools.utils.Result; +import com.epmet.commons.tools.validator.ValidatorUtils; +import com.epmet.dto.form.SsoLoginFormDTO; +import com.epmet.dto.result.SsoLoginResultDTO; +import com.epmet.service.SsoService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -10,4 +18,20 @@ import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("sso/resi") public class SsoController { + + @Autowired + private SsoService ssoService; + + /** + * @Description 0、入口:得到token + * @Param formDTO + * @author zxc + * @date 2021/1/18 下午4:59 + */ + @PostMapping("login") + public Result ssoLogin(@RequestBody SsoLoginFormDTO formDTO){ + ValidatorUtils.validateEntity(formDTO, SsoLoginFormDTO.SsoLoginForm.class); + return new Result().ok(ssoService.ssoLogin(formDTO)); + } + } diff --git a/epmet-auth/src/main/java/com/epmet/dto/form/SsoLoginFormDTO.java b/epmet-auth/src/main/java/com/epmet/dto/form/SsoLoginFormDTO.java new file mode 100644 index 0000000000..b50818d3cc --- /dev/null +++ b/epmet-auth/src/main/java/com/epmet/dto/form/SsoLoginFormDTO.java @@ -0,0 +1,39 @@ +package com.epmet.dto.form; + +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import java.io.Serializable; + +/** + * @Author zxc + * @DateTime 2021/1/18 下午4:43 + */ +@Data +public class SsoLoginFormDTO implements Serializable { + + private static final long serialVersionUID = -6543952487970013031L; + + public interface SsoLoginForm{} + + /** + * sso票据,有效期为300秒 + */ + @NotBlank(message = "sso票据不能为空",groups = SsoLoginForm.class) + private String ticket; + + /** + * 三方平台应用AppId + */ + @NotBlank(message = "三方平台应用AppId不能为空",groups = SsoLoginForm.class) + private String appId; + + /** + * app类型 resi;居民段,work:工作端 + */ + @NotBlank(message = "app不能为空",groups = SsoLoginForm.class) + private String app; + + @NotBlank(message = "client不能为空",groups = SsoLoginForm.class) + private String client; +} diff --git a/epmet-auth/src/main/java/com/epmet/dto/result/SsoLoginResultDTO.java b/epmet-auth/src/main/java/com/epmet/dto/result/SsoLoginResultDTO.java new file mode 100644 index 0000000000..09e4bdb32d --- /dev/null +++ b/epmet-auth/src/main/java/com/epmet/dto/result/SsoLoginResultDTO.java @@ -0,0 +1,21 @@ +package com.epmet.dto.result; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + * @Author zxc + * @DateTime 2021/1/18 下午4:45 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class SsoLoginResultDTO implements Serializable { + + private static final long serialVersionUID = 3491079788196180035L; + + private String token = ""; +} diff --git a/epmet-auth/src/main/java/com/epmet/redis/SsoRedis.java b/epmet-auth/src/main/java/com/epmet/redis/SsoRedis.java new file mode 100644 index 0000000000..4bd3f1e0fb --- /dev/null +++ b/epmet-auth/src/main/java/com/epmet/redis/SsoRedis.java @@ -0,0 +1,39 @@ +package com.epmet.redis; + +import cn.hutool.core.bean.BeanUtil; +import com.epmet.commons.tools.redis.RedisKeys; +import com.epmet.commons.tools.redis.RedisUtils; +import com.epmet.commons.tools.security.dto.TokenDto; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Map; + +/** + * @Author zxc + * @DateTime 2021/1/18 下午5:09 + */ +@Component +public class SsoRedis { + + @Autowired + private RedisUtils redisUtils; + + /** + * @Description token放入缓存 + * @Param user + * @Param expire + * @author zxc + * @date 2021/1/18 下午5:10 + */ + public void set(TokenDto user, long expire) { + if (user == null) { + return; + } + String key = RedisKeys.getCpUserKey(user.getApp(), "wxmp", user.getUserId()); + //bean to map + Map map = BeanUtil.beanToMap(user, false, true); + redisUtils.hMSet(key, map, expire); + } + +} diff --git a/epmet-auth/src/main/java/com/epmet/service/SsoService.java b/epmet-auth/src/main/java/com/epmet/service/SsoService.java index d65cbcfe95..3cbd2b9af2 100644 --- a/epmet-auth/src/main/java/com/epmet/service/SsoService.java +++ b/epmet-auth/src/main/java/com/epmet/service/SsoService.java @@ -1,8 +1,20 @@ package com.epmet.service; +import com.epmet.dto.form.SsoLoginFormDTO; +import com.epmet.dto.result.SsoLoginResultDTO; + /** * @Author zxc * @DateTime 2021/1/18 下午4:34 */ public interface SsoService { + + /** + * @Description 0、入口:得到token + * @Param formDTO + * @author zxc + * @date 2021/1/18 下午4:59 + */ + SsoLoginResultDTO ssoLogin(SsoLoginFormDTO formDTO); + } diff --git a/epmet-auth/src/main/java/com/epmet/service/impl/SsoServiceImpl.java b/epmet-auth/src/main/java/com/epmet/service/impl/SsoServiceImpl.java index c40737fd92..bafae81684 100644 --- a/epmet-auth/src/main/java/com/epmet/service/impl/SsoServiceImpl.java +++ b/epmet-auth/src/main/java/com/epmet/service/impl/SsoServiceImpl.java @@ -1,9 +1,35 @@ package com.epmet.service.impl; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.epmet.commons.thirdplat.apiservice.jcet.JcetApiService; +import com.epmet.commons.thirdplat.dto.result.jcet.UserInfoResultDTO; +import com.epmet.commons.tools.exception.RenException; +import com.epmet.commons.tools.security.dto.TokenDto; +import com.epmet.commons.tools.utils.ConvertUtils; +import com.epmet.commons.tools.utils.DateUtils; +import com.epmet.commons.tools.utils.HttpClientManager; +import com.epmet.commons.tools.utils.Result; +import com.epmet.constant.SsoConstant; +import com.epmet.dto.PaCustomerDTO; +import com.epmet.dto.UserDTO; +import com.epmet.dto.form.SsoLoginFormDTO; +import com.epmet.dto.form.UserInfoFormDTO; +import com.epmet.dto.result.SsoLoginResultDTO; +import com.epmet.feign.EpmetUserOpenFeignClient; +import com.epmet.jwt.JwtTokenProperties; +import com.epmet.jwt.JwtTokenUtils; +import com.epmet.redis.SsoRedis; import com.epmet.service.SsoService; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.io.UnsupportedEncodingException; +import java.util.HashMap; +import java.util.Map; + /** * @Author zxc * @DateTime 2021/1/18 下午4:35 @@ -11,4 +37,129 @@ import org.springframework.stereotype.Service; @Service @Slf4j public class SsoServiceImpl implements SsoService { + + @Autowired + private SsoRedis ssoRedis; + + @Autowired + private JwtTokenUtils jwtTokenUtils; + + @Autowired + private JwtTokenProperties jwtTokenProperties; + + @Autowired + private JcetApiService jcetApiService; + + @Autowired + private EpmetUserOpenFeignClient epmetUserOpenFeignClient; + + /** + * @Description 0、入口:得到token + * @Param formDTO + * @author zxc + * @date 2021/1/18 下午4:59 + */ + @Override + public SsoLoginResultDTO ssoLogin(SsoLoginFormDTO formDTO) { + String userId = ""; + if (formDTO.getAppId().equals(SsoConstant.WINE_CITY_E_OPEN_APP_ID)) { + UserInfoResultDTO userInfo = null; + try { + userInfo = jcetApiService.getUserInfoByTicket(formDTO.getTicket()); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + if (null == userInfo){ + throw new RenException(SsoConstant.USER_INFO_IS_NULL); + } + UserInfoFormDTO userInfoFormDTO = ConvertUtils.sourceToTarget(userInfo, UserInfoFormDTO.class); + userInfoFormDTO.setApp(formDTO.getApp()); + Result userDTOResult = epmetUserOpenFeignClient.saveUserInfo(userInfoFormDTO); + if (!userDTOResult.success()){ + throw new RenException(SsoConstant.INSERT_UPDATE_USER_FAILURE); + } + userId = userDTOResult.getData().getId(); + } + if (StringUtils.isBlank(userId)){ + throw new RenException(SsoConstant.USER_ID_IS_NULL); + } + //生成业务token + String token = this.generateToken(formDTO.getApp(),formDTO.getClient(), userId); + //存放Redis + String customerId = getCustomerId(formDTO.getAppId()); + if (StringUtils.isBlank(customerId)){ + throw new RenException(SsoConstant.CUSTOMER_ID_IS_NULL); + } + this.disposeTokenDto(formDTO, userId, token, customerId); + return new SsoLoginResultDTO(token); + } + + /** + * @Description token放缓存 + * @Param formDTO + * @Param userId + * @Param token + * @Param customerId + * @author zxc + * @date 2021/1/18 下午5:32 + */ + public void disposeTokenDto(SsoLoginFormDTO formDTO, String userId, String token, String customerId){ + int expire = jwtTokenProperties.getExpire(); + TokenDto tokenDto = new TokenDto(); + tokenDto.setCustomerId(customerId); + tokenDto.setApp(formDTO.getApp()); + tokenDto.setClient(formDTO.getClient()); + tokenDto.setUserId(userId); + tokenDto.setToken(token); + tokenDto.setUpdateTime(System.currentTimeMillis()); + tokenDto.setExpireTime(jwtTokenUtils.getExpiration(token).getTime()); + ssoRedis.set(tokenDto, expire); + log.info("截止时间:" + DateUtils.format(jwtTokenUtils.getExpiration(token), "yyyy-MM-dd HH:mm:ss")); + } + + /** + * @Description 居民端登陆生成业务token的key + * @Param app + * @Param client + * @Param userId + * @author zxc + * @date 2021/1/18 下午5:14 + */ + private String generateToken(String app,String client, String userId) { + Map map = new HashMap<>(16); + map.put("app", app); + map.put("client", client); + map.put("userId", userId); + String token = jwtTokenUtils.createToken(map); + log.info("app:" + app + ";client:" + client + ";userId:" + userId + ";生成token[" + token + "]"); + return token; + } + + /** + * @Description 获取customerId + * @Param appId + * @author zxc + * @date 2021/1/19 下午1:47 + */ + public String getCustomerId(String appId){ + JSONObject jsonObject = new JSONObject(); + String customerMsgUrl = "https://epmet-cloud.elinkservice.cn/api/third/customermp/getcustomermsg/"; + String data = HttpClientManager.getInstance().sendPostByJSON(customerMsgUrl + appId, JSON.toJSONString(jsonObject)).getData(); + log.info("调用third服务,根据appId查询客户信息:httpclient->url:" + customerMsgUrl + ",结果->" + data); + JSONObject toResult = JSON.parseObject(data); + Result mapToResult = ConvertUtils.mapToEntity(toResult, Result.class); + if (null != toResult.get("code")) { + mapToResult.setCode(((Integer) toResult.get("code")).intValue()); + } + if (!mapToResult.success()) { + log.error(String.format("根据appId查询客户信息失败,对应appId->" + appId)); + throw new RenException(mapToResult.getMsg()); + } + Object publicCustomerResultDTO = mapToResult.getData(); + JSONObject json = JSON.parseObject(publicCustomerResultDTO.toString()); + Map map = (Map) json.get("customer"); + PaCustomerDTO customer = ConvertUtils.mapToEntity(map, PaCustomerDTO.class); + log.info("小程序登陆third服务获取客户用户信息PaCustomerDTO->" + customer); + return customer.getId(); + } } diff --git a/epmet-user/epmet-user-client/src/main/java/com/epmet/dto/form/UserInfoFormDTO.java b/epmet-user/epmet-user-client/src/main/java/com/epmet/dto/form/UserInfoFormDTO.java new file mode 100644 index 0000000000..6c349d424e --- /dev/null +++ b/epmet-user/epmet-user-client/src/main/java/com/epmet/dto/form/UserInfoFormDTO.java @@ -0,0 +1,40 @@ +package com.epmet.dto.form; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.NotBlank; +import java.io.Serializable; + +/** + * @Author zxc + * @DateTime 2021/1/19 上午10:31 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class UserInfoFormDTO implements Serializable { + + private static final long serialVersionUID = 3394557656494741201L; + + public interface UserInfoForm{} + + /** + * 工作端:WORK、居民端:resi、运营端:oper + */ + @NotBlank(message = "app类型不能为空",groups = {UserInfoForm.class}) + private String app; + + /** + * UID 用户唯一标识 即wx_open_id + */ + @NotBlank(message = "UID不能为空",groups = {UserInfoForm.class}) + private String uid; + + private String name; + private String mobile; + private String account; + + private String userId; +} diff --git a/epmet-user/epmet-user-client/src/main/java/com/epmet/feign/EpmetUserOpenFeignClient.java b/epmet-user/epmet-user-client/src/main/java/com/epmet/feign/EpmetUserOpenFeignClient.java index b31f94ddff..2e8729fb15 100644 --- a/epmet-user/epmet-user-client/src/main/java/com/epmet/feign/EpmetUserOpenFeignClient.java +++ b/epmet-user/epmet-user-client/src/main/java/com/epmet/feign/EpmetUserOpenFeignClient.java @@ -469,4 +469,13 @@ public interface EpmetUserOpenFeignClient { @PostMapping(value = "epmetuser/staffrole/specificrolesstaffs", consumes = MediaType.APPLICATION_JSON_VALUE) Result> specificRolesStaffs(RolesUsersListFormDTO rolesUsersListFormDTO); + + /** + * @Description 新增或更新用户信息 + * @Param formDTO + * @author zxc + * @date 2021/1/19 上午10:35 + */ + @PostMapping(value = "/epmetuser/user/saveuserinfo") + Result saveUserInfo(@RequestBody UserInfoFormDTO formDTO); } diff --git a/epmet-user/epmet-user-client/src/main/java/com/epmet/feign/fallback/EpmetUserOpenFeignClientFallback.java b/epmet-user/epmet-user-client/src/main/java/com/epmet/feign/fallback/EpmetUserOpenFeignClientFallback.java index 69371a19cd..3ed6c85e73 100644 --- a/epmet-user/epmet-user-client/src/main/java/com/epmet/feign/fallback/EpmetUserOpenFeignClientFallback.java +++ b/epmet-user/epmet-user-client/src/main/java/com/epmet/feign/fallback/EpmetUserOpenFeignClientFallback.java @@ -331,4 +331,9 @@ public class EpmetUserOpenFeignClientFallback implements EpmetUserOpenFeignClien public Result> specificRolesStaffs(RolesUsersListFormDTO rolesUsersListFormDTO) { return ModuleUtils.feignConError(ServiceConstant.EPMET_USER_SERVER, "specificRolesStaffs", rolesUsersListFormDTO); } + + @Override + public Result saveUserInfo(UserInfoFormDTO formDTO) { + return ModuleUtils.feignConError(ServiceConstant.EPMET_USER_SERVER, "saveUserInfo", formDTO); + } } diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/constant/UserConstant.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/constant/UserConstant.java index a82038d368..2214369866 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/constant/UserConstant.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/constant/UserConstant.java @@ -28,6 +28,11 @@ public interface UserConstant { */ String CLIENT_WEB = "web"; + /** + * app + */ + String APP = "app"; + /** * 微信端 */ diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/controller/UserController.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/controller/UserController.java index a5959e0825..a88a812828 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/controller/UserController.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/controller/UserController.java @@ -154,4 +154,16 @@ public class UserController { return new Result().ok(userService.getUserBasicInfo(formDTO)); } + /** + * @Description 新增或更新用户信息 + * @Param formDTO + * @author zxc + * @date 2021/1/19 上午10:35 + */ + @PostMapping("saveuserinfo") + public Result saveUserInfo(@RequestBody UserInfoFormDTO formDTO){ + ValidatorUtils.validateEntity(formDTO, UserInfoFormDTO.UserInfoForm.class); + return new Result().ok(userService.saveUserInfo(formDTO)); + } + } diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/dao/UserWechatDao.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/dao/UserWechatDao.java index d0f6b2def7..bd6839ac59 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/dao/UserWechatDao.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/dao/UserWechatDao.java @@ -4,6 +4,7 @@ import com.epmet.commons.mybatis.dao.BaseDao; import com.epmet.dto.UserDTO; import com.epmet.dto.UserHeadPhotoDTO; import com.epmet.dto.UserWechatDTO; +import com.epmet.dto.form.UserInfoFormDTO; import com.epmet.dto.form.WxLoginUserInfoFormDTO; import com.epmet.dto.result.CertifiedResultDTO; import com.epmet.dto.result.CustomerUser4PointResultDTO; @@ -77,4 +78,21 @@ public interface UserWechatDao extends BaseDao{ //临时用下in List selectNotInUserBaseInfoTemp(); + + /** + * @Description 根据app、uid查询用户是否存在 + * @Param uid + * @Param app + * @author zxc + * @date 2021/1/19 上午10:42 + */ + UserDTO selectUserDTOByUid(@Param("uid")String uid,@Param("app")String app); + + /** + * @Description 修改user_weChat信息 + * @Param formDTO + * @author zxc + * @date 2021/1/19 上午11:04 + */ + int editByUserId(UserInfoFormDTO formDTO); } diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/service/UserService.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/service/UserService.java index b83d85af51..cc2fb1aecf 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/service/UserService.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/service/UserService.java @@ -86,4 +86,12 @@ public interface UserService extends BaseService { * @Description 居民端-查询用户基础信息 **/ UserBasicInfo getUserBasicInfo(UserBasicInfoFormDTO formDTO); + + /** + * @Description 新增或更新用户信息 + * @Param formDTO + * @author zxc + * @date 2021/1/19 上午10:35 + */ + UserDTO saveUserInfo(UserInfoFormDTO formDTO); } diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/UserServiceImpl.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/UserServiceImpl.java index 103a7a16c0..fe21e876d8 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/UserServiceImpl.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/UserServiceImpl.java @@ -279,4 +279,46 @@ public class UserServiceImpl extends BaseServiceImpl implem return baseDao.selectUserBasicInfo(formDTO); } + /** + * @Description 新增或更新用户信息 + * @Param formDTO + * @author zxc + * @date 2021/1/19 上午10:35 + */ + @Override + public UserDTO saveUserInfo(UserInfoFormDTO formDTO) { + UserDTO result = new UserDTO(); + UserDTO userDTO = userWechatDao.selectUserDTOByUid(formDTO.getUid(), formDTO.getApp()); + if (null == userDTO){ + // 用户不存在 + //user表新增 + UserEntity userEntity = new UserEntity(); + userEntity.setFromApp(UserConstant.APP_RESI); + userEntity.setFromClient(UserConstant.APP); + if (baseDao.insert(userEntity) < NumConstant.ONE) { + log.error("小程序登陆,居民端user表新增数据失败"); + throw new RenException(UserConstant.SAVE_USER); + } + UserWechatEntity entity = new UserWechatEntity(); + entity.setMobile(formDTO.getMobile()); + entity.setUserId(userEntity.getId()); + entity.setNickname(formDTO.getName()); + entity.setWxOpenId(formDTO.getUid()); + if (userWechatDao.insert(entity) < NumConstant.ONE) { + log.error("小程序登陆,居民端user_wechat表新增数据失败"); + throw new RenException(UserConstant.SAVE_USER_WECHAT); + } + result.setId(userEntity.getId()); + }else { + // 用户已存在 + formDTO.setUserId(userDTO.getId()); + if (userWechatDao.editByUserId(formDTO) < NumConstant.ONE) { + log.error("小程序登陆,居民端user_weChat表更新数据失败"); + throw new RenException(UserConstant.UPDATE_USER_WECHAT); + } + result.setId(userDTO.getId()); + } + return result; + } + } diff --git a/epmet-user/epmet-user-server/src/main/resources/mapper/UserWechatDao.xml b/epmet-user/epmet-user-server/src/main/resources/mapper/UserWechatDao.xml index d51c98cf19..f59dc05973 100644 --- a/epmet-user/epmet-user-server/src/main/resources/mapper/UserWechatDao.xml +++ b/epmet-user/epmet-user-server/src/main/resources/mapper/UserWechatDao.xml @@ -38,6 +38,24 @@ USER_ID = #{userId} + + + UPDATE user_wechat + + + NICKNAME = #{name}, + + + MOBILE = #{mobile}, + + UPDATED_BY = #{userId}, + UPDATED_TIME = NOW() + + WHERE + DEL_FLAG = 0 + AND USER_ID = #{userId} + + + + +