|
|
@ -1,11 +1,9 @@ |
|
|
|
package com.epmet.wxapi.service.impl; |
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.epmet.commons.tools.utils.HttpClientManager; |
|
|
|
import com.epmet.commons.tools.utils.Result; |
|
|
|
import com.epmet.wxapi.constant.WxMaCodeConstant; |
|
|
|
import com.epmet.wxapi.param.WxMaCodeAuditStatus; |
|
|
|
import com.epmet.wxapi.param.WxMaCodeAuditStatusReq; |
|
|
|
import com.epmet.wxapi.param.WxMaCodeCommitReq; |
|
|
|
import com.epmet.wxapi.param.WxMaCodeSubmitAuditRequest; |
|
|
@ -13,16 +11,12 @@ import com.epmet.wxapi.result.*; |
|
|
|
import com.epmet.wxapi.service.WxMaCodeService; |
|
|
|
import com.google.gson.Gson; |
|
|
|
import com.google.gson.GsonBuilder; |
|
|
|
import io.micrometer.core.instrument.util.JsonUtils; |
|
|
|
import lombok.Data; |
|
|
|
import me.chanjar.weixin.common.error.WxErrorException; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
import java.net.URLEncoder; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
/** |
|
|
@ -33,161 +27,186 @@ import java.util.List; |
|
|
|
*/ |
|
|
|
@Service |
|
|
|
public class WxMaCodeServiceImpl implements WxMaCodeService { |
|
|
|
private static final String ERR_CODE = "errcode"; |
|
|
|
private static final String ERR_MSG = "errmsg"; |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxResult commit(String accessToken, WxMaCodeCommitReq commitRequest) throws WxErrorException { |
|
|
|
WxResult result = new WxResult(); |
|
|
|
String url = WxMaCodeConstant.COMMIT_URL + "?" + "access_token=" + accessToken; |
|
|
|
Result<String> commitResult = HttpClientManager.getInstance().sendPostByJSON(url, toJson(commitRequest)); |
|
|
|
if(!commitResult.success()) { |
|
|
|
result.setErrorCode(commitResult.getCode()); |
|
|
|
result.setErrorMsg(commitResult.getMsg()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
JSONObject jsonObject = JSONObject.parseObject(commitResult.getData()); |
|
|
|
result.setErrorCode(jsonObject.getInteger(ERR_CODE)); |
|
|
|
result.setErrorMsg(jsonObject.getString(ERR_MSG)); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxResult getQrCode(String accessToken, String path) throws WxErrorException{ |
|
|
|
WxResult<Byte[]> result = new WxResult<>(); |
|
|
|
StringBuilder url = new StringBuilder(WxMaCodeConstant.GET_QRCODE_URL).append("?access_token").append(accessToken); |
|
|
|
if (StringUtils.isNotBlank(path)) { |
|
|
|
try { |
|
|
|
url.append("?path=").append(URLEncoder.encode(path, StandardCharsets.UTF_8.name())); |
|
|
|
} catch (UnsupportedEncodingException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
Result<Byte[]> qrCodeResult = HttpClientManager.getInstance().sendGetFile(url.toString(), null); |
|
|
|
if(!qrCodeResult.success()) { |
|
|
|
result.setErrorCode(qrCodeResult.getCode()); |
|
|
|
result.setErrorMsg(qrCodeResult.getMsg()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
result.setData(qrCodeResult.getData()); |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxResult getCategory(String accessToken) { |
|
|
|
WxResult<List<WxMaCategoryResult>> result = new WxResult<>(); |
|
|
|
String url = WxMaCodeConstant.GET_CATEGORY_URL + "?" + "access_token=" + accessToken; |
|
|
|
Result<String> getCategoryResult = HttpClientManager.getInstance().sendGet(url, null); |
|
|
|
if(!getCategoryResult.success()) { |
|
|
|
result.setErrorCode(getCategoryResult.getCode()); |
|
|
|
result.setErrorMsg(getCategoryResult.getMsg()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
WxMaGetCategoryResult categoryResult = JSONObject.parseObject(getCategoryResult.getData(), WxMaGetCategoryResult.class); |
|
|
|
result.setErrorCode(categoryResult.getErrcode()); |
|
|
|
result.setErrorMsg(categoryResult.getErrmsg()); |
|
|
|
result.setData(categoryResult.getCategoryList()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxResult getPage(String accessToken) { |
|
|
|
WxResult<List<String>> result = new WxResult<>(); |
|
|
|
String url = WxMaCodeConstant.GET_PAGE_URL + "?" + "access_token=" + accessToken; |
|
|
|
Result<String> getPageResult = HttpClientManager.getInstance().sendGet(url, null); |
|
|
|
if(!getPageResult.success()) { |
|
|
|
result.setErrorCode(getPageResult.getCode()); |
|
|
|
result.setErrorMsg(getPageResult.getMsg()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
WxMaGetPageResult pageResult = JSONObject.parseObject(getPageResult.getData(), WxMaGetPageResult.class); |
|
|
|
result.setErrorCode(pageResult.getErrcode()); |
|
|
|
result.setErrorMsg(pageResult.getErrmsg()); |
|
|
|
result.setData(pageResult.getPageList()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxResult submitAudit(String accessToken, WxMaCodeSubmitAuditRequest auditRequest) { |
|
|
|
WxResult result = new WxResult(); |
|
|
|
String url = WxMaCodeConstant.SUBMIT_AUDIT_URL + "?" + "access_token=" + accessToken; |
|
|
|
Result<String> submitResult = HttpClientManager.getInstance().sendPostByJSON(url, toJson(auditRequest)); |
|
|
|
if(!submitResult.success()) { |
|
|
|
result.setErrorCode(submitResult.getCode()); |
|
|
|
result.setErrorMsg(submitResult.getMsg()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
JSONObject jsonObject = JSONObject.parseObject(submitResult.getData()); |
|
|
|
result.setErrorCode(jsonObject.getInteger(ERR_CODE)); |
|
|
|
result.setErrorMsg(jsonObject.getString(ERR_MSG)); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxResult getAuditStatus(String accessToken, WxMaCodeAuditStatusReq request) { |
|
|
|
WxResult<WxMaAuditStatusResult> result = new WxResult<>(); |
|
|
|
String url = WxMaCodeConstant.COMMIT_URL + "?" + "access_token=" + accessToken; |
|
|
|
Result<String> statusResult = HttpClientManager.getInstance().sendPostByJSON(url, toJson(request)); |
|
|
|
if(!statusResult.success()) { |
|
|
|
result.setErrorCode(statusResult.getCode()); |
|
|
|
result.setErrorMsg(statusResult.getMsg()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
WxMaAuditStatusResult auditStatusResult = JSONObject.parseObject(statusResult.getData(), WxMaAuditStatusResult.class); |
|
|
|
result.ok(auditStatusResult); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxResult release(String accessToken) { |
|
|
|
WxResult result = new WxResult(); |
|
|
|
String url = WxMaCodeConstant.RELEASE_URL + "?" + "access_token=" + accessToken; |
|
|
|
Result<String> releaseResult = HttpClientManager.getInstance().sendPostByJSON(url, null); |
|
|
|
if(!releaseResult.success()) { |
|
|
|
result.setErrorCode(releaseResult.getCode()); |
|
|
|
result.setErrorMsg(releaseResult.getMsg()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
JSONObject jsonObject = JSONObject.parseObject(releaseResult.getData()); |
|
|
|
result.setErrorCode(jsonObject.getInteger(ERR_CODE)); |
|
|
|
result.setErrorMsg(jsonObject.getString(ERR_MSG)); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxResult undoCodeAudit(String accessToken) { |
|
|
|
WxResult result = new WxResult(); |
|
|
|
String url = WxMaCodeConstant.UNDO_CODE_AUDIT_URL + "?" + "access_token=" + accessToken; |
|
|
|
Result<String> undoResult = HttpClientManager.getInstance().sendGet(url, null); |
|
|
|
if(!undoResult.success()) { |
|
|
|
result.setErrorCode(undoResult.getCode()); |
|
|
|
result.setErrorMsg(undoResult.getMsg()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
JSONObject jsonObject = JSONObject.parseObject(undoResult.getData()); |
|
|
|
result.setErrorCode(jsonObject.getInteger(ERR_CODE)); |
|
|
|
result.setErrorMsg(jsonObject.getString(ERR_MSG)); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
String url = "{\"errcode\":0,\"errmsg\":\"ok\",\"category_list\":[{\"first_class\":\"工具\",\"second_class\":\"备忘录\",\"first_id\":1,\"second_id\":2,}\n" + |
|
|
|
"{\"first_class\":\"教育\",\"second_class\":\"学历教育\",\"third_class\":\"高等\",\"first_id\":3,\"second_id\":4,\"third_id\":5,}]}"; |
|
|
|
WxMaGetCategoryResult video = JSONObject.parseObject(url, WxMaGetCategoryResult.class); |
|
|
|
WxResult<List<WxMaCategoryResult>> result = new WxResult<>(); |
|
|
|
result.setErrorCode(video.getErrcode()); |
|
|
|
result.setErrorMsg(video.getErrmsg()); |
|
|
|
result.setData(video.getCategoryList()); |
|
|
|
System.out.println(result); |
|
|
|
WxMaCodeSubmitAuditRequest request = new WxMaCodeSubmitAuditRequest(); |
|
|
|
request.setVersionDesc("aasdf"); |
|
|
|
} |
|
|
|
private String toJson(Object object) { |
|
|
|
GsonBuilder gsonBuilder = new GsonBuilder(); |
|
|
|
gsonBuilder.setPrettyPrinting(); |
|
|
|
Gson gson = gsonBuilder.create(); |
|
|
|
return gson.toJson(object); |
|
|
|
} |
|
|
|
private static final String ERR_CODE = "errcode"; |
|
|
|
private static final String ERR_MSG = "errmsg"; |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxResult<List<WxMaTemplateResult>> getTemplateList(String accessToken) { |
|
|
|
WxResult<List<WxMaTemplateResult>> result = new WxResult<>(); |
|
|
|
String url = WxMaCodeConstant.COMMIT_URL + "?" + "access_token=" + accessToken; |
|
|
|
Result<String> templateListResult = HttpClientManager.getInstance().sendGet(url, null); |
|
|
|
if (!templateListResult.success()) { |
|
|
|
result.setErrorCode(templateListResult.getCode()); |
|
|
|
result.setErrorMsg(templateListResult.getMsg()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
Gson gson = new Gson(); |
|
|
|
WxMaTemplateListResult templateList = new Gson().fromJson(templateListResult.getData(), WxMaTemplateListResult.class); |
|
|
|
result.setErrorCode(templateList.getErrCode()); |
|
|
|
result.setErrorMsg(templateList.getErrMsg()); |
|
|
|
result.setData(templateList.getTemplateList()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxResult commit(String accessToken, WxMaCodeCommitReq commitRequest) { |
|
|
|
WxResult result = new WxResult(); |
|
|
|
String url = WxMaCodeConstant.COMMIT_URL + "?" + "access_token=" + accessToken; |
|
|
|
Result<String> commitResult = HttpClientManager.getInstance().sendPostByJSON(url, toJson(commitRequest)); |
|
|
|
if (!commitResult.success()) { |
|
|
|
result.setErrorCode(commitResult.getCode()); |
|
|
|
result.setErrorMsg(commitResult.getMsg()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
JSONObject jsonObject = JSONObject.parseObject(commitResult.getData()); |
|
|
|
result.setErrorCode(jsonObject.getInteger(ERR_CODE)); |
|
|
|
result.setErrorMsg(jsonObject.getString(ERR_MSG)); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxResult getQrCode(String accessToken, String path) { |
|
|
|
WxResult<Byte[]> result = new WxResult<>(); |
|
|
|
StringBuilder url = new StringBuilder(WxMaCodeConstant.GET_QRCODE_URL).append("?access_token").append(accessToken); |
|
|
|
if (StringUtils.isNotBlank(path)) { |
|
|
|
try { |
|
|
|
url.append("?path=").append(URLEncoder.encode(path, StandardCharsets.UTF_8.name())); |
|
|
|
} catch (UnsupportedEncodingException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
Result<Byte[]> qrCodeResult = HttpClientManager.getInstance().sendGetFile(url.toString(), null); |
|
|
|
if (!qrCodeResult.success()) { |
|
|
|
result.setErrorCode(qrCodeResult.getCode()); |
|
|
|
result.setErrorMsg(qrCodeResult.getMsg()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
result.setData(qrCodeResult.getData()); |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxResult getCategory(String accessToken) { |
|
|
|
WxResult<List<WxMaCategoryResult>> result = new WxResult<>(); |
|
|
|
String url = WxMaCodeConstant.GET_CATEGORY_URL + "?" + "access_token=" + accessToken; |
|
|
|
Result<String> getCategoryResult = HttpClientManager.getInstance().sendGet(url, null); |
|
|
|
if (!getCategoryResult.success()) { |
|
|
|
result.setErrorCode(getCategoryResult.getCode()); |
|
|
|
result.setErrorMsg(getCategoryResult.getMsg()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
WxMaGetCategoryResult categoryResult = JSONObject.parseObject(getCategoryResult.getData(), WxMaGetCategoryResult.class); |
|
|
|
result.setErrorCode(categoryResult.getErrcode()); |
|
|
|
result.setErrorMsg(categoryResult.getErrmsg()); |
|
|
|
result.setData(categoryResult.getCategoryList()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxResult getPage(String accessToken) { |
|
|
|
WxResult<List<String>> result = new WxResult<>(); |
|
|
|
String url = WxMaCodeConstant.GET_PAGE_URL + "?" + "access_token=" + accessToken; |
|
|
|
Result<String> getPageResult = HttpClientManager.getInstance().sendGet(url, null); |
|
|
|
if (!getPageResult.success()) { |
|
|
|
result.setErrorCode(getPageResult.getCode()); |
|
|
|
result.setErrorMsg(getPageResult.getMsg()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
WxMaGetPageResult pageResult = JSONObject.parseObject(getPageResult.getData(), WxMaGetPageResult.class); |
|
|
|
result.setErrorCode(pageResult.getErrcode()); |
|
|
|
result.setErrorMsg(pageResult.getErrmsg()); |
|
|
|
result.setData(pageResult.getPageList()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxResult<String> submitAudit(String accessToken, WxMaCodeSubmitAuditRequest auditRequest) { |
|
|
|
WxResult<String> result = new WxResult<>(); |
|
|
|
String url = WxMaCodeConstant.SUBMIT_AUDIT_URL + "?" + "access_token=" + accessToken; |
|
|
|
Result<String> submitResult = HttpClientManager.getInstance().sendPostByJSON(url, toJson(auditRequest)); |
|
|
|
if (!submitResult.success()) { |
|
|
|
result.setErrorCode(submitResult.getCode()); |
|
|
|
result.setErrorMsg(submitResult.getMsg()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
JSONObject jsonObject = JSONObject.parseObject(submitResult.getData()); |
|
|
|
result.setErrorCode(jsonObject.getInteger(ERR_CODE)); |
|
|
|
result.setErrorMsg(jsonObject.getString(ERR_MSG)); |
|
|
|
result.setData(jsonObject.getString("auditid")); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxResult<WxMaAuditStatusResult> getAuditStatus(String accessToken, WxMaCodeAuditStatusReq request) { |
|
|
|
WxResult<WxMaAuditStatusResult> result = new WxResult<>(); |
|
|
|
String url = WxMaCodeConstant.COMMIT_URL + "?" + "access_token=" + accessToken; |
|
|
|
Result<String> statusResult = HttpClientManager.getInstance().sendPostByJSON(url, toJson(request)); |
|
|
|
if (!statusResult.success()) { |
|
|
|
result.setErrorCode(statusResult.getCode()); |
|
|
|
result.setErrorMsg(statusResult.getMsg()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
WxMaAuditStatusResult auditStatusResult = JSONObject.parseObject(statusResult.getData(), WxMaAuditStatusResult.class); |
|
|
|
if (!auditStatusResult.success()){ |
|
|
|
result.setErrorCode(auditStatusResult.getErrcode()); |
|
|
|
result.setErrorMsg(auditStatusResult.getErrmsg()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
result.ok(auditStatusResult); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxResult release(String accessToken) { |
|
|
|
WxResult result = new WxResult(); |
|
|
|
String url = WxMaCodeConstant.RELEASE_URL + "?" + "access_token=" + accessToken; |
|
|
|
Result<String> releaseResult = HttpClientManager.getInstance().sendPostByJSON(url, null); |
|
|
|
if (!releaseResult.success()) { |
|
|
|
result.setErrorCode(releaseResult.getCode()); |
|
|
|
result.setErrorMsg(releaseResult.getMsg()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
JSONObject jsonObject = JSONObject.parseObject(releaseResult.getData()); |
|
|
|
result.setErrorCode(jsonObject.getInteger(ERR_CODE)); |
|
|
|
result.setErrorMsg(jsonObject.getString(ERR_MSG)); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxResult undoCodeAudit(String accessToken) { |
|
|
|
WxResult result = new WxResult(); |
|
|
|
String url = WxMaCodeConstant.UNDO_CODE_AUDIT_URL + "?" + "access_token=" + accessToken; |
|
|
|
Result<String> undoResult = HttpClientManager.getInstance().sendGet(url, null); |
|
|
|
if (!undoResult.success()) { |
|
|
|
result.setErrorCode(undoResult.getCode()); |
|
|
|
result.setErrorMsg(undoResult.getMsg()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
JSONObject jsonObject = JSONObject.parseObject(undoResult.getData()); |
|
|
|
result.setErrorCode(jsonObject.getInteger(ERR_CODE)); |
|
|
|
result.setErrorMsg(jsonObject.getString(ERR_MSG)); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
String url = "{\"errcode\":0,\"errmsg\":\"ok\",\"category_list\":[{\"first_class\":\"工具\",\"second_class\":\"备忘录\",\"first_id\":1,\"second_id\":2,}\n" + |
|
|
|
"{\"first_class\":\"教育\",\"second_class\":\"学历教育\",\"third_class\":\"高等\",\"first_id\":3,\"second_id\":4,\"third_id\":5,}]}"; |
|
|
|
WxMaGetCategoryResult video = JSONObject.parseObject(url, WxMaGetCategoryResult.class); |
|
|
|
WxResult<List<WxMaCategoryResult>> result = new WxResult<>(); |
|
|
|
result.setErrorCode(video.getErrcode()); |
|
|
|
result.setErrorMsg(video.getErrmsg()); |
|
|
|
result.setData(video.getCategoryList()); |
|
|
|
System.out.println(result); |
|
|
|
WxMaCodeSubmitAuditRequest request = new WxMaCodeSubmitAuditRequest(); |
|
|
|
request.setVersionDesc("aasdf"); |
|
|
|
} |
|
|
|
|
|
|
|
private String toJson(Object object) { |
|
|
|
GsonBuilder gsonBuilder = new GsonBuilder(); |
|
|
|
gsonBuilder.setPrettyPrinting(); |
|
|
|
Gson gson = gsonBuilder.create(); |
|
|
|
return gson.toJson(object); |
|
|
|
} |
|
|
|
} |