Comprehensive Java SDK for WeChat MiniApp development with complete platform integration
—
Complete live streaming functionality including room management, broadcast control, product integration, audience management, and real-time interaction features for WeChat MiniApp live commerce.
Core live streaming operations for room management and broadcast control.
public interface WxMaLiveService {
// Room Management
WxMaCreateRoomResult createRoom(WxMaLiveRoomInfo roomInfo) throws WxErrorException;
boolean deleteRoom(Integer roomId) throws WxErrorException;
boolean editRoom(WxMaLiveRoomInfo roomInfo) throws WxErrorException;
WxMaLiveResult getLiveInfo(Integer start, Integer limit) throws WxErrorException;
List<WxMaLiveResult.RoomInfo> getLiveInfos() throws WxErrorException;
WxMaLiveResult getLiveReplay(Integer roomId, Integer start, Integer limit) throws WxErrorException;
// Room Features
String getPushUrl(Integer roomId) throws WxErrorException;
WxMaLiveSharedCode getSharedCode(Integer roomId, String params) throws WxErrorException;
boolean updatefeedpublic(Integer roomId, Integer isFeedsPublic) throws WxErrorException;
boolean updatereplay(Integer roomId, Integer closeReplay) throws WxErrorException;
boolean updatekf(Integer roomId, Integer closeKf) throws WxErrorException;
boolean updatecomment(Integer roomId, Integer banComment) throws WxErrorException;
// Assistant Management
boolean addAssistant(Integer roomId, List<WxMaLiveAssistantInfo> users) throws WxErrorException;
boolean modifyAssistant(Integer roomId, String username, String nickname) throws WxErrorException;
boolean removeAssistant(Integer roomId, String username) throws WxErrorException;
List<WxMaAssistantResult.Assistant> getAssistantList(Integer roomId) throws WxErrorException;
// Sub-anchor Management
boolean addSubanchor(Integer roomId, String username) throws WxErrorException;
boolean modifySubanchor(Integer roomId, String username) throws WxErrorException;
boolean deleteSubanchor(Integer roomId) throws WxErrorException;
String getSubanchor(Integer roomId) throws WxErrorException;
// Product Management in Live Room
boolean addGoodsToRoom(Integer roomId, List<Integer> goodsIds) throws WxErrorException;
boolean onsale(Integer roomId, Integer goodsId, Integer onSale) throws WxErrorException;
boolean deleteInRoom(Integer roomId, Integer goodsId) throws WxErrorException;
boolean push(Integer roomId, Integer goodsId) throws WxErrorException;
boolean sort(Integer roomId, List<Map<String, String>> goods) throws WxErrorException;
String getVideo(Integer roomId, Integer goodsId) throws WxErrorException;
}Product management specifically for live streaming scenarios.
public interface WxMaLiveGoodsService {
// Live streaming product management
WxMaLiveGoodsResult addGoods(WxMaLiveGoodsInfo goodsInfo) throws WxErrorException;
boolean deleteGoods(Integer goodsId) throws WxErrorException;
boolean updateGoods(WxMaLiveGoodsInfo goodsInfo) throws WxErrorException;
WxMaLiveGoodsInfo getGoods(Integer goodsId) throws WxErrorException;
WxMaLiveGoodsList getGoodsList(Integer offset, Integer limit, Integer status) throws WxErrorException;
boolean auditGoods(Integer goodsId) throws WxErrorException;
boolean resetAuditGoods(Integer goodsId) throws WxErrorException;
}Audience and participant management for live streaming rooms.
public interface WxMaLiveMemberService {
// Member role management
boolean addRole(Integer roomId, String username, String role) throws WxErrorException;
boolean deleteRole(Integer roomId, String username, String role) throws WxErrorException;
List<WxMaLiveMemberResult.Member> getRole(Integer roomId, String role) throws WxErrorException;
}Comprehensive data models for live streaming room configuration and management.
public class WxMaLiveRoomInfo implements Serializable {
private Integer roomId; // Room ID (for updates)
private String name; // Room name
private String coverImg; // Cover image URL
private Long startTime; // Start timestamp (seconds)
private Long endTime; // End timestamp (seconds)
private String anchorName; // Anchor name
private String anchorWechat; // Anchor WeChat ID
private String shareImg; // Share image URL
private Integer feedsPublic; // Official collection (0: no, 1: yes)
private Integer type; // Room type (1: phone landscape, 0: phone portrait)
private Integer screenType; // Screen type (0: horizontal, 1: vertical)
private Integer closeLike; // Close like (0: open, 1: close)
private Integer closeGoods; // Close goods (0: open, 1: close)
private Integer closeComment; // Close comment (0: open, 1: close)
private Integer closeReplay; // Close replay (0: open, 1: close)
private Integer closeShare; // Close share (0: open, 1: close)
private Integer closeKf; // Close customer service (0: open, 1: close)
// Getters and setters
public Integer getRoomId();
public void setRoomId(Integer roomId);
public String getName();
public void setName(String name);
public String getCoverImg();
public void setCoverImg(String coverImg);
public Long getStartTime();
public void setStartTime(Long startTime);
public Long getEndTime();
public void setEndTime(Long endTime);
public String getAnchorName();
public void setAnchorName(String anchorName);
public String getAnchorWechat();
public void setAnchorWechat(String anchorWechat);
public String getShareImg();
public void setShareImg(String shareImg);
// Utility methods
public Date getStartTimeAsDate();
public Date getEndTimeAsDate();
public static WxMaLiveRoomInfo fromJson(String json);
public String toJson();
}public class WxMaCreateRoomResult implements Serializable {
private Integer roomId; // Created room ID
private String qrCodeUrl; // QR code URL for sharing
public Integer getRoomId();
public void setRoomId(Integer roomId);
public String getQrCodeUrl();
public void setQrCodeUrl(String qrCodeUrl);
}
public class WxMaLiveResult implements Serializable {
private Integer total; // Total count
private List<RoomInfo> roomInfo; // Room information list
public static class RoomInfo {
private String name; // Room name
private Integer roomId; // Room ID
private String coverImg; // Cover image
private String shareImg; // Share image
private Integer liveStatus; // Live status (101: not started, 102: live, 103: ended, 104: banned, 105: paused)
private Long startTime; // Start time
private Long endTime; // End time
private String anchorName; // Anchor name
private List<GoodsInfo> goods; // Goods in room
// Getters and setters
public String getName();
public void setName(String name);
public Integer getRoomId();
public void setRoomId(Integer roomId);
public Integer getLiveStatus();
public void setLiveStatus(Integer liveStatus);
public List<GoodsInfo> getGoods();
public void setGoods(List<GoodsInfo> goods);
}
public static class GoodsInfo {
private String coverImg; // Product cover image
private String url; // Product page URL
private Integer price; // Price (in cents)
private String name; // Product name
private Integer priceType; // Price type (1: one price, 2: price range)
// Getters and setters
public String getName();
public void setName(String name);
public Integer getPrice();
public void setPrice(Integer price);
public String getCoverImg();
public void setCoverImg(String coverImg);
}
}Models for managing assistants and roles in live streaming rooms.
public class WxMaLiveAssistantInfo implements Serializable {
private String username; // WeChat number
private String nickname; // Display nickname
public WxMaLiveAssistantInfo();
public WxMaLiveAssistantInfo(String username, String nickname);
public String getUsername();
public void setUsername(String username);
public String getNickname();
public void setNickname(String nickname);
}
public class WxMaAssistantResult implements Serializable {
private Integer count; // Assistant count
private Integer maxCount; // Maximum allowed
private List<Assistant> list; // Assistant list
public static class Assistant {
private String username; // WeChat number
private String nickname; // Display nickname
private Long timestamp; // Add timestamp
public String getUsername();
public void setUsername(String username);
public String getNickname();
public void setNickname(String nickname);
public Long getTimestamp();
public void setTimestamp(Long timestamp);
}
}Product models specifically for live streaming scenarios.
public class WxMaLiveGoodsInfo implements Serializable {
private Integer goodsId; // Goods ID
private String coverImgUrl; // Cover image URL
private String name; // Product name
private Integer priceType; // Price type (1: single price, 2: range)
private Integer price; // Price (in cents, for single price)
private Integer price2; // End price (in cents, for range)
private String url; // Product detail page URL
private Integer priceTicketPrice; // Original price (for display)
private String priceTicketDesc; // Price description
private Integer auditStatus; // Audit status (0: not submitted, 1: pending, 2: rejected, 3: approved)
// Getters and setters
public Integer getGoodsId();
public void setGoodsId(Integer goodsId);
public String getName();
public void setName(String name);
public String getCoverImgUrl();
public void setCoverImgUrl(String coverImgUrl);
public Integer getPriceType();
public void setPriceType(Integer priceType);
public Integer getPrice();
public void setPrice(Integer price);
public String getUrl();
public void setUrl(String url);
public Integer getAuditStatus();
public void setAuditStatus(Integer auditStatus);
}@Service
public class LiveStreamingService {
@Autowired
private WxMaService wxMaService;
public WxMaCreateRoomResult createLiveRoom(LiveRoomCreateRequest request) {
try {
// Upload cover and share images
String coverImgUrl = uploadImageToWeChat(request.getCoverImage());
String shareImgUrl = uploadImageToWeChat(request.getShareImage());
// Create room info
WxMaLiveRoomInfo roomInfo = new WxMaLiveRoomInfo();
roomInfo.setName(request.getRoomName());
roomInfo.setCoverImg(coverImgUrl);
roomInfo.setShareImg(shareImgUrl);
roomInfo.setAnchorName(request.getAnchorName());
roomInfo.setAnchorWechat(request.getAnchorWechatId());
// Set time (convert to seconds)
roomInfo.setStartTime(request.getStartTime().getTime() / 1000);
roomInfo.setEndTime(request.getEndTime().getTime() / 1000);
// Set room features
roomInfo.setType(request.getType()); // 0: portrait, 1: landscape
roomInfo.setScreenType(request.getScreenType()); // 0: horizontal, 1: vertical
roomInfo.setCloseLike(request.getCloseLike()); // 0: allow, 1: disable
roomInfo.setCloseGoods(request.getCloseGoods()); // 0: show, 1: hide
roomInfo.setCloseComment(request.getCloseComment()); // 0: allow, 1: disable
roomInfo.setCloseReplay(request.getCloseReplay()); // 0: allow, 1: disable
roomInfo.setCloseKf(request.getCloseKf()); // 0: allow, 1: disable
roomInfo.setFeedsPublic(request.getFeedsPublic()); // 0: private, 1: public
// Create room
WxMaCreateRoomResult result = wxMaService.getLiveService()
.createRoom(roomInfo);
logger.info("Live room created: {} - {}", result.getRoomId(), request.getRoomName());
// Store room info in database
saveRoomToDatabase(result, request);
return result;
} catch (WxErrorException e) {
logger.error("Failed to create live room: {}", e.getMessage());
throw new LiveRoomCreationException("Live room creation failed", e);
}
}
}public void updateLiveRoom(Integer roomId, LiveRoomUpdateRequest request) {
try {
// Get existing room info
WxMaLiveResult liveResult = wxMaService.getLiveService()
.getLiveInfo(0, 100);
WxMaLiveResult.RoomInfo existingRoom = liveResult.getRoomInfo().stream()
.filter(room -> room.getRoomId().equals(roomId))
.findFirst()
.orElseThrow(() -> new LiveRoomNotFoundException("Room not found: " + roomId));
// Update room info
WxMaLiveRoomInfo updateInfo = new WxMaLiveRoomInfo();
updateInfo.setRoomId(roomId);
updateInfo.setName(request.getName() != null ? request.getName() : existingRoom.getName());
if (request.getCoverImage() != null) {
String newCoverUrl = uploadImageToWeChat(request.getCoverImage());
updateInfo.setCoverImg(newCoverUrl);
}
if (request.getStartTime() != null) {
updateInfo.setStartTime(request.getStartTime().getTime() / 1000);
}
if (request.getEndTime() != null) {
updateInfo.setEndTime(request.getEndTime().getTime() / 1000);
}
// Apply updates
boolean success = wxMaService.getLiveService().editRoom(updateInfo);
if (success) {
logger.info("Live room updated: {}", roomId);
}
} catch (WxErrorException e) {
logger.error("Failed to update live room {}: {}", roomId, e.getMessage());
throw new LiveRoomUpdateException("Room update failed", e);
}
}public void configureRoomFeatures(Integer roomId, RoomFeaturesConfig config) {
try {
// Update individual features
if (config.getFeedsPublic() != null) {
wxMaService.getLiveService().updatefeedpublic(roomId, config.getFeedsPublic());
}
if (config.getCloseReplay() != null) {
wxMaService.getLiveService().updatereplay(roomId, config.getCloseReplay());
}
if (config.getCloseKf() != null) {
wxMaService.getLiveService().updatekf(roomId, config.getCloseKf());
}
if (config.getBanComment() != null) {
wxMaService.getLiveService().updatecomment(roomId, config.getBanComment());
}
logger.info("Room features updated for room: {}", roomId);
} catch (WxErrorException e) {
logger.error("Failed to update room features for room {}: {}",
roomId, e.getMessage());
}
}public void addLiveAssistants(Integer roomId, List<AssistantInfo> assistants) {
try {
List<WxMaLiveAssistantInfo> assistantInfos = assistants.stream()
.map(assistant -> new WxMaLiveAssistantInfo(
assistant.getWechatId(),
assistant.getNickname()
))
.collect(Collectors.toList());
boolean success = wxMaService.getLiveService()
.addAssistant(roomId, assistantInfos);
if (success) {
logger.info("Added {} assistants to room {}", assistants.size(), roomId);
}
} catch (WxErrorException e) {
logger.error("Failed to add assistants to room {}: {}", roomId, e.getMessage());
}
}
public void manageAssistant(Integer roomId, String wechatId, String action, String nickname) {
try {
switch (action.toLowerCase()) {
case "modify":
wxMaService.getLiveService().modifyAssistant(roomId, wechatId, nickname);
logger.info("Modified assistant {} in room {}", wechatId, roomId);
break;
case "remove":
wxMaService.getLiveService().removeAssistant(roomId, wechatId);
logger.info("Removed assistant {} from room {}", wechatId, roomId);
break;
default:
logger.warn("Unknown assistant action: {}", action);
}
} catch (WxErrorException e) {
logger.error("Failed to {} assistant {} in room {}: {}",
action, wechatId, roomId, e.getMessage());
}
}public void addSubAnchor(Integer roomId, String wechatId) {
try {
boolean success = wxMaService.getLiveService().addSubanchor(roomId, wechatId);
if (success) {
logger.info("Added sub-anchor {} to room {}", wechatId, roomId);
}
} catch (WxErrorException e) {
logger.error("Failed to add sub-anchor {} to room {}: {}",
wechatId, roomId, e.getMessage());
}
}
public String getSubAnchor(Integer roomId) {
try {
return wxMaService.getLiveService().getSubanchor(roomId);
} catch (WxErrorException e) {
logger.error("Failed to get sub-anchor for room {}: {}", roomId, e.getMessage());
return null;
}
}@Service
public class LiveProductService {
public void addProductsToLiveRoom(Integer roomId, List<Integer> productIds) {
try {
boolean success = wxMaService.getLiveService()
.addGoodsToRoom(roomId, productIds);
if (success) {
logger.info("Added {} products to live room {}", productIds.size(), roomId);
// Set initial products as on-sale
for (Integer productId : productIds) {
wxMaService.getLiveService().onsale(roomId, productId, 1);
}
}
} catch (WxErrorException e) {
logger.error("Failed to add products to room {}: {}", roomId, e.getMessage());
}
}
public void manageProductInRoom(Integer roomId, Integer productId, String action) {
try {
switch (action.toLowerCase()) {
case "onsale":
wxMaService.getLiveService().onsale(roomId, productId, 1);
logger.info("Put product {} on sale in room {}", productId, roomId);
break;
case "offsale":
wxMaService.getLiveService().onsale(roomId, productId, 0);
logger.info("Took product {} off sale in room {}", productId, roomId);
break;
case "push":
wxMaService.getLiveService().push(roomId, productId);
logger.info("Pushed product {} in room {}", productId, roomId);
break;
case "remove":
wxMaService.getLiveService().deleteInRoom(roomId, productId);
logger.info("Removed product {} from room {}", productId, roomId);
break;
}
} catch (WxErrorException e) {
logger.error("Failed to {} product {} in room {}: {}",
action, productId, roomId, e.getMessage());
}
}
public void sortProductsInRoom(Integer roomId, List<ProductSortInfo> sortOrder) {
try {
List<Map<String, String>> goods = sortOrder.stream()
.map(product -> {
Map<String, String> goodsMap = new HashMap<>();
goodsMap.put("goodsId", product.getProductId().toString());
goodsMap.put("sortOrder", product.getSortOrder().toString());
return goodsMap;
})
.collect(Collectors.toList());
boolean success = wxMaService.getLiveService().sort(roomId, goods);
if (success) {
logger.info("Updated product sort order for room {}", roomId);
}
} catch (WxErrorException e) {
logger.error("Failed to sort products in room {}: {}", roomId, e.getMessage());
}
}
}public Integer createLiveProduct(LiveProductCreateRequest request) {
try {
// Upload product cover image
String coverImgUrl = uploadImageToWeChat(request.getCoverImage());
WxMaLiveGoodsInfo goodsInfo = new WxMaLiveGoodsInfo();
goodsInfo.setCoverImgUrl(coverImgUrl);
goodsInfo.setName(request.getName());
goodsInfo.setPriceType(request.getPriceType()); // 1: single, 2: range
if (request.getPriceType() == 1) {
goodsInfo.setPrice(request.getPrice());
} else {
goodsInfo.setPrice(request.getMinPrice());
goodsInfo.setPrice2(request.getMaxPrice());
}
goodsInfo.setUrl(request.getProductUrl());
goodsInfo.setPriceTicketPrice(request.getOriginalPrice());
goodsInfo.setPriceTicketDesc(request.getPriceDescription());
WxMaLiveGoodsResult result = wxMaService.getLiveGoodsService()
.addGoods(goodsInfo);
Integer goodsId = result.getGoodsId();
logger.info("Live product created: {} - {}", goodsId, request.getName());
// Submit for audit
wxMaService.getLiveGoodsService().auditGoods(goodsId);
return goodsId;
} catch (WxErrorException e) {
logger.error("Failed to create live product: {}", e.getMessage());
throw new LiveProductCreationException("Live product creation failed", e);
}
}public void updateLiveProduct(Integer goodsId, LiveProductUpdateRequest request) {
try {
// Get existing product
WxMaLiveGoodsInfo existingProduct = wxMaService.getLiveGoodsService()
.getGoods(goodsId);
// Update fields
if (request.getName() != null) {
existingProduct.setName(request.getName());
}
if (request.getPrice() != null) {
existingProduct.setPrice(request.getPrice());
}
if (request.getCoverImage() != null) {
String newCoverUrl = uploadImageToWeChat(request.getCoverImage());
existingProduct.setCoverImgUrl(newCoverUrl);
}
// Update product
boolean success = wxMaService.getLiveGoodsService()
.updateGoods(existingProduct);
if (success) {
logger.info("Live product updated: {}", goodsId);
// Re-submit for audit if needed
if (request.isResubmitForAudit()) {
wxMaService.getLiveGoodsService().resetAuditGoods(goodsId);
wxMaService.getLiveGoodsService().auditGoods(goodsId);
}
}
} catch (WxErrorException e) {
logger.error("Failed to update live product {}: {}", goodsId, e.getMessage());
}
}
public void deleteLiveProduct(Integer goodsId) {
try {
boolean success = wxMaService.getLiveGoodsService().deleteGoods(goodsId);
if (success) {
logger.info("Live product deleted: {}", goodsId);
}
} catch (WxErrorException e) {
logger.error("Failed to delete live product {}: {}", goodsId, e.getMessage());
}
}@Service
public class LiveAnalyticsService {
public LiveRoomStats getLiveRoomStats(Integer roomId) {
try {
// Get room info
WxMaLiveResult roomResult = wxMaService.getLiveService()
.getLiveInfo(0, 100);
WxMaLiveResult.RoomInfo room = roomResult.getRoomInfo().stream()
.filter(r -> r.getRoomId().equals(roomId))
.findFirst()
.orElse(null);
if (room == null) {
throw new LiveRoomNotFoundException("Room not found: " + roomId);
}
// Get replay data
WxMaLiveResult replayResult = wxMaService.getLiveService()
.getLiveReplay(roomId, 0, 100);
// Build statistics
LiveRoomStats stats = new LiveRoomStats();
stats.setRoomId(roomId);
stats.setRoomName(room.getName());
stats.setLiveStatus(room.getLiveStatus());
stats.setProductCount(room.getGoods() != null ? room.getGoods().size() : 0);
stats.setReplayCount(replayResult.getTotal());
return stats;
} catch (WxErrorException e) {
logger.error("Failed to get room stats for {}: {}", roomId, e.getMessage());
throw new LiveAnalyticsException("Failed to get room statistics", e);
}
}
@Scheduled(fixedRate = 300000) // Every 5 minutes
public void monitorLiveRooms() {
try {
List<WxMaLiveResult.RoomInfo> rooms = wxMaService.getLiveService()
.getLiveInfos();
for (WxMaLiveResult.RoomInfo room : rooms) {
if (room.getLiveStatus() == 102) { // Currently live
// Monitor live room metrics
monitorRoomMetrics(room);
}
}
} catch (WxErrorException e) {
logger.error("Live room monitoring failed: {}", e.getMessage());
}
}
private void monitorRoomMetrics(WxMaLiveResult.RoomInfo room) {
// Track viewer count, engagement, product performance
// Send alerts if needed
logger.info("Room {} is live with {} products",
room.getRoomId(),
room.getGoods() != null ? room.getGoods().size() : 0);
}
}@Component
public class LiveRoomEventHandler {
@EventListener
public void handleLiveRoomStart(LiveRoomStartEvent event) {
Integer roomId = event.getRoomId();
try {
// Get push URL for streaming
String pushUrl = wxMaService.getLiveService().getPushUrl(roomId);
// Notify streaming system
streamingService.startStream(roomId, pushUrl);
// Send notifications to followers
notificationService.notifyLiveStart(roomId);
logger.info("Live room {} started with push URL: {}", roomId, pushUrl);
} catch (WxErrorException e) {
logger.error("Failed to handle live room start for {}: {}",
roomId, e.getMessage());
}
}
@EventListener
public void handleLiveRoomEnd(LiveRoomEndEvent event) {
Integer roomId = event.getRoomId();
// Generate room performance report
LiveRoomStats stats = liveAnalyticsService.getLiveRoomStats(roomId);
// Save metrics to database
metricsService.saveLiveRoomMetrics(stats);
logger.info("Live room {} ended. Stats saved.", roomId);
}
}This live streaming module provides comprehensive functionality for creating, managing, and monitoring WeChat MiniApp live streaming rooms with full product integration, role management, and real-time interaction capabilities.
Install with Tessl CLI
npx tessl i tessl/maven-com-github-binarywang--weixin-java-miniapp