FamilyCloudServiceImpl.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package cn.sikey.mcdisk.service;
  2. import cn.hutool.core.date.DateUtil;
  3. import cn.hutool.http.HttpStatus;
  4. import cn.hutool.json.JSONArray;
  5. import cn.hutool.json.JSONObject;
  6. import cn.hutool.json.JSONUtil;
  7. import cn.sikey.framework.common.exception.ServiceException;
  8. import cn.sikey.mcdisk.controller.app.wristwatch.vo.DeleteContentsReqVO;
  9. import cn.sikey.mcdisk.controller.app.wristwatch.vo.GetFileUploadURLReqVO;
  10. import cn.sikey.mcdisk.enums.ApiEndpointsEnum;
  11. import cn.sikey.mcdisk.enums.CatalogTypeEnum;
  12. import cn.sikey.mcdisk.enums.PhotoThemeEnum;
  13. import cn.sikey.mcdisk.pojo.McdiskResult;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.apache.commons.lang3.StringUtils;
  16. import org.springframework.stereotype.Service;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.Objects;
  20. import java.util.Optional;
  21. /**
  22. * @Author: nelson
  23. * @Date: 2025/5/29
  24. * @Description: 家庭云
  25. */
  26. @Service
  27. @Slf4j
  28. public class FamilyCloudServiceImpl extends AbstractMcdiskService implements FamilyCloudService {
  29. /**
  30. * 创建家庭云
  31. *
  32. * @param cloudName 家庭云名称
  33. * @param accountType 用户类型
  34. * @param ticket ticket
  35. * @param accessToken accessToken
  36. * @return
  37. */
  38. @Override
  39. public McdiskResult createFamilyCloud(String cloudName, Integer accountType, String ticket, String accessToken) {
  40. // 校验家庭云名称
  41. McdiskResult result = validateCloudNameUniqueness(cloudName, accountType, ticket, accessToken);
  42. if (Objects.nonNull(result)) {
  43. log.info("createFamilyCloud:{}", JSONUtil.toJsonStr(result));
  44. return result;
  45. }
  46. // 准备请求参数
  47. String apiUrl = mcdiskConfig.getPath() + ApiEndpointsEnum.CREATE_FAMILY_CLOUD.getPath();
  48. JSONObject jsonObject = new JSONObject();
  49. jsonObject.set("cloudName", cloudName);
  50. String jsonBody = jsonObject.toString();
  51. // 准备请求头
  52. Map<String, String> headers = getHeaders(ticket, accessToken);
  53. // 发送请求
  54. McdiskResult mcdiskResult = getMcdiskResult(apiUrl, jsonBody, headers);
  55. log.info("[创建家庭云]返回数据:{}", mcdiskResult);
  56. return mcdiskResult;
  57. }
  58. /**
  59. * 创建家庭云相册
  60. *
  61. * @param cloudID 家庭云ID
  62. * @param theme 相册主题
  63. * @param ticket ticket
  64. * @param accessToken accessToken
  65. * @return
  66. */
  67. @Override
  68. public McdiskResult createCloudPhoto(String cloudID, Integer theme, String photoName, String ticket, String accessToken) {
  69. // 校验家庭云下的相册唯一性
  70. McdiskResult result = validateCloudPhotoNameUniqueness(photoName, cloudID, ticket, accessToken);
  71. if (Objects.nonNull(result)) {
  72. log.info("createCloudPhoto:{}", JSONUtil.toJsonStr(result));
  73. return result;
  74. }
  75. // 准备请求参数
  76. String apiUrl = mcdiskConfig.getPath() + ApiEndpointsEnum.CREATE_CLOUD_PHOTO.getPath();
  77. JSONObject jsonObject = new JSONObject();
  78. jsonObject.set("photoName", photoName);
  79. jsonObject.set("cloudID", cloudID);
  80. if (Objects.nonNull(theme) && theme.intValue() == PhotoThemeEnum.FAMILY_PHOTO_ALBUM.getValue()) {
  81. // 当theme为1:亲子相册时 必传
  82. jsonObject.set("themeDate", DateUtil.formatDate(DateUtil.date()));
  83. }
  84. jsonObject.set("theme", theme);
  85. String jsonBody = jsonObject.toString();
  86. // 准备请求头
  87. Map<String, String> headers = getHeaders(ticket, accessToken);
  88. // 发送请求
  89. McdiskResult mcdiskResult = getMcdiskResult(apiUrl, jsonBody, headers);
  90. log.info("[创建家庭云相册]返回数据:{}", mcdiskResult);
  91. return mcdiskResult;
  92. }
  93. /**
  94. * 上传文件到家庭云
  95. *
  96. * @param getFileUploadURLReqVO 上传文件到家庭云
  97. * @param accessToken accessToken
  98. * @return
  99. */
  100. @Override
  101. public McdiskResult getFileUploadURL(GetFileUploadURLReqVO getFileUploadURLReqVO, String accessToken) {
  102. // 准备请求参数
  103. String apiUrl = mcdiskConfig.getPath() + ApiEndpointsEnum.GET_FILE_UPLOAD_URL.getPath();
  104. boolean catalogTypeFlag = false;
  105. if (getFileUploadURLReqVO.getCatalogType().intValue() != CatalogTypeEnum.AVATAR_DIRECTORY.getValue()) {
  106. Optional.ofNullable(getFileUploadURLReqVO.getCloudType()).orElseThrow(() -> new ServiceException(HttpStatus.HTTP_INTERNAL_ERROR, "云类型是空"));
  107. if (StringUtils.isBlank(getFileUploadURLReqVO.getCloudID())) {
  108. throw new ServiceException(HttpStatus.HTTP_INTERNAL_ERROR, "家庭云ID是空");
  109. }
  110. catalogTypeFlag = true;
  111. }
  112. // 创建根 JSON 对象
  113. JSONObject jsonObject = new JSONObject();
  114. // 添加 commonAccountInfo 对象
  115. JSONObject commonAccountInfo = new JSONObject();
  116. commonAccountInfo.set("accountType", getFileUploadURLReqVO.getCommonAccountInfo().getAccountType());
  117. jsonObject.set("commonAccountInfo", commonAccountInfo);
  118. jsonObject.set("path", getFileUploadURLReqVO.getPath());
  119. if (catalogTypeFlag) {
  120. jsonObject.set("cloudID", getFileUploadURLReqVO.getCloudID());
  121. jsonObject.set("cloudType", getFileUploadURLReqVO.getCloudType());
  122. }
  123. jsonObject.set("catalogType", getFileUploadURLReqVO.getCatalogType());
  124. jsonObject.set("fileCount", getFileUploadURLReqVO.getFileCount());
  125. jsonObject.set("totalSize", getFileUploadURLReqVO.getTotalSize());
  126. List<GetFileUploadURLReqVO.UploadContent> uploadContentList = getFileUploadURLReqVO.getUploadContentList();
  127. JSONArray jsonArray = new JSONArray();
  128. uploadContentList.forEach(uploadContent -> {
  129. JSONObject js = new JSONObject();
  130. js.set("comlexFlag", uploadContent.getComlexFlag());
  131. js.set("contentDesc", uploadContent.getContentDesc());
  132. js.set("contentName", uploadContent.getContentName());
  133. js.set("contentSize", uploadContent.getContentSize());
  134. js.set("fileEtag", uploadContent.getFileEtag());
  135. js.set("fileVersion", uploadContent.getFileVersion());
  136. if (Objects.nonNull(uploadContent.getManualRename())) {
  137. js.set("manualRename", uploadContent.getManualRename());
  138. } else if (StringUtils.isNotBlank(uploadContent.getSeqNo())) {
  139. js.set("seqNo", uploadContent.getSeqNo());
  140. }
  141. jsonArray.add(js);
  142. });
  143. jsonObject.set("uploadContentList", jsonArray);
  144. String jsonBody = jsonObject.toString();
  145. // 准备请求头
  146. Map<String, String> headers = getHeaders(getFileUploadURLReqVO.getTicket(),accessToken);
  147. // 发送请求
  148. McdiskResult mcdiskResult = getMcdiskResult(apiUrl, jsonBody, headers);
  149. log.info("[上传文件到家庭云]返回数据:{}", mcdiskResult);
  150. return mcdiskResult;
  151. }
  152. /**
  153. * 删除家庭云文件
  154. *
  155. * @param deleteContentsReqVO 家庭云文件
  156. * @param accessToken accessToken
  157. * @return
  158. */
  159. @Override
  160. public McdiskResult deleteContents(DeleteContentsReqVO deleteContentsReqVO, String accessToken) {
  161. boolean catalogTypeFlag = false;
  162. if (deleteContentsReqVO.getCatalogType().intValue() != CatalogTypeEnum.AVATAR_DIRECTORY.getValue()) {
  163. Optional.ofNullable(deleteContentsReqVO.getCloudType()).orElseThrow(() -> new ServiceException(HttpStatus.HTTP_INTERNAL_ERROR, "云类型是空"));
  164. if (StringUtils.isBlank(deleteContentsReqVO.getCloudID())) {
  165. throw new ServiceException(HttpStatus.HTTP_INTERNAL_ERROR, "家庭云ID是空");
  166. }
  167. catalogTypeFlag = true;
  168. }
  169. // 准备请求参数
  170. String apiUrl = mcdiskConfig.getPath() + ApiEndpointsEnum.DELETE_CONTENTS.getPath();
  171. JSONObject jsonObject = new JSONObject();
  172. JSONObject accountType = new JSONObject();
  173. accountType.set("accountType", deleteContentsReqVO.getCommonAccountInfo().getAccountType());
  174. jsonObject.set("commonAccountInfo", accountType);
  175. jsonObject.set("contentIDList", deleteContentsReqVO.getContentIDList());
  176. jsonObject.set("path", deleteContentsReqVO.getPath());
  177. jsonObject.set("catalogType", deleteContentsReqVO.getCatalogType());
  178. if (catalogTypeFlag) {
  179. jsonObject.set("cloudID", deleteContentsReqVO.getCloudID());
  180. jsonObject.set("cloudType", deleteContentsReqVO.getCloudType());
  181. }
  182. String jsonBody = jsonObject.toString();
  183. // 准备请求头
  184. Map<String, String> headers = getHeaders(deleteContentsReqVO.getTicket(),accessToken);
  185. // 发送请求
  186. McdiskResult mcdiskResult = getMcdiskResult(apiUrl, jsonBody, headers);
  187. log.info("[删除家庭云文件]返回数据:{}", mcdiskResult);
  188. return mcdiskResult;
  189. }
  190. }