diff --git a/src/main/java/com/zh/project0512/controller/manage/VideoController.java b/src/main/java/com/zh/project0512/controller/manage/VideoController.java index d09f0fc..e27ef34 100644 --- a/src/main/java/com/zh/project0512/controller/manage/VideoController.java +++ b/src/main/java/com/zh/project0512/controller/manage/VideoController.java @@ -7,15 +7,13 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.zh.project0512.annotation.AdminCheckAuthorityAnnotation; import com.zh.project0512.annotation.AdminTokenValid; import com.zh.project0512.mapper.UserEffectRecordsMapper; -import com.zh.project0512.model.AppMessage; -import com.zh.project0512.model.UserEffectRecords; -import com.zh.project0512.model.Video; -import com.zh.project0512.model.VideoEffectSetting; +import com.zh.project0512.model.*; import com.zh.project0512.service.IUserEffectRecordsService; import com.zh.project0512.service.IUserService; import com.zh.project0512.service.IVideoEffectSettingService; import com.zh.project0512.service.IVideoService; import com.zh.project0512.utils.AppMessageUtil; +import com.zh.project0512.utils.HttpUtil; import com.zh.project0512.utils.MybatisPlusUtil; import com.zh.project0512.utils.result.HttpStatusEnum; import com.zh.project0512.utils.result.Result; @@ -54,6 +52,8 @@ public class VideoController { private IVideoEffectSettingService videoEffectSettingService; @Autowired private UserEffectRecordsMapper userEffectRecordsMapper; + @Autowired + private HttpUtil httpUtil; @Data static class VParam { @@ -165,7 +165,7 @@ public class VideoController { up.set("effectResult", effectResult); up.set("updateAt", LocalDateTime.now()); videoService.update(up); - userEffectRecordsMapper.saveEffectResult(new UserEffectRecords().setUserId(video.getUserId()).setEffectResultTran(effectResult-originEffectResult).setCreateAt(LocalDateTime.now())); + userEffectRecordsMapper.saveEffectResult(new UserEffectRecords().setUserId(video.getUserId()).setEffectResultTran(effectResult - originEffectResult).setCreateAt(LocalDateTime.now())); String platForm = "未知平台"; switch (video.getType()) { case 1: @@ -182,8 +182,20 @@ public class VideoController { break; default: } - AppMessageUtil.sendMessage(new AppMessage().setTitle("传播效果审核结果").setDescription("恭喜您," + platForm + "传播效果审核通过,平台评定传播值为" + effectResult + "。") - .setUrl("pages/quest/subpage/detail/detail?id=" + video.getTaskId()).setReceiverId(video.getUserId())); + AppMessage appMessage = new AppMessage().setTitle("传播效果审核结果") + .setDescription("恭喜您," + platForm + "传播效果审核通过,平台评定传播值为" + effectResult + "。") + .setUrl("pages/quest/subpage/detail/detail?id=" + video.getTaskId()) + .setReceiverId(video.getUserId()); + // 企业微信消息通知 + String touser = userService.getById(video.getUserId()).getUserid(); + if (touser != null) { + appMessage.setTouser(touser); +// List + JSONObject content_item = new JSONObject(); +// content_item.fluentPut("") + httpUtil.qywxMessage(httpUtil.qywxGetToken(),touser,"传播效果审核",null,appMessage.getUrl(),null); + } + AppMessageUtil.sendMessage(appMessage); return Result.success("", "审核完成"); } diff --git a/src/main/java/com/zh/project0512/model/AppMessage.java b/src/main/java/com/zh/project0512/model/AppMessage.java index f52ae6e..b050c63 100644 --- a/src/main/java/com/zh/project0512/model/AppMessage.java +++ b/src/main/java/com/zh/project0512/model/AppMessage.java @@ -4,7 +4,9 @@ import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.extension.activerecord.Model; import com.baomidou.mybatisplus.annotation.TableId; + import java.time.LocalDateTime; + import com.baomidou.mybatisplus.annotation.TableField; import lombok.Data; import lombok.EqualsAndHashCode; @@ -58,6 +60,12 @@ public class AppMessage extends Model { @TableField("receiverId") private Integer receiverId; + /** + * 接受用户企业微信userid + */ + @TableField("touser") + private String touser; + /** * 创建时间 */ diff --git a/src/main/java/com/zh/project0512/utils/AppMessageUtil.java b/src/main/java/com/zh/project0512/utils/AppMessageUtil.java index 1e2945c..596ab52 100644 --- a/src/main/java/com/zh/project0512/utils/AppMessageUtil.java +++ b/src/main/java/com/zh/project0512/utils/AppMessageUtil.java @@ -2,6 +2,7 @@ package com.zh.project0512.utils; import com.zh.project0512.mapper.AppMessageMapper; import com.zh.project0512.model.AppMessage; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; @@ -15,7 +16,6 @@ import java.time.LocalDateTime; public class AppMessageUtil { @Resource AppMessageMapper appMessageMapper; - //解决工具类无法调用Dao层数据,数据为null //静态初始化当前类 private static AppMessageUtil appMessageUtil; diff --git a/src/main/java/com/zh/project0512/utils/HttpUtil.java b/src/main/java/com/zh/project0512/utils/HttpUtil.java index 99c411f..707313c 100644 --- a/src/main/java/com/zh/project0512/utils/HttpUtil.java +++ b/src/main/java/com/zh/project0512/utils/HttpUtil.java @@ -1,6 +1,7 @@ package com.zh.project0512.utils; import com.alibaba.fastjson.JSONObject; +import com.zh.project0512.model.AppMessage; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -21,42 +22,75 @@ public class HttpUtil { private String corpid; @Value("${qywx.corpsecret}") private String corpsecret; + @Value("${qywx.agentid}") + private String agentid; + @Value("${wx.appid}") + private String appid; - public String qywxGetToken(){ + public String qywxGetToken() { String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"; String requestUrl = url + "?corpid=" + corpid + "&corpsecret=" + corpsecret; String str = httpRequest(requestUrl, "GET", null); return JSONObject.parseObject(str).getString("access_token"); } - public JSONObject qywxDepartmentList(String access_token){ + + public JSONObject qywxDepartmentList(String access_token) { String url = "https://qyapi.weixin.qq.com/cgi-bin/department/list"; String requestUrl = url + "?access_token=" + access_token; String str = httpRequest(requestUrl, "GET", null); return JSONObject.parseObject(str); } - public JSONObject qywxUserSession(String access_token,String code){ + + public JSONObject qywxUserSession(String access_token, String code) { String url = "https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session"; - String requestUrl = url + "?access_token=" + access_token +"&js_code="+ code +"&grant_type=authorization_code"; + String requestUrl = url + "?access_token=" + access_token + "&js_code=" + code + "&grant_type=authorization_code"; String str = httpRequest(requestUrl, "GET", null); return JSONObject.parseObject(str); } - public JSONObject qywxUser(String access_token,String userid){ + + public JSONObject qywxUser(String access_token, String userid) { String url = "https://qyapi.weixin.qq.com/cgi-bin/user/get"; - String requestUrl = url + "?access_token=" + access_token +"&userid="+ userid ; + String requestUrl = url + "?access_token=" + access_token + "&userid=" + userid; String str = httpRequest(requestUrl, "GET", null); return JSONObject.parseObject(str); } - public Integer qywxDepartmentParentid(String access_token,int id,int loop){ + public Integer qywxDepartmentParentid(String access_token, int id, int loop) { String url = " https://qyapi.weixin.qq.com/cgi-bin/department/get"; - String requestUrl = url + "?access_token=" + access_token +"&id="+ id ; + String requestUrl = url + "?access_token=" + access_token + "&id=" + id; String str = httpRequest(requestUrl, "GET", null); Integer parentid = JSONObject.parseObject(str).getJSONObject("department").getInteger("parentid"); - if( parentid != 1 && loop< 8 ){ - qywxDepartmentParentid(access_token,parentid,++loop); + if (parentid != 1 && loop < 8) { + qywxDepartmentParentid(access_token, parentid, ++loop); } return parentid; } + + /** + * 企业微信消息生成 + * @param access_token + * @param touser:成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个)eg:"zhangsan|lisi" + * @param title:必填,消息标题,长度限制4-12个汉字 + * @param description:消息标题,长度限制4-12个汉字 + * @param wxAppUrl:点击消息卡片后的小程序页面,最长1024个字节,仅限本小程序内的页面。该字段不填则消息点击后不跳转。 + * @param content_item:消息内容键值对,最多允许10个item + */ + public void qywxMessage(String access_token,String touser, String title,String description,String wxAppUrl,JSONObject content_item) { + JSONObject obj = new JSONObject(); + obj.fluentPut("msgtype", "miniprogram_notice").fluentPut("agentid", agentid).fluentPut("touser", touser) + .fluentPut("miniprogram_notice", + new JSONObject().fluentPut("appid",appid) + .fluentPut("description",description) + .fluentPut("page",wxAppUrl) + .fluentPut("title",title) + .fluentPut("content_item",content_item)); + String url = " https://qyapi.weixin.qq.com/cgi-bin/message/send"; + String requestUrl = url + "?access_token=" + access_token; + String str = httpPost(requestUrl, obj); + System.out.println(obj); + System.out.println(str); + } + public static String httpRequest(String requestUrl, String requestMethod, String output) { try { URL url = new URL(requestUrl); @@ -125,7 +159,7 @@ public class HttpUtil { new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { - result=result.concat(line); + result = result.concat(line); } } catch (Exception e) { System.out.println("发送 POST 请求出现异常!" + e); diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index b49fb21..dfeae61 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -102,6 +102,7 @@ wx: qywx: corpid: wwf8467e6545ca4032 corpsecret: u0bzd2vXOib7a6sVy60fwzR9v0Xgea929CApV4eChD0 + agentid: 1000003 # 抖音开放平台配置信息 dy: clientKey: awty3wnxx90jper9 #抖音开放平台key