微信小程序直播间创建、微信小程序直播商品添加、删除、查询状态
This commit is contained in:
parent
591ce04e42
commit
b0f46a78a6
@ -0,0 +1,63 @@
|
||||
package cn.lili.modules.broadcast.entity.dos;
|
||||
|
||||
import cn.lili.base.BaseEntity;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
|
||||
/**
|
||||
* 小程序直播商品
|
||||
* @author Bulbasaur
|
||||
* @date: 2021/5/17 9:34 上午
|
||||
*
|
||||
*/
|
||||
@Data
|
||||
@Entity
|
||||
@ApiModel(value = "直播商品")
|
||||
@TableName("li_commodity")
|
||||
@Table(name = "li_commodity")
|
||||
public class Commodity extends BaseEntity {
|
||||
|
||||
@ApiModelProperty(value = "图片mediaID")
|
||||
private String coverImgUrl;
|
||||
|
||||
@ApiModelProperty(value = "商品名称")
|
||||
private String name;
|
||||
|
||||
//1:一口价(只需要传入price,price2不传)
|
||||
// 2:价格区间(price字段为左边界,price2字段为右边界,price和price2必传)
|
||||
// 3:显示折扣价(price字段为原价,price2字段为现价, price和price2必传
|
||||
@ApiModelProperty(value = "价格类型")
|
||||
private Integer priceType;
|
||||
|
||||
@ApiModelProperty(value = "价格")
|
||||
private Double price;
|
||||
|
||||
@ApiModelProperty(value = "价格2")
|
||||
private Double price2;
|
||||
|
||||
@ApiModelProperty(value = "商品详情页的小程序路径")
|
||||
private String url;
|
||||
|
||||
@ApiModelProperty(value = "微信程序直播商品ID")
|
||||
private String liveGoodsId;
|
||||
|
||||
@ApiModelProperty(value = "审核单ID")
|
||||
private String auditId;
|
||||
|
||||
@ApiModelProperty(value = "审核状态")
|
||||
private String auditStatus;
|
||||
|
||||
@ApiModelProperty(value = "店铺ID")
|
||||
private String storeId;
|
||||
|
||||
@ApiModelProperty(value = "商品ID")
|
||||
private String goodsId;
|
||||
|
||||
@ApiModelProperty(value = "规格ID")
|
||||
private String skuId;
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
package cn.lili.modules.broadcast.entity.dos;
|
||||
|
||||
import cn.lili.base.BaseEntity;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
|
||||
/**
|
||||
* 小程序直播间
|
||||
*
|
||||
* @author Bulbasaur
|
||||
* @date: 2021/5/17 9:47 上午
|
||||
*/
|
||||
@Data
|
||||
@Entity
|
||||
@ApiModel(value = "直播间")
|
||||
@TableName("li_studio")
|
||||
@Table(name = "li_studio")
|
||||
public class Studio extends BaseEntity {
|
||||
|
||||
/**
|
||||
* 直播间名字,最短3个汉字,最长17个汉字,1个汉字相当于2个字符
|
||||
*/
|
||||
@ApiModelProperty(value = "直播间名字")
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 背景图,填入mediaID(mediaID获取后,三天内有效);图片mediaID的获取,请参考以下文档: https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/New_temporary_materials.html;直播间背景图,图片规则:建议像素1080*1920,大小不超过2M
|
||||
*/
|
||||
@ApiModelProperty(value = "背景图")
|
||||
private String coverImg;
|
||||
|
||||
/**
|
||||
* 直播计划开始时间(开播时间需要在当前时间的10分钟后 并且 开始时间不能在 6 个月后)
|
||||
*/
|
||||
@ApiModelProperty(value = "开始时间")
|
||||
private String startTime;
|
||||
|
||||
/**
|
||||
* 直播计划结束时间(开播时间和结束时间间隔不得短于30分钟,不得超过24小时)
|
||||
*/
|
||||
@ApiModelProperty(value = "结束时间")
|
||||
private String endTime;
|
||||
|
||||
/**
|
||||
* 主播昵称,最短2个汉字,最长15个汉字,1个汉字相当于2个字符
|
||||
*/
|
||||
@ApiModelProperty(value = "主播昵称")
|
||||
private String anchorName;
|
||||
|
||||
/**
|
||||
* 主播微信号,如果未实名认证,需要先前往“小程序直播”小程序进行实名验证, 小程序二维码链接:https://res.wx.qq.com/op_res/9rSix1dhHfK4rR049JL0PHJ7TpOvkuZ3mE0z7Ou_Etvjf-w1J_jVX0rZqeStLfwh
|
||||
*/
|
||||
@ApiModelProperty(value = "主播微信号")
|
||||
private String anchorWechat;
|
||||
|
||||
/**
|
||||
* 分享图,填入mediaID(mediaID获取后,三天内有效);图片mediaID的获取,请参考以下文档: https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/New_temporary_materials.html;直播间分享图,图片规则:建议像素800*640,大小不超过1M;
|
||||
*/
|
||||
@ApiModelProperty(value = "分享图")
|
||||
private String shareImg;
|
||||
|
||||
/**
|
||||
* 购物直播频道封面图,填入mediaID(mediaID获取后,三天内有效);图片mediaID的获取,请参考以下文档: https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/New_temporary_materials.html; 购物直播频道封面图,图片规则:建议像素800*800,大小不超过100KB;
|
||||
*/
|
||||
@ApiModelProperty(value = "封面图")
|
||||
private String feedsImg;
|
||||
|
||||
/**
|
||||
* 回放视频链接
|
||||
*/
|
||||
@ApiModelProperty(value = "回放视频链接")
|
||||
private String mediaUrl;
|
||||
|
||||
|
||||
@ApiModelProperty(value = "房间ID")
|
||||
private String roomId;
|
||||
|
||||
@ApiModelProperty(value = "店铺ID")
|
||||
private String storeId;
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package cn.lili.modules.broadcast.entity.dto;
|
||||
|
||||
/**
|
||||
* @author liushuai(liushuai711 @ gmail.com)
|
||||
* @version v4.1
|
||||
* @Description:
|
||||
* @since 2021/5/17 11:34 上午
|
||||
*/
|
||||
public enum CommodityAuditStatusEnum {
|
||||
|
||||
/**
|
||||
* 未审核
|
||||
*/
|
||||
WAIT_AUDIT("0"),
|
||||
/**
|
||||
* 审核中
|
||||
*/
|
||||
AUDITING("1"),
|
||||
/**
|
||||
* 审核通过
|
||||
*/
|
||||
AUDIT_PASS("2"),
|
||||
/**
|
||||
* 审核失败
|
||||
*/
|
||||
AUDIT_REJECTION("3");
|
||||
|
||||
private final String status;
|
||||
|
||||
CommodityAuditStatusEnum(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package cn.lili.modules.broadcast.entity.dto;
|
||||
|
||||
import cn.lili.common.utils.BeanUtil;
|
||||
import cn.lili.modules.broadcast.entity.dos.Commodity;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 微信小程序直播商品DTO
|
||||
* @author Bulbasaur
|
||||
* @date: 2021/5/17 11:03 上午
|
||||
*
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class GoodsInfo {
|
||||
|
||||
@ApiModelProperty(value = "图片mediaID")
|
||||
private String coverImgUrl;
|
||||
|
||||
@ApiModelProperty(value = "商品名称")
|
||||
private String name;
|
||||
|
||||
//1:一口价(只需要传入price,price2不传)
|
||||
// 2:价格区间(price字段为左边界,price2字段为右边界,price和price2必传)
|
||||
// 3:显示折扣价(price字段为原价,price2字段为现价, price和price2必传
|
||||
@ApiModelProperty(value = "价格类型")
|
||||
private Integer priceType;
|
||||
|
||||
@ApiModelProperty(value = "价格")
|
||||
private Double price;
|
||||
|
||||
@ApiModelProperty(value = "价格2")
|
||||
private Double price2;
|
||||
|
||||
@ApiModelProperty(value = "商品详情页的小程序路径")
|
||||
private String url;
|
||||
|
||||
public GoodsInfo(Commodity commodity){
|
||||
BeanUtil.copyProperties(commodity, this);
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package cn.lili.modules.broadcast.mapper;
|
||||
|
||||
import cn.lili.modules.broadcast.entity.dos.Commodity;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 直播商品数据层
|
||||
*
|
||||
* @author Bulbasaur
|
||||
* @date: 2021/5/17 9:56 上午
|
||||
*/
|
||||
public interface CommodityMapper extends BaseMapper<Commodity> {
|
||||
|
||||
@Select("SELECT live_goods_id FROM li_commodity WHERE audit_status='0' or audit_status='1'")
|
||||
List<String> getAuditCommodity();
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package cn.lili.modules.broadcast.mapper;
|
||||
|
||||
import cn.lili.modules.broadcast.entity.dos.Studio;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* 直播间数据层
|
||||
*
|
||||
* @author Bulbasaur
|
||||
* @date: 2021/5/17 9:56 上午
|
||||
*/
|
||||
public interface StudioMapper extends BaseMapper<Studio> {
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package cn.lili.modules.broadcast.service;
|
||||
|
||||
import cn.lili.modules.broadcast.entity.dos.Commodity;
|
||||
|
||||
/**
|
||||
* 直播商品业务层
|
||||
*
|
||||
* @author Bulbasaur
|
||||
* @date: 2021/5/17 10:39 上午
|
||||
*/
|
||||
public interface CommodityService {
|
||||
|
||||
/**
|
||||
* 添加直播商品
|
||||
* @return 添加结果
|
||||
*/
|
||||
boolean addCommodity(Commodity commodity);
|
||||
|
||||
/**
|
||||
* 删除直播商品
|
||||
* @param goodsId 直播商品ID
|
||||
* @return 删除结果
|
||||
*/
|
||||
boolean deleteCommodity(String goodsId);
|
||||
|
||||
/**
|
||||
* 查询微信小程序直播商品审核状态
|
||||
*/
|
||||
void getGoodsWarehouse();
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package cn.lili.modules.broadcast.service;
|
||||
|
||||
import cn.lili.modules.broadcast.entity.dos.Studio;
|
||||
|
||||
/**
|
||||
* 直播间业务层
|
||||
*
|
||||
* @author Bulbasaur
|
||||
* @date: 2021/5/17 10:02 上午
|
||||
*/
|
||||
public interface StudioService {
|
||||
|
||||
/**
|
||||
* 创建直播间
|
||||
* 直播间默认手机直播
|
||||
* 默认开启:点赞、商品货架、评论、回放
|
||||
* @param studio 直播间
|
||||
* @return 开启状态
|
||||
*/
|
||||
Boolean create(Studio studio);
|
||||
|
||||
/**
|
||||
* 获取直播间回放
|
||||
* @param roomId 房间ID
|
||||
* @return 直播间回放地址
|
||||
*/
|
||||
String getLiveInfo(String roomId);
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package cn.lili.modules.broadcast.serviceimpl;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.lili.common.security.context.UserContext;
|
||||
import cn.lili.modules.broadcast.entity.dos.Commodity;
|
||||
import cn.lili.modules.broadcast.mapper.CommodityMapper;
|
||||
import cn.lili.modules.broadcast.service.CommodityService;
|
||||
import cn.lili.modules.broadcast.util.WechatLivePlayerUtil;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 直播商品业务层实现
|
||||
*
|
||||
* @author Bulbasaur
|
||||
* @date: 2021/5/17 11:16 上午
|
||||
*/
|
||||
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, Commodity> implements CommodityService {
|
||||
|
||||
@Autowired
|
||||
private WechatLivePlayerUtil wechatLivePlayerUtil;
|
||||
|
||||
@Override
|
||||
public boolean addCommodity(Commodity commodity) {
|
||||
JSONObject json =wechatLivePlayerUtil.addGoods(commodity);
|
||||
commodity.setLiveGoodsId(json.getStr("goodsId"));
|
||||
commodity.setAuditId(json.getStr("auditId"));
|
||||
commodity.setStoreId(UserContext.getCurrentUser().getStoreId());
|
||||
return this.save(commodity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteCommodity(String goodsId) {
|
||||
JSONObject json =wechatLivePlayerUtil.deleteGoods(goodsId);
|
||||
if(json.getStr("errcode").equals("0")){
|
||||
return this.remove(this.lambdaQuery().eq(Commodity::getLiveGoodsId,goodsId));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getGoodsWarehouse() {
|
||||
//查询审核中的商品
|
||||
List<String> goodsIdList=this.baseMapper.getAuditCommodity();
|
||||
//同步状态
|
||||
JSONObject json =wechatLivePlayerUtil.deleteGoods(goodsIdList);
|
||||
//修改状态
|
||||
List<Map<String,String>> list=(List)json.get("goods");
|
||||
for (Map<String,String> map:list){
|
||||
//修改审核状态
|
||||
this.update(this.lambdaUpdate()
|
||||
.eq(Commodity::getLiveGoodsId,map.get("goods_id"))
|
||||
.set(Commodity::getAuditStatus,map.get("audit_status")));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package cn.lili.modules.broadcast.serviceimpl;
|
||||
|
||||
import cn.lili.common.security.context.UserContext;
|
||||
import cn.lili.modules.broadcast.entity.dos.Studio;
|
||||
import cn.lili.modules.broadcast.mapper.StudioMapper;
|
||||
import cn.lili.modules.broadcast.service.StudioService;
|
||||
import cn.lili.modules.broadcast.util.WechatLivePlayerUtil;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
/**
|
||||
* 小程序直播间业务层实现
|
||||
*
|
||||
* @author Bulbasaur
|
||||
* @date: 2021/5/17 10:04 上午
|
||||
*/
|
||||
public class StudioServiceImpl extends ServiceImpl<StudioMapper, Studio> implements StudioService {
|
||||
|
||||
@Autowired
|
||||
private WechatLivePlayerUtil wechatLivePlayerUtil;
|
||||
|
||||
@Override
|
||||
public Boolean create(Studio studio) {
|
||||
|
||||
//创建小程序直播
|
||||
String roomId=wechatLivePlayerUtil.create(studio);
|
||||
studio.setRoomId(roomId);
|
||||
studio.setStoreId(UserContext.getCurrentUser().getStoreId());
|
||||
return this.save(studio);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLiveInfo(String roomId) {
|
||||
Studio studio=this.getByRoomId(roomId);
|
||||
//获取直播间并判断回放内容是否为空,如果为空则获取直播间回放并保存
|
||||
if(studio.getMediaUrl()!=null){
|
||||
return studio.getMediaUrl();
|
||||
}else{
|
||||
String mediaUrl= wechatLivePlayerUtil.getLiveInfo(roomId);
|
||||
studio.setMediaUrl(mediaUrl);
|
||||
this.save(studio);
|
||||
return mediaUrl;
|
||||
}
|
||||
}
|
||||
|
||||
private Studio getByRoomId(String roomId){
|
||||
return this.getOne(this.lambdaQuery().eq(Studio::getRoomId,roomId)) ;
|
||||
}
|
||||
}
|
@ -0,0 +1,152 @@
|
||||
package cn.lili.modules.broadcast.util;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.lili.modules.base.entity.enums.ClientTypeEnum;
|
||||
import cn.lili.modules.broadcast.entity.dos.Commodity;
|
||||
import cn.lili.modules.broadcast.entity.dos.Studio;
|
||||
import cn.lili.modules.broadcast.entity.dto.GoodsInfo;
|
||||
import cn.lili.modules.message.util.WechatAccessTokenUtil;
|
||||
import cn.lili.modules.system.utils.HttpUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 微信小程序直播工具类
|
||||
* @author Bulbasaur
|
||||
* @date: 2021/5/17 10:16 上午
|
||||
*
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class WechatLivePlayerUtil {
|
||||
|
||||
@Autowired
|
||||
private WechatAccessTokenUtil wechatAccessTokenUtil;
|
||||
|
||||
/**
|
||||
* 创建小程序直播间
|
||||
* @param studio 小程序直播
|
||||
* @return 房间ID
|
||||
*/
|
||||
public String create(Studio studio){
|
||||
//获取token
|
||||
String token = wechatAccessTokenUtil.cgiAccessToken(ClientTypeEnum.H5);
|
||||
//发送url
|
||||
String url = "https://api.weixin.qq.com/wxaapi/broadcast/room/create?access_token="+ token;
|
||||
Map<String, String> map = new HashMap<>();
|
||||
|
||||
// 直播间名字
|
||||
map.put("name", studio.getName());
|
||||
// 背景图
|
||||
map.put("coverImg", studio.getCoverImg());
|
||||
// 直播计划开始时间
|
||||
map.put("startTime", studio.getStartTime());
|
||||
// 直播计划结束时间
|
||||
map.put("endTime", studio.getEndTime());
|
||||
// 主播昵称
|
||||
map.put("anchorName", studio.getAnchorName());
|
||||
// 主播微信号
|
||||
map.put("anchorWechat", studio.getAnchorWechat());
|
||||
// 分享图
|
||||
map.put("shareImg", studio.getShareImg());
|
||||
// 购物直播频道封面图
|
||||
map.put("feedsImg", studio.getFeedsImg());
|
||||
// 直播间类型
|
||||
map.put("type", "0");
|
||||
// 是否关闭点赞
|
||||
map.put("closeLike", "0");
|
||||
// 是否关闭货架
|
||||
map.put("closeGoods", "0");
|
||||
// 是否关闭评论
|
||||
map.put("closeComment","0");
|
||||
// 直播间名字
|
||||
map.put("closeReplay", "0");
|
||||
|
||||
String content = HttpUtils.doPostWithJson(url, map);
|
||||
JSONObject json = new JSONObject(content);
|
||||
log.info("微信小程序直播间创建结果:" + content);
|
||||
return json.getStr("roomId");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取直播间回放
|
||||
* @param roomId 房间ID
|
||||
* @return 回放地址
|
||||
*/
|
||||
public String getLiveInfo(String roomId){
|
||||
//获取token
|
||||
String token = wechatAccessTokenUtil.cgiAccessToken(ClientTypeEnum.H5);
|
||||
//发送url
|
||||
String url = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token="+ token;
|
||||
Map<String, String> map = new HashMap<>();
|
||||
// 获取回放
|
||||
map.put("action", "get_replay");
|
||||
// 直播间ID
|
||||
map.put("room_id", roomId);
|
||||
// 起始拉取视频,0表示从第一个视频片段开始拉取
|
||||
map.put("start", "0");
|
||||
// 每次拉取的数量,建议100以内
|
||||
map.put("limit", "1");
|
||||
|
||||
String content = HttpUtils.doPostWithJson(url, map);
|
||||
JSONObject json = new JSONObject(content);
|
||||
log.info("微信小程序获取信息:" + content);
|
||||
//TODO get media_url
|
||||
return json.getStr("live_replay");
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加直播商品
|
||||
* @param commodity 直播商品
|
||||
* @return 添加结果
|
||||
*/
|
||||
public JSONObject addGoods(Commodity commodity){
|
||||
//获取token
|
||||
String token = wechatAccessTokenUtil.cgiAccessToken(ClientTypeEnum.H5);
|
||||
//发送url
|
||||
String url = "https://api.weixin.qq.com/wxaapi/broadcast/goods/add?access_token="+ token;
|
||||
GoodsInfo goodsInfo=new GoodsInfo(commodity);
|
||||
String content = HttpUtils.doPostWithJson(url, goodsInfo);
|
||||
JSONObject json = new JSONObject(content);
|
||||
log.info("微信小程序添加直播商品结果:" + content);
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除直播商品
|
||||
* @param goodsId 商品ID
|
||||
* @return 删除结果
|
||||
*/
|
||||
public JSONObject deleteGoods(String goodsId){
|
||||
//获取token
|
||||
String token = wechatAccessTokenUtil.cgiAccessToken(ClientTypeEnum.H5);
|
||||
//发送url
|
||||
String url = "https://api.weixin.qq.com/wxaapi/broadcast/goods/delete?access_token="+ token;
|
||||
String content = HttpUtils.doPostWithJson(url, goodsId);
|
||||
JSONObject json = new JSONObject(content);
|
||||
log.info("微信小程序删除直播商品结果:" + content);
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询直播商品状态
|
||||
* @param goodsIdList 商品ID列表
|
||||
* @return 删除结果
|
||||
*/
|
||||
public JSONObject deleteGoods(List<String> goodsIdList){
|
||||
//获取token
|
||||
String token = wechatAccessTokenUtil.cgiAccessToken(ClientTypeEnum.H5);
|
||||
//发送url
|
||||
String url = "https://api.weixin.qq.com/wxa/business/getgoodswarehouse?access_token="+ token;
|
||||
String content = HttpUtils.doPostWithJson(url, goodsIdList);
|
||||
JSONObject json = new JSONObject(content);
|
||||
log.info("微信小程序查询直播商品结果:" + content);
|
||||
return json;
|
||||
|
||||
}
|
||||
}
|
@ -1,21 +1,14 @@
|
||||
package cn.lili.modules.system.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.protocol.HTTP;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
/**
|
||||
* Http工具
|
||||
@ -28,14 +21,6 @@ public final class HttpUtils {
|
||||
public static final int HTTP_CONN_TIMEOUT = 100000;
|
||||
public static final int HTTP_SOCKET_TIMEOUT = 100000;
|
||||
|
||||
public static String doPost(String reqUrl, Map<String, String> parameters) {
|
||||
return doPost(reqUrl, parameters, "UTF-8", HTTP_CONN_TIMEOUT, HTTP_SOCKET_TIMEOUT);
|
||||
}
|
||||
|
||||
public static String doPost(String reqUrl, Map<String, String> parameters, String encoding) {
|
||||
return doPost(reqUrl, parameters, encoding, HTTP_CONN_TIMEOUT, HTTP_SOCKET_TIMEOUT);
|
||||
}
|
||||
|
||||
public static String doPost(String reqUrl, Map<String, String> parameters, String encoding, int connectTimeout,
|
||||
int readTimeout) {
|
||||
HttpURLConnection urlConn = null;
|
||||
@ -54,8 +39,8 @@ public final class HttpUtils {
|
||||
/**
|
||||
* post携带json请求
|
||||
*
|
||||
* @param reqUrl
|
||||
* @param jsonParameters
|
||||
* @param reqUrl 请求地址
|
||||
* @param jsonParameters 参数
|
||||
* @return
|
||||
*/
|
||||
public static String doPostWithJson(String reqUrl, Map<String, String> jsonParameters) {
|
||||
@ -94,6 +79,49 @@ public final class HttpUtils {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* post携带json请求
|
||||
*
|
||||
* @param reqUrl 请求地址
|
||||
* @param object 对象
|
||||
* @return
|
||||
*/
|
||||
public static String doPostWithJson(String reqUrl, Object object) {
|
||||
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
URL url = new URL(reqUrl);// 创建连接
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setDoOutput(true);
|
||||
connection.setDoInput(true);
|
||||
connection.setUseCaches(false);
|
||||
connection.setInstanceFollowRedirects(true);
|
||||
connection.setRequestMethod("POST"); // 设置请求方式
|
||||
// connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
|
||||
connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
|
||||
connection.connect();
|
||||
//一定要用BufferedReader 来接收响应, 使用字节来接收响应的方法是接收不到内容的
|
||||
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8); // utf-8编码
|
||||
out.append(JSONObject.toJSONString(object));
|
||||
out.flush();
|
||||
out.close();
|
||||
// 读取响应
|
||||
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
||||
String line;
|
||||
String res = "";
|
||||
while ((line = reader.readLine()) != null) {
|
||||
res += line;
|
||||
}
|
||||
reader.close();
|
||||
|
||||
return res;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "error"; // 自定义错误信息
|
||||
|
||||
}
|
||||
|
||||
private static HttpURLConnection sendPost(String reqUrl,
|
||||
Map<String, String> parameters, String encoding, int connectTimeout, int readTimeout) {
|
||||
HttpURLConnection urlConn = null;
|
||||
@ -178,16 +206,6 @@ public final class HttpUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* UTF-8编码
|
||||
*
|
||||
* @param link
|
||||
* @return
|
||||
*/
|
||||
public static String doGet(String link) {
|
||||
return doGet(link, "UTF-8", HTTP_CONN_TIMEOUT, HTTP_SOCKET_TIMEOUT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将parameters中数据转换成用"&"链接的http请求参数形式
|
||||
*
|
||||
@ -217,146 +235,4 @@ public final class HttpUtils {
|
||||
}
|
||||
return params.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* post请求封装 参数为{"a":1,"b":2,"c":3}
|
||||
*
|
||||
* @param path 接口地址
|
||||
* @param Info 参数
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String postResponse(String path, JSONObject Info) {
|
||||
HttpClient client = new DefaultHttpClient();
|
||||
HttpPost post = new HttpPost(path);
|
||||
|
||||
post.setHeader("Content-Type", "application/json");
|
||||
post.addHeader("Authorization", "Basic YWRtaW46");
|
||||
String result = "";
|
||||
|
||||
try {
|
||||
StringEntity s = new StringEntity(Info.toString(), "utf-8");
|
||||
System.out.println("<-------------------->");
|
||||
System.out.println(s);
|
||||
System.out.println("<-------------------->");
|
||||
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
|
||||
post.setEntity(s);
|
||||
// 发送请求
|
||||
HttpResponse httpResponse = client.execute(post);
|
||||
|
||||
// 获取响应输入流
|
||||
InputStream inStream = httpResponse.getEntity().getContent();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, StandardCharsets.UTF_8));
|
||||
StringBuilder strber = new StringBuilder();
|
||||
String line = null;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
strber.append(line + "\n");
|
||||
}
|
||||
inStream.close();
|
||||
|
||||
result = strber.toString();
|
||||
System.out.println(result);
|
||||
|
||||
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
||||
System.out.println("请求服务器成功,做相应处理");
|
||||
} else {
|
||||
System.out.println("请求服务端失败");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("请求异常");
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String http(String url, String proxyUrl, int proxyPort, Map<String, String> params, String chartSet)
|
||||
throws Exception {
|
||||
URL u = null;
|
||||
HttpURLConnection con = null;
|
||||
// 构建请求参数
|
||||
StringBuffer sb = new StringBuffer();
|
||||
OutputStreamWriter osw = null;
|
||||
BufferedReader br = null;
|
||||
if (params != null) {
|
||||
int i = 0;
|
||||
for (Entry<String, String> e : params.entrySet()) {
|
||||
if (i != 0) {
|
||||
sb.append("&");
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
sb.append(e.getKey());
|
||||
if (e.getValue() != null && !e.getValue().equals("")) {
|
||||
sb.append("=");
|
||||
sb.append(e.getValue());///URLEncoder.encode(sign, "UTF-8")
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("连接:" + url);
|
||||
System.out.println("发送:" + sb.toString());
|
||||
try {
|
||||
u = new URL(url);
|
||||
if (null != proxyUrl && !proxyUrl.equals("")) {
|
||||
System.out.println("代理的IP是:" + proxyUrl + ",代理端口:" + proxyPort);
|
||||
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyUrl, proxyPort));
|
||||
con = (HttpURLConnection) u.openConnection(proxy);
|
||||
} else {
|
||||
con = (HttpURLConnection) u.openConnection();
|
||||
}
|
||||
con.setConnectTimeout(30000);
|
||||
con.setReadTimeout(700000);
|
||||
con.setRequestMethod("POST");
|
||||
con.setDoOutput(true);
|
||||
con.setDoInput(true);
|
||||
con.setUseCaches(false);
|
||||
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
con.setRequestProperty("Charset", "UTF-8");
|
||||
osw = new OutputStreamWriter(con.getOutputStream(), StandardCharsets.UTF_8);
|
||||
osw.write(sb.toString());
|
||||
osw.flush();
|
||||
} catch (SocketTimeoutException e) {
|
||||
throw new Exception();
|
||||
} catch (Exception e) {
|
||||
throw new Exception();
|
||||
} finally {
|
||||
try {
|
||||
osw.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
try {
|
||||
br = new BufferedReader(new InputStreamReader(con.getInputStream(),
|
||||
StandardCharsets.UTF_8));
|
||||
String temp;
|
||||
while ((temp = br.readLine()) != null) {
|
||||
buffer.append(temp);
|
||||
buffer.append("\n");
|
||||
}
|
||||
} catch (SocketTimeoutException e) {
|
||||
throw new Exception();
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new Exception();
|
||||
} catch (Exception e) {
|
||||
throw new Exception();
|
||||
} finally {
|
||||
try {
|
||||
if (osw != null) {
|
||||
osw.close();
|
||||
}
|
||||
if (br != null) {
|
||||
br.close();
|
||||
}
|
||||
if (con != null) {
|
||||
con.disconnect();
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user