Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

微信开放平台:增加小程序代码模板库管理 #560

Merged
merged 3 commits into from
Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.open.bean.WxOpenMaCodeTemplate;
import me.chanjar.weixin.open.bean.message.WxOpenXmlMessage;
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerInfoResult;
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerOptionResult;
import me.chanjar.weixin.open.bean.result.WxOpenQueryAuthResult;

import java.util.List;

/**
* @author <a href="https://github.com/007gzs">007</a>
*/
public interface WxOpenComponentService {

String API_COMPONENT_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
String API_CREATE_PREAUTHCODE_URL = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode";
String API_QUERY_AUTH_URL = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth";
Expand All @@ -23,7 +25,6 @@ public interface WxOpenComponentService {
String API_GET_AUTHORIZER_OPTION_URL = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_option";
String API_SET_AUTHORIZER_OPTION_URL = "https://api.weixin.qq.com/cgi-bin/component/api_set_authorizer_option";


String COMPONENT_LOGIN_PAGE_URL = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s";
String CONNECT_OAUTH2_AUTHORIZE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s&component_appid=%s#wechat_redirect";

Expand Down Expand Up @@ -87,4 +88,48 @@ public interface WxOpenComponentService {

WxMaJscode2SessionResult miniappJscode2Session(String appId, String jsCode) throws WxErrorException;

/**
* 代小程序实现业务
* <p>
* 小程序代码模版库管理:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1506504150_nMMh6&token=&lang=zh_CN
* access_token 为 component_access_token
*/
String GET_TEMPLATE_DRAFT_LIST_URL = "https://api.weixin.qq.com/wxa/gettemplatedraftlist";
String GET_TEMPLATE_LIST_URL = "https://api.weixin.qq.com/wxa/gettemplatelist";
String ADD_TO_TEMPLATE_URL = "https://api.weixin.qq.com/wxa/addtotemplate";
String DELETE_TEMPLATE_URL = "https://api.weixin.qq.com/wxa/deletetemplate";

/**
* 获取草稿箱内的所有临时代码草稿
*
* @return 草稿箱代码模板列表(draftId)
* @throws WxErrorException 获取失败时返回,具体错误码请看此接口的注释文档
*/
List<WxOpenMaCodeTemplate> getTemplateDraftList() throws WxErrorException;

/**
* 获取代码模版库中的所有小程序代码模版
*
* @return 小程序代码模版列表(templateId)
* @throws WxErrorException 获取失败时返回,具体错误码请看此接口的注释文档
*/
List<WxOpenMaCodeTemplate> getTemplateList() throws WxErrorException;

/**
* 将草稿箱的草稿选为小程序代码模版
*
* @param draftId 草稿ID,本字段可通过“获取草稿箱内的所有临时代码草稿”接口获得
* @throws WxErrorException 操作失败时抛出,具体错误码请看此接口的注释文档
* @see #getTemplateDraftList
*/
void addToTemplate(long draftId) throws WxErrorException;

/**
* 删除指定小程序代码模版
*
* @param templateId 要删除的模版ID
* @throws WxErrorException 操作失败时抛出,具体错误码请看此接口的注释文档
* @see #getTemplateList
*/
void deleteTemplate(long templateId) throws WxErrorException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.crypto.SHA1;
Expand All @@ -15,6 +17,7 @@
import me.chanjar.weixin.open.api.WxOpenService;
import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken;
import me.chanjar.weixin.open.bean.WxOpenComponentAccessToken;
import me.chanjar.weixin.open.bean.WxOpenMaCodeTemplate;
import me.chanjar.weixin.open.bean.auth.WxOpenAuthorizationInfo;
import me.chanjar.weixin.open.bean.message.WxOpenXmlMessage;
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerInfoResult;
Expand All @@ -26,13 +29,14 @@
import org.slf4j.LoggerFactory;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;

/**
* @author <a href="https://github.com/007gzs">007</a>
*/
public class WxOpenComponentServiceImpl implements WxOpenComponentService {

private static final JsonParser JSON_PARSER = new JsonParser();
private static final Map<String, WxMaService> WX_OPEN_MA_SERVICE_MAP = new Hashtable<>();
private static final Map<String, WxMpService> WX_OPEN_MP_SERVICE_MAP = new Hashtable<>();

Expand Down Expand Up @@ -288,4 +292,45 @@ public WxMaJscode2SessionResult miniappJscode2Session(String appId, String jsCod
return WxMaJscode2SessionResult.fromJson(responseContent);
}

@Override
public List<WxOpenMaCodeTemplate> getTemplateDraftList() throws WxErrorException {
String responseContent = get(GET_TEMPLATE_DRAFT_LIST_URL);
JsonObject response = JSON_PARSER.parse(StringUtils.defaultString(responseContent, "{}")).getAsJsonObject();
boolean hasDraftList = response.has("draft_list");
if (hasDraftList) {
return WxOpenGsonBuilder.create().fromJson(response.getAsJsonArray("draft_list"),
new TypeToken<List<WxOpenMaCodeTemplate>>() {
}.getType());
} else {
return null;
}
}

@Override
public List<WxOpenMaCodeTemplate> getTemplateList() throws WxErrorException {
String responseContent = get(GET_TEMPLATE_LIST_URL);
JsonObject response = JSON_PARSER.parse(StringUtils.defaultString(responseContent, "{}")).getAsJsonObject();
boolean hasDraftList = response.has("template_list");
if (hasDraftList) {
return WxOpenGsonBuilder.create().fromJson(response.getAsJsonArray("template_list"),
new TypeToken<List<WxOpenMaCodeTemplate>>() {
}.getType());
} else {
return null;
}
}

@Override
public void addToTemplate(long draftId) throws WxErrorException {
JsonObject param = new JsonObject();
param.addProperty("draft_id", draftId);
post(ADD_TO_TEMPLATE_URL, param.toString());
}

@Override
public void deleteTemplate(long templateId) throws WxErrorException {
JsonObject param = new JsonObject();
param.addProperty("template_id", templateId);
post(DELETE_TEMPLATE_URL, param.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package me.chanjar.weixin.open.bean;

import com.google.gson.annotations.SerializedName;
import lombok.Data;

import java.io.Serializable;

/**
* @author <a href="https://github.com/charmingoh">Charming</a>
* @since 2018-04-26 17:10
*/
@Data
public class WxOpenMaCodeTemplate implements Serializable {
private static final long serialVersionUID = -3278116984473619010L;
/**
* 草稿id
*/
@SerializedName(value = "draftId", alternate = "draft_id")
private Long draftId;
/**
* 模版id
*/
@SerializedName(value = "templateId", alternate = "template_id")
private Long templateId;
/**
* 模版版本号,开发者自定义字段
*/
@SerializedName(value = "userVersion", alternate = "user_version")
private String userVersion;
/**
* 模版描述 开发者自定义字段
*/
@SerializedName(value = "userDesc", alternate = "user_desc")
private String userDesc;
/**
* 开发者上传草稿时间 / 被添加为模版的时间
*/
@SerializedName(value = "createTime", alternate = "create_time")
private Long createTime;
}