| @@ -9,6 +9,7 @@ import com.iformall.domain.po.WxAppinfo; | |||
| import com.iformall.domain.po.WxCUser; | |||
| import com.iformall.domain.po.WxCUserBasicInfo; | |||
| import com.iformall.exception.MallinkException; | |||
| import com.iformall.service.CUserTokenService; | |||
| import com.iformall.service.WxAppinfoService; | |||
| import com.iformall.service.WxCUserBasicInfoService; | |||
| import com.iformall.service.WxCUserService; | |||
| @@ -20,6 +21,7 @@ import org.apache.commons.lang3.StringUtils; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.beans.factory.annotation.Qualifier; | |||
| import org.springframework.data.redis.core.RedisTemplate; | |||
| import org.springframework.data.redis.core.ValueOperations; | |||
| import org.springframework.web.bind.WebDataBinder; | |||
| import org.springframework.web.bind.annotation.InitBinder; | |||
| import org.springframework.web.bind.annotation.RestController; | |||
| @@ -31,6 +33,7 @@ import java.beans.PropertyEditorSupport; | |||
| import java.text.ParseException; | |||
| import java.text.SimpleDateFormat; | |||
| import java.util.Date; | |||
| import java.util.concurrent.TimeUnit; | |||
| @RestController | |||
| public class BaseController { | |||
| @@ -48,11 +51,14 @@ public class BaseController { | |||
| @Autowired | |||
| private WxCUserBasicInfoService wxCUserBasicInfoService; | |||
| @Autowired | |||
| private CUserTokenService cUserTokenService; | |||
| @Autowired | |||
| @Qualifier("baseCUserTokenRedisTemplate") | |||
| RedisTemplate<String, BaseCUserEntity> baseCUserTokenRedisTemplate; | |||
| @InitBinder | |||
| public void InitBinder(WebDataBinder dataBinder) { | |||
| dataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() { | |||
| @@ -93,11 +99,11 @@ public class BaseController { | |||
| return tenantEntity; | |||
| } | |||
| public Long getUserId() { | |||
| HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | |||
| Long cUserId = (Long) request.getAttribute(Constant.LOGIN_USER_KEY); | |||
| return cUserId; | |||
| } | |||
| // public Long getUserId() { | |||
| // HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | |||
| // Long cUserId = (Long) request.getAttribute(Constant.LOGIN_USER_KEY); | |||
| // return cUserId; | |||
| // } | |||
| public Long getMemberId() throws Exception { | |||
| HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | |||
| @@ -113,8 +119,10 @@ public class BaseController { | |||
| * 请尽量多用getTenantId, getUserId | |||
| */ | |||
| public WxCUser getWxCUser() { | |||
| Long cUserId = getUserId(); | |||
| WxCUser user = wxCUserService.getById(cUserId); | |||
| HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | |||
| WxCUser user = (WxCUser) request.getAttribute(Constant.REQUEST_WX_C_USER_KEY); | |||
| // Long cUserId = getUserId(); | |||
| // WxCUser user = wxCUserService.getById(cUserId); | |||
| if (user == null) { | |||
| throw new MallinkException(ErrorCode.USER_IS_EMPTY); | |||
| } | |||
| @@ -127,7 +135,7 @@ public class BaseController { | |||
| } | |||
| return user; | |||
| } | |||
| /** | |||
| * | |||
| */ | |||
| @@ -44,7 +44,6 @@ public class WxMsgValidationcodeController extends BaseController { | |||
| @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true), | |||
| @ApiImplicitParam(name = "type", value = "场景", dataType = "Integer", paramType = "query", required = true)}) | |||
| public ResultData sendvalidationcode(String phone, Integer type) { | |||
| WxCUser user = getWxCUser(); | |||
| WxMsgValidationcode wxMsgValidationcode = new WxMsgValidationcode(); | |||
| wxMsgValidationcode.updateTenantInfo(getTenantInfo()); | |||
| wxMsgValidationcode.setPhone(phone); | |||
| @@ -67,6 +67,7 @@ public class AuthorizationInterceptor extends HandlerInterceptorAdapter { | |||
| //设置userId到request里,后续根据userId,获取用户信息 | |||
| request.setAttribute(Constant.LOGIN_USER_KEY, cUser.getId()); | |||
| request.setAttribute(Constant.TENANT_ID, cUser.getTenantId()); | |||
| request.setAttribute(Constant.REQUEST_WX_C_USER_KEY, cUser); | |||
| if (StringUtils.isNotBlank(cUser.getParentTenantId())) { | |||
| request.setAttribute(Constant.PARENT_TENANT_ID, cUser.getParentTenantId()); | |||
| } | |||
| @@ -17,7 +17,7 @@ public interface WxCUserMapper extends CommonMapper<WxCUser, Long> { | |||
| WxCUser findByUnionId(WxCUser record); | |||
| BaseCUserEntity findByToken(String token); | |||
| WxCUser findByToken(String token); | |||
| long findCount(WxCUserBasicInfoDto dto); | |||
| @@ -1,5 +1,6 @@ | |||
| package com.iformall.service.impl; | |||
| import com.iformall.domain.po.WxCUser; | |||
| import com.iformall.domain.po.base.BaseCUserEntity; | |||
| import com.iformall.mapper.WxCUserMapper; | |||
| import com.iformall.service.CUserTokenService; | |||
| @@ -28,7 +29,7 @@ public class CUserTokenServiceImpl implements CUserTokenService { | |||
| @Override | |||
| public BaseCUserEntity getByToken(String token) { | |||
| BaseCUserEntity user = null; | |||
| BaseCUserEntity baseUser = null; | |||
| logger.info("获取用户信息start ..." + token); | |||
| String key = Constant.tokenPrev + token; | |||
| @@ -38,17 +39,19 @@ public class CUserTokenServiceImpl implements CUserTokenService { | |||
| boolean hasKey = baseCUserTokenRedisTemplate.hasKey(key); | |||
| if(hasKey) { | |||
| // 从缓存获取用户信息 | |||
| user = operations.get(key); | |||
| baseUser = operations.get(key); | |||
| logger.info("缓存中获取用户信息End-" + token); | |||
| return user; | |||
| return baseUser; | |||
| } | |||
| if(token.endsWith(Constant.TOKEN_WXC_END)){ | |||
| user = wxCUserMapper.findByToken(token); | |||
| WxCUser user = wxCUserMapper.findByToken(token); | |||
| operations.set(key, user, 3600, TimeUnit.SECONDS); | |||
| baseUser = user; | |||
| } | |||
| // 插入缓存 | |||
| operations.set(key, user, 3600, TimeUnit.SECONDS); | |||
| //operations.set(key, user, 3600, TimeUnit.SECONDS); | |||
| logger.info("DB中获取用户信息End-" + token); | |||
| return user; | |||
| return baseUser; | |||
| } | |||
| @@ -30,6 +30,7 @@ public class Constant { | |||
| public static final String LOGIN_MEMBER_KEY = "LOGIN_MEMBER_KEY"; | |||
| public static final String TENANT_ID = "TENANT_ID"; | |||
| public static final String PARENT_TENANT_ID = "PARENT_TENANT_ID"; | |||
| public static final String REQUEST_WX_C_USER_KEY = "REQUEST_WX_C_USER"; | |||
| public static final String adminPage = "https://admin.malls.iformall.com"; | |||
| @@ -231,12 +231,12 @@ | |||
| </select> | |||
| <select id="findByOpenId" parameterType="com.iformall.domain.po.WxCUser" resultMap="BaseResultMap"> | |||
| select * from wx_c_user | |||
| select <include refid="allColumns"/> from wx_c_user | |||
| where `app_id` = #{appId} and `open_id` = #{openId} | |||
| </select> | |||
| <select id="findByUnionId" parameterType="com.iformall.domain.po.WxCUser" resultMap="BaseResultMap"> | |||
| select * from wx_c_user | |||
| select <include refid="allColumns"/> from wx_c_user | |||
| where `open_app_id` = #{openAppId} and `union_id` = #{unionId} | |||
| </select> | |||
| @@ -250,8 +250,8 @@ | |||
| <result column="expire_time" jdbcType="TIMESTAMP" property="expireTime"/> | |||
| </resultMap> | |||
| <select id="findByToken" resultMap="BaseCUserResultMap"> | |||
| select id,user_id,tenant_id,parent_tenant_id,token,expire_time from wx_c_user | |||
| <select id="findByToken" resultMap="BaseResultMap"> | |||
| select <include refid="allColumns"/> from wx_c_user | |||
| where `token` = #{token} | |||
| </select> | |||