|
|
@ -11,7 +11,8 @@ import androidx.annotation.NonNull;
|
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.idormy.sms.forwarder.MyApplication;
|
|
|
|
import com.idormy.sms.forwarder.model.SenderModel;
|
|
|
|
|
|
|
|
import com.idormy.sms.forwarder.model.vo.QYWXAppSettingVo;
|
|
|
|
import com.idormy.sms.forwarder.utils.LogUtil;
|
|
|
|
import com.idormy.sms.forwarder.utils.LogUtil;
|
|
|
|
import com.idormy.sms.forwarder.utils.SettingUtil;
|
|
|
|
import com.idormy.sms.forwarder.utils.SettingUtil;
|
|
|
|
|
|
|
|
|
|
|
@ -36,17 +37,33 @@ public class SenderQyWxAppMsg extends SenderBaseMsg {
|
|
|
|
|
|
|
|
|
|
|
|
static final String TAG = "SenderQyWxAppMsg";
|
|
|
|
static final String TAG = "SenderQyWxAppMsg";
|
|
|
|
|
|
|
|
|
|
|
|
public static void sendMsg(final long logId, final Handler handError, String corpID, String agentID, String secret, String toUser, String content, boolean forceRefresh) throws Exception {
|
|
|
|
public static void sendMsg(final long logId, final Handler handError, final SenderModel senderModel, final QYWXAppSettingVo qYWXAppSettingVo, String content) throws Exception {
|
|
|
|
Log.i(TAG, "sendMsg corpID:" + corpID + " agentID:" + agentID + " secret:" + secret + " toUser:" + toUser + " content:" + content + " forceRefresh:" + forceRefresh);
|
|
|
|
|
|
|
|
|
|
|
|
if (qYWXAppSettingVo == null) {
|
|
|
|
|
|
|
|
Toast(handError, TAG, "参数错误");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String corpID = qYWXAppSettingVo.getCorpID();
|
|
|
|
|
|
|
|
String agentID = qYWXAppSettingVo.getAgentID();
|
|
|
|
|
|
|
|
String secret = qYWXAppSettingVo.getSecret();
|
|
|
|
|
|
|
|
String toUser = qYWXAppSettingVo.getToUser();
|
|
|
|
|
|
|
|
Boolean atAll = qYWXAppSettingVo.getAtAll();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Log.i(TAG, "sendMsg corpID:" + corpID + " agentID:" + agentID + " secret:" + secret + " toUser:" + toUser + " content:" + content);
|
|
|
|
|
|
|
|
|
|
|
|
if (corpID == null || corpID.isEmpty() || agentID == null || agentID.isEmpty() || secret == null || secret.isEmpty()) {
|
|
|
|
if (corpID == null || corpID.isEmpty() || agentID == null || agentID.isEmpty() || secret == null || secret.isEmpty()) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//TODO:判断access_token是否失效
|
|
|
|
Observable
|
|
|
|
if (forceRefresh
|
|
|
|
.create((ObservableEmitter<Object> emitter) -> {
|
|
|
|
|| MyApplication.QyWxAccessToken == null || MyApplication.QyWxAccessToken.isEmpty()
|
|
|
|
Toast(handError, TAG, "开始请求接口...");
|
|
|
|
|| System.currentTimeMillis() > MyApplication.QyWxAccessTokenExpiresIn) {
|
|
|
|
|
|
|
|
|
|
|
|
//TODO:获取有效access_token
|
|
|
|
|
|
|
|
String accessToken = qYWXAppSettingVo.getAccessToken();
|
|
|
|
|
|
|
|
if (accessToken == null || accessToken.isEmpty()) {
|
|
|
|
|
|
|
|
|
|
|
|
String getTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?";
|
|
|
|
String getTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?";
|
|
|
|
getTokenUrl += "corpid=" + corpID;
|
|
|
|
getTokenUrl += "corpid=" + corpID;
|
|
|
|
getTokenUrl += "&corpsecret=" + secret;
|
|
|
|
getTokenUrl += "&corpsecret=" + secret;
|
|
|
@ -59,6 +76,12 @@ public class SenderQyWxAppMsg extends SenderBaseMsg {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call call, @NonNull final IOException e) {
|
|
|
|
public void onFailure(@NonNull Call call, @NonNull final IOException e) {
|
|
|
|
LogUtil.updateLog(logId, 0, e.getMessage());
|
|
|
|
LogUtil.updateLog(logId, 0, e.getMessage());
|
|
|
|
|
|
|
|
qYWXAppSettingVo.setAccessToken("");
|
|
|
|
|
|
|
|
qYWXAppSettingVo.setExpiresIn(0L);
|
|
|
|
|
|
|
|
if (senderModel != null) {
|
|
|
|
|
|
|
|
senderModel.setJsonSetting(JSON.toJSONString(qYWXAppSettingVo));
|
|
|
|
|
|
|
|
SenderUtil.updateSender(senderModel);
|
|
|
|
|
|
|
|
}
|
|
|
|
Log.d(TAG, "onFailure:" + e.getMessage());
|
|
|
|
Log.d(TAG, "onFailure:" + e.getMessage());
|
|
|
|
if (handError != null) {
|
|
|
|
if (handError != null) {
|
|
|
|
Message msg = new Message();
|
|
|
|
Message msg = new Message();
|
|
|
@ -67,6 +90,8 @@ public class SenderQyWxAppMsg extends SenderBaseMsg {
|
|
|
|
bundle.putString("DATA", "获取access_token失败:" + e.getMessage());
|
|
|
|
bundle.putString("DATA", "获取access_token失败:" + e.getMessage());
|
|
|
|
msg.setData(bundle);
|
|
|
|
msg.setData(bundle);
|
|
|
|
handError.sendMessage(msg);
|
|
|
|
handError.sendMessage(msg);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
emitter.onError(new RuntimeException("请求接口异常..."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -77,12 +102,19 @@ public class SenderQyWxAppMsg extends SenderBaseMsg {
|
|
|
|
JSONObject jsonObject = JSON.parseObject(json);
|
|
|
|
JSONObject jsonObject = JSON.parseObject(json);
|
|
|
|
int errcode = jsonObject.getInteger("errcode");
|
|
|
|
int errcode = jsonObject.getInteger("errcode");
|
|
|
|
if (errcode == 0) {
|
|
|
|
if (errcode == 0) {
|
|
|
|
MyApplication.QyWxAccessToken = jsonObject.getString("access_token");
|
|
|
|
String access_token = jsonObject.getString("access_token");
|
|
|
|
MyApplication.QyWxAccessTokenExpiresIn = System.currentTimeMillis() + (jsonObject.getInteger("expires_in") - 120) * 1000L; //提前2分钟过期
|
|
|
|
long expires_in = System.currentTimeMillis() + (jsonObject.getInteger("expires_in") - 120) * 1000L; //提前2分钟过期
|
|
|
|
Log.d(TAG, "access_token:" + MyApplication.QyWxAccessToken);
|
|
|
|
Log.d(TAG, "access_token:" + access_token);
|
|
|
|
Log.d(TAG, "expires_in:" + MyApplication.QyWxAccessTokenExpiresIn);
|
|
|
|
Log.d(TAG, "expires_in:" + expires_in);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
qYWXAppSettingVo.setAccessToken(access_token);
|
|
|
|
|
|
|
|
qYWXAppSettingVo.setExpiresIn(expires_in);
|
|
|
|
|
|
|
|
if (senderModel != null) {
|
|
|
|
|
|
|
|
senderModel.setJsonSetting(JSON.toJSONString(qYWXAppSettingVo));
|
|
|
|
|
|
|
|
SenderUtil.updateSender(senderModel);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sendTextMsg(logId, handError, agentID, toUser, content);
|
|
|
|
sendTextMsg(emitter, logId, handError, agentID, toUser, content, access_token);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
String errmsg = jsonObject.getString("errmsg");
|
|
|
|
String errmsg = jsonObject.getString("errmsg");
|
|
|
|
LogUtil.updateLog(logId, 0, errmsg);
|
|
|
|
LogUtil.updateLog(logId, 0, errmsg);
|
|
|
@ -95,18 +127,35 @@ public class SenderQyWxAppMsg extends SenderBaseMsg {
|
|
|
|
msg.setData(bundle);
|
|
|
|
msg.setData(bundle);
|
|
|
|
handError.sendMessage(msg);
|
|
|
|
handError.sendMessage(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
emitter.onError(new RuntimeException("请求接口异常..."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
sendTextMsg(logId, handError, agentID, toUser, content);
|
|
|
|
sendTextMsg(emitter, logId, handError, agentID, toUser, content, accessToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}).retryWhen((Observable<Throwable> errorObservable) -> errorObservable
|
|
|
|
|
|
|
|
.zipWith(Observable.just(
|
|
|
|
|
|
|
|
SettingUtil.getRetryDelayTime(1),
|
|
|
|
|
|
|
|
SettingUtil.getRetryDelayTime(2),
|
|
|
|
|
|
|
|
SettingUtil.getRetryDelayTime(3),
|
|
|
|
|
|
|
|
SettingUtil.getRetryDelayTime(4),
|
|
|
|
|
|
|
|
SettingUtil.getRetryDelayTime(5)
|
|
|
|
|
|
|
|
), (Throwable e, Integer time) -> time)
|
|
|
|
|
|
|
|
.flatMap((Integer delay) -> {
|
|
|
|
|
|
|
|
Toast(handError, TAG, "请求接口异常," + delay + "秒后重试");
|
|
|
|
|
|
|
|
return Observable.timer(delay, TimeUnit.SECONDS);
|
|
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
.subscribe(System.out::println);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//发送文本消息
|
|
|
|
//发送文本消息
|
|
|
|
public static void sendTextMsg(final long logId, final Handler handError, String agentID, String toUser, String content) {
|
|
|
|
public static void sendTextMsg(ObservableEmitter<Object> emitter, final long logId, final Handler handError, String agentID, String toUser, String content, String accessToken) {
|
|
|
|
|
|
|
|
|
|
|
|
Map textMsgMap = new HashMap();
|
|
|
|
Map textMsgMap = new HashMap();
|
|
|
|
textMsgMap.put("touser", toUser);
|
|
|
|
textMsgMap.put("touser", toUser);
|
|
|
@ -117,14 +166,11 @@ public class SenderQyWxAppMsg extends SenderBaseMsg {
|
|
|
|
textText.put("content", content);
|
|
|
|
textText.put("content", content);
|
|
|
|
textMsgMap.put("text", textText);
|
|
|
|
textMsgMap.put("text", textText);
|
|
|
|
|
|
|
|
|
|
|
|
final String requestUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + MyApplication.QyWxAccessToken;
|
|
|
|
final String requestUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + accessToken;
|
|
|
|
Log.i(TAG, "requestUrl:" + requestUrl);
|
|
|
|
Log.i(TAG, "requestUrl:" + requestUrl);
|
|
|
|
final String requestMsg = JSON.toJSONString(textMsgMap);
|
|
|
|
final String requestMsg = JSON.toJSONString(textMsgMap);
|
|
|
|
Log.i(TAG, "requestMsg:" + requestMsg);
|
|
|
|
Log.i(TAG, "requestMsg:" + requestMsg);
|
|
|
|
|
|
|
|
|
|
|
|
Observable
|
|
|
|
|
|
|
|
.create((ObservableEmitter<Object> emitter) -> {
|
|
|
|
|
|
|
|
Toast(handError, TAG, "开始请求接口...");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OkHttpClient client = new OkHttpClient();
|
|
|
|
OkHttpClient client = new OkHttpClient();
|
|
|
|
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), requestMsg);
|
|
|
|
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), requestMsg);
|
|
|
@ -158,19 +204,6 @@ public class SenderQyWxAppMsg extends SenderBaseMsg {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
}).retryWhen((Observable<Throwable> errorObservable) -> errorObservable
|
|
|
|
|
|
|
|
.zipWith(Observable.just(
|
|
|
|
|
|
|
|
SettingUtil.getRetryDelayTime(1),
|
|
|
|
|
|
|
|
SettingUtil.getRetryDelayTime(2),
|
|
|
|
|
|
|
|
SettingUtil.getRetryDelayTime(3),
|
|
|
|
|
|
|
|
SettingUtil.getRetryDelayTime(4),
|
|
|
|
|
|
|
|
SettingUtil.getRetryDelayTime(5)
|
|
|
|
|
|
|
|
), (Throwable e, Integer time) -> time)
|
|
|
|
|
|
|
|
.flatMap((Integer delay) -> {
|
|
|
|
|
|
|
|
Toast(handError, TAG, "请求接口异常," + delay + "秒后重试");
|
|
|
|
|
|
|
|
return Observable.timer(delay, TimeUnit.SECONDS);
|
|
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
.subscribe(System.out::println);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|