| New file |
| | |
| | | package com.xiaopeng.account.util; |
| | | |
| | | import com.alibaba.fastjson2.JSONArray; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.xiaopeng.evi.oss.service.AliOssService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * @author MiTao |
| | | * @date 2026-03-25 11:24 |
| | | * |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | public class AliOssDownloadUtil { |
| | | |
| | | @Autowired |
| | | private AliOssService aliOssService; |
| | | |
| | | public String getPictureJsonArray(String json) { |
| | | JSONArray res = new JSONArray(); |
| | | if (json == null || json.isEmpty()) { |
| | | return res.toJSONString(); |
| | | } |
| | | |
| | | if (json.startsWith("{")) { |
| | | JSONObject jsonObject = JSONObject.parseObject(json); |
| | | if (Objects.isNull(jsonObject)) { |
| | | return res.toJSONString(); |
| | | } |
| | | String downloadUrl = ""; |
| | | try { |
| | | downloadUrl = aliOssService.getDownloadUrl( |
| | | jsonObject.getString("id"), jsonObject.getString("name"), true); |
| | | } catch (UnsupportedEncodingException e) { |
| | | log.error("获取图片下载地址失败", e); |
| | | } |
| | | jsonObject.put("url", downloadUrl); |
| | | res.add(jsonObject); |
| | | |
| | | } else if (json.startsWith("[")) { |
| | | JSONArray jsonArray = JSONArray.parseArray(json); |
| | | for (int i = 0; i < jsonArray.size(); i++) { |
| | | JSONObject jsonObject = jsonArray.getJSONObject(i); |
| | | if (Objects.isNull(jsonObject)) { |
| | | continue; |
| | | } |
| | | String downloadUrl = ""; |
| | | try { |
| | | downloadUrl = aliOssService.getDownloadUrl( |
| | | jsonObject.getString("id"), jsonObject.getString("name"), true); |
| | | } catch (UnsupportedEncodingException e) { |
| | | log.error("获取图片下载地址失败", e); |
| | | } |
| | | jsonObject.put("url", downloadUrl); |
| | | } |
| | | res = jsonArray; |
| | | } |
| | | return res.toJSONString(); |
| | | } |
| | | |
| | | /** |
| | | * JSONObject类型图片 |
| | | * @param json |
| | | * @return |
| | | */ |
| | | public String getPictureJSONObject(String json){ |
| | | JSONObject jsonObject = JSONObject.parseObject(json); |
| | | if (Objects.isNull(jsonObject)) { |
| | | return new JSONObject().toJSONString(); |
| | | } |
| | | String downloadUrl = jsonObject.getString("url"); |
| | | try { |
| | | downloadUrl = aliOssService.getDownloadUrl( |
| | | jsonObject.getString("id"), jsonObject.getString("name"), true); |
| | | } catch (UnsupportedEncodingException e) { |
| | | log.error("获取图片下载地址失败", e); |
| | | } |
| | | jsonObject.put("url", downloadUrl); |
| | | return jsonObject.toJSONString(); |
| | | } |
| | | } |