From aec7e5d70964bcda81266a988ae8528afcab6a81 Mon Sep 17 00:00:00 2001 From: nies Date: Tue, 29 Mar 2022 18:57:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=80=9A=E8=BF=87=E6=89=8B?= =?UTF-8?q?=E6=9C=BA=E5=8F=B7=E6=89=B9=E9=87=8F=E5=8F=91=E9=80=81=E9=92=89?= =?UTF-8?q?=E9=92=89=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/base/WhiteController.java | 243 ++++++++++++++++++--- 1 file changed, 215 insertions(+), 28 deletions(-) diff --git a/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/controller/base/WhiteController.java b/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/controller/base/WhiteController.java index 506b441..6910a76 100644 --- a/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/controller/base/WhiteController.java +++ b/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/controller/base/WhiteController.java @@ -29,12 +29,10 @@ import cn.estsh.i3plus.pojo.model.common.ImppEmail; import cn.estsh.i3plus.pojo.model.common.ImppSmsContent; import cn.estsh.i3plus.pojo.model.common.UserModel; import cn.estsh.i3plus.pojo.model.license.ImppLicense; -import cn.estsh.i3plus.pojo.model.platform.SysLoginModel; -import cn.estsh.i3plus.pojo.model.platform.SysRoleModel; -import cn.estsh.i3plus.pojo.model.platform.UserDetailModel; -import cn.estsh.i3plus.pojo.model.platform.UserDetailPagerModel; +import cn.estsh.i3plus.pojo.model.platform.*; import cn.estsh.i3plus.pojo.platform.bean.*; -import cn.estsh.i3plus.sdk.dingtalk.cn.estsh.i3plus.sdk.service.IDingTalkService; +import cn.estsh.i3plus.sdk.dingtalk.dingrobot.DingRobotRequestBody; +import cn.estsh.i3plus.sdk.dingtalk.service.IDingTalkService; import cn.estsh.impp.framework.base.controller.CoreBaseController; import cn.estsh.impp.framework.boot.auth.AuthUtil; import cn.estsh.impp.framework.boot.exception.ImppBusiException; @@ -42,10 +40,24 @@ import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder; import cn.estsh.impp.framework.boot.license.ImppLicenseTool; import cn.estsh.impp.framework.boot.license.serviceimpl.ImppLicenseDecoder; import cn.estsh.impp.framework.boot.util.*; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest; +import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse; +import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTOHeaders; +import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTORequest; +import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTOResponse; +import com.aliyun.tea.TeaException; +import com.aliyun.teaopenapi.models.Config; +import com.aliyun.teautil.models.RuntimeOptions; +import com.dingtalk.api.DefaultDingTalkClient; +import com.dingtalk.api.DingTalkClient; +import com.dingtalk.api.request.OapiV2UserGetbymobileRequest; import com.dingtalk.api.response.OapiV2UserGetResponse; +import com.dingtalk.api.response.OapiV2UserGetbymobileResponse; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; +import com.taobao.api.ApiException; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; @@ -320,6 +332,7 @@ public class WhiteController extends CoreBaseController { return ResultBean.success("查询成功").setResultList(menuList); } + @GetMapping(value = "/auth/dingtalk/login") @ApiOperation(value = "登录", notes = "登录") public ResultBean loginByPhoneNumber(HttpServletRequest request, String dingTalkTmpAuthCode, @@ -422,7 +435,7 @@ public class WhiteController extends CoreBaseController { if (e.getCause() instanceof ImppBusiException) { ImppBusiException imppBusiException = (ImppBusiException) e.getCause(); result.setErrorMsg(LocaleUtils.getLocaleRes(imppBusiException.getErrorDetail())); - }else { + } else { result.setErrorMsg(e.getCause().getMessage()); } } else { @@ -440,8 +453,8 @@ public class WhiteController extends CoreBaseController { userLoginStatus = CommonEnumUtil.USER_LOGIN_STATUS.SYSTEM_ERROR; LOGGER.warn("Impp业务异常Exception 登录失败 ", e); } finally { - if (result != null && !result.isSuccess() ){ - if(CommonEnumUtil.USER_LOGIN_STATUS.LOGIN_SUCCESS.getValue() == userLoginStatus.getValue()){ + if (result != null && !result.isSuccess()) { + if (CommonEnumUtil.USER_LOGIN_STATUS.LOGIN_SUCCESS.getValue() == userLoginStatus.getValue()) { userLoginStatus = CommonEnumUtil.USER_LOGIN_STATUS.SYSTEM_ERROR; } } @@ -449,7 +462,7 @@ public class WhiteController extends CoreBaseController { recordSysUserLog(null, loginModel.getLoginName(), userLoginStatus.getValue()); } - LOGGER.info("会员{}登陆登录完成,登录{},语言:{} , 登录耗时:{}", loginModel.getLoginName(),result.isSuccess()? "成功":"失败", BaseThreadLocal.getThreadLanguageCode(), (System.currentTimeMillis() - startTime)); + LOGGER.info("会员{}登陆登录完成,登录{},语言:{} , 登录耗时:{}", loginModel.getLoginName(), result.isSuccess() ? "成功" : "失败", BaseThreadLocal.getThreadLanguageCode(), (System.currentTimeMillis() - startTime)); return result; } @@ -956,14 +969,15 @@ public class WhiteController extends CoreBaseController { /** * 根据登录名,邮箱获取用户账号信息 + * * @param sysUser * @return */ @PostMapping(value = "/user/query-user") - @ApiOperation(value = "根据条件查询单个用户信息",notes = "根据条件查询单个用户信息") - public ResultBean querySysUser(@RequestBody SysUser sysUser){ - ValidatorBean.checkNotNull(sysUser.getUserLoginName(),"用户登录名不能为空!"); - ValidatorBean.checkNotNull(sysUser.getUserEmail(),"用户email不能为空!"); + @ApiOperation(value = "根据条件查询单个用户信息", notes = "根据条件查询单个用户信息") + public ResultBean querySysUser(@RequestBody SysUser sysUser) { + ValidatorBean.checkNotNull(sysUser.getUserLoginName(), "用户登录名不能为空!"); + ValidatorBean.checkNotNull(sysUser.getUserEmail(), "用户email不能为空!"); SysUser user = null; try { user = sysUserService.getSysUserByLoginNameAndEmail(sysUser.getUserLoginName(), sysUser.getUserEmail()); @@ -975,9 +989,9 @@ public class WhiteController extends CoreBaseController { @PostMapping(value = "/sendEmail") @ApiOperation(value = "无需登录,发送邮件") - public ResultBean sendEmail(@RequestBody ImppEmail imppEmail){ - ValidatorBean.checkNotNull(imppEmail.getContent(),"消息内容不能为空"); - ValidatorBean.checkNotNull(imppEmail.getEmail(),"email地址不能为空"); + public ResultBean sendEmail(@RequestBody ImppEmail imppEmail) { + ValidatorBean.checkNotNull(imppEmail.getContent(), "消息内容不能为空"); + ValidatorBean.checkNotNull(imppEmail.getEmail(), "email地址不能为空"); try { MailUtil mailUtil = new MailUtil(); mailUtil.init(); @@ -988,25 +1002,25 @@ public class WhiteController extends CoreBaseController { mailUtil.setBody(imppEmail.getContent()); mailUtil.send(); } catch (Exception e) { - LOGGER.error("发送邮件:{}失败:{}",imppEmail.getEmail(),e.getMessage()); - return ResultBean.fail("发送失败,请检查邮箱是否输入正确!"); + LOGGER.error("发送邮件:{}失败:{}", imppEmail.getEmail(), e.getMessage()); + return ResultBean.fail("发送失败,请检查邮箱是否输入正确!"); } return ResultBean.success("验证码已发送!"); } @PostMapping(value = "/update-vendor-pwd") @ApiOperation(value = "忘记密码") - public ResultBean updateVendorPwd(@RequestBody UserModel userModel){ - ValidatorBean.checkNotNull(userModel.getLoginName(),"登录名不能为空!"); - ValidatorBean.checkNotNull(userModel.getPassword(),"密码不能为空!"); - sysUserService.updateSysUserPwdByLoginName(userModel.getLoginName(),userModel.getPassword()); + public ResultBean updateVendorPwd(@RequestBody UserModel userModel) { + ValidatorBean.checkNotNull(userModel.getLoginName(), "登录名不能为空!"); + ValidatorBean.checkNotNull(userModel.getPassword(), "密码不能为空!"); + sysUserService.updateSysUserPwdByLoginName(userModel.getLoginName(), userModel.getPassword()); return ResultBean.success("修改成功!"); } @PostMapping("/sendSms") @ApiOperation("无需登录 发送短信") - public ResultBean sendSms(@RequestBody ImppSmsContent imppSmsContent){ - return smsSendRecordController.send(imppSmsContent); + public ResultBean sendSms(@RequestBody ImppSmsContent imppSmsContent) { + return smsSendRecordController.send(imppSmsContent); } @@ -1026,8 +1040,8 @@ public class WhiteController extends CoreBaseController { } //过滤SysRefUserRole roleList = sysRoleService.findAllByRoleProperty(sysRole); - if (ObjectUtils.isEmpty(roleList)){ - return ResultBean.success("操作成功"); + if (ObjectUtils.isEmpty(roleList)) { + return ResultBean.success("操作成功"); } List roleIdList = roleList.stream().map(el -> el.getId()).collect(Collectors.toList()); @@ -1044,13 +1058,186 @@ public class WhiteController extends CoreBaseController { * 根据登录的用户名批量修改账号状态 * * @param userLoginNames 用户名数组 - * @param status 状态 + * @param status 状态 * @return 处理结果 */ @PutMapping(value = "/batch-status/userLoginNames") @ApiOperation(value = "根据用户名批量修改账号状态", notes = "根据用户名批量修改账号状态") public ResultBean updateBatchStatusByUserLoginNames(String[] userLoginNames, Integer status) { - return sysUserController.updateBatchStatusByUserLoginNames(userLoginNames,status); + return sysUserController.updateBatchStatusByUserLoginNames(userLoginNames, status); + } + + + private static final Long ACCESS_TOKEN_EXPIRE_TIME = 7080L; + private static final String ACCESS_TOKEN_KEY = "ding_talk:access_token"; + + /** + * 钉钉单聊机器人批量发送消息 + * @param dingSendBatchRequestModel + * @return + */ + @PostMapping(value = "/dingRobot/sendBatchRequest") + @ApiOperation(value = "根据手机号 钉钉单聊机器人批量发送消息", notes = "根据手机号 钉钉单聊机器人批量发送消息") + public ResultBean singleRobotsendBatchRequest(@RequestBody DingSendBatchRequestModel dingSendBatchRequestModel ) { + if(ObjectUtils.isEmpty(dingSendBatchRequestModel)){ + return ResultBean.fail("消息内容不能为空"); + } + List phoneNumberList = dingSendBatchRequestModel.getPhoneNumberList(); + String title = dingSendBatchRequestModel.getTitle(); + String content = dingSendBatchRequestModel.getContent(); + if (StringUtils.isBlank(content)) { + return ResultBean.fail("消息内容不能为空"); + } + if (ObjectUtils.isEmpty(phoneNumberList)) { + return ResultBean.fail("手机号不能为空"); + } + phoneNumberList = phoneNumberList.stream().filter(StringUtils::isNotBlank).collect(Collectors.toList()); + if (ObjectUtils.isEmpty(phoneNumberList)) { + return ResultBean.fail("手机号不能为空"); + } + Config config = new Config(); + config.protocol = "https"; + config.regionId = "central"; + com.aliyun.dingtalkrobot_1_0.Client client = null; + try { + client = new com.aliyun.dingtalkrobot_1_0.Client(config); + } catch (Exception e) { + e.printStackTrace(); + return ResultBean.fail(e).setMsg("发送失败"); + } + BatchSendOTOHeaders batchSendOTOHeaders = new BatchSendOTOHeaders(); + batchSendOTOHeaders.xAcsDingtalkAccessToken = getAccessToken(); + BatchSendOTORequest batchSendOTORequest = new BatchSendOTORequest(); + List userIdList = new ArrayList(); + phoneNumberList.forEach(el -> { + ResultBean result = getDingUserIdByPhoneNumber(el, batchSendOTOHeaders.xAcsDingtalkAccessToken); + if (result.isSuccess()) { + userIdList.add((String) result.getResultObject()); + } + + }); + if (userIdList.isEmpty()) { + return ResultBean.fail("发送失败,无法根据手机号获取用户id"); + } +// userIdList.add("27202755621041923"); +// userIdList.add("070537336424114523"); + + String dingTalkAppKey = RedisCacheTool.getSysConfigStrVal(PlatformConstWords.DINGTALK_APP_KEY); + batchSendOTORequest.setRobotCode(dingTalkAppKey); + batchSendOTORequest.setUserIds(userIdList); + // markdown officialImageMsg + DingRobotRequestBody requestBody = generateMarkdown(title, content); + batchSendOTORequest.setMsgKey("sampleMarkdown"); + batchSendOTORequest.setMsgParam(JSON.toJSONString(requestBody.getMarkdown())); + try { + BatchSendOTOResponse response = client.batchSendOTOWithOptions(batchSendOTORequest, batchSendOTOHeaders, new RuntimeOptions()); + System.out.println(JSON.toJSONString(response)); + } catch (TeaException err) { + err.printStackTrace(); + if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) { + // err 中含有 code 和 message 属性,可帮助开发定位问题 + } + return ResultBean.fail(err).setMsg("发送失败"); + + } catch (Exception _err) { + _err.printStackTrace(); + TeaException err = new TeaException(_err.getMessage(), _err); + if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) { + // err 中含有 code 和 message 属性,可帮助开发定位问题 + } + return ResultBean.fail(err).setMsg("发送失败"); + } + return ResultBean.success("发送成功"); + } + + private DingRobotRequestBody generateMarkdown(String title, String content) { + DingRobotRequestBody dingRobotRequestBody = new DingRobotRequestBody(); + DingRobotRequestBody.MarkDown markDown = new DingRobotRequestBody.MarkDown(); + markDown.setTitle(title); + dingRobotRequestBody.setMsgType("markdown"); +// markDown.setText(" # Andon 通知" + +// "\n"+ +// "\n"+ +// " [![qGTEi6.png](https://s1.ax1x.com/2022/03/24/qGTEi6.png)](https://imgtu.com/i/qGTEi6) EP产线-01工位,物料缺料,请及时处理 " +// ); +// markDown.setText(" # Andon 通知" + +// "\n"+ +// "\n"+ +// " [![qGTEi6.png](https://s1.ax1x.com/2022/03/24/qGTEi6.png)](https://imgtu.com/i/qGTEi6) EP产线-01工位,物料缺料,请及时处理 " +// ); +// markDown.setText(" ## Andon 通知" + +// "\n"+ +// "\n"+ +// " [![qGTEi6.png](https://s1.ax1x.com/2022/03/24/qGTEi6.png)](https://imgtu.com/i/qGTEi6) EP产线-01工位,物料缺料,处理措施:联络供应商送货" +// ); + markDown.setText(content); + dingRobotRequestBody.setMarkDown(markDown); + return dingRobotRequestBody; + } + + /** + * 根据手机号获取钉钉的uid + * + * @param phoneNumber + * @return + */ + public ResultBean getDingUserIdByPhoneNumber(String phoneNumber, String accessToken) { + try { + DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile"); + OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest(); + req.setMobile(phoneNumber); +// req.setMobile("13298408382"); + OapiV2UserGetbymobileResponse rsp = client.execute(req, accessToken); + return ResultBean.success("获取用户id成功").setResultObject(rsp.getResult().getUserid()); + } catch (ApiException e) { + e.printStackTrace(); + return ImppExceptionBuilder.newInstance().buildExceptionResult(e); + } + } + + /** + * 获取accessToken + * + * @return + */ + private String getAccessToken() { + Object accessTokenCached = redisCore.getObject(ACCESS_TOKEN_KEY); + if (accessTokenCached != null) { + return String.valueOf(accessTokenCached); + } + Config config = new Config(); + config.protocol = "https"; + config.regionId = "central"; + com.aliyun.dingtalkoauth2_1_0.Client client = null; + String dingTalkAppKey = RedisCacheTool.getSysConfigStrVal(PlatformConstWords.DINGTALK_APP_KEY); + String dingTalkAppSecret = RedisCacheTool.getSysConfigStrVal(PlatformConstWords.DINGTALK_APP_SECRET); + try { + client = new com.aliyun.dingtalkoauth2_1_0.Client(config); + } catch (Exception e) { + e.printStackTrace(); + } + GetAccessTokenRequest getAccessTokenRequest = new GetAccessTokenRequest() + .setAppKey(dingTalkAppKey) + .setAppSecret(dingTalkAppSecret); + try { + GetAccessTokenResponse response = client.getAccessToken(getAccessTokenRequest); + if (!StringUtils.isEmpty(response.body.accessToken)) { + redisCore.putObject(ACCESS_TOKEN_KEY, response.body.accessToken, ACCESS_TOKEN_EXPIRE_TIME); + } + return response.body.accessToken; + } catch (TeaException err) { + if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) { + // err 中含有 code 和 message 属性,可帮助开发定位问题 + } + + } catch (Exception _err) { + TeaException err = new TeaException(_err.getMessage(), _err); + if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) { + // err 中含有 code 和 message 属性,可帮助开发定位问题 + } + + } + return ""; }