ソースを参照

API对接 接口

wangyunpeng 2 ヶ月 前
コミット
ed0a17afdc
15 ファイル変更350 行追加3 行削除
  1. 6 0
      api-module/src/main/java/com/tzld/piaoquan/api/common/enums/ExceptionEnum.java
  2. 55 0
      api-module/src/main/java/com/tzld/piaoquan/api/controller/contentplatform/ContentPlatformApiController.java
  3. 3 1
      api-module/src/main/java/com/tzld/piaoquan/api/dao/mapper/contentplatform/ext/ContentPlatformPlanMapperExt.java
  4. 9 0
      api-module/src/main/java/com/tzld/piaoquan/api/model/param/contentplatform/api/GetContentPageParam.java
  5. 14 0
      api-module/src/main/java/com/tzld/piaoquan/api/model/param/contentplatform/api/GetTokenParam.java
  6. 27 0
      api-module/src/main/java/com/tzld/piaoquan/api/model/param/contentplatform/api/GetVideoPageUrlParam.java
  7. 13 0
      api-module/src/main/java/com/tzld/piaoquan/api/model/vo/contentplatform/api/VideoIdTitleVO.java
  8. 18 0
      api-module/src/main/java/com/tzld/piaoquan/api/model/vo/contentplatform/api/VideoPageUrlVO.java
  9. 2 0
      api-module/src/main/java/com/tzld/piaoquan/api/service/contentplatform/ContentPlatformAccountService.java
  10. 19 0
      api-module/src/main/java/com/tzld/piaoquan/api/service/contentplatform/ContentPlatformApiService.java
  11. 2 0
      api-module/src/main/java/com/tzld/piaoquan/api/service/contentplatform/ContentPlatformPlanService.java
  12. 2 1
      api-module/src/main/java/com/tzld/piaoquan/api/service/contentplatform/impl/ContentPlatformAccountServiceImpl.java
  13. 162 0
      api-module/src/main/java/com/tzld/piaoquan/api/service/contentplatform/impl/ContentPlatformApiServiceImpl.java
  14. 2 1
      api-module/src/main/java/com/tzld/piaoquan/api/service/contentplatform/impl/ContentPlatformPlanServiceImpl.java
  15. 16 0
      api-module/src/main/resources/mapper/contentplatform/ext/ContentPlatformPlanMapperExt.xml

+ 6 - 0
api-module/src/main/java/com/tzld/piaoquan/api/common/enums/ExceptionEnum.java

@@ -38,6 +38,12 @@ public enum ExceptionEnum {
     QW_PLAN_NOT_EXISTS(3003, "企微计划不存在"),
     GZH_ACCOUNT_NOT_EXISTS(3004, "公众号不存在"),
     GZH_ACCOUNT_NOT_AUTH(3005, "公众号未授权,无法使用平台发布"),
+
+    // api
+    API_TOKEN_ERROR(4000, "token无效"),
+    API_PAGE_PARAM_ERROR(4001, "请输入正确的分页参数"),
+    API_PAGE_SIZE_TOO_LARGE(4002, "每页获取数量不能超过500条"),
+    API_PAGE_URL_SIZE_TOO_LARGE(4002, "获取视频嵌入路径 每次最多20条"),
     ;
     private int code;
     private String msg;

+ 55 - 0
api-module/src/main/java/com/tzld/piaoquan/api/controller/contentplatform/ContentPlatformApiController.java

@@ -0,0 +1,55 @@
+package com.tzld.piaoquan.api.controller.contentplatform;
+
+import com.tzld.piaoquan.api.annotation.JwtIgnore;
+import com.tzld.piaoquan.api.model.param.contentplatform.api.GetContentPageParam;
+import com.tzld.piaoquan.api.model.param.contentplatform.api.GetTokenParam;
+import com.tzld.piaoquan.api.model.param.contentplatform.api.GetVideoPageUrlParam;
+import com.tzld.piaoquan.api.model.vo.contentplatform.api.VideoIdTitleVO;
+import com.tzld.piaoquan.api.model.vo.contentplatform.api.VideoPageUrlVO;
+import com.tzld.piaoquan.api.service.contentplatform.ContentPlatformApiService;
+import com.tzld.piaoquan.growth.common.common.base.CommonResponse;
+import com.tzld.piaoquan.growth.common.utils.page.Page;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.List;
+
+@RestController
+@RequestMapping("/contentPlatform/api")
+public class ContentPlatformApiController {
+
+    @Autowired
+    ContentPlatformApiService apiService;
+
+    @ApiOperation(value = "获取token")
+    @PostMapping("/getToken")
+    @JwtIgnore
+    public CommonResponse<String> getToken(@RequestBody GetTokenParam param) {
+        return CommonResponse.success(apiService.getToken(param));
+    }
+
+    @ApiOperation(value = "分页获取内容库内容")
+    @PostMapping("/getContent")
+    @JwtIgnore
+    public CommonResponse<Page<VideoIdTitleVO>> getContent(HttpServletRequest request,
+                                                           @RequestBody GetContentPageParam param) {
+        String token = request.getHeader("token");
+        return CommonResponse.success(apiService.getContent(token, param));
+    }
+
+    @ApiOperation(value = "获取视频嵌入路径")
+    @PostMapping("/getVideoPageUrl")
+    @JwtIgnore
+    public CommonResponse<List<VideoPageUrlVO>> getVideoPageUrl(HttpServletRequest request,
+                                                                @RequestBody GetVideoPageUrlParam param) {
+        String token = request.getHeader("token");
+        return CommonResponse.success(apiService.getVideoPageUrl(token, param));
+    }
+
+
+}

+ 3 - 1
api-module/src/main/java/com/tzld/piaoquan/api/dao/mapper/contentplatform/ext/ContentPlatformPlanMapperExt.java

@@ -60,5 +60,7 @@ public interface ContentPlatformPlanMapperExt {
 
     void updateVideoAggStatus(@Param("videoId") Long videoId, @Param("now") Long now);
 
-    List<ContentPlatformVideoAgg> getVideoAggList(List<String> dtList);
+    List<ContentPlatformVideoAgg> getVideoAggList(@Param("dtList") List<String> dtList);
+
+    List<ContentPlatformVideo> getVideoListByIds(@Param("videoIds") List<Long> videoIds);
 }

+ 9 - 0
api-module/src/main/java/com/tzld/piaoquan/api/model/param/contentplatform/api/GetContentPageParam.java

@@ -0,0 +1,9 @@
+package com.tzld.piaoquan.api.model.param.contentplatform.api;
+
+import com.tzld.piaoquan.api.model.param.PageParam;
+import lombok.Data;
+
+@Data
+public class GetContentPageParam extends PageParam {
+
+}

+ 14 - 0
api-module/src/main/java/com/tzld/piaoquan/api/model/param/contentplatform/api/GetTokenParam.java

@@ -0,0 +1,14 @@
+package com.tzld.piaoquan.api.model.param.contentplatform.api;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+@Data
+public class GetTokenParam {
+
+    @ApiModelProperty(value = "telNum")
+    private String telNum;
+
+    @ApiModelProperty(value = "密码md5")
+    private String password;
+}

+ 27 - 0
api-module/src/main/java/com/tzld/piaoquan/api/model/param/contentplatform/api/GetVideoPageUrlParam.java

@@ -0,0 +1,27 @@
+package com.tzld.piaoquan.api.model.param.contentplatform.api;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+public class GetVideoPageUrlParam {
+
+    @ApiModelProperty(value = "合作类型:0-公众号 1-企微")
+    private Integer type;
+    @ApiModelProperty(value = "待嵌入视频列表")
+    private List<VideoItem> videoList;
+
+    @Data
+    public static class VideoItem {
+        @ApiModelProperty(value = "videoId")
+        private Long videoId;
+        @ApiModelProperty(value = "嵌入场景 0-自动回复 1-服务号定期推送")
+        private Integer type;
+        @ApiModelProperty(value = "公众号ghId")
+        private String ghId;
+        @ApiModelProperty(value = "公众号类型")
+        private Integer accountType;
+    }
+}

+ 13 - 0
api-module/src/main/java/com/tzld/piaoquan/api/model/vo/contentplatform/api/VideoIdTitleVO.java

@@ -0,0 +1,13 @@
+package com.tzld.piaoquan.api.model.vo.contentplatform.api;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+@Data
+public class VideoIdTitleVO {
+
+    @ApiModelProperty(value = "videoId")
+    private Long videoId;
+    @ApiModelProperty(value = "标题")
+    private String title;
+}

+ 18 - 0
api-module/src/main/java/com/tzld/piaoquan/api/model/vo/contentplatform/api/VideoPageUrlVO.java

@@ -0,0 +1,18 @@
+package com.tzld.piaoquan.api.model.vo.contentplatform.api;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+@Data
+public class VideoPageUrlVO {
+
+    @ApiModelProperty(value = "videoId")
+    private Long videoId;
+    @ApiModelProperty(value = "标题")
+    private String title;
+    @ApiModelProperty(value = "封面")
+    private String cover;
+    @ApiModelProperty(value = "嵌入小程序页面路径")
+    private String pageUrl;
+
+}

+ 2 - 0
api-module/src/main/java/com/tzld/piaoquan/api/service/contentplatform/ContentPlatformAccountService.java

@@ -11,6 +11,8 @@ import java.util.List;
 public interface ContentPlatformAccountService {
     AccountLoginVO login(AccountLoginParam param);
 
+    ContentPlatformAccount getAccountByTelNum(String telNum);
+
     void sendMessageVerifyCode(AccountSendMessageParam param);
 
     Page<AccountVO> pageAccount(AccountListParam param);

+ 19 - 0
api-module/src/main/java/com/tzld/piaoquan/api/service/contentplatform/ContentPlatformApiService.java

@@ -0,0 +1,19 @@
+package com.tzld.piaoquan.api.service.contentplatform;
+
+import com.tzld.piaoquan.api.model.param.contentplatform.api.GetContentPageParam;
+import com.tzld.piaoquan.api.model.param.contentplatform.api.GetTokenParam;
+import com.tzld.piaoquan.api.model.param.contentplatform.api.GetVideoPageUrlParam;
+import com.tzld.piaoquan.api.model.vo.contentplatform.api.VideoIdTitleVO;
+import com.tzld.piaoquan.api.model.vo.contentplatform.api.VideoPageUrlVO;
+import com.tzld.piaoquan.growth.common.utils.page.Page;
+
+import java.util.List;
+
+public interface ContentPlatformApiService {
+
+    String getToken(GetTokenParam param);
+
+    Page<VideoIdTitleVO> getContent(String token, GetContentPageParam param);
+
+    List<VideoPageUrlVO> getVideoPageUrl(String token, GetVideoPageUrlParam param);
+}

+ 2 - 0
api-module/src/main/java/com/tzld/piaoquan/api/service/contentplatform/ContentPlatformPlanService.java

@@ -67,4 +67,6 @@ public interface ContentPlatformPlanService {
     void gzhPlanImport(List<String> channels);
 
     void qwPlanImport(List<String> channels);
+
+    void saveQwPlanVideo(QwPlanSaveVideoParam videoParam, Long planId, Long loginAccountId);
 }

+ 2 - 1
api-module/src/main/java/com/tzld/piaoquan/api/service/contentplatform/impl/ContentPlatformAccountServiceImpl.java

@@ -88,7 +88,8 @@ public class ContentPlatformAccountServiceImpl implements ContentPlatformAccount
         return result;
     }
 
-    private ContentPlatformAccount getAccountByTelNum(String telNum) {
+    @Override
+    public ContentPlatformAccount getAccountByTelNum(String telNum) {
         ContentPlatformAccountExample example = new ContentPlatformAccountExample();
         example.createCriteria().andTelNumEqualTo(telNum);
         List<ContentPlatformAccount> accountList = accountMapper.selectByExample(example);

+ 162 - 0
api-module/src/main/java/com/tzld/piaoquan/api/service/contentplatform/impl/ContentPlatformApiServiceImpl.java

@@ -0,0 +1,162 @@
+package com.tzld.piaoquan.api.service.contentplatform.impl;
+
+import com.alibaba.fastjson.JSON;
+import com.tzld.piaoquan.api.common.enums.ExceptionEnum;
+import com.tzld.piaoquan.api.common.enums.contentplatform.AccountStatusEnum;
+import com.tzld.piaoquan.api.common.enums.contentplatform.QwPlanTypeEnum;
+import com.tzld.piaoquan.api.common.exception.CommonException;
+import com.tzld.piaoquan.api.dao.mapper.contentplatform.ext.ContentPlatformPlanMapperExt;
+import com.tzld.piaoquan.api.model.param.contentplatform.QwPlanSaveVideoParam;
+import com.tzld.piaoquan.api.model.param.contentplatform.VideoContentListParam;
+import com.tzld.piaoquan.api.model.param.contentplatform.api.GetContentPageParam;
+import com.tzld.piaoquan.api.model.param.contentplatform.api.GetTokenParam;
+import com.tzld.piaoquan.api.model.param.contentplatform.api.GetVideoPageUrlParam;
+import com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformAccount;
+import com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformQwPlan;
+import com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformVideo;
+import com.tzld.piaoquan.api.model.vo.contentplatform.api.VideoIdTitleVO;
+import com.tzld.piaoquan.api.model.vo.contentplatform.api.VideoPageUrlVO;
+import com.tzld.piaoquan.api.service.contentplatform.ContentPlatformAccountService;
+import com.tzld.piaoquan.api.service.contentplatform.ContentPlatformApiService;
+import com.tzld.piaoquan.api.service.contentplatform.ContentPlatformPlanService;
+import com.tzld.piaoquan.growth.common.service.MessageAttachmentService;
+import com.tzld.piaoquan.growth.common.utils.MessageUtil;
+import com.tzld.piaoquan.growth.common.utils.RedisUtils;
+import com.tzld.piaoquan.growth.common.utils.page.Page;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+@Service
+public class ContentPlatformApiServiceImpl implements ContentPlatformApiService {
+
+    @Autowired
+    private ContentPlatformAccountService accountService;
+    @Autowired
+    private ContentPlatformPlanService planService;
+    @Autowired
+    private ContentPlatformPlanMapperExt planMapperExt;
+    @Autowired
+    private MessageAttachmentService messageAttachmentService;
+
+    @Autowired
+    private RedisUtils redisUtils;
+
+    @Value("${video.min.score:0.6}")
+    private Double videoMinScore;
+
+    @Override
+    public String getToken(GetTokenParam param) {
+        ContentPlatformAccount account = accountService.getAccountByTelNum(param.getTelNum());
+        if (Objects.isNull(account)) {
+            throw new CommonException(ExceptionEnum.ACCOUNT_NOT_EXISTS_WRONG);
+        }
+        if (account.getStatus() == AccountStatusEnum.FORBIDDEN.getVal()) {
+            throw new CommonException(ExceptionEnum.ACCOUNT_BANNED);
+        }
+        if (!account.getPassword().equals(param.getPassword())) {
+            throw new CommonException(ExceptionEnum.LOGIN_PASSWORD_WRONG);
+        }
+        String token = UUID.randomUUID().toString().replace("-", "");
+        saveTokenToRedis(account, token, 4 * 60 * 60L);
+        return token;
+    }
+
+    private void saveTokenToRedis(ContentPlatformAccount account, String token, Long expireTime) {
+        String tokenPrefix = "api.{token}";
+        String redisKey = tokenPrefix.replace("{token}", token);
+        redisUtils.setValueWithExpire(redisKey, JSON.toJSONString(account), expireTime);
+    }
+
+    @Override
+    public Page<VideoIdTitleVO> getContent(String token, GetContentPageParam param) {
+        checkToken(token);
+        if (Objects.isNull(param.getPageNum()) || Objects.isNull(param.getPageSize())
+                || param.getPageNum() <= 0 || param.getPageSize() <= 0) {
+            throw new CommonException(ExceptionEnum.API_PAGE_PARAM_ERROR);
+        }
+        if (param.getPageSize() > 500) {
+            throw new CommonException(ExceptionEnum.API_PAGE_SIZE_TOO_LARGE);
+        }
+        Page<VideoIdTitleVO> result = new Page<>(param.getPageNum(), param.getPageSize());
+        int offset = (param.getPageNum() - 1) * param.getPageSize();
+        String dt = planMapperExt.getVideoMaxDt();
+        VideoContentListParam listParam = new VideoContentListParam();
+        int count = planMapperExt.getVideoCount(listParam, dt, videoMinScore);
+        result.setTotalSize(count);
+        if (count == 0) {
+            return result;
+        }
+        List<ContentPlatformVideo> videoList = planMapperExt.getVideoList(listParam, dt, videoMinScore, offset, param.getPageSize());
+        List<VideoIdTitleVO> list = videoList.stream().map(video -> {
+            VideoIdTitleVO vo = new VideoIdTitleVO();
+            vo.setVideoId(video.getVideoId());
+            vo.setTitle(video.getTitle());
+            return vo;
+        }).collect(Collectors.toList());
+        result.setObjs(list);
+        return result;
+    }
+
+    private ContentPlatformAccount checkToken(String token) {
+        String tokenPrefix = "api.{token}";
+        String redisKey = tokenPrefix.replace("{token}", token);
+        String value = redisUtils.getString(redisKey);
+        if (StringUtils.isBlank(value)) {
+            throw new CommonException(ExceptionEnum.API_TOKEN_ERROR);
+        }
+        return JSON.parseObject(value, ContentPlatformAccount.class);
+    }
+
+    @Override
+    public List<VideoPageUrlVO> getVideoPageUrl(String token, GetVideoPageUrlParam param) {
+        ContentPlatformAccount account = checkToken(token);
+        if (CollectionUtils.isEmpty(param.getVideoList()) || param.getVideoList().size() > 20) {
+            throw new CommonException(ExceptionEnum.API_PAGE_URL_SIZE_TOO_LARGE);
+        }
+        List<Long> videoIds = param.getVideoList().stream().map(GetVideoPageUrlParam.VideoItem::getVideoId).collect(Collectors.toList());
+        List<ContentPlatformVideo> videoList = planMapperExt.getVideoListByIds(videoIds);
+        Map<Long, ContentPlatformVideo> videoMap = videoList.stream().collect(Collectors.toMap(ContentPlatformVideo::getVideoId, video -> video));
+        List<VideoPageUrlVO> result = new ArrayList<>();
+        if (param.getType() == 1) {
+            for (GetVideoPageUrlParam.VideoItem item : param.getVideoList()) {
+                ContentPlatformQwPlan qwPlan = new ContentPlatformQwPlan();
+                qwPlan.setType(param.getType());
+                qwPlan.setScene(0);
+                String pageUrl = messageAttachmentService.getPage(account.getChannel(), "wxbdd2a2e93d9a6e25",
+                        "dyyqw", "企微", QwPlanTypeEnum.from(param.getType()).getDescription(),
+                        "位置1", item.getVideoId());
+                String rootSourceId = MessageUtil.getRootSourceId(pageUrl);
+                qwPlan.setPageUrl(pageUrl);
+                qwPlan.setRootSourceId(rootSourceId);
+                qwPlan.setCreateAccountId(account.getId());
+                qwPlan.setCreateTimestamp(System.currentTimeMillis());
+                planMapperExt.insertQwPlanReturnId(qwPlan);
+                // 保存视频内容
+                ContentPlatformVideo video = videoMap.get(item.getVideoId());
+                QwPlanSaveVideoParam videoParam = new QwPlanSaveVideoParam();
+                videoParam.setVideoId(video.getVideoId());
+                videoParam.setTitle(video.getTitle());
+                videoParam.setCover(video.getCover());
+                videoParam.setVideo(video.getVideo());
+                planService.saveQwPlanVideo(videoParam, qwPlan.getId(), account.getId());
+                VideoPageUrlVO vo = new VideoPageUrlVO();
+                vo.setVideoId(item.getVideoId());
+                vo.setTitle(video.getTitle());
+                vo.setCover(video.getCover());
+                vo.setPageUrl(pageUrl);
+                result.add(vo);
+            }
+        } else {
+            for (GetVideoPageUrlParam.VideoItem item : param.getVideoList()) {
+
+            }
+        }
+        return result;
+    }
+}

+ 2 - 1
api-module/src/main/java/com/tzld/piaoquan/api/service/contentplatform/impl/ContentPlatformPlanServiceImpl.java

@@ -493,7 +493,8 @@ public class ContentPlatformPlanServiceImpl implements ContentPlatformPlanServic
         return buildQwPlanItemVOList(list);
     }
 
-    private void saveQwPlanVideo(QwPlanSaveVideoParam videoParam, Long planId, Long loginAccountId) {
+    @Override
+    public void saveQwPlanVideo(QwPlanSaveVideoParam videoParam, Long planId, Long loginAccountId) {
         ContentPlatformQwPlanVideo item = new ContentPlatformQwPlanVideo();
         item.setPlanId(planId);
         item.setVideoId(videoParam.getVideoId());

+ 16 - 0
api-module/src/main/resources/mapper/contentplatform/ext/ContentPlatformPlanMapperExt.xml

@@ -222,4 +222,20 @@
         where video_id = #{videoId}
     </update>
 
+    <select id="getVideoListByIds"
+            resultType="com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformVideo">
+        SELECT t1.*
+        FROM content_platform_video t1
+        INNER JOIN (
+        SELECT video_id, MAX(dt) AS max_dt
+        FROM content_platform_video
+        WHERE video_id IN
+        <foreach collection="videoIds" item="item" open="(" close=")" separator=",">
+            #{item}
+        </foreach>
+        GROUP BY video_id
+        ) t2 ON t1.video_id = t2.video_id AND t1.dt = t2.max_dt
+        ORDER BY t1.dt DESC
+    </select>
+
 </mapper>