From 89989ff8ff6b38dcee3cab7fa68446ceca5df1de Mon Sep 17 00:00:00 2001 From: zhanghua <314079846@qq.com> Date: 星期二, 26 九月 2023 10:20:03 +0800 Subject: [PATCH] bug修改 --- ycl-platform/src/main/java/com/ycl/controller/dingding/DingController.java | 210 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 206 insertions(+), 4 deletions(-) diff --git a/ycl-platform/src/main/java/com/ycl/controller/dingding/DingController.java b/ycl-platform/src/main/java/com/ycl/controller/dingding/DingController.java index 2110dbf..de8b669 100644 --- a/ycl-platform/src/main/java/com/ycl/controller/dingding/DingController.java +++ b/ycl-platform/src/main/java/com/ycl/controller/dingding/DingController.java @@ -1,29 +1,58 @@ package com.ycl.controller.dingding; +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.xxpt.gateway.shared.client.http.ExecutableClient; +import com.alibaba.xxpt.gateway.shared.client.http.GetClient; +import com.alibaba.xxpt.gateway.shared.client.http.PostClient; +import com.aliyun.oss.OSS; +import com.aliyun.oss.OSSClientBuilder; +import com.aliyun.oss.model.ObjectMetadata; import com.ycl.api.CommonResult; import com.ycl.bo.AdminUserDetails; import com.ycl.common.dingding.DingCommon; +import com.ycl.config.DingConfig; import com.ycl.controller.BaseController; import com.ycl.entity.dingding.DingUserInfo; import com.ycl.service.auth.AuthService; import com.ycl.service.ding.BookRemarkService; import com.ycl.service.ding.DingService; +import com.ycl.service.oss.OssService; +import com.ycl.utils.ConstantPropertiesUtils; +import com.ycl.utils.common.RandomUtils; import com.ycl.vo.AddressBookVO; import com.ycl.vo.NewAddressBookVO; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.FileItemFactory; +import org.apache.commons.fileupload.disk.DiskFileItemFactory; +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.MediaType; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.commons.CommonsMultipartFile; +import javax.annotation.Resource; +import java.io.*; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; +import static com.ycl.common.constant.DingConst.*; + @RestController @RequestMapping("/ding") @Api(tags = "閽夐拤") +@Slf4j public class DingController extends BaseController { @Autowired @@ -46,6 +75,16 @@ @Autowired private BookRemarkService bookRemarkService; + @Resource + private ExecutableClient executableClient; + + @Resource + private DingConfig dingConfig; + + + @Autowired + private OssService ossService; + @ApiOperation(value = "鏍规嵁閽夐拤鎺堟潈鐮佽幏鍙杢oken") @GetMapping("/dingLogin") public CommonResult<Map> dingLogin(@RequestParam String code) { @@ -60,11 +99,64 @@ return CommonResult.success(map); } + @ApiOperation(value = "鏍规嵁閽夐拤鎺堟潈鐮佽幏鍙杍sapiToken") + @GetMapping("/dingTicker") + public CommonResult<Map> dingTicker() { + String accessToken = getToken(); + String ticket = getTiker(accessToken); + HashMap<String, Object> map = new HashMap<>(); + + map.put("accessToken", accessToken); + + map.put("ticket", ticket); + return CommonResult.success(map); + } + + private String getToken() { + //璋冪敤API + GetClient getTokenClient = executableClient.newGetClient(GET_TOKEN); + //璁剧疆鍙傛暟 + getTokenClient.addParameter("appkey", dingConfig.getAppKey()); + getTokenClient.addParameter("appsecret", dingConfig.getAppSecret()); + String apiResult = getTokenClient.get(); + return parsingResult(apiResult); + } + + private String getTiker(String accToken) { + //璋冪敤API + PostClient postClient = executableClient.newPostClient(GET_TIKER); + //璁剧疆鍙傛暟 + if (ObjectUtil.isNotNull(accToken)) { + postClient.addParameter("accessToken", accToken); + postClient.addParameter("appkey", dingConfig.getAppKey()); + postClient.addParameter("appsecret", dingConfig.getAppSecret()); + String apiResult = postClient.post(); + return parsingResult(apiResult); + } + return null; + } + + private String parsingResult(String apiResult) { + if (ObjectUtil.isNotNull(apiResult)) { + JSONObject resJson = JSONObject.parseObject(apiResult); + if (resJson.getBoolean("success")) { + JSONObject content = resJson.getJSONObject("content"); + if (content.getBoolean("success")) { + JSONObject dataObj = content.getJSONObject("data"); + String accessToken = dataObj.getString("accessToken"); + return accessToken; + } + } else { + log.error(resJson.toJSONString()); + } + } + return null; + } + @ApiOperation(value = "閫氳褰�") @GetMapping("/addressBook") public CommonResult<List<AddressBookVO>> getAddressBook() { AdminUserDetails user = (AdminUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); - return CommonResult.success(dingService.getAddressBook(user.getUserId())); } @@ -77,15 +169,12 @@ } - - @ApiOperation(value = "閫氳褰曢�掑綊") @GetMapping("/getDddressBook") public CommonResult<NewAddressBookVO> getNewAddressBook(@RequestParam("id") Long id) { AdminUserDetails user = (AdminUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); return CommonResult.success(dingService.getAddressBookById(id, user.getUserId())); } - @ApiOperation(value = "淇敼閫氳褰曞娉�") @@ -95,5 +184,118 @@ return CommonResult.success(bookRemarkService.updateRemark(user, userId, remark)); } + @ApiOperation(value = "閫氳褰曢�掑綊") + @GetMapping("/getVideo/{mediaId}") + public CommonResult<String> getVideo(@PathVariable String mediaId) { + String accessToken = getToken(); + MultipartFile file = getDownload(accessToken, mediaId); + String url = ossService.uploadImages(file); + return CommonResult.success(url); + } + + + private MultipartFile getDownload(String access_token, String media_id) { + CloseableHttpResponse response = null; + try { + String api = "/media/download"; + GetClient getClient = executableClient.newGetClient(api); + //璁剧疆鍙傛暟 + getClient.addParameter("access_token", "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ7XCJ1c2VySWRcIjozLFwidXNlcm5hbWVcIjpcImFkbWluXCJ9IiwiY3JlYXRlZCI6MTY5NDY2NDE5MjA4MiwiZXhwIjoxNjk1MjY4OTkyfQ.fSc0sEfLhD3pgrguYZ0Q4Tr-0wjZNwY2xnGsaGzN1d4oXMnEoX9MJmDQcwEiZ1uLKZbGQCqUtJciAIvINmNq9w "); + getClient.addParameter("media_id", media_id); + + response = getClient.getB(); + HttpEntity entity = response.getEntity(); + InputStream is = entity.getContent(); + File voice = new File("D:/IMG_4401.MOV"); + OutputStream fos = new FileOutputStream(voice); + int cache = 10 * 1024; + byte[] buffer = new byte[cache]; + int ch = 0; + while ((ch = is.read(buffer)) != -1) { + fos.write(buffer, 0, ch); + } + is.close(); + fos.flush(); + fos.close(); + DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); + + String strDate2 = dtf2.format(LocalDateTime.now()); + String fileName = strDate2 + RandomUtils.generateRandomInt(4) + ".MOV"; + return getMultipartFile(is, fileName); + + } catch (Exception e) { + System.out.println(e); + } finally { + if (response != null) { + try { + //鐗瑰埆鎻愰啋锛氶渶瑕佽皟鐢╮esponse鐨刢lose鏂规硶鍏抽棴缃戠粶杩炴帴锛侊紒锛� + response.close(); + } catch (Exception e) { + System.out.println(e); + } + } + } + return null; + } + + /** + * 鑾峰彇灏佽寰桵ultipartFile + * + * @param inputStream inputStream + * @param fileName fileName + * @return MultipartFile + */ + public MultipartFile getMultipartFile(InputStream inputStream, String fileName) { + FileItem fileItem = createFileItem(inputStream, fileName); + //CommonsMultipartFile鏄痜eign瀵筸ultipartFile鐨勫皝瑁咃紝浣嗘槸瑕丗ileItem绫诲璞� + return new CommonsMultipartFile(fileItem); + } + + + /** + * FileItem绫诲璞″垱寤� + * + * @param inputStream inputStream + * @param fileName fileName + * @return FileItem + */ + public FileItem createFileItem(InputStream inputStream, String fileName) { + FileItemFactory factory = new DiskFileItemFactory(16, null); + String textFieldName = "file"; + FileItem item = factory.createItem(textFieldName, MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName); + int bytesRead = 0; + byte[] buffer = new byte[8192]; + OutputStream os = null; + //浣跨敤杈撳嚭娴佽緭鍑鸿緭鍏ユ祦鐨勫瓧鑺� + try { + os = item.getOutputStream(); + while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) { + os.write(buffer, 0, bytesRead); + } + inputStream.close(); + } catch (IOException e) { + log.error("Stream copy exception", e); + throw new IllegalArgumentException("鏂囦欢涓婁紶澶辫触"); + } finally { + if (os != null) { + try { + os.close(); + } catch (IOException e) { + log.error("Stream close exception", e); + + } + } + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + log.error("Stream close exception", e); + } + } + } + + return item; + } } + -- Gitblit v1.8.0