From 6690a88f2011c01d31ae8743abe7e423e40c2e7f Mon Sep 17 00:00:00 2001 From: zhangye <654600784@qq.com> Date: Tue, 10 Mar 2020 15:12:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E6=8D=AEuserid=E8=8E=B7=E5=8F=96token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ccsens/health/api/DebugController.java | 8 +- health/src/main/resources/application.yml | 4 +- .../com/ccsens/tall/config/SpringConfig.java | 1 + .../com/ccsens/tall/service/IUserService.java | 2 +- .../com/ccsens/tall/service/UserService.java | 85 +++++++++++++++---- .../com/ccsens/tall/web/UserController.java | 16 +++- .../java/com/ccsens/util/wx/WxXcxUtil.java | 36 ++++---- .../test/java/com/ccsens/util/Base64Test.java | 4 +- 8 files changed, 110 insertions(+), 46 deletions(-) diff --git a/health/src/main/java/com/ccsens/health/api/DebugController.java b/health/src/main/java/com/ccsens/health/api/DebugController.java index c90b6bb5..7a66cbf2 100644 --- a/health/src/main/java/com/ccsens/health/api/DebugController.java +++ b/health/src/main/java/com/ccsens/health/api/DebugController.java @@ -36,10 +36,10 @@ public class DebugController { color.r = "243"; color.g = "139"; color.b = "0"; - WxXcxUtil.getWxCode("pages/sign/sign" - ,"1011&d=1217647686598135808&t=0",color,"/home/cloud/tall/uploads/qrCode/2020-03-07/158357267174.png"); -// WxXcxUtil.getWxCodeTest("pages/user-code/user-code?d=1217647686598135808" -// ,"d=1217647686598135808",color,"/home/cloud/tall/uploads/qrCode/2020-03-07/158357267174.png"); + WxXcxUtil.getWxCode("pages/index/index" + ,"d=1218855229722857472",color,"/home/cloud/tall/uploads/qrCode/0011.png"); +// WxXcxUtil.getWxCodeTest("pages/index/index?t=eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1ODM4MDc3MzcsInN1YiI6IjEyMTg4NTUyMjk3MjI4NTc0NzIiLCJhdXRoSWQiOiIxMTc3MDQwNjY" +// ,"d=1217647686598135808",color,"/home/cloud/tall/uploads/qrCode/158357267174.png"); return JsonResponse.newInstance().ok("完成"); } diff --git a/health/src/main/resources/application.yml b/health/src/main/resources/application.yml index 5889ff7f..5c2cd5c4 100644 --- a/health/src/main/resources/application.yml +++ b/health/src/main/resources/application.yml @@ -1,4 +1,4 @@ spring: profiles: - active: test - include: common, util-test \ No newline at end of file + active: dev + include: common, util-dev \ No newline at end of file diff --git a/tall/src/main/java/com/ccsens/tall/config/SpringConfig.java b/tall/src/main/java/com/ccsens/tall/config/SpringConfig.java index 9a212308..0befd015 100644 --- a/tall/src/main/java/com/ccsens/tall/config/SpringConfig.java +++ b/tall/src/main/java/com/ccsens/tall/config/SpringConfig.java @@ -142,6 +142,7 @@ public class SpringConfig implements WebMvcConfigurer { .excludePathPatterns("/users/claims") .excludePathPatterns("/users/member") .excludePathPatterns("/users/allMemberAll") + .excludePathPatterns("/users/userId") .addPathPatterns("/plugins/**") .excludePathPatterns("/plugins/sign") .excludePathPatterns("/plugins/fuzzy") diff --git a/tall/src/main/java/com/ccsens/tall/service/IUserService.java b/tall/src/main/java/com/ccsens/tall/service/IUserService.java index 2aa1f7d4..d6dfd311 100644 --- a/tall/src/main/java/com/ccsens/tall/service/IUserService.java +++ b/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; - + String getTokenByUserId(Long userId); } diff --git a/tall/src/main/java/com/ccsens/tall/service/UserService.java b/tall/src/main/java/com/ccsens/tall/service/UserService.java index 928f4289..485381e1 100644 --- a/tall/src/main/java/com/ccsens/tall/service/UserService.java +++ b/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.RandomUtil; import cn.hutool.core.util.StrUtil; +import cn.hutool.extra.servlet.ServletUtil; import com.ccsens.cloudutil.feign.HealthFeignClient; import com.ccsens.tall.bean.dto.ProjectDto; import com.ccsens.tall.bean.dto.UserDto; @@ -72,7 +73,7 @@ public class UserService implements IUserService { */ @Override 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; //1.登陆 userSignVo = __signin(identifyType, identifier, credential, redirect); @@ -90,7 +91,7 @@ public class UserService implements IUserService { return wxEnterpriseLogin(identifier, credential, redirect); case Wxmp: credential = credential == null ? "old" : credential; - return wxmplogin(identifier,credential); + return wxmplogin(identifier, credential); case OAUTH2_Wx: return wxLogin(identifyType, identifier); case Wx_H5: @@ -112,6 +113,7 @@ public class UserService implements IUserService { /** * 企业登录 + * * @param identifier * @param credential * @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 - WxXcxUtil.WechatUser wechatUser = WxXcxUtil.getUserInfo(code,gameType); + WxXcxUtil.WechatUser wechatUser = WxXcxUtil.getUserInfo(code, gameType); String openId = wechatUser.openid; String unionId = wechatUser.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; } @Data - private static class WxEnterpriseParam{ + private static class WxEnterpriseParam { private String suiteAccessToken; private String userTicket; private String redirect; } /** - * * @param openId * @param unionId * @return */ private UserVo.UserSign getUserSign(String openId, String unionId, - byte identifyType, WxEnterpriseParam param) { + byte identifyType, WxEnterpriseParam param) { UserVo.UserSign userSignVo;//1.查找对应账户,不存在则注册 List authList = null; SysAuth theAuth = null; - if(ObjectUtil.isNotNull(openId)) { + if (ObjectUtil.isNotNull(openId)) { SysAuthExample authExample = new SysAuthExample(); authExample.createCriteria().andIdentifyTypeEqualTo(identifyType) .andIdentifierEqualTo(openId); @@ -420,7 +421,7 @@ public class UserService implements IUserService { } if (CollectionUtil.isEmpty(authList)) { List sysAuthList = null; - if(ObjectUtil.isNotNull(unionId)) { + if (ObjectUtil.isNotNull(unionId)) { SysAuthExample sysAuthExample = new SysAuthExample(); sysAuthExample.createCriteria().andCredentialEqualTo(unionId); sysAuthList = authDao.selectByExample(sysAuthExample); @@ -478,6 +479,7 @@ public class UserService implements IUserService { /** * 获取企业微信信息,并绑定用户 + * * @param identifyType * @param param * @param user @@ -1032,6 +1034,7 @@ public class UserService implements IUserService { /** * 查询用户是否关注此项目 + * * @param currentUserId * @param projectId * @return @@ -1039,14 +1042,14 @@ public class UserService implements IUserService { @Override public Boolean getIsAttention(Long currentUserId, Long projectId) { Boolean isAttention = false; - if(ObjectUtil.isNotNull(sysProjectDao.selectByPrimaryKey(projectId))){ + if (ObjectUtil.isNotNull(sysProjectDao.selectByPrimaryKey(projectId))) { UserAttentionExample attentionExample = new UserAttentionExample(); attentionExample.createCriteria().andProjectIdEqualTo(projectId).andUserIdEqualTo(currentUserId); List attentionList = userAttentionDao.selectByExample(attentionExample); - if(CollectionUtil.isNotEmpty(attentionList)){ + if (CollectionUtil.isNotEmpty(attentionList)) { isAttention = true; } - }else { + } else { throw new BaseException(CodeEnum.NOT_PROJECT); } return isAttention; @@ -1054,6 +1057,7 @@ public class UserService implements IUserService { /** * 用户关注某个项目 + * * @param currentUserId * @param projectIdDto */ @@ -1061,11 +1065,11 @@ public class UserService implements IUserService { public void userAttentionProject(Long currentUserId, ProjectDto.ProjectIdDto projectIdDto) { //查找项目 SysProject project = sysProjectDao.selectByPrimaryKey(projectIdDto.getProjectId()); - if(ObjectUtil.isNotNull(project)){ + if (ObjectUtil.isNotNull(project)) { UserAttentionExample attentionExample = new UserAttentionExample(); attentionExample.createCriteria().andProjectIdEqualTo(projectIdDto.getProjectId()).andUserIdEqualTo(currentUserId); List attentionList = userAttentionDao.selectByExample(attentionExample); - if(CollectionUtil.isNotEmpty(attentionList)){ + if (CollectionUtil.isNotEmpty(attentionList)) { throw new BaseException(CodeEnum.ALREADY_ATTENTION); } UserAttention attention = new UserAttention(); @@ -1073,7 +1077,7 @@ public class UserService implements IUserService { attention.setUserId(currentUserId); attention.setProjectId(projectIdDto.getProjectId()); userAttentionDao.insertSelective(attention); - }else { + } else { throw new BaseException(CodeEnum.NOT_PROJECT); } } @@ -1245,7 +1249,7 @@ public class UserService implements IUserService { public UserVo.WxInfo updateUserInfo(Long currentUserId, UserDto.WxInfo userInfo) { //通过userid查找到用户 SysUser user = userDao.selectByPrimaryKey(currentUserId); - if(ObjectUtil.isNull(user)){ + if (ObjectUtil.isNull(user)) { throw new BaseException(CodeEnum.NOT_LOGIN); } @@ -1298,6 +1302,7 @@ public class UserService implements IUserService { /** * 更改绑定手机号(不要密码) + * * @param userId * @param phoneInfo * @throws Exception @@ -1325,7 +1330,7 @@ public class UserService implements IUserService { userSign.setUserId(phoneAuth.getUserId()); userSign.setAuthId(phoneAuth.getId()); - }else { + } else { //添加一条手机号认证信息 SysAuth sysAuth = new SysAuth(); sysAuth.setId(snowflake.nextId()); @@ -1347,4 +1352,48 @@ public class UserService implements IUserService { } 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 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 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(); + } } diff --git a/tall/src/main/java/com/ccsens/tall/web/UserController.java b/tall/src/main/java/com/ccsens/tall/web/UserController.java index 83f7df65..d7c2d958 100644 --- a/tall/src/main/java/com/ccsens/tall/web/UserController.java +++ b/tall/src/main/java/com/ccsens/tall/web/UserController.java @@ -302,7 +302,21 @@ public class UserController { 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 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 = "") @ApiImplicitParams({ @ApiImplicitParam(name="token",value = "token",required = true,paramType = "query") diff --git a/util/src/main/java/com/ccsens/util/wx/WxXcxUtil.java b/util/src/main/java/com/ccsens/util/wx/WxXcxUtil.java index 8d3178fd..d62f8429 100644 --- a/util/src/main/java/com/ccsens/util/wx/WxXcxUtil.java +++ b/util/src/main/java/com/ccsens/util/wx/WxXcxUtil.java @@ -249,24 +249,24 @@ public class WxXcxUtil { HttpsUtil.httpsRequest(url,"POST",postStr,path); } -// /** -// * 获取小程序二维码/小程序码 -// * @return -// * @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()); -// -// WechatCodeA wechatCodeA = new WechatCodeA(); -// wechatCodeA.path = page; -//// wechatCode.path = page+"?"+scene; -// -//// WechatCodeA.line_color = color; -// -// String postStr = JacksonUtil.beanToJson(wechatCodeA); -// System.out.println(postStr); -// HttpsUtil.httpsRequest(url,"POST",postStr,path); -// } + /** + * 获取小程序二维码/小程序码(长度128,有数量限制) + * @return + * @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()); + + WechatCodeA wechatCodeA = new WechatCodeA(); + wechatCodeA.path = page; +// wechatCode.path = page+"?"+scene; + +// WechatCodeA.line_color = color; + + String postStr = JacksonUtil.beanToJson(wechatCodeA); + System.out.println(postStr); + HttpsUtil.httpsRequest(url,"POST",postStr,path); + } /** * 统一下单 diff --git a/util/src/test/java/com/ccsens/util/Base64Test.java b/util/src/test/java/com/ccsens/util/Base64Test.java index e0f812f1..59d0db75 100644 --- a/util/src/test/java/com/ccsens/util/Base64Test.java +++ b/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 com.ccsens.util.wx.WxXcxUtil; import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import javax.servlet.ServletOutputStream; import java.io.ByteArrayOutputStream; @@ -29,8 +30,7 @@ public class Base64Test { //// out.write(decode); //// out.close(); // System.out.println(System.currentTimeMillis()); -// } - +// @Test public void test01() throws Exception{