@@ -965,4 +965,27 @@ public interface WxMpService { | |||||
* @throws WxErrorException | * @throws WxErrorException | ||||
*/ | */ | ||||
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException; | public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException; | ||||
/* | |||||
* <pre> | |||||
* 设置所属行业 | |||||
* 官方文档中暂未告知响应内容 | |||||
* 详情请见:http://mp.weixin.qq.com/wiki/5/6dde9eaa909f83354e0094dc3ad99e05.html#.E8.AE.BE.E7.BD.AE.E6.89.80.E5.B1.9E.E8.A1.8C.E4.B8.9A | |||||
* </pre> | |||||
* @param wxMpIndustry | |||||
* @return JsonObject | |||||
* @throws WxErrorException | |||||
*/ | |||||
String setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException; | |||||
/*** | |||||
* <pre> | |||||
* 获取设置的行业信息 | |||||
* 详情请见:http://mp.weixin.qq.com/wiki/5/6dde9eaa909f83354e0094dc3ad99e05.html#.E8.AE.BE.E7.BD.AE.E6.89.80.E5.B1.9E.E8.A1.8C.E4.B8.9A | |||||
* </pre> | |||||
* | |||||
* @return wxMpIndustry | |||||
* @throws WxErrorException | |||||
*/ | |||||
WxMpIndustry getIndustry() throws WxErrorException; | |||||
} | } |
@@ -1294,4 +1294,20 @@ public class WxMpServiceImpl implements WxMpService { | |||||
return execute(new MediaImgUploadRequestExecutor(), url, file); | return execute(new MediaImgUploadRequestExecutor(), url, file); | ||||
} | } | ||||
@Override | |||||
public String setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException { | |||||
if (null == wxMpIndustry.getPrimaryIndustry() || null == wxMpIndustry.getPrimaryIndustry().getId() | |||||
|| null == wxMpIndustry.getSecondIndustry() || null == wxMpIndustry.getSecondIndustry().getId()) { | |||||
throw new IllegalArgumentException("industry id is empty"); | |||||
} | |||||
String url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry"; | |||||
return execute(new SimplePostRequestExecutor(), url, wxMpIndustry.toJson()); | |||||
} | |||||
@Override | |||||
public WxMpIndustry getIndustry() throws WxErrorException { | |||||
String url = "https://api.weixin.qq.com/cgi-bin/template/get_industry"; | |||||
String responseContent = execute(new SimpleGetRequestExecutor(), url, null); | |||||
return WxMpIndustry.fromJson(responseContent); | |||||
} | |||||
} | } |
@@ -0,0 +1,38 @@ | |||||
package me.chanjar.weixin.mp.bean; | |||||
import java.io.Serializable; | |||||
/** | |||||
* @author miller | |||||
* 官方文档中,创建和获取的数据结构不一样。所以采用冗余字段的方式,实现相应的接口 | |||||
*/ | |||||
public class Industry implements Serializable { | |||||
private static final long serialVersionUID = -1707184885588012142L; | |||||
private String id; | |||||
private String firstClass; | |||||
private String secondClass; | |||||
public String getId() { | |||||
return id; | |||||
} | |||||
public void setId(String id) { | |||||
this.id = id; | |||||
} | |||||
public String getFirstClass() { | |||||
return firstClass; | |||||
} | |||||
public void setFirstClass(String firstClass) { | |||||
this.firstClass = firstClass; | |||||
} | |||||
public String getSecondClass() { | |||||
return secondClass; | |||||
} | |||||
public void setSecondClass(String secondClass) { | |||||
this.secondClass = secondClass; | |||||
} | |||||
} |
@@ -0,0 +1,39 @@ | |||||
package me.chanjar.weixin.mp.bean; | |||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||||
import java.io.Serializable; | |||||
/** | |||||
* @author miller | |||||
*/ | |||||
public class WxMpIndustry implements Serializable { | |||||
private static final long serialVersionUID = -7700398224795914722L; | |||||
private Industry primaryIndustry; | |||||
private Industry secondIndustry; | |||||
public static WxMpIndustry fromJson(String json) { | |||||
return WxMpGsonBuilder.create().fromJson(json, WxMpIndustry.class); | |||||
} | |||||
public String toJson() { | |||||
return WxMpGsonBuilder.create().toJson(this); | |||||
} | |||||
public Industry getPrimaryIndustry() { | |||||
return primaryIndustry; | |||||
} | |||||
public void setPrimaryIndustry(Industry primaryIndustry) { | |||||
this.primaryIndustry = primaryIndustry; | |||||
} | |||||
public Industry getSecondIndustry() { | |||||
return secondIndustry; | |||||
} | |||||
public void setSecondIndustry(Industry secondIndustry) { | |||||
this.secondIndustry = secondIndustry; | |||||
} | |||||
} |
@@ -42,6 +42,7 @@ public class WxMpGsonBuilder { | |||||
INSTANCE.registerTypeAdapter(WxMpCard.class, new WxMpCardGsonAdapter()); | INSTANCE.registerTypeAdapter(WxMpCard.class, new WxMpCardGsonAdapter()); | ||||
INSTANCE.registerTypeAdapter(WxMpMassPreviewMessage.class, new WxMpMassPreviewMessageGsonAdapter()); | INSTANCE.registerTypeAdapter(WxMpMassPreviewMessage.class, new WxMpMassPreviewMessageGsonAdapter()); | ||||
INSTANCE.registerTypeAdapter(WxMediaImgUploadResult.class, new WxMediaImgUploadResultGsonAdapter()); | INSTANCE.registerTypeAdapter(WxMediaImgUploadResult.class, new WxMediaImgUploadResultGsonAdapter()); | ||||
INSTANCE.registerTypeAdapter(WxMpIndustry.class, new WxMpIndustryGsonAdapter()); | |||||
} | } | ||||
public static Gson create() { | public static Gson create() { | ||||
@@ -0,0 +1,44 @@ | |||||
package me.chanjar.weixin.mp.util.json; | |||||
import com.google.gson.JsonDeserializationContext; | |||||
import com.google.gson.JsonDeserializer; | |||||
import com.google.gson.JsonElement; | |||||
import com.google.gson.JsonObject; | |||||
import com.google.gson.JsonParseException; | |||||
import com.google.gson.JsonSerializationContext; | |||||
import com.google.gson.JsonSerializer; | |||||
import me.chanjar.weixin.common.util.json.GsonHelper; | |||||
import me.chanjar.weixin.mp.bean.Industry; | |||||
import me.chanjar.weixin.mp.bean.WxMpIndustry; | |||||
import java.lang.reflect.Type; | |||||
/** | |||||
* @author miller | |||||
*/ | |||||
public class WxMpIndustryGsonAdapter implements JsonSerializer<WxMpIndustry>, JsonDeserializer<WxMpIndustry> { | |||||
@Override | |||||
public JsonElement serialize(WxMpIndustry wxMpIndustry, Type type, JsonSerializationContext jsonSerializationContext) { | |||||
JsonObject json = new JsonObject(); | |||||
json.addProperty("industry_id1", wxMpIndustry.getPrimaryIndustry().getId()); | |||||
json.addProperty("industry_id2", wxMpIndustry.getSecondIndustry().getId()); | |||||
return json; | |||||
} | |||||
@Override | |||||
public WxMpIndustry deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { | |||||
WxMpIndustry wxMpIndustry = new WxMpIndustry(); | |||||
JsonObject primaryIndustry = jsonElement.getAsJsonObject().get("primary_industry").getAsJsonObject(); | |||||
wxMpIndustry.setPrimaryIndustry(convertFromJson(primaryIndustry)); | |||||
JsonObject secondaryIndustry = jsonElement.getAsJsonObject().get("secondary_industry").getAsJsonObject(); | |||||
wxMpIndustry.setSecondIndustry(convertFromJson(secondaryIndustry)); | |||||
return wxMpIndustry; | |||||
} | |||||
private Industry convertFromJson(JsonObject json) { | |||||
Industry industry = new Industry(); | |||||
industry.setFirstClass(GsonHelper.getString(json, "first_class")); | |||||
industry.setSecondClass(GsonHelper.getString(json, "second_class")); | |||||
return industry; | |||||
} | |||||
} |