|
| 1 | +package cn.binarywang.wx.miniapp.api.impl; |
| 2 | + |
| 3 | +import cn.binarywang.wx.miniapp.api.WxMaQrcodeJumpService; |
| 4 | +import cn.binarywang.wx.miniapp.api.WxMaService; |
| 5 | +import cn.binarywang.wx.miniapp.bean.qrcode.WxMaQrcodeJumpRule; |
| 6 | +import cn.binarywang.wx.miniapp.bean.qrcode.WxMaQrcodeJumpRuleListResponse; |
| 7 | +import com.google.gson.JsonObject; |
| 8 | +import lombok.RequiredArgsConstructor; |
| 9 | +import me.chanjar.weixin.common.error.WxErrorException; |
| 10 | + |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.QrcodeJump.*; |
| 17 | +import static me.chanjar.weixin.common.util.json.WxGsonBuilder.create; |
| 18 | + |
| 19 | +/** |
| 20 | + * {@link WxMaQrcodeJumpService} 实现。 |
| 21 | + */ |
| 22 | +@RequiredArgsConstructor |
| 23 | +public class WxMaQrcodeJumpServiceImpl implements WxMaQrcodeJumpService { |
| 24 | + private final WxMaService wxMaService; |
| 25 | + |
| 26 | + @Override |
| 27 | + public String addRule(WxMaQrcodeJumpRule rule) throws WxErrorException { |
| 28 | + return this.wxMaService.post(QRCODE_JUMP_ADD, create().toJson(rule)); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public List<WxMaQrcodeJumpRule> getRules(Boolean isDefault, String prefix) throws WxErrorException { |
| 33 | + final JsonObject request = new JsonObject(); |
| 34 | + if (isDefault != null) { |
| 35 | + request.addProperty("is_default", isDefault); |
| 36 | + } |
| 37 | + if (prefix != null) { |
| 38 | + request.addProperty("prefix", prefix); |
| 39 | + } |
| 40 | + |
| 41 | + String response = this.wxMaService.post(QRCODE_JUMP_GET, request.toString()); |
| 42 | + WxMaQrcodeJumpRuleListResponse result = create().fromJson(response, WxMaQrcodeJumpRuleListResponse.class); |
| 43 | + if (result == null || result.getRuleList() == null || result.getRuleList().isEmpty()) { |
| 44 | + return Collections.emptyList(); |
| 45 | + } |
| 46 | + return result.getRuleList(); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public List<WxMaQrcodeJumpRule> getRuleList(Integer getType, Integer pageNum, Integer pageSize) throws WxErrorException { |
| 51 | + final JsonObject request = new JsonObject(); |
| 52 | + if (getType != null) { |
| 53 | + request.addProperty("get_type", getType); |
| 54 | + } |
| 55 | + if (pageNum != null) { |
| 56 | + request.addProperty("page_num", pageNum); |
| 57 | + } |
| 58 | + if (pageSize != null) { |
| 59 | + request.addProperty("page_size", pageSize); |
| 60 | + } |
| 61 | + |
| 62 | + String response = this.wxMaService.post(QRCODE_JUMP_GET_LIST, request.toString()); |
| 63 | + WxMaQrcodeJumpRuleListResponse result = create().fromJson(response, WxMaQrcodeJumpRuleListResponse.class); |
| 64 | + if (result == null || result.getRuleList() == null || result.getRuleList().isEmpty()) { |
| 65 | + return Collections.emptyList(); |
| 66 | + } |
| 67 | + return result.getRuleList(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public String deleteRule(String prefix) throws WxErrorException { |
| 72 | + final Map<String, String> request = new HashMap<>(1); |
| 73 | + request.put("prefix", prefix); |
| 74 | + return this.wxMaService.post(QRCODE_JUMP_DELETE, create().toJson(request)); |
| 75 | + } |
| 76 | +} |
0 commit comments