|
| 1 | +package me.chanjar.weixin.cp.api.impl; |
| 2 | + |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import lombok.RequiredArgsConstructor; |
| 5 | +import me.chanjar.weixin.common.error.WxErrorException; |
| 6 | +import me.chanjar.weixin.common.util.json.GsonParser; |
| 7 | +import me.chanjar.weixin.cp.api.WxCpExportService; |
| 8 | +import me.chanjar.weixin.cp.api.WxCpService; |
| 9 | +import me.chanjar.weixin.cp.bean.export.WxCpExportRequest; |
| 10 | +import me.chanjar.weixin.cp.bean.export.WxCpExportResult; |
| 11 | +import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; |
| 12 | + |
| 13 | +import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Export.*; |
| 14 | + |
| 15 | +/** |
| 16 | + * 异步导出接口 |
| 17 | + * |
| 18 | + * @author <a href="https://github.com/zhongjun96">zhongjun</a> |
| 19 | + * @date 2022/4/21 |
| 20 | + **/ |
| 21 | +@RequiredArgsConstructor |
| 22 | +public class WxCpExportServiceImpl implements WxCpExportService { |
| 23 | + |
| 24 | + private final WxCpService mainService; |
| 25 | + |
| 26 | + @Override |
| 27 | + public String simpleUser(WxCpExportRequest params) throws WxErrorException { |
| 28 | + return export(SIMPLE_USER, params); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public String user(WxCpExportRequest params) throws WxErrorException { |
| 33 | + return export(USER, params); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public String department(WxCpExportRequest params) throws WxErrorException { |
| 38 | + return export(DEPARTMENT, params); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public String tagUser(WxCpExportRequest params) throws WxErrorException { |
| 43 | + return export(TAG_USER, params); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public WxCpExportResult getResult(String jobId) throws WxErrorException { |
| 48 | + String url = String.format(this.mainService.getWxCpConfigStorage().getApiUrl(GET_RESULT), jobId); |
| 49 | + String responseContent = this.mainService.get(url, null); |
| 50 | + return WxCpGsonBuilder.create().fromJson(responseContent, WxCpExportResult.class); |
| 51 | + } |
| 52 | + |
| 53 | + private String export(String path, WxCpExportRequest params) throws WxErrorException { |
| 54 | + String url = this.mainService.getWxCpConfigStorage().getApiUrl(path); |
| 55 | + String responseContent = this.mainService.post(url, params.toJson()); |
| 56 | + JsonObject tmpJson = GsonParser.parse(responseContent); |
| 57 | + return tmpJson.get("jobid").getAsString(); |
| 58 | + } |
| 59 | +} |
0 commit comments