Skip to content

Commit af2f122

Browse files
committed
行业相关api
1 parent 7a816cf commit af2f122

File tree

6 files changed

+161
-0
lines changed

6 files changed

+161
-0
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,4 +965,27 @@ public void markCardCode(String code, String cardId, String openId, boolean isMa
965965
* @throws WxErrorException
966966
*/
967967
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException;
968+
969+
/*
970+
* <pre>
971+
* 设置所属行业
972+
* 官方文档中暂未告知响应内容
973+
* 详情请见: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
974+
* </pre>
975+
* @param wxMpIndustry
976+
* @return JsonObject
977+
* @throws WxErrorException
978+
*/
979+
String setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException;
980+
981+
/***
982+
* <pre>
983+
* 获取设置的行业信息
984+
* 详情请见: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
985+
* </pre>
986+
*
987+
* @return wxMpIndustry
988+
* @throws WxErrorException
989+
*/
990+
WxMpIndustry getIndustry() throws WxErrorException;
968991
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,4 +1294,20 @@ public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException
12941294
return execute(new MediaImgUploadRequestExecutor(), url, file);
12951295
}
12961296

1297+
@Override
1298+
public String setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException {
1299+
if (null == wxMpIndustry.getPrimaryIndustry() || null == wxMpIndustry.getPrimaryIndustry().getId()
1300+
|| null == wxMpIndustry.getSecondIndustry() || null == wxMpIndustry.getSecondIndustry().getId()) {
1301+
throw new IllegalArgumentException("industry id is empty");
1302+
}
1303+
String url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry";
1304+
return execute(new SimplePostRequestExecutor(), url, wxMpIndustry.toJson());
1305+
}
1306+
1307+
@Override
1308+
public WxMpIndustry getIndustry() throws WxErrorException {
1309+
String url = "https://api.weixin.qq.com/cgi-bin/template/get_industry";
1310+
String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
1311+
return WxMpIndustry.fromJson(responseContent);
1312+
}
12971313
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package me.chanjar.weixin.mp.bean;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* @author miller
7+
* 官方文档中,创建和获取的数据结构不一样。所以采用冗余字段的方式,实现相应的接口
8+
*/
9+
public class Industry implements Serializable {
10+
private static final long serialVersionUID = -1707184885588012142L;
11+
private String id;
12+
private String firstClass;
13+
private String secondClass;
14+
15+
public String getId() {
16+
return id;
17+
}
18+
19+
public void setId(String id) {
20+
this.id = id;
21+
}
22+
23+
public String getFirstClass() {
24+
return firstClass;
25+
}
26+
27+
public void setFirstClass(String firstClass) {
28+
this.firstClass = firstClass;
29+
}
30+
31+
public String getSecondClass() {
32+
return secondClass;
33+
}
34+
35+
public void setSecondClass(String secondClass) {
36+
this.secondClass = secondClass;
37+
}
38+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package me.chanjar.weixin.mp.bean;
2+
3+
4+
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
5+
6+
import java.io.Serializable;
7+
8+
/**
9+
* @author miller
10+
*/
11+
public class WxMpIndustry implements Serializable {
12+
private static final long serialVersionUID = -7700398224795914722L;
13+
private Industry primaryIndustry;
14+
private Industry secondIndustry;
15+
16+
public static WxMpIndustry fromJson(String json) {
17+
return WxMpGsonBuilder.create().fromJson(json, WxMpIndustry.class);
18+
}
19+
20+
public String toJson() {
21+
return WxMpGsonBuilder.create().toJson(this);
22+
}
23+
24+
public Industry getPrimaryIndustry() {
25+
return primaryIndustry;
26+
}
27+
28+
public void setPrimaryIndustry(Industry primaryIndustry) {
29+
this.primaryIndustry = primaryIndustry;
30+
}
31+
32+
public Industry getSecondIndustry() {
33+
return secondIndustry;
34+
}
35+
36+
public void setSecondIndustry(Industry secondIndustry) {
37+
this.secondIndustry = secondIndustry;
38+
}
39+
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpGsonBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class WxMpGsonBuilder {
4242
INSTANCE.registerTypeAdapter(WxMpCard.class, new WxMpCardGsonAdapter());
4343
INSTANCE.registerTypeAdapter(WxMpMassPreviewMessage.class, new WxMpMassPreviewMessageGsonAdapter());
4444
INSTANCE.registerTypeAdapter(WxMediaImgUploadResult.class, new WxMediaImgUploadResultGsonAdapter());
45+
INSTANCE.registerTypeAdapter(WxMpIndustry.class, new WxMpIndustryGsonAdapter());
4546
}
4647

4748
public static Gson create() {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package me.chanjar.weixin.mp.util.json;
2+
3+
import com.google.gson.JsonDeserializationContext;
4+
import com.google.gson.JsonDeserializer;
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonObject;
7+
import com.google.gson.JsonParseException;
8+
import com.google.gson.JsonSerializationContext;
9+
import com.google.gson.JsonSerializer;
10+
import me.chanjar.weixin.common.util.json.GsonHelper;
11+
import me.chanjar.weixin.mp.bean.Industry;
12+
import me.chanjar.weixin.mp.bean.WxMpIndustry;
13+
14+
import java.lang.reflect.Type;
15+
16+
/**
17+
* @author miller
18+
*/
19+
public class WxMpIndustryGsonAdapter implements JsonSerializer<WxMpIndustry>, JsonDeserializer<WxMpIndustry> {
20+
@Override
21+
public JsonElement serialize(WxMpIndustry wxMpIndustry, Type type, JsonSerializationContext jsonSerializationContext) {
22+
JsonObject json = new JsonObject();
23+
json.addProperty("industry_id1", wxMpIndustry.getPrimaryIndustry().getId());
24+
json.addProperty("industry_id2", wxMpIndustry.getSecondIndustry().getId());
25+
return json;
26+
}
27+
28+
@Override
29+
public WxMpIndustry deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
30+
WxMpIndustry wxMpIndustry = new WxMpIndustry();
31+
JsonObject primaryIndustry = jsonElement.getAsJsonObject().get("primary_industry").getAsJsonObject();
32+
wxMpIndustry.setPrimaryIndustry(convertFromJson(primaryIndustry));
33+
JsonObject secondaryIndustry = jsonElement.getAsJsonObject().get("secondary_industry").getAsJsonObject();
34+
wxMpIndustry.setSecondIndustry(convertFromJson(secondaryIndustry));
35+
return wxMpIndustry;
36+
}
37+
38+
private Industry convertFromJson(JsonObject json) {
39+
Industry industry = new Industry();
40+
industry.setFirstClass(GsonHelper.getString(json, "first_class"));
41+
industry.setSecondClass(GsonHelper.getString(json, "second_class"));
42+
return industry;
43+
}
44+
}

0 commit comments

Comments
 (0)