Merge pull request #97 from johnnytung/develop

实现部分硬件平台接口
This commit is contained in:
Binary Wang
2016-12-15 14:20:31 +08:00
committed by GitHub
16 changed files with 674 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
package me.chanjar.weixin.mp.api;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.device.*;
/**
* Created by keungtung on 10/12/2016.
*/
public interface WxMpDeviceService {
/**
* <pre>
* 主动发送消息给设备
* 详情请见http://iot.weixin.qq.com/wiki/new/index.html?page=3-4-3
* </pre>
*/
TransMsgResp transMsg(WxDeviceMsg msg) throws WxErrorException;
/**
* <pre>
* 获取一组新的deviceid和设备二维码
* 详情请见http://iot.weixin.qq.com/wiki/new/index.html?page=3-4-6
* </pre>
* @param productId 产品id
* @return 返回WxDeviceQrCodeResult
*/
WxDeviceQrCodeResult getQrCode(String productId) throws WxErrorException;
/**
* <pre>
* 将device id及其属性信息提交公众平台进行授权
* 详情请见http://iot.weixin.qq.com/wiki/new/index.html?page=3-4-6
* </pre>
* @param wxDeviceAuthorize 授权请求对象
* @return WxDeviceAuthorizeResult
*/
WxDeviceAuthorizeResult authorize(WxDeviceAuthorize wxDeviceAuthorize) throws WxErrorException;
/**
* <pre>
* 第三方后台绑定成功后,通知公众平台
* 详情请见http://iot.weixin.qq.com/wiki/new/index.html/page=3-4-7
* </pre>
* @param wxDeviceBind 绑定请求对象
* @return WxDeviceBindResult
*/
WxDeviceBindResult bind(WxDeviceBind wxDeviceBind) throws WxErrorException;
/**
* <pre>
* 强制绑定用户和设备
* 详情请见http://iot.weixin.qq.com/wiki/new/index.html?page=3-4-7
* </pre>
* @param wxDeviceBind 强制绑定请求对象
* @return WxDeviceBindResult
*/
WxDeviceBindResult compelBind(WxDeviceBind wxDeviceBind) throws WxErrorException;
/**
* <pre>
* 第三方确认用户和设备的解绑操作
* 详情请见http://iot.weixin.qq.com/wiki/new/index.html/page=3-4-7
* </pre>
* @param wxDeviceBind 绑定请求对象
* @return WxDeviceBidResult
*/
WxDeviceBindResult unbind(WxDeviceBind wxDeviceBind) throws WxErrorException;
/**
* <pre>
* 强制解绑用户和设备
* 详情请见http://iot.weixin.qq.com/wiki/new/index.html?page=3-4-7
* </pre>
* @param wxDeviceBind 强制解绑请求对象
* @return WxDeviceBindResult
*/
WxDeviceBindResult compelUnbind(WxDeviceBind wxDeviceBind) throws WxErrorException;
}

View File

@@ -345,4 +345,11 @@ public interface WxMpService {
* @return WxMpTemplateMsgService
*/
WxMpTemplateMsgService getTemplateMsgService();
/**
* 返回硬件平台相关接口方法的实现类对象,以方便调用其各个接口
*
* @return WxMpDeviceService
*/
WxMpDeviceService getDeviceService();
}

View File

@@ -0,0 +1,72 @@
package me.chanjar.weixin.mp.api.impl;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpDeviceService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.device.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by keungtung on 10/12/2016.
*/
public class WxMpDeviceServiceImpl implements WxMpDeviceService {
private static final String API_URL_PREFIX = "https://api.weixin.qq.com/device";
private static Logger log = LoggerFactory.getLogger(WxMpMenuServiceImpl.class);
private WxMpService wxMpService;
WxMpDeviceServiceImpl(WxMpService wxMpService) {
this.wxMpService = wxMpService;
}
@Override
public TransMsgResp transMsg(WxDeviceMsg msg) throws WxErrorException {
String url = API_URL_PREFIX + "/transmsg";
String response = this.wxMpService.post(url,msg.toJson());
return TransMsgResp.fromJson(response);
}
@Override
public WxDeviceQrCodeResult getQrCode(String productId) throws WxErrorException {
String url = API_URL_PREFIX + "/getqrcode";
String response = this.wxMpService.get(url, "product_id=" + productId);
return WxDeviceQrCodeResult.fromJson(response);
}
@Override
public WxDeviceAuthorizeResult authorize(WxDeviceAuthorize wxDeviceAuthorize) throws WxErrorException {
String url = API_URL_PREFIX + "/authorize_device";
String response = this.wxMpService.post(url,wxDeviceAuthorize.toJson());
return WxDeviceAuthorizeResult.fromJson(response);
}
@Override
public WxDeviceBindResult bind(WxDeviceBind wxDeviceBind) throws WxErrorException {
String url = API_URL_PREFIX + "/bind";
String response = this.wxMpService.post(url,wxDeviceBind.toJson());
return WxDeviceBindResult.fromJson(response);
}
@Override
public WxDeviceBindResult compelBind(WxDeviceBind wxDeviceBind) throws WxErrorException {
String url = API_URL_PREFIX + "/compel_bind";
String response = this.wxMpService.post(url,wxDeviceBind.toJson());
return WxDeviceBindResult.fromJson(response);
}
@Override
public WxDeviceBindResult unbind(WxDeviceBind wxDeviceBind) throws WxErrorException {
String url = API_URL_PREFIX + "/unbind?";
String response = this.wxMpService.post(url, wxDeviceBind.toJson());
return WxDeviceBindResult.fromJson(response);
}
@Override
public WxDeviceBindResult compelUnbind(WxDeviceBind wxDeviceBind) throws WxErrorException {
String url = API_URL_PREFIX + "/compel_unbind?";
String response = this.wxMpService.post(url, wxDeviceBind.toJson());
return WxDeviceBindResult.fromJson(response);
}
}

View File

@@ -62,6 +62,8 @@ public class WxMpServiceImpl implements WxMpService {
private WxMpTemplateMsgService templateMsgService = new WxMpTemplateMsgServiceImpl(this);
private WxMpDeviceService deviceService = new WxMpDeviceServiceImpl(this);
private CloseableHttpClient httpClient;
private HttpHost httpProxy;
@@ -540,4 +542,8 @@ public class WxMpServiceImpl implements WxMpService {
return this.templateMsgService;
}
@Override
public WxMpDeviceService getDeviceService() {
return this.deviceService;
}
}