From 097ceed2faac7b5c517a77ad18d48e654ec8fa28 Mon Sep 17 00:00:00 2001 From: wulin Date: Tue, 25 Aug 2020 14:23:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0token=E7=9A=84code=E5=92=8Cco?= =?UTF-8?q?detime?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/lz/common/utils/DingTalkUtil.java | 12 +++- src/main/java/com/lz/config/ShiroConfig.java | 1 + .../controller/ResultRecordController.java | 36 ++++++++++- .../job/business/DingtalkBusiness.java | 59 +++++++++++++++++-- .../modules/job/task/DingtalkSynDataJob.java | 4 +- .../lz/modules/sys/dao/SysUserTokenDao.java | 3 + .../sys/entity/SysUserTokenEntity.java | 22 +++++++ .../sys/service/SysUserTokenService.java | 1 + .../app/impl/ResultRecordServiceImpl.java | 11 +++- .../service/impl/SysUserTokenServiceImpl.java | 36 +++++++++++ .../controller/DingTalkLoginController.java | 13 ++-- .../ThirdMsgSendRecordController.java | 5 +- src/main/resources/application-dev.yml | 4 ++ src/main/resources/application-prod.yml | 4 ++ src/main/resources/application-test.yml | 4 ++ .../resources/mapper/sys/SysUserTokenDao.xml | 8 ++- 16 files changed, 203 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/lz/common/utils/DingTalkUtil.java b/src/main/java/com/lz/common/utils/DingTalkUtil.java index 1799f78e..2c029589 100644 --- a/src/main/java/com/lz/common/utils/DingTalkUtil.java +++ b/src/main/java/com/lz/common/utils/DingTalkUtil.java @@ -92,9 +92,15 @@ public class DingTalkUtil { * @throws IOException */ public String getAccessToken(String appid) { - try { + ThirdAppConfig thirdAppConfig = thirdAppConfigService.getByAppId(appid); + return getAccessTokenWitchEntity(thirdAppConfig); + + } + + public String getAccessTokenWitchEntity(ThirdAppConfig thirdAppConfig) { + try { if(thirdAppConfig != null){ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken"); OapiGettokenRequest req = new OapiGettokenRequest(); @@ -103,7 +109,9 @@ public class DingTalkUtil { req.setHttpMethod("GET"); OapiGettokenResponse rsp = client.execute(req); String resultStr = rsp.getBody(); + logger.info("钉钉请求返回", rsp.getBody()); JSONObject dataObj = JSON.parseObject(resultStr); + String tenant_access_token = dataObj.getString("access_token"); return tenant_access_token; }else{ @@ -347,7 +355,7 @@ public class DingTalkUtil { user.setType(1); user.setRealName(staffEntity.getName()); user.setUserNo(staffEntity.getMobile()); - return sysUserTokenService.createToken(user); + return sysUserTokenService.createTokenSetTokenCode(user, code); } return R.error("用户不存在"); } diff --git a/src/main/java/com/lz/config/ShiroConfig.java b/src/main/java/com/lz/config/ShiroConfig.java index 7337a0bb..e0f719a2 100644 --- a/src/main/java/com/lz/config/ShiroConfig.java +++ b/src/main/java/com/lz/config/ShiroConfig.java @@ -65,6 +65,7 @@ public class ShiroConfig { filterMap.put("/dtlg/login", "anon"); filterMap.put("/dtlg/luck", "anon"); filterMap.put("/dtlg/look", "anon"); + filterMap.put("/dtlg/jump", "anon"); filterMap.put("/luck/getLuckById", "anon"); filterMap.put("/luck/updateLuck", "anon"); filterMap.put("/**", "oauth2"); diff --git a/src/main/java/com/lz/modules/app/controller/ResultRecordController.java b/src/main/java/com/lz/modules/app/controller/ResultRecordController.java index d14b3e45..5c41849c 100644 --- a/src/main/java/com/lz/modules/app/controller/ResultRecordController.java +++ b/src/main/java/com/lz/modules/app/controller/ResultRecordController.java @@ -1,5 +1,6 @@ package com.lz.modules.app.controller; +import com.lz.common.emun.WorkMsgTypeEnum; import com.lz.common.utils.*; import com.lz.modules.app.dto.DepartmentsDto; import com.lz.modules.app.dto.StaffDepartmentDto; @@ -16,6 +17,7 @@ import com.lz.modules.flow.model.Auth; import com.lz.modules.flow.req.ResultDetailReq; import com.lz.modules.flow.service.FlowRecordService; import com.lz.modules.flow.service.RecordAuthService; +import com.lz.modules.job.business.DingtalkBusiness; import com.lz.modules.sys.controller.AbstractController; import com.lz.modules.sys.entity.SysUserEntity; import com.lz.modules.sys.entity.app.ResultComment; @@ -28,12 +30,14 @@ import org.apache.commons.collections.CollectionUtils; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.math.BigDecimal; +import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; @@ -69,6 +73,15 @@ public class ResultRecordController extends AbstractController { @Autowired private FlowRecordService flowRecordService; + @Autowired + DingtalkBusiness dingtalkBusiness; + + @Value("${dingtalk.appid}") + private String appid; + + @Value("${domain.main}") + private String domain; + /** * 列表 @@ -245,10 +258,31 @@ public class ResultRecordController extends AbstractController { flowRecordService.updateFlowRecordById(flowRecord); } } + StaffEntity mySelf = staffService.selectStaffById(resultRecord.getStaffId()); + r = R.ok("成功") + .put("from", mySelf) + .put("to", mySelf) + .put("type", WorkMsgTypeEnum.REJECT); } } resultCommentService.addOrUpdateComment(req, getUserId(), status); - return r != null ? r : R.ok("成功"); + if(r != null && (int)r.get("code") == 0){//下面推送消息 + StaffEntity mySelf = (StaffEntity)r.get("from"); + StaffEntity toSelf = (StaffEntity)r.get("to"); + WorkMsgTypeEnum workMsgTypeEnum = (WorkMsgTypeEnum)r.get("type"); + String url = domain + "/management/dingtalklogin?url=";//免登接口 + String jump = domain + "/management/recorddetail?id=" + req.getRecordResultId() + + "&recordType=3" ;//跳转接口 + jump = URLEncoder.encode(jump); + String msg = dingtalkBusiness.sendWorkMSGByEntity(appid, mySelf, toSelf, workMsgTypeEnum, url + jump); + if(msg.equals("OK")){ + return r; + }else{ + return R.error(msg); + } + } + return R.ok("成功"); + //return r != null ? r : R.ok("成功"); } /** diff --git a/src/main/java/com/lz/modules/job/business/DingtalkBusiness.java b/src/main/java/com/lz/modules/job/business/DingtalkBusiness.java index ae427989..3b613260 100644 --- a/src/main/java/com/lz/modules/job/business/DingtalkBusiness.java +++ b/src/main/java/com/lz/modules/job/business/DingtalkBusiness.java @@ -21,6 +21,10 @@ import com.lz.modules.app.service.StaffOccupationService; import com.lz.modules.app.service.StaffService; import com.lz.modules.job.model.responseBo.DepartmentInfosBo; import com.lz.modules.job.model.responseBo.DepartmentStaffBo; +import com.lz.modules.sys.dao.SysUserTokenDao; +import com.lz.modules.sys.entity.SysUserTokenEntity; +import com.lz.modules.third.entity.ThirdAppConfig; +import com.lz.modules.third.service.ThirdAppConfigService; import com.mchange.lang.IntegerUtils; import com.taobao.api.ApiException; import org.slf4j.LoggerFactory; @@ -28,6 +32,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.annotation.Resource; +import java.net.URLEncoder; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -41,7 +47,8 @@ import java.util.Map; @Component(value = "dingtalkBusiness") public class DingtalkBusiness { protected final static org.slf4j.Logger logger = LoggerFactory.getLogger(DingtalkBusiness.class); - + //token过期时间12小时 + private final static int EXPIRE = 3600 * 12; @Autowired DingTalkUtil dingTalkUtil; @@ -57,6 +64,12 @@ public class DingtalkBusiness { @Resource StaffOccupationService staffOccupationService; + @Autowired + ThirdAppConfigService thirdAppConfigService; + + @Autowired + private SysUserTokenDao sysUserTokenDao; + @Resource StaffDao staffDao; @@ -180,24 +193,40 @@ public class DingtalkBusiness { //获取Token StaffEntity toStaffEntity = staffService.selectStaffById(toStaffid); StaffEntity fromStaffEntity = staffService.selectStaffById(fromStaffid); + + WorkMsgTypeEnum workMsgTypeEnum = WorkMsgTypeEnum.findRoleTypeByCode(type); + return sendWorkMSGByEntity(appid, fromStaffEntity, toStaffEntity, workMsgTypeEnum, "https://www.baidu.com"); + } + + public String sendWorkMSGByEntity(String appid, StaffEntity fromStaffEntity, StaffEntity toStaffEntity, + WorkMsgTypeEnum workMsgTypeEnum, String url){ if(toStaffEntity != null && fromStaffEntity != null){ - WorkMsgTypeEnum workMsgTypeEnum = WorkMsgTypeEnum.findRoleTypeByCode(type); - String token = dingTalkUtil.getAccessToken(appid); + ThirdAppConfig thirdAppConfig = thirdAppConfigService.getByAppId(appid); + String token = dingTalkUtil.getAccessTokenWitchEntity(thirdAppConfig); if(token != null && token.length() > 0){ + //下面防止第二次发送消息时钉钉不推送 + if(url.contains("?")){ + url += "&halokit=" + System.currentTimeMillis(); + }else{ + url += "?halokit=" + System.currentTimeMillis(); + } + url = URLEncoder.encode(url); + url = "dingtalk://dingtalkclient/action/openapp?corpid=" + thirdAppConfig.getCorpId() + + "&container_type=work_platform&app_id=0_" + appid + "&redirect_type=jump&redirect_url=" + url; + String content = workMsgTypeEnum.getContent().replace("@", fromStaffEntity.getName()); return dingTalkUtil.sendSingleActionCardMSG(appid, toStaffEntity, workMsgTypeEnum.getTitle(), - content, workMsgTypeEnum.getBtnText(), "https://www.baidu.com?time=" + System.currentTimeMillis(), token); - + content, workMsgTypeEnum.getBtnText(), url, token); }else{ return "token无效"; } } return "无相关人员信息"; - } public R login(String code, String appid) { + logger.info("钉钉登录appid{}", appid); String token = dingTalkUtil.getAccessToken(appid); if(token != null && token.length() > 0){ return dingTalkUtil.getUserIdByCode(code, token); @@ -212,4 +241,22 @@ public class DingtalkBusiness { } return R.error("授权失败,未授权"); } + + public R jump(String code) { + SysUserTokenEntity tokenEntity = sysUserTokenDao.getByTokenCode(code); + if(tokenEntity != null){//token不存在或者超过2分钟 + long big = System.currentTimeMillis() - tokenEntity.getCodeTime().getTime(); + logger.info("跳转的时间差{}", big); + if(big < 120000L){ + //这里不更新时间了 + tokenEntity.setTokenCode(""); + sysUserTokenDao.updateById(tokenEntity); + long expire = tokenEntity.getExpireTime().getTime() - System.currentTimeMillis(); + return R.ok().put("token", tokenEntity.getToken()).put("expire", expire); + } + + } + logger.info("token的token_code不存在"); + return R.error("未授权登录"); + } } diff --git a/src/main/java/com/lz/modules/job/task/DingtalkSynDataJob.java b/src/main/java/com/lz/modules/job/task/DingtalkSynDataJob.java index fedb5c50..ca424ac9 100644 --- a/src/main/java/com/lz/modules/job/task/DingtalkSynDataJob.java +++ b/src/main/java/com/lz/modules/job/task/DingtalkSynDataJob.java @@ -14,6 +14,7 @@ import com.lz.modules.job.business.FeishuBusiness; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** @@ -29,7 +30,8 @@ public class DingtalkSynDataJob implements ITask { @Autowired DingtalkBusiness dingtalkBusiness; - static String appid = "856016278"; + @Value("${dingtalk.appid}") + private String appid; @Override public void run(String params) { diff --git a/src/main/java/com/lz/modules/sys/dao/SysUserTokenDao.java b/src/main/java/com/lz/modules/sys/dao/SysUserTokenDao.java index 38290580..bbaa423e 100644 --- a/src/main/java/com/lz/modules/sys/dao/SysUserTokenDao.java +++ b/src/main/java/com/lz/modules/sys/dao/SysUserTokenDao.java @@ -11,6 +11,7 @@ package com.lz.modules.sys.dao; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.lz.modules.sys.entity.SysUserTokenEntity; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; /** * 系统用户Token @@ -23,4 +24,6 @@ public interface SysUserTokenDao extends BaseMapper { SysUserTokenEntity queryByToken(String token); Long insertBySysUserTokenEntity(SysUserTokenEntity tokenEntity); + + SysUserTokenEntity getByTokenCode(@Param("code") String code); } diff --git a/src/main/java/com/lz/modules/sys/entity/SysUserTokenEntity.java b/src/main/java/com/lz/modules/sys/entity/SysUserTokenEntity.java index 39f5e481..6dba3f42 100644 --- a/src/main/java/com/lz/modules/sys/entity/SysUserTokenEntity.java +++ b/src/main/java/com/lz/modules/sys/entity/SysUserTokenEntity.java @@ -37,6 +37,10 @@ public class SysUserTokenEntity implements Serializable { private Date updateTime; //0表示系统用户,1表示普通员工 private Integer type; + //token对应的code,一次查询有效,查询后置空 + private String tokenCode; + //toke_code对应添加的时间超过该事件+3分钟失效 + private Date codeTime; /** * * @return @@ -112,6 +116,22 @@ public class SysUserTokenEntity implements Serializable { this.type = type; } + public String getTokenCode() { + return tokenCode; + } + + public void setTokenCode(String tokenCode) { + this.tokenCode = tokenCode; + } + + public Date getCodeTime() { + return codeTime; + } + + public void setCodeTime(Date codeTime) { + this.codeTime = codeTime; + } + @Override public String toString() { return "SysUserToken{" + @@ -120,6 +140,8 @@ public class SysUserTokenEntity implements Serializable { ",expireTime=" + expireTime + ",updateTime=" + updateTime + ",type=" + type + + ",tokenCode=" + tokenCode + + ",codeTime=" + codeTime + "}"; } diff --git a/src/main/java/com/lz/modules/sys/service/SysUserTokenService.java b/src/main/java/com/lz/modules/sys/service/SysUserTokenService.java index cff603ca..3dfb4fe2 100644 --- a/src/main/java/com/lz/modules/sys/service/SysUserTokenService.java +++ b/src/main/java/com/lz/modules/sys/service/SysUserTokenService.java @@ -32,4 +32,5 @@ public interface SysUserTokenService extends IService { */ void logout(long userId); + R createTokenSetTokenCode(SysUserEntity user, String code); } diff --git a/src/main/java/com/lz/modules/sys/service/app/impl/ResultRecordServiceImpl.java b/src/main/java/com/lz/modules/sys/service/app/impl/ResultRecordServiceImpl.java index 0a5dab9b..c96d70c6 100644 --- a/src/main/java/com/lz/modules/sys/service/app/impl/ResultRecordServiceImpl.java +++ b/src/main/java/com/lz/modules/sys/service/app/impl/ResultRecordServiceImpl.java @@ -2,6 +2,7 @@ package com.lz.modules.sys.service.app.impl; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.lz.common.emun.WorkMsgTypeEnum; import com.lz.common.utils.*; import com.lz.modules.app.dao.DepartmentsDao; import com.lz.modules.app.dao.DepartmentsStaffRelateDao; @@ -332,7 +333,10 @@ public class ResultRecordServiceImpl extends ServiceImpl luckRecords = luckRecordService.selectLuckRecordByLuckId(luckId); String value = "暂无人员报名"; if(luckRecords.size() > 0){ - /* -

- 姓名:吴林 是否中将:中将 -

*/ value = ""; for (int i = 0; i < luckRecords.size(); i++){ LuckRecord luckRecord = luckRecords.get(i); diff --git a/src/main/java/com/lz/modules/third/controller/ThirdMsgSendRecordController.java b/src/main/java/com/lz/modules/third/controller/ThirdMsgSendRecordController.java index d527c7e4..1a7ef49d 100644 --- a/src/main/java/com/lz/modules/third/controller/ThirdMsgSendRecordController.java +++ b/src/main/java/com/lz/modules/third/controller/ThirdMsgSendRecordController.java @@ -9,6 +9,7 @@ import com.lz.modules.job.business.DingtalkBusiness; import com.lz.modules.third.entity.ThirdMsgSendRecord; import com.lz.modules.third.service.ThirdMsgSendRecordService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import java.util.HashMap; @@ -24,8 +25,8 @@ public class ThirdMsgSendRecordController { @Autowired private DingtalkBusiness dingtalkBusiness; - - static String appid = "856016278"; + @Value("${dingtalk.appid}") + private String appid; @RequestMapping("/list") public R list(@RequestBody String body) { diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 16e8aaaa..f3c0b9d9 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -30,6 +30,10 @@ spring: multi-statement-allow: true console: env: dev +dingtalk: + appid: 855818566 +domain: + main: "http://192.168.43.94:8001" ##多数据源的配置 #dynamic: diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 1633607c..b6ac0e3a 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -30,6 +30,10 @@ spring: multi-statement-allow: true console: env: prod +dingtalk: + appid: 856016278 +domain: + main: "https://lzmanagement.ldxinyong.com" ##多数据源的配置 #dynamic: diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml index b7d2b667..be50f689 100644 --- a/src/main/resources/application-test.yml +++ b/src/main/resources/application-test.yml @@ -30,6 +30,10 @@ spring: multi-statement-allow: true console: env: test +dingtalk: + appid: 855818566 +domain: + main: "http:/localhost" ##多数据源的配置 #dynamic: diff --git a/src/main/resources/mapper/sys/SysUserTokenDao.xml b/src/main/resources/mapper/sys/SysUserTokenDao.xml index 51b2b142..47abba9f 100644 --- a/src/main/resources/mapper/sys/SysUserTokenDao.xml +++ b/src/main/resources/mapper/sys/SysUserTokenDao.xml @@ -10,12 +10,16 @@ token, expire_time, type, + token_code, + code_time, update_time )values( #{userId}, #{token}, #{expireTime}, #{type}, + tokenCode, + codeTime, now() ) @@ -25,6 +29,8 @@ select * from sys_user_token where token = #{value} - +