package com.changhong.epc.rely.api.service.impl; import com.changhong.epc.constter.base.BaseConst; import com.changhong.epc.constter.base.Context; import com.changhong.epc.constter.system.UserInfo; import com.changhong.epc.constter.system.prop.ChangHongProperties; import com.changhong.epc.rely.api.service.UserService; import com.changhong.epc.rely.api.tool.CscAesUtils; import com.changhong.epc.rely.api.tool.HttpUtil; import com.iemsoft.framework.cloud.core.thread.ThreadData; import com.iemsoft.framework.cloud.core.tools.ObjectUtil; import com.iemsoft.framework.cloud.redis.service.CacheUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.ui.ModelMap; import javax.annotation.Resource; import java.util.concurrent.TimeoutException; @Service public class UserServiceImpl implements UserService, BaseConst { @Resource private UserInfoServiceImpl userInfoService; @Value("${aes.key:0123456789ABCDEF}") private String aesKey; @Override public UserInfo getUserInfo() { UserInfo userInfo = userInfoService.getUserInfo(Context.getLoginInfo()); if(userInfo != null) { userInfo.setLoginInfo(Context.getLoginInfo()); userInfo.setAesOpenId(CscAesUtils.encrypt(userInfo.getLoginInfo().getOpenId(), aesKey)); ThreadData.set(USER_INFO, userInfo); } return userInfo; } @Service @Slf4j public static class UserInfoServiceImpl { @Autowired private ChangHongProperties changHongProperties; /** * 获得userInfo * * @param loginInfo * @return */ public UserInfo getUserInfo(UserInfo.LogInInfo loginInfo) { if(ObjectUtil.empty(loginInfo)){ return null; } String key = String.format("userInfo_%s" , loginInfo.getOpenId()); UserInfo userInfo = CacheUtils.get(key,UserInfo.class); // UserInfo userInfo = null; if(userInfo!=null){ return userInfo; } if(loginInfo.getToken() == null || loginInfo.getOpenId() == null){ return null; } try { userInfo = HttpUtil.get( changHongProperties.getPassUrl(ChangHongProperties.PassPortName.USER_DETIAL , new ModelMap("token", loginInfo.getToken()) .addAttribute("openId", loginInfo.getOpenId()) .addAttribute("livemode", changHongProperties.getSsoLivemode())) , null, UserInfo.class); } catch (TimeoutException e) { log.error(e.getMessage(), e); } CacheUtils.set(key,userInfo,1 * 60 * 60 * 24); return userInfo; } } }