|
| 1 | +package cn.binarywang.wx.miniapp.api.impl; |
| 2 | + |
| 3 | +import cn.binarywang.wx.miniapp.api.WxMaCodeService; |
| 4 | +import cn.binarywang.wx.miniapp.api.WxMaService; |
| 5 | +import cn.binarywang.wx.miniapp.bean.code.WxMaCategory; |
| 6 | +import cn.binarywang.wx.miniapp.bean.code.WxMaCodeAuditStatus; |
| 7 | +import cn.binarywang.wx.miniapp.bean.code.WxMaCodeCommitRequest; |
| 8 | +import cn.binarywang.wx.miniapp.bean.code.WxMaCodeSubmitAuditRequest; |
| 9 | +import cn.binarywang.wx.miniapp.bean.code.WxMaCodeVersionDistribution; |
| 10 | +import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; |
| 11 | +import com.google.gson.JsonObject; |
| 12 | +import com.google.gson.JsonParser; |
| 13 | +import com.google.gson.reflect.TypeToken; |
| 14 | +import me.chanjar.weixin.common.bean.result.WxError; |
| 15 | +import me.chanjar.weixin.common.exception.WxErrorException; |
| 16 | +import me.chanjar.weixin.common.util.http.BaseMediaDownloadRequestExecutor; |
| 17 | +import me.chanjar.weixin.common.util.http.RequestExecutor; |
| 18 | +import me.chanjar.weixin.common.util.json.GsonHelper; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author <a href="https://github.com/charmingoh">Charming</a> |
| 28 | + * @since 2018-04-26 20:00 |
| 29 | + */ |
| 30 | +public class WxMaCodeServiceImpl implements WxMaCodeService { |
| 31 | + private static final JsonParser JSON_PARSER = new JsonParser(); |
| 32 | + private WxMaService wxMaService; |
| 33 | + |
| 34 | + public WxMaCodeServiceImpl(WxMaService wxMaService) { |
| 35 | + this.wxMaService = wxMaService; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void commit(WxMaCodeCommitRequest commitRequest) throws WxErrorException { |
| 40 | + this.wxMaService.post(COMMIT_URL, commitRequest.toJson()); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public byte[] getQrCode() throws WxErrorException { |
| 45 | + String appId = this.wxMaService.getWxMaConfig().getAppid(); |
| 46 | + Path qrCodeFilePath = null; |
| 47 | + try { |
| 48 | + RequestExecutor<File, String> executor = BaseMediaDownloadRequestExecutor |
| 49 | + .create(this.wxMaService.getRequestHttp(), Files.createTempDirectory("weixin-java-tools-ma-" + appId).toFile()); |
| 50 | + qrCodeFilePath = this.wxMaService.execute(executor, GET_QRCODE_URL, null).toPath(); |
| 51 | + return Files.readAllBytes(qrCodeFilePath); |
| 52 | + } catch (IOException e) { |
| 53 | + throw new WxErrorException(WxError.builder().errorMsg(e.getMessage()).build(), e); |
| 54 | + } finally { |
| 55 | + if (qrCodeFilePath != null) { |
| 56 | + try { |
| 57 | + // 及时删除二维码文件,避免积压过多缓存文件 |
| 58 | + Files.delete(qrCodeFilePath); |
| 59 | + } catch (Exception ignored) { |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public List<WxMaCategory> getCategory() throws WxErrorException { |
| 67 | + String responseContent = this.wxMaService.get(GET_CATEGORY_URL, null); |
| 68 | + JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); |
| 69 | + boolean hasCategoryList = jsonObject.has("category_list"); |
| 70 | + if (hasCategoryList) { |
| 71 | + return WxMaGsonBuilder.create().fromJson(jsonObject.getAsJsonArray("category_list"), |
| 72 | + new TypeToken<List<WxMaCategory>>() { |
| 73 | + }.getType()); |
| 74 | + } else { |
| 75 | + return null; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public List<String> getPage() throws WxErrorException { |
| 81 | + String responseContent = this.wxMaService.get(GET_PAGE_URL, null); |
| 82 | + JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); |
| 83 | + boolean hasPageList = jsonObject.has("page_list"); |
| 84 | + if (hasPageList) { |
| 85 | + return WxMaGsonBuilder.create().fromJson(jsonObject.getAsJsonArray("page_list"), |
| 86 | + new TypeToken<List<String>>() { |
| 87 | + }.getType()); |
| 88 | + } else { |
| 89 | + return null; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public long submitAudit(WxMaCodeSubmitAuditRequest auditRequest) throws WxErrorException { |
| 95 | + String responseContent = this.wxMaService.post(SUBMIT_AUDIT_URL, auditRequest.toJson()); |
| 96 | + JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); |
| 97 | + return GsonHelper.getLong(jsonObject, "auditid"); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public WxMaCodeAuditStatus getAuditStatus(long auditId) throws WxErrorException { |
| 102 | + JsonObject param = new JsonObject(); |
| 103 | + param.addProperty("auditid", auditId); |
| 104 | + String responseContent = this.wxMaService.post(GET_AUDIT_STATUS_URL, param.toString()); |
| 105 | + return WxMaCodeAuditStatus.fromJson(responseContent); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public WxMaCodeAuditStatus getLatestAuditStatus() throws WxErrorException { |
| 110 | + String responseContent = this.wxMaService.get(GET_LATEST_AUDIT_STATUS_URL, null); |
| 111 | + return WxMaCodeAuditStatus.fromJson(responseContent); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void release() throws WxErrorException { |
| 116 | + this.wxMaService.post(RELEASE_URL, "{}"); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void changeVisitStatus(String action) throws WxErrorException { |
| 121 | + JsonObject param = new JsonObject(); |
| 122 | + param.addProperty("action", action); |
| 123 | + this.wxMaService.post(CHANGE_VISIT_STATUS_URL, param.toString()); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void revertCodeRelease() throws WxErrorException { |
| 128 | + this.wxMaService.get(REVERT_CODE_RELEASE_URL, null); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public WxMaCodeVersionDistribution getSupportVersion() throws WxErrorException { |
| 133 | + String responseContent = this.wxMaService.post(GET_SUPPORT_VERSION_URL, "{}"); |
| 134 | + return WxMaCodeVersionDistribution.fromJson(responseContent); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void setSupportVersion(String version) throws WxErrorException { |
| 139 | + JsonObject param = new JsonObject(); |
| 140 | + param.addProperty("version", version); |
| 141 | + this.wxMaService.post(SET_SUPPORT_VERSION_URL, param.toString()); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public void undoCodeAudit() throws WxErrorException { |
| 146 | + this.wxMaService.get(UNDO_CODE_AUDIT_URL, null); |
| 147 | + } |
| 148 | +} |
0 commit comments