Browse Source

根据userid获取token

master
zhangye 5 years ago
parent
commit
6690a88f20
  1. 8
      health/src/main/java/com/ccsens/health/api/DebugController.java
  2. 4
      health/src/main/resources/application.yml
  3. 1
      tall/src/main/java/com/ccsens/tall/config/SpringConfig.java
  4. 2
      tall/src/main/java/com/ccsens/tall/service/IUserService.java
  5. 85
      tall/src/main/java/com/ccsens/tall/service/UserService.java
  6. 16
      tall/src/main/java/com/ccsens/tall/web/UserController.java
  7. 36
      util/src/main/java/com/ccsens/util/wx/WxXcxUtil.java
  8. 4
      util/src/test/java/com/ccsens/util/Base64Test.java

8
health/src/main/java/com/ccsens/health/api/DebugController.java

@ -36,10 +36,10 @@ public class DebugController {
color.r = "243"; color.r = "243";
color.g = "139"; color.g = "139";
color.b = "0"; color.b = "0";
WxXcxUtil.getWxCode("pages/sign/sign" WxXcxUtil.getWxCode("pages/index/index"
,"1011&d=1217647686598135808&t=0",color,"/home/cloud/tall/uploads/qrCode/2020-03-07/158357267174.png"); ,"d=1218855229722857472",color,"/home/cloud/tall/uploads/qrCode/0011.png");
// WxXcxUtil.getWxCodeTest("pages/user-code/user-code?d=1217647686598135808" // WxXcxUtil.getWxCodeTest("pages/index/index?t=eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1ODM4MDc3MzcsInN1YiI6IjEyMTg4NTUyMjk3MjI4NTc0NzIiLCJhdXRoSWQiOiIxMTc3MDQwNjY"
// ,"d=1217647686598135808",color,"/home/cloud/tall/uploads/qrCode/2020-03-07/158357267174.png"); // ,"d=1217647686598135808",color,"/home/cloud/tall/uploads/qrCode/158357267174.png");
return JsonResponse.newInstance().ok("完成"); return JsonResponse.newInstance().ok("完成");
} }

4
health/src/main/resources/application.yml

@ -1,4 +1,4 @@
spring: spring:
profiles: profiles:
active: test active: dev
include: common, util-test include: common, util-dev

1
tall/src/main/java/com/ccsens/tall/config/SpringConfig.java

@ -142,6 +142,7 @@ public class SpringConfig implements WebMvcConfigurer {
.excludePathPatterns("/users/claims") .excludePathPatterns("/users/claims")
.excludePathPatterns("/users/member") .excludePathPatterns("/users/member")
.excludePathPatterns("/users/allMemberAll") .excludePathPatterns("/users/allMemberAll")
.excludePathPatterns("/users/userId")
.addPathPatterns("/plugins/**") .addPathPatterns("/plugins/**")
.excludePathPatterns("/plugins/sign") .excludePathPatterns("/plugins/sign")
.excludePathPatterns("/plugins/fuzzy") .excludePathPatterns("/plugins/fuzzy")

2
tall/src/main/java/com/ccsens/tall/service/IUserService.java

@ -73,5 +73,5 @@ public interface IUserService {
UserVo.UserSign changePhoneNotPassword(Long userId,UserDto.WxBindingPhone phoneInfo) throws Exception; UserVo.UserSign changePhoneNotPassword(Long userId,UserDto.WxBindingPhone phoneInfo) throws Exception;
String getTokenByUserId(Long userId);
} }

85
tall/src/main/java/com/ccsens/tall/service/UserService.java

@ -7,6 +7,7 @@ import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.ccsens.cloudutil.feign.HealthFeignClient; import com.ccsens.cloudutil.feign.HealthFeignClient;
import com.ccsens.tall.bean.dto.ProjectDto; import com.ccsens.tall.bean.dto.ProjectDto;
import com.ccsens.tall.bean.dto.UserDto; import com.ccsens.tall.bean.dto.UserDto;
@ -72,7 +73,7 @@ public class UserService implements IUserService {
*/ */
@Override @Override
public UserVo.UserSign signin(WebConstant.CLIENT_TYPE clientType, WebConstant.IDENTIFY_TYPE identifyType, public UserVo.UserSign signin(WebConstant.CLIENT_TYPE clientType, WebConstant.IDENTIFY_TYPE identifyType,
String identifier, String credential, String clientIp,String redirect) throws Exception { String identifier, String credential, String clientIp, String redirect) throws Exception {
UserVo.UserSign userSignVo = null; UserVo.UserSign userSignVo = null;
//1.登陆 //1.登陆
userSignVo = __signin(identifyType, identifier, credential, redirect); userSignVo = __signin(identifyType, identifier, credential, redirect);
@ -90,7 +91,7 @@ public class UserService implements IUserService {
return wxEnterpriseLogin(identifier, credential, redirect); return wxEnterpriseLogin(identifier, credential, redirect);
case Wxmp: case Wxmp:
credential = credential == null ? "old" : credential; credential = credential == null ? "old" : credential;
return wxmplogin(identifier,credential); return wxmplogin(identifier, credential);
case OAUTH2_Wx: case OAUTH2_Wx:
return wxLogin(identifyType, identifier); return wxLogin(identifyType, identifier);
case Wx_H5: case Wx_H5:
@ -112,6 +113,7 @@ public class UserService implements IUserService {
/** /**
* 企业登录 * 企业登录
*
* @param identifier * @param identifier
* @param credential * @param credential
* @return * @return
@ -384,35 +386,34 @@ public class UserService implements IUserService {
/** /**
* 微信小程序 * 微信小程序
*/ */
private UserVo.UserSign wxmplogin(String code,String gameType) throws Exception { private UserVo.UserSign wxmplogin(String code, String gameType) throws Exception {
//0.获取openid //0.获取openid
WxXcxUtil.WechatUser wechatUser = WxXcxUtil.getUserInfo(code,gameType); WxXcxUtil.WechatUser wechatUser = WxXcxUtil.getUserInfo(code, gameType);
String openId = wechatUser.openid; String openId = wechatUser.openid;
String unionId = wechatUser.unionid; String unionId = wechatUser.unionid;
log.info("小程序登录,openid:{} ,unionId:{}", openId, unionId); log.info("小程序登录,openid:{} ,unionId:{}", openId, unionId);
UserVo.UserSign userSignVo = getUserSign(openId, unionId, (byte) WebConstant.IDENTIFY_TYPE.Wxmp.value,null); UserVo.UserSign userSignVo = getUserSign(openId, unionId, (byte) WebConstant.IDENTIFY_TYPE.Wxmp.value, null);
return userSignVo; return userSignVo;
} }
@Data @Data
private static class WxEnterpriseParam{ private static class WxEnterpriseParam {
private String suiteAccessToken; private String suiteAccessToken;
private String userTicket; private String userTicket;
private String redirect; private String redirect;
} }
/** /**
*
* @param openId * @param openId
* @param unionId * @param unionId
* @return * @return
*/ */
private UserVo.UserSign getUserSign(String openId, String unionId, private UserVo.UserSign getUserSign(String openId, String unionId,
byte identifyType, WxEnterpriseParam param) { byte identifyType, WxEnterpriseParam param) {
UserVo.UserSign userSignVo;//1.查找对应账户,不存在则注册 UserVo.UserSign userSignVo;//1.查找对应账户,不存在则注册
List<SysAuth> authList = null; List<SysAuth> authList = null;
SysAuth theAuth = null; SysAuth theAuth = null;
if(ObjectUtil.isNotNull(openId)) { if (ObjectUtil.isNotNull(openId)) {
SysAuthExample authExample = new SysAuthExample(); SysAuthExample authExample = new SysAuthExample();
authExample.createCriteria().andIdentifyTypeEqualTo(identifyType) authExample.createCriteria().andIdentifyTypeEqualTo(identifyType)
.andIdentifierEqualTo(openId); .andIdentifierEqualTo(openId);
@ -420,7 +421,7 @@ public class UserService implements IUserService {
} }
if (CollectionUtil.isEmpty(authList)) { if (CollectionUtil.isEmpty(authList)) {
List<SysAuth> sysAuthList = null; List<SysAuth> sysAuthList = null;
if(ObjectUtil.isNotNull(unionId)) { if (ObjectUtil.isNotNull(unionId)) {
SysAuthExample sysAuthExample = new SysAuthExample(); SysAuthExample sysAuthExample = new SysAuthExample();
sysAuthExample.createCriteria().andCredentialEqualTo(unionId); sysAuthExample.createCriteria().andCredentialEqualTo(unionId);
sysAuthList = authDao.selectByExample(sysAuthExample); sysAuthList = authDao.selectByExample(sysAuthExample);
@ -478,6 +479,7 @@ public class UserService implements IUserService {
/** /**
* 获取企业微信信息并绑定用户 * 获取企业微信信息并绑定用户
*
* @param identifyType * @param identifyType
* @param param * @param param
* @param user * @param user
@ -1032,6 +1034,7 @@ public class UserService implements IUserService {
/** /**
* 查询用户是否关注此项目 * 查询用户是否关注此项目
*
* @param currentUserId * @param currentUserId
* @param projectId * @param projectId
* @return * @return
@ -1039,14 +1042,14 @@ public class UserService implements IUserService {
@Override @Override
public Boolean getIsAttention(Long currentUserId, Long projectId) { public Boolean getIsAttention(Long currentUserId, Long projectId) {
Boolean isAttention = false; Boolean isAttention = false;
if(ObjectUtil.isNotNull(sysProjectDao.selectByPrimaryKey(projectId))){ if (ObjectUtil.isNotNull(sysProjectDao.selectByPrimaryKey(projectId))) {
UserAttentionExample attentionExample = new UserAttentionExample(); UserAttentionExample attentionExample = new UserAttentionExample();
attentionExample.createCriteria().andProjectIdEqualTo(projectId).andUserIdEqualTo(currentUserId); attentionExample.createCriteria().andProjectIdEqualTo(projectId).andUserIdEqualTo(currentUserId);
List<UserAttention> attentionList = userAttentionDao.selectByExample(attentionExample); List<UserAttention> attentionList = userAttentionDao.selectByExample(attentionExample);
if(CollectionUtil.isNotEmpty(attentionList)){ if (CollectionUtil.isNotEmpty(attentionList)) {
isAttention = true; isAttention = true;
} }
}else { } else {
throw new BaseException(CodeEnum.NOT_PROJECT); throw new BaseException(CodeEnum.NOT_PROJECT);
} }
return isAttention; return isAttention;
@ -1054,6 +1057,7 @@ public class UserService implements IUserService {
/** /**
* 用户关注某个项目 * 用户关注某个项目
*
* @param currentUserId * @param currentUserId
* @param projectIdDto * @param projectIdDto
*/ */
@ -1061,11 +1065,11 @@ public class UserService implements IUserService {
public void userAttentionProject(Long currentUserId, ProjectDto.ProjectIdDto projectIdDto) { public void userAttentionProject(Long currentUserId, ProjectDto.ProjectIdDto projectIdDto) {
//查找项目 //查找项目
SysProject project = sysProjectDao.selectByPrimaryKey(projectIdDto.getProjectId()); SysProject project = sysProjectDao.selectByPrimaryKey(projectIdDto.getProjectId());
if(ObjectUtil.isNotNull(project)){ if (ObjectUtil.isNotNull(project)) {
UserAttentionExample attentionExample = new UserAttentionExample(); UserAttentionExample attentionExample = new UserAttentionExample();
attentionExample.createCriteria().andProjectIdEqualTo(projectIdDto.getProjectId()).andUserIdEqualTo(currentUserId); attentionExample.createCriteria().andProjectIdEqualTo(projectIdDto.getProjectId()).andUserIdEqualTo(currentUserId);
List<UserAttention> attentionList = userAttentionDao.selectByExample(attentionExample); List<UserAttention> attentionList = userAttentionDao.selectByExample(attentionExample);
if(CollectionUtil.isNotEmpty(attentionList)){ if (CollectionUtil.isNotEmpty(attentionList)) {
throw new BaseException(CodeEnum.ALREADY_ATTENTION); throw new BaseException(CodeEnum.ALREADY_ATTENTION);
} }
UserAttention attention = new UserAttention(); UserAttention attention = new UserAttention();
@ -1073,7 +1077,7 @@ public class UserService implements IUserService {
attention.setUserId(currentUserId); attention.setUserId(currentUserId);
attention.setProjectId(projectIdDto.getProjectId()); attention.setProjectId(projectIdDto.getProjectId());
userAttentionDao.insertSelective(attention); userAttentionDao.insertSelective(attention);
}else { } else {
throw new BaseException(CodeEnum.NOT_PROJECT); throw new BaseException(CodeEnum.NOT_PROJECT);
} }
} }
@ -1245,7 +1249,7 @@ public class UserService implements IUserService {
public UserVo.WxInfo updateUserInfo(Long currentUserId, UserDto.WxInfo userInfo) { public UserVo.WxInfo updateUserInfo(Long currentUserId, UserDto.WxInfo userInfo) {
//通过userid查找到用户 //通过userid查找到用户
SysUser user = userDao.selectByPrimaryKey(currentUserId); SysUser user = userDao.selectByPrimaryKey(currentUserId);
if(ObjectUtil.isNull(user)){ if (ObjectUtil.isNull(user)) {
throw new BaseException(CodeEnum.NOT_LOGIN); throw new BaseException(CodeEnum.NOT_LOGIN);
} }
@ -1298,6 +1302,7 @@ public class UserService implements IUserService {
/** /**
* 更改绑定手机号不要密码 * 更改绑定手机号不要密码
*
* @param userId * @param userId
* @param phoneInfo * @param phoneInfo
* @throws Exception * @throws Exception
@ -1325,7 +1330,7 @@ public class UserService implements IUserService {
userSign.setUserId(phoneAuth.getUserId()); userSign.setUserId(phoneAuth.getUserId());
userSign.setAuthId(phoneAuth.getId()); userSign.setAuthId(phoneAuth.getId());
}else { } else {
//添加一条手机号认证信息 //添加一条手机号认证信息
SysAuth sysAuth = new SysAuth(); SysAuth sysAuth = new SysAuth();
sysAuth.setId(snowflake.nextId()); sysAuth.setId(snowflake.nextId());
@ -1347,4 +1352,48 @@ public class UserService implements IUserService {
} }
return userSign; return userSign;
} }
/**
* 通过userId获取token
*
* @param userId
* @return
*/
@Override
public String getTokenByUserId(Long userId) {
if (ObjectUtil.isNull(userId)) {
throw new BaseException(CodeEnum.PARAM_ERROR);
}
Object object = redisUtil.get(RedisKeyManager.getTokenCachedKey(userId));
if (ObjectUtil.isNull(object)) {
UserVo.UserSign userSignVo = null;
SysAuthExample authExample = new SysAuthExample();
authExample.createCriteria().andUserIdEqualTo(userId);
List<SysAuth> authList = authDao.selectByExample(authExample);
if (CollectionUtil.isNotEmpty(authList)) {
userSignVo = new UserVo.UserSign();
userSignVo.setUserId(authList.get(0).getUserId());
userSignVo.setAuthId(authList.get(0).getId());
}
//3.生成token(access_token,refresh_token)
if (ObjectUtil.isNotNull(userSignVo)) {
Map<String, Object> theMap = CollectionUtil.newHashMap();
theMap.put("authId", String.valueOf(userSignVo.getAuthId()));
Long tokenExpired = 3600 * 1000L * 24 * 100;
String token =
JwtUtil.createJWT(userId + "",
theMap, tokenExpired,
WebConstant.JWT_ACCESS_TOKEN_SECERT);
redisUtil.set(RedisKeyManager.getTokenCachedKey((Long) userId),
token, tokenExpired / 1000);
object = token;
} else {
throw new BaseException(CodeEnum.PARAM_ERROR);
}
}
return object.toString();
}
} }

16
tall/src/main/java/com/ccsens/tall/web/UserController.java

@ -302,7 +302,21 @@ public class UserController {
return JsonResponse.newInstance().ok(); return JsonResponse.newInstance().ok();
} }
/*===============================================================================================*/
@ApiOperation(value = "通过userId获取token",notes = "")
@ApiImplicitParams({
@ApiImplicitParam(name="projectId",value = "projectId",required = true,paramType = "query")
})
@RequestMapping(value = "/userId",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
public JsonResponse<UserVo.TokenToUserId> getTokenByUserId(HttpServletRequest request,
@RequestParam(required = true) Long userId) throws Exception {
String token = userService.getTokenByUserId(userId);
return JsonResponse.newInstance().ok(token);
}
/*===============================================================================================*/
@ApiOperation(value = "根据token获取userId",notes = "") @ApiOperation(value = "根据token获取userId",notes = "")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name="token",value = "token",required = true,paramType = "query") @ApiImplicitParam(name="token",value = "token",required = true,paramType = "query")

36
util/src/main/java/com/ccsens/util/wx/WxXcxUtil.java

@ -249,24 +249,24 @@ public class WxXcxUtil {
HttpsUtil.httpsRequest(url,"POST",postStr,path); HttpsUtil.httpsRequest(url,"POST",postStr,path);
} }
// /** /**
// * 获取小程序二维码/小程序码 * 获取小程序二维码/小程序码(长度128有数量限制)
// * @return * @return
// * @throws Exception * @throws Exception
// */ */
// public static void getWxCodeTest(String page,String scene,LineColor color,String path) throws Exception { public static void getWxCodeTest(String page,String scene,LineColor color,String path) throws Exception {
// String url = String.format(URL_GET_WX_CODE_C, WxGzhUtil.getAccessToken()); String url = String.format(URL_GET_WX_CODE_C, WxGzhUtil.getAccessToken());
//
// WechatCodeA wechatCodeA = new WechatCodeA(); WechatCodeA wechatCodeA = new WechatCodeA();
// wechatCodeA.path = page; wechatCodeA.path = page;
//// wechatCode.path = page+"?"+scene; // wechatCode.path = page+"?"+scene;
//
//// WechatCodeA.line_color = color; // WechatCodeA.line_color = color;
//
// String postStr = JacksonUtil.beanToJson(wechatCodeA); String postStr = JacksonUtil.beanToJson(wechatCodeA);
// System.out.println(postStr); System.out.println(postStr);
// HttpsUtil.httpsRequest(url,"POST",postStr,path); HttpsUtil.httpsRequest(url,"POST",postStr,path);
// } }
/** /**
* 统一下单 * 统一下单

4
util/src/test/java/com/ccsens/util/Base64Test.java

@ -4,6 +4,7 @@ import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import com.ccsens.util.wx.WxXcxUtil; import com.ccsens.util.wx.WxXcxUtil;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -29,8 +30,7 @@ public class Base64Test {
//// out.write(decode); //// out.write(decode);
//// out.close(); //// out.close();
// System.out.println(System.currentTimeMillis()); // System.out.println(System.currentTimeMillis());
// } //
@Test @Test
public void test01() throws Exception{ public void test01() throws Exception{

Loading…
Cancel
Save