| New file |
| | |
| | | package com.ruoyi.system.utils.util; |
| | | |
| | | import cn.hutool.http.HttpUtil; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.ruoyi.common.redis.service.RedisService; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.system.dto.XiaoeLiveDTO; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.MalformedURLException; |
| | | import java.net.URL; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * 小鹅通工具类 |
| | | * @author mitao |
| | | * @date 2025/1/8 |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | public class XiaoeUtils { |
| | | @Resource |
| | | private RedisService redisService; |
| | | private static final String BASE_URL = "https://api.xiaoe-tech.com/token"; |
| | | //店铺的业务id |
| | | private String app_id = ""; |
| | | //应用的唯一标识,通过 client_id 来鉴别应用的身份 |
| | | private String client_id = ""; |
| | | //应用的凭证秘钥,即client_secret,用来保证应用来源的可靠性,防止被伪造 |
| | | private String secret_key = ""; |
| | | //固定填写client_credential |
| | | private String grant_type = "client_credential"; |
| | | //获取直播列表 |
| | | private static final String LIVE_PAGE_LIST = "https://api.xiaoe-tech.com/xe.alive.list.get/1.0.0"; |
| | | private static final String LIVE_PAGE_LIST_V2 = "https://api.xiaoe-tech.com/xe.alive.list.get/2.0.0"; |
| | | //获取直播详情 |
| | | private static final String LIVE_DETAIL = "https://api.xiaoe-tech.com/xe.alive.detail.get/1.0.0"; |
| | | private static final String LIVE_DETAIL_V2 = "https://api.xiaoe-tech.com/xe.alive.detail.get/2.0.0"; |
| | | //创建直播 |
| | | private static final String LIVE_ADD = "https://api.xiaoe-tech.com/xe.alive.live.create/1.0.0"; |
| | | //编辑直播 |
| | | private static final String LIVE_EDIT = "https://api.xiaoe-tech.com/xe.alive.live.update/1.0.0"; |
| | | //删除直播 |
| | | private static final String LIVE_DELETE = "https://api.xiaoe-tech.com/xe.alive.live.delete/1.0.0"; |
| | | |
| | | |
| | | /** |
| | | * 获取小鹅通access_token |
| | | * @return |
| | | */ |
| | | private String getAccessToken() { |
| | | Boolean flag = redisService.hasKey("xiaoe:access_token"); |
| | | String accessToken = ""; |
| | | if (flag) { |
| | | accessToken = redisService.getCacheObject("xiaoe:access_token"); |
| | | } else { |
| | | String urlString = BASE_URL + |
| | | "?app_id=" + this.app_id + |
| | | "&client_id=" + this.client_id + |
| | | "&secret_key=" + this.secret_key + |
| | | "&grant_type=" + this.grant_type; |
| | | String result = null; |
| | | try { |
| | | URL reqURL = new URL(urlString); |
| | | HttpURLConnection httpURLConnection = (HttpURLConnection) reqURL.openConnection(); |
| | | InputStreamReader isr = new InputStreamReader(httpURLConnection.getInputStream()); |
| | | char[] chars = new char[1024]; |
| | | result = ""; |
| | | int len; |
| | | while ((len = isr.read(chars)) != -1) { |
| | | result += new String(chars, 0, len); |
| | | } |
| | | isr.close(); |
| | | } catch (MalformedURLException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | JSONObject jsonObject = JSONObject.parseObject(result); |
| | | log.info("获取access_token返回结果:{}", result); |
| | | Map data = (Map) jsonObject.get("data"); |
| | | accessToken = (String)data.get("access_token"); |
| | | Integer expiresIn = (int) data.get("expires_in"); |
| | | redisService.setCacheObject("xiaoe:access_token", accessToken,Long.parseLong(expiresIn.toString()), TimeUnit.SECONDS); |
| | | } |
| | | return accessToken; |
| | | } |
| | | |
| | | /** |
| | | * 获取直播列表 |
| | | * @param dto |
| | | * @return |
| | | */ |
| | | // public Page<XiaoeLiveVo> getLivePageList(XiaoeLiveQueryDto dto) { |
| | | // Map<String,Object> postParams = new HashMap<>(); |
| | | // postParams.put("search_content", dto.getSearchContent()); |
| | | // postParams.put("create_mode", dto.getCreateMode()); |
| | | // postParams.put("state", 0); |
| | | // postParams.put("search_alive_type", dto.getSearchAliveType()); |
| | | // postParams.put("alive_play_state", dto.getAlivePlayState()); |
| | | // postParams.put("page", dto.getPage()); |
| | | // postParams.put("page_size", dto.getPageSize()); |
| | | // postParams.put("access_token", getAccessToken()); |
| | | // String post = HttpUtil.post(LIVE_PAGE_LIST, JSONObject.toJSONString(postParams)); |
| | | // Page<XiaoeLiveVo> xiaoeLiveVOPage = new Page<>(); |
| | | // if (StringUtils.isNotBlank(post)) { |
| | | // log.info("获取直播列表返回结果:{}", post); |
| | | // JSONObject jsonObject = JSONObject.parseObject(post); |
| | | // if (jsonObject.get("code").equals(0) ) { |
| | | // JSONObject data = jsonObject.getJSONObject("data"); |
| | | // JSONArray list = data.getJSONArray("list"); |
| | | // if (CollectionUtil.isNotEmpty(list)) { |
| | | // List<XiaoeLiveVo> xiaoeLiveVos = JSONArray.parseArray(list.toJSONString(), XiaoeLiveVo.class); |
| | | // xiaoeLiveVOPage.setRecords(xiaoeLiveVos); |
| | | // xiaoeLiveVOPage.setCurrent(dto.getPage()); |
| | | // xiaoeLiveVOPage.setPages(data.getLong("page_count")); |
| | | // } |
| | | // } |
| | | // } |
| | | // return xiaoeLiveVOPage; |
| | | // } |
| | | |
| | | /** |
| | | *获取直播详情 |
| | | * @param id 直播ID |
| | | * @return |
| | | */ |
| | | // public XiaoeLiveVo getLiveDetail(String id) { |
| | | // Map<String, Object> postParams = new HashMap<>(); |
| | | // postParams.put("id", id); |
| | | // postParams.put("access_token", getAccessToken()); |
| | | // String post = HttpUtil.post(LIVE_DETAIL, JSONObject.toJSONString(postParams)); |
| | | // XiaoeLiveVo xiaoeLiveVO = null; |
| | | // if (StringUtils.isNotBlank(post)) { |
| | | // JSONObject jsonObject = JSONObject.parseObject(post); |
| | | // if (jsonObject.get("code").equals(0)) { |
| | | // JSONObject data = jsonObject.getJSONObject("data"); |
| | | // xiaoeLiveVO = JSONObject.parseObject(data.toJSONString(), XiaoeLiveVo.class); |
| | | // } |
| | | // } |
| | | // return xiaoeLiveVO; |
| | | // } |
| | | |
| | | /** |
| | | * 获取直播列表2.0 |
| | | * @param dto |
| | | * @return |
| | | */ |
| | | // public Page<XiaoeLiveVoV2> getLivePageListV2(XiaoeLiveQueryDto dto) { |
| | | // Map<String,Object> postParams = new HashMap<>(); |
| | | // postParams.put("search_content", dto.getSearchContent()); |
| | | // postParams.put("create_mode", dto.getCreateMode()); |
| | | // postParams.put("state", 0); |
| | | // postParams.put("search_alive_type", dto.getSearchAliveType()); |
| | | // postParams.put("alive_play_state", dto.getAlivePlayState()); |
| | | // postParams.put("page", dto.getPage()); |
| | | // postParams.put("page_size", dto.getPageSize()); |
| | | // postParams.put("access_token", getAccessToken()); |
| | | // postParams.put("zb_start_at_min", DateUtil.format(DateUtils.getTodayStartTime(), DatePattern.NORM_DATETIME_PATTERN)); |
| | | // String post = HttpUtil.post(LIVE_PAGE_LIST_V2, JSONObject.toJSONString(postParams)); |
| | | // Page<XiaoeLiveVoV2> xiaoeLiveVOPage = new Page<>(); |
| | | // if (StringUtils.isNotBlank(post)) { |
| | | // log.info("获取直播详情2.0返回结果:{}", post); |
| | | // JSONObject jsonObject = JSONObject.parseObject(post); |
| | | // if (jsonObject.get("code").equals(0)) { |
| | | // JSONObject data = jsonObject.getJSONObject("data"); |
| | | // JSONArray list = data.getJSONArray("live_list"); |
| | | // if (!list.isEmpty()){ |
| | | // List<XiaoeLiveVoV2> xiaoeLiveVos = JSONArray.parseArray(list.toJSONString(), XiaoeLiveVoV2.class); |
| | | // xiaoeLiveVOPage.setRecords(xiaoeLiveVos); |
| | | // xiaoeLiveVOPage.setCurrent(dto.getPage()); |
| | | // xiaoeLiveVOPage.setTotal(data.getLong("total_count")); |
| | | // } |
| | | // } |
| | | // } |
| | | // return xiaoeLiveVOPage; |
| | | // } |
| | | |
| | | /** |
| | | *获取直播详情 |
| | | * @param id 直播ID |
| | | * @return |
| | | */ |
| | | // public XiaoeLiveDetailVOV2 getLiveDetailV2(String id) { |
| | | // Map<String, Object> postParams = new HashMap<>(); |
| | | // postParams.put("id", id); |
| | | // postParams.put("access_token", getAccessToken()); |
| | | // String post = HttpUtil.post(LIVE_DETAIL_V2, JSONObject.toJSONString(postParams)); |
| | | // XiaoeLiveDetailVOV2 vo = null; |
| | | // if (StringUtils.isNotBlank(post)) { |
| | | // JSONObject jsonObject = JSONObject.parseObject(post); |
| | | // if (jsonObject.get("code").equals(0)) { |
| | | // JSONObject data = jsonObject.getJSONObject("data"); |
| | | // vo = JSONObject.parseObject(data.toJSONString(), XiaoeLiveDetailVOV2.class); |
| | | // } |
| | | // } |
| | | // return vo; |
| | | // } |
| | | /** |
| | | * 创建直播 |
| | | * @param dto |
| | | * @return |
| | | */ |
| | | public String addLive(XiaoeLiveDTO dto) { |
| | | Map<String, Object> requestParams = new HashMap<>(); |
| | | Map<String, Object> resourceInfoMap = new HashMap<>(); |
| | | resourceInfoMap.put("title", dto.getTitle()); |
| | | resourceInfoMap.put("summary", dto.getSummary()); |
| | | resourceInfoMap.put("zb_start_at", dto.getZbStartAt()); |
| | | resourceInfoMap.put("zb_stop_at", dto.getZbStopAt()); |
| | | resourceInfoMap.put("alive_type", dto.getAliveType()); |
| | | resourceInfoMap.put("descrb", dto.getDescrb()); |
| | | requestParams.put("resource_info", resourceInfoMap); |
| | | // 配置信息 |
| | | Map<String, Object> moudleInfoMap = new HashMap<>(); |
| | | moudleInfoMap.put("alive_mode", dto.getAliveMode()); |
| | | requestParams.put("module_info", moudleInfoMap); |
| | | //讲师信息 |
| | | List<Map<String, Object>> roleList = new ArrayList<>(); |
| | | Map<String, Object> roleInfoMap = new HashMap<>(); |
| | | roleInfoMap.put("role_name", "讲师"); |
| | | roleInfoMap.put("user_id", dto.getUserId()); |
| | | roleList.add(roleInfoMap); |
| | | requestParams.put("role_info", roleList); |
| | | //商品信息 |
| | | Map<String, Object> goodsInfoMap = new HashMap<>(); |
| | | goodsInfoMap.put("sale_type", 1);//售卖类型:1-单独售卖、2-关联售卖 |
| | | goodsInfoMap.put("payment_type", dto.getPaymentType()); |
| | | if (dto.getPaymentType().equals(3)) { |
| | | goodsInfoMap.put("resource_password", dto.getResourcePassword()); |
| | | } |
| | | requestParams.put("goods_info", goodsInfoMap); |
| | | requestParams.put("access_token", getAccessToken()); |
| | | log.info("创建直播参数:{}" ,JSONObject.toJSONString(requestParams)); |
| | | String post = HttpUtil.post(LIVE_ADD, JSONObject.toJSONString(requestParams)); |
| | | if (StringUtils.isNotBlank(post)) { |
| | | JSONObject jsonObject = JSONObject.parseObject(post); |
| | | log.info("创建直播返回结果:{}" ,post); |
| | | if (jsonObject.get("code").equals(0)) { |
| | | JSONObject data = jsonObject.getJSONObject("data"); |
| | | return data.getString("id"); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | /** |
| | | * 编辑直播 |
| | | * @param dto |
| | | * @return |
| | | */ |
| | | public Boolean editLive(XiaoeLiveDTO dto) { |
| | | boolean flag = false; |
| | | Map<String, Object> requestParams = new HashMap<>(); |
| | | Map<String, Object> resourceInfoMap = new HashMap<>(); |
| | | resourceInfoMap.put("id", dto.getId()); |
| | | resourceInfoMap.put("title", dto.getTitle()); |
| | | resourceInfoMap.put("summary", dto.getSummary()); |
| | | resourceInfoMap.put("zb_start_at", dto.getZbStartAt()); |
| | | resourceInfoMap.put("zb_stop_at", dto.getZbStopAt()); |
| | | resourceInfoMap.put("alive_type", dto.getAliveType()); |
| | | resourceInfoMap.put("descrb", dto.getDescrb()); |
| | | requestParams.put("resource_info", resourceInfoMap); |
| | | // 配置信息 |
| | | Map<String, Object> moudleInfoMap = new HashMap<>(); |
| | | moudleInfoMap.put("alive_mode", dto.getAliveMode()); |
| | | requestParams.put("module_info", moudleInfoMap); |
| | | //讲师信息 |
| | | List<Map<String, Object>> roleList = new ArrayList<>(); |
| | | Map<String, Object> roleInfoMap = new HashMap<>(); |
| | | roleInfoMap.put("role_name", "讲师"); |
| | | roleInfoMap.put("user_id", dto.getUserId()); |
| | | roleList.add(roleInfoMap); |
| | | requestParams.put("role_info", roleList); |
| | | //商品信息 |
| | | Map<String, Object> goodsInfoMap = new HashMap<>(); |
| | | goodsInfoMap.put("sale_type", 1);//售卖类型:1-单独售卖、2-关联售卖 |
| | | goodsInfoMap.put("payment_type", dto.getPaymentType()); |
| | | if (dto.getPaymentType().equals(3)) { |
| | | goodsInfoMap.put("resource_password", dto.getResourcePassword()); |
| | | } |
| | | requestParams.put("goods_info", goodsInfoMap); |
| | | requestParams.put("access_token", getAccessToken()); |
| | | log.info("编辑直播参数:{}" ,JSONObject.toJSONString(requestParams)); |
| | | String post = HttpUtil.post(LIVE_EDIT, JSONObject.toJSONString(requestParams)); |
| | | if (StringUtils.isNotBlank(post)) { |
| | | JSONObject jsonObject = JSONObject.parseObject(post); |
| | | log.info("编辑直播返回结果:{}" ,post); |
| | | if (jsonObject.get("code").equals(0)) { |
| | | flag = true; |
| | | } |
| | | } |
| | | return flag; |
| | | } |
| | | |
| | | /** |
| | | * 删除直播 |
| | | * @param id |
| | | * @return |
| | | */ |
| | | public Boolean deleteLive(String id) { |
| | | boolean flag = false; |
| | | Map<String, Object> requestParams = new HashMap<>(); |
| | | requestParams.put("id", id); |
| | | requestParams.put("access_token", getAccessToken()); |
| | | String post = HttpUtil.post(LIVE_DELETE, JSONObject.toJSONString(requestParams)); |
| | | if (StringUtils.isNotBlank(post)) { |
| | | JSONObject jsonObject = JSONObject.parseObject(post); |
| | | log.info("删除直播返回结果:{}" ,post); |
| | | if (jsonObject.get("code").equals(0)) { |
| | | flag = true; |
| | | } |
| | | } |
| | | return flag; |
| | | } |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | String urlString = BASE_URL + |
| | | "?app_id=appwmuwNWD48082" + |
| | | "&client_id=xopYwOAqNI36444"+ |
| | | "&secret_key=qKFxbGR0OlKX85PVyfCvkRF1P6fLRBEu" + |
| | | "&grant_type=client_credential"; |
| | | String result = null; |
| | | try { |
| | | URL reqURL = new URL(urlString); |
| | | HttpURLConnection httpURLConnection = (HttpURLConnection) reqURL.openConnection(); |
| | | InputStreamReader isr = new InputStreamReader(httpURLConnection.getInputStream()); |
| | | char[] chars = new char[1024]; |
| | | result = ""; |
| | | int len; |
| | | while ((len = isr.read(chars)) != -1) { |
| | | result += new String(chars, 0, len); |
| | | } |
| | | isr.close(); |
| | | } catch (MalformedURLException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | JSONObject jsonObject = JSONObject.parseObject(result); |
| | | System.out.println(jsonObject); |
| | | Map data = (Map) jsonObject.get("data"); |
| | | String accessToken = (String) data.get("access_token"); |
| | | System.out.println(accessToken); |
| | | } |
| | | |
| | | } |