|
| 1 | +package cn.binarywang.wx.miniapp.api; |
| 2 | + |
| 3 | +import cn.binarywang.wx.miniapp.config.WxMaConfig; |
| 4 | +import me.chanjar.weixin.common.exception.WxErrorException; |
| 5 | +import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; |
| 6 | +import me.chanjar.weixin.common.util.http.RequestExecutor; |
| 7 | +import me.chanjar.weixin.common.util.http.RequestHttp; |
| 8 | + |
| 9 | +/** |
| 10 | + * @author <a href="https://github.com/binarywang">Binary Wang</a> |
| 11 | + */ |
| 12 | +public interface WxMaService { |
| 13 | + /** |
| 14 | + * 获取access_token |
| 15 | + */ |
| 16 | + String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"; |
| 17 | + |
| 18 | + /** |
| 19 | + * <pre> |
| 20 | + * 验证消息的确来自微信服务器 |
| 21 | + * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319&token=&lang=zh_CN |
| 22 | + * </pre> |
| 23 | + */ |
| 24 | + boolean checkSignature(String timestamp, String nonce, String signature); |
| 25 | + |
| 26 | + /** |
| 27 | + * 获取access_token, 不强制刷新access_token |
| 28 | + * |
| 29 | + * @see #getAccessToken(boolean) |
| 30 | + */ |
| 31 | + String getAccessToken() throws WxErrorException; |
| 32 | + |
| 33 | + /** |
| 34 | + * <pre> |
| 35 | + * 获取access_token,本方法线程安全 |
| 36 | + * 且在多线程同时刷新时只刷新一次,避免超出2000次/日的调用次数上限 |
| 37 | + * |
| 38 | + * 另:本service的所有方法都会在access_token过期是调用此方法 |
| 39 | + * |
| 40 | + * 程序员在非必要情况下尽量不要主动调用此方法 |
| 41 | + * |
| 42 | + * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183&token=&lang=zh_CN |
| 43 | + * </pre> |
| 44 | + * |
| 45 | + * @param forceRefresh 强制刷新 |
| 46 | + */ |
| 47 | + String getAccessToken(boolean forceRefresh) throws WxErrorException; |
| 48 | + |
| 49 | + /** |
| 50 | + * 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求 |
| 51 | + */ |
| 52 | + String get(String url, String queryParam) throws WxErrorException; |
| 53 | + |
| 54 | + /** |
| 55 | + * 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求 |
| 56 | + */ |
| 57 | + String post(String url, String postData) throws WxErrorException; |
| 58 | + |
| 59 | + /** |
| 60 | + * <pre> |
| 61 | + * Service没有实现某个API的时候,可以用这个, |
| 62 | + * 比{@link #get}和{@link #post}方法更灵活,可以自己构造RequestExecutor用来处理不同的参数和不同的返回类型。 |
| 63 | + * 可以参考,{@link MediaUploadRequestExecutor}的实现方法 |
| 64 | + * </pre> |
| 65 | + */ |
| 66 | + <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException; |
| 67 | + |
| 68 | + /** |
| 69 | + * <pre> |
| 70 | + * 设置当微信系统响应系统繁忙时,要等待多少 retrySleepMillis(ms) * 2^(重试次数 - 1) 再发起重试 |
| 71 | + * 默认:1000ms |
| 72 | + * </pre> |
| 73 | + */ |
| 74 | + void setRetrySleepMillis(int retrySleepMillis); |
| 75 | + |
| 76 | + /** |
| 77 | + * <pre> |
| 78 | + * 设置当微信系统响应系统繁忙时,最大重试次数 |
| 79 | + * 默认:5次 |
| 80 | + * </pre> |
| 81 | + */ |
| 82 | + void setMaxRetryTimes(int maxRetryTimes); |
| 83 | + |
| 84 | + /** |
| 85 | + * 获取WxMaConfig 对象 |
| 86 | + * |
| 87 | + * @return WxMaConfig |
| 88 | + */ |
| 89 | + WxMaConfig getWxMaConfig(); |
| 90 | + |
| 91 | + /** |
| 92 | + * 注入 {@link WxMaConfig} 的实现 |
| 93 | + */ |
| 94 | + void setWxMaConfig(WxMaConfig wxConfigProvider); |
| 95 | + |
| 96 | + /** |
| 97 | + * 返回消息(客服消息和模版消息)发送接口方法实现类,以方便调用其各个接口 |
| 98 | + * |
| 99 | + * @return WxMaMsgService |
| 100 | + */ |
| 101 | + WxMaMsgService getMsgService(); |
| 102 | + |
| 103 | + /** |
| 104 | + * 返回素材相关接口方法的实现类对象,以方便调用其各个接口 |
| 105 | + * |
| 106 | + * @return WxMaMediaService |
| 107 | + */ |
| 108 | + WxMaMediaService getMediaService(); |
| 109 | + |
| 110 | + /** |
| 111 | + * 返回用户相关接口方法的实现类对象,以方便调用其各个接口 |
| 112 | + * |
| 113 | + * @return WxMaUserService |
| 114 | + */ |
| 115 | + WxMaUserService getUserService(); |
| 116 | + |
| 117 | + /** |
| 118 | + * 返回二维码相关接口方法的实现类对象,以方便调用其各个接口 |
| 119 | + * |
| 120 | + * @return WxMaQrcodeService |
| 121 | + */ |
| 122 | + WxMaQrcodeService getQrcodeService(); |
| 123 | + |
| 124 | + /** |
| 125 | + * 初始化http请求对象 |
| 126 | + */ |
| 127 | + void initHttp(); |
| 128 | + |
| 129 | + /** |
| 130 | + * 请求http请求相关信息 |
| 131 | + */ |
| 132 | + RequestHttp getRequestHttp(); |
| 133 | + |
| 134 | + |
| 135 | +} |
0 commit comments