/** 删除永久素材 */ public static WcResult deleteAbidingMedia(String tenantId, String mediaId) { WcResult rs = getClient(tenantId).deleteMaterial(WcCache.getAccessToken(tenantId), mediaId); if (rs != null && rs.isSuccess()) { WcMedia.me.delete(tenantId, mediaId); } return rs; }
/** 上传文件到upyun和微信服务器 */ public static String upload(String tenantId, MediaType type, File file, boolean isForever) { Media media = null; // 上传到upyun TODO String url = ""; // 上传到微信服务器 String accessToken = WcCache.getAccessToken(tenantId); if (isForever) { media = getClient(tenantId).uploadAbidingMedia(type, file, accessToken); } else { media = getClient(tenantId).uploadTempMedia(type, file, accessToken); } // 删除临时文件 file.delete(); // 回调校验accessToken是否有效 WcCache.refreshAccessTokenIfInvalid(media, tenantId); Assert.isTrue(media != null && media.isSuccess(), "media upload to wechat fail"); // 将信息存库 WcMedia wm = new WcMedia(); wm.set("tenant_id", tenantId); wm.set("media_id", media.getMedia_id()); wm.set("type", type.getFlag()); wm.set("url", url); if (!isForever) { // 临时素材 3 * 24 * 60 * 60 * 1000 wm.set("expires_time", new Date(media.getCreated_at() + 3 * 24 * 60 * 60 * 1000L - 5L)); } wm.set("create_time", new Date(media.getCreated_at())); Assert.isTrue(wm.save(), "save wxmedia"); return media.getMedia_id(); }