package com.ruoyi.goods.utils;
|
|
import cn.hutool.http.HttpUtil;
|
import com.alibaba.fastjson2.JSONArray;
|
import com.alibaba.fastjson2.JSONObject;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.ruoyi.common.core.utils.StringUtils;
|
import com.ruoyi.common.redis.service.RedisService;
|
import com.ruoyi.goods.domain.dto.XiaoeCourseQueryDto;
|
import com.ruoyi.goods.domain.dto.XiaoeLiveDto;
|
import com.ruoyi.goods.domain.dto.XiaoeLiveQueryDto;
|
import com.ruoyi.goods.domain.vo.XiaoeCourseChapterVO;
|
import com.ruoyi.goods.domain.vo.XiaoeCourseGroupVO;
|
import com.ruoyi.goods.domain.vo.XiaoeCourseVO;
|
import com.ruoyi.goods.domain.vo.XiaoeLiveVo;
|
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.Comparator;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* 小鹅通工具类
|
* @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 = "appwmuwNWD48082";
|
//应用的唯一标识,通过 client_id 来鉴别应用的身份
|
private String client_id = "xopYwOAqNI36444";
|
//应用的凭证秘钥,即client_secret,用来保证应用来源的可靠性,防止被伪造
|
private String secret_key = "qKFxbGR0OlKX85PVyfCvkRF1P6fLRBEu";
|
//固定填写client_credential
|
private String grant_type = "client_credential";
|
//获取直播列表
|
private static final String LIVE_PAGE_LIST = "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_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";
|
//课程列表
|
private static final String COURSE_PAGE_LIST = "https://api.xiaoe-tech.com/xe.course.course.list/1.0.0";
|
//课程小节
|
private static final String COURSE_CHAPTER = "https://api.xiaoe-tech.com/xe.course.course.chapter.get/1.0.0";
|
//获取店铺商品分组列表
|
private static final String COURSE_GROUP_LIST = "https://api.xiaoe-tech.com/xe.resource_tags.list/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);
|
Map data = (Map) jsonObject.get("data");
|
accessToken = (String) data.get("access_token");
|
}
|
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", dto.getState());
|
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)) {
|
JSONObject jsonObject = JSONObject.parseObject(post);
|
if (jsonObject.get("code").equals(0)) {
|
JSONObject data = jsonObject.getJSONObject("data");
|
JSONArray list = data.getJSONArray("list");
|
List<XiaoeLiveVo> xiaoeLiveVos = JSONArray.parseArray(list.toJSONString(), XiaoeLiveVo.class);
|
xiaoeLiveVOPage.setRecords(xiaoeLiveVos);
|
xiaoeLiveVOPage.setCurrent(data.getLong("page"));
|
xiaoeLiveVOPage.setTotal(data.getLong("total"));
|
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;
|
}
|
|
/**
|
* 创建直播
|
* @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);
|
//讲师信息
|
Map<String, Object> roleInfoMap = new HashMap<>();
|
roleInfoMap.put("user_id", dto.getUserId());
|
requestParams.put("role_info", roleInfoMap);
|
//商品信息
|
Map<String, Object> goodsInfoMap = new HashMap<>();
|
goodsInfoMap.put("sale_type", 2);//售卖类型: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());
|
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);
|
//讲师信息
|
Map<String, Object> roleInfoMap = new HashMap<>();
|
roleInfoMap.put("user_id", dto.getUserId());
|
requestParams.put("role_info", roleInfoMap);
|
//商品信息
|
Map<String, Object> goodsInfoMap = new HashMap<>();
|
goodsInfoMap.put("sale_type", 2);//售卖类型: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());
|
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;
|
}
|
|
/**
|
* 获取商品分组列表
|
* @return
|
*/
|
public List<XiaoeCourseGroupVO> getCourseGroupList(){
|
Map<String, Object> requestParams = new HashMap<>();
|
requestParams.put("page", 1);
|
requestParams.put("page_size", 50);
|
requestParams.put("access_token", getAccessToken());
|
String post = HttpUtil.post(COURSE_GROUP_LIST, JSONObject.toJSONString(requestParams));
|
List<XiaoeCourseGroupVO> xiaoeCourseGroupVOList = new ArrayList<>();
|
if (StringUtils.isNotBlank(post)) {
|
JSONObject jsonObject = JSONObject.parseObject(post);
|
log.info("获取商品分组返回结果:{}" ,post);
|
if (jsonObject.get("code").equals(0)) {
|
JSONObject data = jsonObject.getJSONObject("data");
|
JSONArray jsonArray = data.getJSONArray("list");
|
xiaoeCourseGroupVOList = JSONArray.parseArray(jsonArray.toJSONString(), XiaoeCourseGroupVO.class);
|
}
|
}
|
return xiaoeCourseGroupVOList;
|
}
|
|
/**
|
* 获取课程列表
|
* @param dto
|
* @return
|
*/
|
public Page<XiaoeCourseVO> getCoursePageList(XiaoeCourseQueryDto dto) {
|
JSONObject requestParams = JSONObject.parseObject(JSONObject.toJSONString(dto));
|
if (dto.getSortBy().equals(1)) {
|
requestParams.put("order_by", "modify");//根据创建时间排序
|
requestParams.put("order_type", "降序");
|
}
|
|
requestParams.put("sale_status", 1);
|
requestParams.put("access_token", getAccessToken());
|
String post = HttpUtil.post(COURSE_PAGE_LIST, requestParams.toJSONString());
|
Page<XiaoeCourseVO> page = 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 jsonArray = data.getJSONArray("list");
|
List<XiaoeCourseVO> xiaoeCourseVOList = JSONArray.parseArray(jsonArray.toJSONString(), XiaoeCourseVO.class);
|
if (dto.getSortBy().equals(2)){
|
xiaoeCourseVOList.sort(Comparator.comparing(XiaoeCourseVO::getCurriculumTime));
|
}
|
page.setRecords(xiaoeCourseVOList);
|
page.setTotal(data.getLong("total"));
|
}
|
}
|
return page;
|
}
|
|
/**
|
* 查询课程目录小节
|
* @param id
|
* @return
|
*/
|
public List<XiaoeCourseChapterVO> getCourseChapterDetail(String id) {
|
Map<String, Object> requestParams = new HashMap<>();
|
requestParams.put("course_id", id);
|
requestParams.put("access_token", getAccessToken());
|
String post = HttpUtil.post(COURSE_CHAPTER, JSONObject.toJSONString(requestParams));
|
List<XiaoeCourseChapterVO> xiaoeCourseChapterVO = new ArrayList<>();
|
if (StringUtils.isNotBlank(post)) {
|
log.info("查询课程目录小节返回结果:{}" ,post);
|
JSONObject jsonObject = JSONObject.parseObject(post);
|
if (jsonObject.get("code").equals(0)) {
|
String data = jsonObject.getString("data");
|
xiaoeCourseChapterVO = JSONArray.parseArray(data, XiaoeCourseChapterVO.class);
|
}
|
}
|
return xiaoeCourseChapterVO;
|
}
|
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);
|
}
|
|
}
|