yumeng 5 лет назад
Родитель
Сommit
d0790b8920

+ 94 - 23
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/controller/KuaiShouMaterialUploadController.java

@@ -4,19 +4,17 @@ import cn.com.ctop.common.module.entity.CtopOauthToken;
 import cn.com.ctop.common.module.service.ICtopOauthTokenService;
 import cn.com.ctop.common.module.utils.Check;
 import cn.com.ctop.common.module.utils.KuaishouInterfaceConstant;
+import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouImageGet;
+import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouVideoGet;
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouImageGetService;
 import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouMaterialUploadService;
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouVideoGetService;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import org.jeecg.common.api.vo.Result;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
-import org.springframework.web.multipart.MultipartFile;
-
-import javax.servlet.http.HttpServletRequest;
-import java.util.Map;
+import org.springframework.web.bind.annotation.*;
 
 @RestController
 @RequestMapping("/kusiShouUpload")
@@ -26,31 +24,87 @@ public class KuaiShouMaterialUploadController {
     private IKuaiShouMaterialUploadService uploadService;
     @Autowired
     private ICtopOauthTokenService oauthTokenService;
+    @Autowired
+    private IKuaiShouImageGetService iKuaiShouImageGetService;
+    @Autowired
+    private IKuaiShouVideoGetService videoGetService;
 
-    @PostMapping(value = "/video", consumes = "multipart/form-data;charset=utf-8")
-    public Map<String, Object> video(@RequestParam("multipartFile") MultipartFile multipartFile, String type, Long accountId, String code, HttpServletRequest request) throws Exception {
+    @GetMapping(value = "/video")
+    public Result<JSONObject> video(@RequestBody JSONObject requestJson) throws Exception {
+        Result<JSONObject> result = new Result<>();
+        try {
+            Long accountId = requestJson.getLong("accountId");
+            CtopOauthToken oauthToken = oauthTokenService.getTokenByAccountId(accountId);
+            if (Check.isNull(oauthToken)) {
+                throw new Exception("未获取到账户信息");
+            }
+            String url = requestJson.getString("url");
+            Integer type = requestJson.getInteger("type");
+            String signature = requestJson.getString("signature");
+            JSONObject videoJson = uploadService.video(url, type, accountId, oauthToken.getAccessToken(), signature);
+            JSONObject returnJson = new JSONObject();
+            if (videoJson.getBoolean("success")) {
+                result.setSuccess(true);
+                returnJson.put("photoId", videoJson.getLong("photoId"));
+                result.setResult(requestJson);
+                result.setMessage(videoJson.getString("message"));
+            } else {
+                result.setSuccess(false);
+                result.setMessage(videoJson.getString("message"));
+            }
 
-        CtopOauthToken oauthToken = oauthTokenService.getTokenByAccountId(accountId);
-        if (Check.isNull(oauthToken)) {
-            throw new Exception("未获取到账户信息");
+        } catch (Exception e) {
+            e.printStackTrace();
+            result.setSuccess(false);
+            result.setMessage(e.getMessage());
         }
 
-        uploadService.video(multipartFile, type, accountId, oauthToken.getAccessToken(), code);
-        return null;
+
+        return result;
     }
 
 
-    @PostMapping(value = "/image", consumes = "multipart/form-data;charset=utf-8")
-    public Result<JSONObject> image(@RequestParam("requestJson") JSONObject requestJson) throws Exception {
-        Result<JSONObject> result = new Result<>();
+    @GetMapping(value = "/checkVideo")
+    public Result<JSONObject> video(String signature, Long accountId) {
+        Result<JSONObject> result = new Result();
         try {
-            Long accountId = requestJson.getLong("accountId");
+            JSONObject returnJson = new JSONObject();
+            QueryWrapper<KuaiShouVideoGet> videoGetQueryWrapper = new QueryWrapper<>();
+            videoGetQueryWrapper.eq("signature", signature);
+            videoGetQueryWrapper.eq("account_id", accountId);
+            videoGetQueryWrapper.last("limit 1");
+            KuaiShouVideoGet videoGet = videoGetService.getOne(videoGetQueryWrapper);
+            if (Check.isNull(videoGet)) {
+                returnJson.put("isExistence", false); // 未存在
+            } else {
+                returnJson.put("isExistence", true);
+                returnJson.put("videoUrl", videoGet.getUrl());
+                returnJson.put("photoId", videoGet.getPhotoId());
+                returnJson.put("signature", signature);
+            }
+            result.setSuccess(true);
+            result.setResult(returnJson);
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            result.setSuccess(false);
+        }
+
+        return result;
+    }
 
+
+    @PostMapping(value = "/image")
+    public Result<JSONArray> image(@RequestBody JSONObject requestJson) throws Exception {
+        Result<JSONArray> result = new Result<>();
+
+        try {
+            Long accountId = requestJson.getLong("accountId");
             CtopOauthToken oauthToken = oauthTokenService.getTokenByAccountId(accountId);
             if (Check.isNull(oauthToken)) {
                 throw new Exception("未获取到账户信息");
             }
-
+            JSONArray returnArr = new JSONArray();
             JSONArray imageArr = requestJson.getJSONArray("uploadImageArr");
             if (!Check.isNull(imageArr)) {
                 for (int i = 0; i < imageArr.size(); i++) {
@@ -62,14 +116,31 @@ public class KuaiShouMaterialUploadController {
                     } else {
                         imageUrl = url;
                     }
-
-
                     String signature = imageJson.getString("signature");
-                    uploadService.kuauiShouImageUpload(url, signature, accountId, oauthToken.getAccessToken());
+                    QueryWrapper<KuaiShouImageGet> imageGetQueryWrapper = new QueryWrapper<>();
+                    imageGetQueryWrapper.eq("signature", signature);
+                    imageGetQueryWrapper.eq("account_id", accountId);
+                    imageGetQueryWrapper.last("limit 1");
+                    KuaiShouImageGet imageGet = iKuaiShouImageGetService.getOne(imageGetQueryWrapper);
+                    if (Check.isNull(imageGet)) {
+                        String imageToken = uploadService.kuauiShouImageUpload(imageUrl, signature, accountId, oauthToken.getAccessToken());
+                        if (!Check.isNull(imageToken)) {
+                            JSONObject returnJson = new JSONObject();
+                            returnJson.put("url", imageUrl);
+                            returnJson.put("signature", signature);
+                            returnArr.add(returnJson);
+                        }
+                    } else {
+                        JSONObject returnJson = new JSONObject();
+                        returnJson.put("url", imageUrl);
+                        returnJson.put("signature", signature);
+                        returnArr.add(returnJson);
+                    }
                 }
 
             }
             result.setSuccess(true);
+            result.setResult(returnArr);
         } catch (Exception e) {
             e.printStackTrace();
             result.setSuccess(false);

+ 2 - 4
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/service/IKuaiShouMaterialUploadService.java

@@ -1,11 +1,9 @@
 package cn.com.ctop.kuaishou.modules.batch.service;
 
-import org.springframework.web.multipart.MultipartFile;
-
-import java.io.IOException;
+import com.alibaba.fastjson.JSONObject;
 
 public interface IKuaiShouMaterialUploadService {
-    void video(MultipartFile multipartFile, String type, Long accountId, String accessToken, String code) throws IOException;
+    JSONObject video(String url, Integer type, Long accountId, String accessToken, String signature);
 
     String kuauiShouImageUpload(String url, String signature, Long accountId, String token);
 }

+ 77 - 22
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/service/impl/KuaiShouMaterialUploadServiceImpl.java

@@ -3,13 +3,17 @@ package cn.com.ctop.kuaishou.modules.batch.service.impl;
 import cn.com.ctop.common.module.enums.MaterialEnum;
 import cn.com.ctop.common.module.utils.Check;
 import cn.com.ctop.common.module.utils.KuaishouInterfaceConstant;
+import cn.com.ctop.common.module.utils.LoadFileUtil;
 import cn.com.ctop.common.module.utils.PropertiesUtils;
 import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouImageGet;
+import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouVideoGet;
 import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouImageGetService;
 import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouMaterialUploadService;
+import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouVideoGetService;
 import com.alibaba.fastjson.JSONObject;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.core.io.FileSystemResource;
 import org.springframework.http.HttpEntity;
 import org.springframework.http.HttpHeaders;
@@ -30,38 +34,89 @@ import java.util.Map;
 @Service
 public class KuaiShouMaterialUploadServiceImpl implements IKuaiShouMaterialUploadService {
 
+    @Value("${oss.replace.replace-value}")
+    private String replaceValue;
+    @Value("${oss.replace.replace-old-value}")
+    private String replaceOldValue;
+    @Value("${oss.replace.download}")
+    private String downloadUrl;
+
     @Autowired
     private IKuaiShouImageGetService iKuaiShouImageGetService;
+    @Autowired
+    private IKuaiShouVideoGetService kuaiShouVideoGetService;
 
     /**
      * 上传文件
      *
-     * @param multipartFile
+     * @param
      * @param type
      * @param accountId
      */
     @Override
-    public void video(MultipartFile multipartFile, String type, Long accountId, String accessToken, String code) throws IOException {
-        String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.VIDEO_UPLOAD;
-        String s = uploadFile(multipartFile, "D:\\tets\\");
-        System.err.println(s);
-
-        File file = new File(s);
-
-        System.err.println(file);
-        FileSystemResource resource = new FileSystemResource(file);
-        Map<String, String> headerMap = new HashMap<>();
-        headerMap.put("Content-Type", "multipart/form-data");
-        headerMap.put("Access-Token", accessToken);
-        JSONObject requestJson = new JSONObject();
-        requestJson.put("signature", code);
-        requestJson.put("file", resource);
-        requestJson.put("type", type);
-        requestJson.put("advertiser_id", accountId);
-        String result = exceptInfoForRestTemplate(url, requestJson, headerMap);
-        System.err.println(result);
-
-        //
+    public JSONObject video(String url, Integer type, Long accountId, String accessToken, String signature) {
+        JSONObject returnJson = new JSONObject();
+        try {
+            String requestUrl = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.VIDEO_UPLOAD;
+            String downloadUrl = url.replace(replaceOldValue, replaceValue);
+            log.info("replaceUrl:{}", url);
+            String localUrl = LoadFileUtil.downLoadFromUrl(url, downloadUrl);
+            File file = new File(localUrl);
+            FileSystemResource resource = new FileSystemResource(file);
+            Map<String, String> headerMap = new HashMap<>();
+            headerMap.put("Content-Type", "multipart/form-data");
+            headerMap.put("Access-Token", accessToken);
+            JSONObject requestJson = new JSONObject();
+            requestJson.put("signature", signature);
+            requestJson.put("file", resource);
+            requestJson.put("type", type);
+            requestJson.put("advertiser_id", accountId);
+            String result = exceptInfoForRestTemplate(requestUrl, requestJson, headerMap);
+            JSONObject resultJson = JSONObject.parseObject(result);
+            if (!Check.isNull(resultJson)) {
+                if (resultJson.getInteger("code") == 0) {
+                    JSONObject dataJson = resultJson.getJSONObject("data");
+                    if (!Check.isNull(dataJson)) {
+                        String photoId = dataJson.getString("photo_id");
+                        KuaiShouVideoGet videoGet = new KuaiShouVideoGet();
+                        videoGet.setAccountId(accountId);
+                        videoGet.setId(accountId + photoId);
+                        videoGet.setUrl(url);
+                        videoGet.setMaterialType(type);
+                        videoGet.setPhotoId(photoId);
+                        videoGet.setSignature(signature);
+                        kuaiShouVideoGetService.saveOrUpdate(videoGet);
+                        returnJson.put("success", true);
+                        returnJson.put("photoId", photoId);
+                        returnJson.put("signature", signature);
+                        returnJson.put("message", "上传成功");
+
+                    }
+                    log.info("快手素材上传完成,accountId:{},code:{},返回信息:{}", accountId, signature, resultJson);
+                } else {
+                    log.error("快手上传素材失败,返回信息:{},请求参数:{}", resultJson, requestJson);
+                    returnJson.put("success", false);
+                    returnJson.put("signature", signature);
+                    returnJson.put("message", resultJson.getString("message"));
+
+                }
+            } else {
+                log.error("快手上传素材返回信息为空请求参数:{}", requestJson);
+                returnJson.put("success", false);
+                returnJson.put("signature", signature);
+                returnJson.put("message", "调用快手后台返回结果为空");
+            }
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            returnJson.put("success", false);
+            returnJson.put("signature", signature);
+            returnJson.put("message", "系统异常");
+
+        }
+        return returnJson;
+
+
     }