diff --git a/app/src/main/java/org/yameida/worktool/MyApplication.kt b/app/src/main/java/org/yameida/worktool/MyApplication.kt index 387b898..9b72a57 100644 --- a/app/src/main/java/org/yameida/worktool/MyApplication.kt +++ b/app/src/main/java/org/yameida/worktool/MyApplication.kt @@ -2,10 +2,12 @@ package org.yameida.worktool import android.app.Application import android.content.Intent +import com.blankj.utilcode.util.GsonUtils import com.blankj.utilcode.util.LogUtils import com.blankj.utilcode.util.SPUtils import com.blankj.utilcode.util.Utils import com.efs.sdk.base.core.util.PackageUtil +import com.google.gson.Gson import com.hjq.toast.ToastUtils import com.tendcloud.tenddata.TalkingDataSDK import com.umeng.commonsdk.UMConfigure @@ -33,6 +35,7 @@ class MyApplication : Application() { super.onCreate() //初始化工具类 Utils.init(this) + GsonUtils.setGsonDelegate(Gson()) //初始化 Toast 框架 ToastUtils.init(this) //初始化友盟统计 diff --git a/app/src/main/java/org/yameida/worktool/model/ExecCallbackBean.kt b/app/src/main/java/org/yameida/worktool/model/ExecCallbackBean.kt index 0227633..2daaf09 100644 --- a/app/src/main/java/org/yameida/worktool/model/ExecCallbackBean.kt +++ b/app/src/main/java/org/yameida/worktool/model/ExecCallbackBean.kt @@ -1,18 +1,75 @@ package org.yameida.worktool.model -import java.util.* - +/** + * 任务执行成功失败结果回传 + */ data class ExecCallbackBean( //原生消息指令 var rawMsg: String? = null, - //1成功 0失败 - var rawSuccess: Int = 0, + //0成功 其他是失败错误码 + var errorCode: Int = 0, //失败原因 var errorReason: String = "", //执行时间 - var runTime: Date = Date() + var runTime: Long = 0, -) : WeworkMessageBean() \ No newline at end of file + //执行耗时 + var timeCost: Double = 0.0 + +) : WeworkMessageBean() { + companion object { + /** + * 错误码 + * 通用格式错误 10xxx + * 执行任务错误 20xxx + * 服务端返回错误 50xxx + */ + const val SUCCESS = 0 + //数据格式错误 + const val ERROR_ILLEGAL_DATA = 101011 + //非法操作 + const val ERROR_ILLEGAL_OPERATION = 101012 + + //创建群失败 + const val ERROR_CREATE_GROUP = 201011 + //群改名失败 + const val ERROR_GROUP_RENAME = 201012 + //群拉人失败 + const val ERROR_GROUP_ADD_MEMBER = 201013 + //群踢人失败 + const val ERROR_GROUP_REMOVE_MEMBER = 201014 + //改群公告失败 + const val ERROR_GROUP_CHANGE_ANNOUNCEMENT = 201015 + //改群备注失败 + const val ERROR_GROUP_CHANGE_REMARK = 201016 + //查找聊天窗失败 + const val ERROR_INTO_ROOM = 201101 + //发送消息失败 + const val ERROR_SEND_MESSAGE = 201102 + //按钮寻找失败 + const val ERROR_BUTTON = 201103 + //目标寻找失败 + const val ERROR_TARGET = 201104 + //转发失败 + const val ERROR_RELAY = 201105 + //重复添加 + const val ERROR_REPEAT = 201106 + //文件下载异常 + const val ERROR_FILE_DOWNLOAD = 201107 + //文件存储异常 + const val ERROR_FILE_STORAGE = 201108 + + //机器人id错误 + const val ERROR_INVALID_ROBOT_ID = 501011 + //机器人key错误 + const val ERROR_INVALID_ROBOT_KEY = 501012 + //机器人重复登录 + const val ERROR_ROBOT_MULTI_LOGIN = 501013 + //机器人其他未知错误 + const val ERROR_OTHER = 501000 + + } +} \ No newline at end of file diff --git a/app/src/main/java/org/yameida/worktool/model/WeworkMessageBean.java b/app/src/main/java/org/yameida/worktool/model/WeworkMessageBean.java index cf1c27a..80ffbba 100644 --- a/app/src/main/java/org/yameida/worktool/model/WeworkMessageBean.java +++ b/app/src/main/java/org/yameida/worktool/model/WeworkMessageBean.java @@ -14,6 +14,7 @@ public class WeworkMessageBean { *

* 消息类型 100 * 上传消息列表 TYPE_RECEIVE_MESSAGE_LIST + * 交互通知 TYPE_CONSOLE_TOAST *

* 全局操作类型 200 * 停止所有任务并返回首页待命 STOP_AND_GO_HOME @@ -21,7 +22,7 @@ public class WeworkMessageBean { * 在房间内发送消息 SEND_MESSAGE * 在房间内指定回复消息 REPLY_MESSAGE * 在房间内转发消息 RELAY_MESSAGE - * 创建群 CREATE_GROUP + * 创建群 INIT_GROUP * 进入群聊并修改群配置 INTO_GROUP_AND_CONFIG * 推送微盘图片 PUSH_MICRO_DISK_IMAGE * 推送微盘文件 PUSH_MICRO_DISK_FILE @@ -44,6 +45,7 @@ public class WeworkMessageBean { */ public static final int HEART_BEAT = 11; public static final int TYPE_RECEIVE_MESSAGE_LIST = 101; + public static final int TYPE_CONSOLE_TOAST = 102; public static final int STOP_AND_GO_HOME = 201; public static final int LOOP_RECEIVE_NEW_MESSAGE = 202; diff --git a/app/src/main/java/org/yameida/worktool/service/GlobalMethod.kt b/app/src/main/java/org/yameida/worktool/service/GlobalMethod.kt index fb80dfa..afa8c7c 100644 --- a/app/src/main/java/org/yameida/worktool/service/GlobalMethod.kt +++ b/app/src/main/java/org/yameida/worktool/service/GlobalMethod.kt @@ -147,10 +147,10 @@ fun backPress() { /** * 上传执行指令结果 */ -fun uploadCommandResult(message: WeworkMessageBean, success: Boolean, reason: String) { +fun uploadCommandResult(message: WeworkMessageBean, errorCode: Int, errorReason: String, startTime: Long) { WeworkController.weworkService.webSocketManager.send( WeworkMessageListBean( - ExecCallbackBean(GsonUtils.toJson(message), if (success) 1 else 0, reason), + ExecCallbackBean(GsonUtils.toJson(message), errorCode, errorReason, startTime, (System.currentTimeMillis() - startTime) / 1000.0), WeworkMessageListBean.SOCKET_TYPE_RAW_CONFIRM, messageId = message.messageId ), true diff --git a/app/src/main/java/org/yameida/worktool/service/MyLooper.kt b/app/src/main/java/org/yameida/worktool/service/MyLooper.kt index f49ec2a..172861e 100644 --- a/app/src/main/java/org/yameida/worktool/service/MyLooper.kt +++ b/app/src/main/java/org/yameida/worktool/service/MyLooper.kt @@ -9,6 +9,7 @@ import com.blankj.utilcode.util.LogUtils import com.google.gson.reflect.TypeToken import okhttp3.WebSocket import org.yameida.worktool.Constant +import org.yameida.worktool.model.ExecCallbackBean import org.yameida.worktool.model.WeworkMessageBean import org.yameida.worktool.model.WeworkMessageListBean import java.nio.charset.StandardCharsets @@ -52,7 +53,7 @@ object MyLooper { fun onMessage(webSocket: WebSocket?, text: String) { val messageList: WeworkMessageListBean = - GsonUtils.fromJson>(text, object : TypeToken>(){}.type) + GsonUtils.fromJson>(text, object : TypeToken>(){}.type) if (messageList.socketType == WeworkMessageListBean.SOCKET_TYPE_HEARTBEAT) { return } @@ -97,6 +98,9 @@ object MyLooper { private fun dealWithMessage(message: WeworkMessageBean) { when (message.type) { + WeworkMessageBean.TYPE_CONSOLE_TOAST -> { + WeworkController.consoleToast(message as ExecCallbackBean) + } WeworkMessageBean.STOP_AND_GO_HOME -> { WeworkController.stopAndGoHome() } @@ -148,7 +152,7 @@ object MyLooper { WeworkController.getFriendInfo(message) } WeworkMessageBean.GET_MY_INFO -> { - WeworkController.getMyInfo() + WeworkController.getMyInfo(message) } WeworkMessageBean.ROBOT_CONTROLLER_TEST -> { WeworkController.test(message) diff --git a/app/src/main/java/org/yameida/worktool/service/WeworkController.kt b/app/src/main/java/org/yameida/worktool/service/WeworkController.kt index fabf9cc..2fa495a 100644 --- a/app/src/main/java/org/yameida/worktool/service/WeworkController.kt +++ b/app/src/main/java/org/yameida/worktool/service/WeworkController.kt @@ -3,6 +3,7 @@ package org.yameida.worktool.service import com.blankj.utilcode.util.* import org.yameida.worktool.Demo import org.yameida.worktool.annotation.RequestMapping +import org.yameida.worktool.model.ExecCallbackBean import org.yameida.worktool.model.WeworkMessageBean /** @@ -15,6 +16,18 @@ object WeworkController { var enableLoopRunning = false var mainLoopRunning = false + /** + * 交互通知 + * @see WeworkMessageBean.TYPE_CONSOLE_TOAST + * @param message#errorCode 失败错误码 + * @param message#errorReason 失败原因 + */ + @RequestMapping + fun consoleToast(message: ExecCallbackBean): Boolean { + LogUtils.d("consoleToast(): ${message.errorCode} ${message.errorReason}") + return WeworkInteractionImpl.consoleToast(message, message.errorCode, message.errorReason) + } + /** * 停止所有任务并返回首页待命 * @see WeworkMessageBean.STOP_AND_GO_HOME @@ -48,7 +61,7 @@ object WeworkController { @RequestMapping fun sendMessage(message: WeworkMessageBean): Boolean { LogUtils.d("sendMessage(): ${message.titleList} ${message.receivedContent} ${message.at} ${message.atList?.joinToString()}") - return WeworkOperationImpl.sendMessage(message.titleList, message.receivedContent, message.at, message.atList) + return WeworkOperationImpl.sendMessage(message, message.titleList, message.receivedContent, message.at, message.atList) } /** @@ -65,6 +78,7 @@ object WeworkController { fun replyMessage(message: WeworkMessageBean): Boolean { LogUtils.d("replyMessage(): ${message.receivedName} ${message.originalContent} ${message.receivedContent}") return WeworkOperationImpl.replyMessage( + message, message.titleList, message.receivedName, message.originalContent, @@ -88,6 +102,7 @@ object WeworkController { fun relayMessage(message: WeworkMessageBean): Boolean { LogUtils.d("relayMessage(): ${message.titleList} ${message.receivedName} ${message.originalContent} ${message.textType} ${message.nameList} ${message.extraText}") return WeworkOperationImpl.relayMessage( + message, message.titleList, message.receivedName, message.originalContent, @@ -109,6 +124,7 @@ object WeworkController { fun initGroup(message: WeworkMessageBean): Boolean { LogUtils.d("initGroup(): ${message.groupName} ${message.selectList} ${message.groupAnnouncement} ${message.groupRemark}") return WeworkOperationImpl.initGroup( + message, message.groupName, message.selectList, message.groupAnnouncement, @@ -142,6 +158,7 @@ object WeworkController { fun intoGroupAndConfig(message: WeworkMessageBean): Boolean { LogUtils.d("intoGroupAndConfig(): ${message.groupName} ${message.newGroupName} ${message.newGroupAnnouncement} ${message.selectList} ${message.showMessageHistory} ${message.removeList} ${message.groupRemark}") return WeworkOperationImpl.intoGroupAndConfig( + message, message.groupName, message.newGroupName, message.newGroupAnnouncement, @@ -163,6 +180,7 @@ object WeworkController { fun pushMicroDiskImage(message: WeworkMessageBean): Boolean { LogUtils.d("pushMicroDiskImage(): ${message.titleList} ${message.objectName} ${message.extraText}") return WeworkOperationImpl.pushMicroDiskImage( + message, message.titleList, message.objectName, message.extraText @@ -180,6 +198,7 @@ object WeworkController { fun pushMicroDiskFile(message: WeworkMessageBean): Boolean { LogUtils.d("pushMicroDiskFile(): ${message.titleList} ${message.objectName} ${message.extraText}") return WeworkOperationImpl.pushMicroDiskFile( + message, message.titleList, message.objectName, message.extraText @@ -197,6 +216,7 @@ object WeworkController { fun pushMicroprogram(message: WeworkMessageBean): Boolean { LogUtils.d("pushMicroprogram(): ${message.titleList} ${message.objectName} ${message.extraText}") return WeworkOperationImpl.pushMicroprogram( + message, message.titleList, message.objectName, message.extraText @@ -215,6 +235,7 @@ object WeworkController { fun pushOffice(message: WeworkMessageBean): Boolean { LogUtils.d("pushOffice(): ${message.titleList} ${message.objectName} ${message.extraText}") return WeworkOperationImpl.pushOffice( + message, message.titleList, message.objectName, message.extraText @@ -234,6 +255,7 @@ object WeworkController { fun pushFile(message: WeworkMessageBean): Boolean { LogUtils.d("pushFile(): ${message.titleList} ${message.objectName} ${message.fileUrl} ${message.fileType} ${message.extraText}") return WeworkOperationImpl.pushFile( + message, message.titleList, message.objectName, message.fileUrl, @@ -250,7 +272,7 @@ object WeworkController { @RequestMapping fun addFriendByPhone(message: WeworkMessageBean): Boolean { LogUtils.d("addFriendByPhone(): ${message.friend}") - return WeworkOperationImpl.addFriendByPhone(message.friend) + return WeworkOperationImpl.addFriendByPhone(message, message.friend) } /** @@ -265,6 +287,7 @@ object WeworkController { fun showGroupInfo(message: WeworkMessageBean): Boolean { LogUtils.d("showGroupInfo(): ${message.titleList} ${message.receivedName} ${message.originalContent} ${message.textType}") return WeworkOperationImpl.showGroupInfo( + message, message.titleList, message.receivedName, message.originalContent, @@ -280,7 +303,7 @@ object WeworkController { @RequestMapping fun getGroupInfo(message: WeworkMessageBean): Boolean { LogUtils.d("getGroupInfo(): ${message.selectList}") - return WeworkGetImpl.getGroupInfo(message.selectList) + return WeworkGetImpl.getGroupInfo(message, message.selectList) } /** @@ -292,7 +315,7 @@ object WeworkController { @RequestMapping fun getFriendInfo(message: WeworkMessageBean): Boolean { LogUtils.d("getFriendInfo(): ${message.selectList}") - return WeworkGetImpl.getFriendInfo(message.selectList) + return WeworkGetImpl.getFriendInfo(message, message.selectList) } /** @@ -300,9 +323,9 @@ object WeworkController { * @see WeworkMessageBean.GET_MY_INFO */ @RequestMapping - fun getMyInfo(): Boolean { + fun getMyInfo(message: WeworkMessageBean): Boolean { LogUtils.d("getMyInfo():") - return WeworkGetImpl.getMyInfo() + return WeworkGetImpl.getMyInfo(message) } } \ No newline at end of file diff --git a/app/src/main/java/org/yameida/worktool/service/WeworkGetImpl.kt b/app/src/main/java/org/yameida/worktool/service/WeworkGetImpl.kt index c30ac01..e333863 100644 --- a/app/src/main/java/org/yameida/worktool/service/WeworkGetImpl.kt +++ b/app/src/main/java/org/yameida/worktool/service/WeworkGetImpl.kt @@ -18,7 +18,7 @@ object WeworkGetImpl { * 获取群信息 * @param selectList 群名列表 为空时去群管理页查询并返回群聊页 */ - fun getGroupInfo(selectList: List): Boolean { + fun getGroupInfo(message: WeworkMessageBean, selectList: List): Boolean { if (selectList.isNullOrEmpty()) { WeworkRoomUtil.intoGroupManager() val groupInfo = getGroupInfoDetail() @@ -39,14 +39,14 @@ object WeworkGetImpl { * 获取好友信息 * @param selectList 好友名列表 */ - fun getFriendInfo(selectList: List): Boolean { + fun getFriendInfo(message: WeworkMessageBean, selectList: List): Boolean { return true } /** * 获取我的信息 */ - fun getMyInfo(): Boolean { + fun getMyInfo(message: WeworkMessageBean): Boolean { if (!goHomeTab("我")) { LogUtils.d("未找到我的信息") log("未找到我的信息") diff --git a/app/src/main/java/org/yameida/worktool/service/WeworkInteractionImpl.kt b/app/src/main/java/org/yameida/worktool/service/WeworkInteractionImpl.kt new file mode 100644 index 0000000..6a4dec7 --- /dev/null +++ b/app/src/main/java/org/yameida/worktool/service/WeworkInteractionImpl.kt @@ -0,0 +1,28 @@ +package org.yameida.worktool.service + +import com.blankj.utilcode.util.* +import com.hjq.toast.ToastUtils +import org.yameida.worktool.model.ExecCallbackBean + + +/** + * 消息类型 100 + */ +object WeworkInteractionImpl { + + /** + * 交互通知 + * @param errorCode 失败错误码 + * @param errorReason 失败原因 + */ + fun consoleToast( + message: ExecCallbackBean, + errorCode: Int, + errorReason: String + ): Boolean { + LogUtils.e("错误提示 错误码: $errorCode 错误信息: $errorReason") + ToastUtils.show("错误提示 错误码: $errorCode 错误信息: $errorReason") + return true + } + +} \ No newline at end of file diff --git a/app/src/main/java/org/yameida/worktool/service/WeworkOperationImpl.kt b/app/src/main/java/org/yameida/worktool/service/WeworkOperationImpl.kt index 2c12cbf..b42282c 100644 --- a/app/src/main/java/org/yameida/worktool/service/WeworkOperationImpl.kt +++ b/app/src/main/java/org/yameida/worktool/service/WeworkOperationImpl.kt @@ -6,6 +6,7 @@ import org.yameida.worktool.model.WeworkMessageBean import com.github.yoojia.qrcode.qrcode.QRCodeDecoder import com.blankj.utilcode.util.* import com.lzy.okgo.OkGo +import org.yameida.worktool.model.ExecCallbackBean import org.yameida.worktool.utils.* import java.io.File import java.lang.Exception @@ -26,28 +27,41 @@ object WeworkOperationImpl { * @see WeworkMessageBean.TEXT_TYPE */ fun sendMessage( + message: WeworkMessageBean, titleList: List, receivedContent: String?, at: String? = null, atList: List? = null ): Boolean { + val startTime = System.currentTimeMillis() if (receivedContent.isNullOrEmpty()) { LogUtils.d("未发现发送内容") + uploadCommandResult(message, ExecCallbackBean.ERROR_ILLEGAL_DATA, "发送内容为空", startTime) return false } + val successList = arrayListOf() + val failList = arrayListOf() for (title in titleList) { if (WeworkRoomUtil.intoRoom(title)) { if (sendChatMessage(receivedContent, at = at, atList = atList)) { + successList.add(title) LogUtils.d("$title: 发送成功") } else { LogUtils.d("$title: 发送失败") + failList.add(title) error("$title: 发送失败 $receivedContent") } } else { + failList.add(title) LogUtils.d("$title: 发送失败 进入房间失败") error("$title: 发送失败 进入房间失败 $receivedContent") } } + if (failList.isNotEmpty()) { + uploadCommandResult(message, ExecCallbackBean.ERROR_SEND_MESSAGE, "发送成功: ${successList.joinToString()} 发送失败: ${failList.joinToString()}", startTime) + return false + } + uploadCommandResult(message, ExecCallbackBean.SUCCESS, "", startTime) return true } @@ -61,12 +75,14 @@ object WeworkOperationImpl { * @see WeworkMessageBean.TEXT_TYPE */ fun replyMessage( + message: WeworkMessageBean, titleList: List, receivedName: String?, originalContent: String, textType: Int, receivedContent: String? ): Boolean { + val startTime = System.currentTimeMillis() if (receivedContent.isNullOrEmpty()) { LogUtils.d("未发现回复内容") return false @@ -83,33 +99,40 @@ object WeworkOperationImpl { ) ) { LogUtils.v("开始回复") - sendChatMessage(receivedContent, reply = true) - LogUtils.d("$title: 回复成功") - WeworkLoopImpl.getChatMessageList() - return true + if (sendChatMessage(receivedContent, reply = true)) { + LogUtils.d("$title: 回复成功") + WeworkLoopImpl.getChatMessageList() + uploadCommandResult(message, ExecCallbackBean.SUCCESS, "", startTime) + return true + } else { + LogUtils.d("$title: 回复发送失败") + uploadCommandResult(message, ExecCallbackBean.ERROR_SEND_MESSAGE, "回复发送失败", startTime) + return false + } } else { LogUtils.d("$title: 回复失败 直接发送答案") error("$title: 回复失败 直接发送答案 $receivedContent") - if (originalContent.isNotEmpty()) { - if (receivedName == null) { - sendChatMessage("【$originalContent】\n$receivedContent") - } else { - sendChatMessage("【$originalContent】\n$receivedContent", receivedName) - } + val text = if (originalContent.isNotEmpty()) "【$originalContent】\n$receivedContent" else receivedContent + if (sendChatMessage(text, receivedName)) { + LogUtils.d("$title: 直接发送答案成功") + WeworkLoopImpl.getChatMessageList() + uploadCommandResult(message, ExecCallbackBean.SUCCESS, "", startTime) + return true } else { - if (receivedName == null) { - sendChatMessage(receivedContent) - } else { - sendChatMessage(receivedContent, receivedName) - } + LogUtils.d("$title: 直接发送答案失败") + uploadCommandResult(message, ExecCallbackBean.ERROR_SEND_MESSAGE, "直接发送答案失败", startTime) + return false } - WeworkLoopImpl.getChatMessageList() } } else { - LogUtils.d("$title: 回复失败") error("$title: 回复失败 $receivedContent") + LogUtils.d("进入房间失败 $title") + uploadCommandResult(message, ExecCallbackBean.ERROR_INTO_ROOM, "进入房间失败 $title", startTime) + return false } } + LogUtils.d("房间名为空") + uploadCommandResult(message, ExecCallbackBean.ERROR_ILLEGAL_DATA, "房间名为空", startTime) return false } @@ -124,6 +147,7 @@ object WeworkOperationImpl { * @see WeworkMessageBean.TEXT_TYPE */ fun relayMessage( + message: WeworkMessageBean, titleList: List, receivedName: String, originalContent: String, @@ -131,6 +155,7 @@ object WeworkOperationImpl { nameList: List, extraText: String? = null ): Boolean { + val startTime = System.currentTimeMillis() for (title in titleList) { if (WeworkRoomUtil.intoRoom(title)) { if (WeworkTextUtil.longClickMessageItem( @@ -182,24 +207,37 @@ object WeworkOperationImpl { * @param groupRemark 修改群备注 选填 */ fun initGroup( + message: WeworkMessageBean, groupName: String, selectList: List?, groupAnnouncement: String?, groupRemark: String? ): Boolean { + val startTime = System.currentTimeMillis() if (!WeworkRoomUtil.isGroupExists(groupName)) { - if (!createGroup() || !groupRename(groupName) || !groupAddMember(selectList) - || !groupChangeAnnouncement(groupAnnouncement) - ) return false - } else { - if (!groupRename(groupName) || !groupAddMember(selectList) - || !groupChangeAnnouncement(groupAnnouncement) - ) return false + if (!createGroup()) { + uploadCommandResult(message, ExecCallbackBean.ERROR_CREATE_GROUP, "创建群失败", startTime) + return false + } } - if (groupRemark != null) { - groupChangeRemark(groupRemark) + if (!groupRename(groupName)) { + uploadCommandResult(message, ExecCallbackBean.ERROR_GROUP_RENAME, "创建群成功 群改名失败", startTime) + return false } -// getGroupQrcode(groupName, groupRemark) + if (!groupAddMember(selectList)) { + uploadCommandResult(message, ExecCallbackBean.ERROR_GROUP_ADD_MEMBER, "创建群成功 群改名成功 群拉人失败 ${selectList?.joinToString()}", startTime) + return false + } + if (!groupChangeAnnouncement(groupAnnouncement)) { + uploadCommandResult(message, ExecCallbackBean.ERROR_GROUP_CHANGE_ANNOUNCEMENT, "创建群成功 群改名成功 群拉人成功 群公告失败", startTime) + return false + } + if (!groupChangeRemark(groupRemark)) { + uploadCommandResult(message, ExecCallbackBean.ERROR_GROUP_CHANGE_REMARK, "创建群成功 群改名成功 群拉人成功 群公告成功 群备注失败", startTime) + return false + } + getGroupQrcode(groupName, groupRemark) + uploadCommandResult(message, ExecCallbackBean.SUCCESS, "", startTime) return true } @@ -215,6 +253,7 @@ object WeworkOperationImpl { * @param removeList 移除群成员名称列表/踢人 选填 */ fun intoGroupAndConfig( + message: WeworkMessageBean, groupName: String, newGroupName: String?, newGroupAnnouncement: String?, @@ -223,39 +262,49 @@ object WeworkOperationImpl { showMessageHistory: Boolean = false, removeList: List? ): Boolean { - if (WeworkRoomUtil.intoRoom(groupName)) { - if (newGroupName != null) { - groupRename(newGroupName) - } - if (!selectList.isNullOrEmpty()) { - groupAddMember(selectList, showMessageHistory) - } - if (!removeList.isNullOrEmpty()) { - groupRemoveMember(removeList) - } - if (newGroupAnnouncement != null) { - groupChangeAnnouncement(newGroupAnnouncement) - } - if (groupRemark != null) { - groupChangeRemark(groupRemark) - } - backPress() - return true + val startTime = System.currentTimeMillis() + if (!WeworkRoomUtil.intoRoom(groupName)) { + uploadCommandResult(message, ExecCallbackBean.ERROR_INTO_ROOM, "进入房间失败 $groupName", startTime) + return false } - return false + if (!groupRename(newGroupName)) { + uploadCommandResult(message, ExecCallbackBean.ERROR_GROUP_RENAME, "进入房间成功 群改名失败", startTime) + return false + } + if (!groupAddMember(selectList, showMessageHistory)) { + uploadCommandResult(message, ExecCallbackBean.ERROR_GROUP_ADD_MEMBER, "进入房间成功 群改名成功 群拉人失败 ${selectList?.joinToString()}", startTime) + return false + } + if (!groupRemoveMember(removeList)) { + uploadCommandResult(message, ExecCallbackBean.ERROR_GROUP_REMOVE_MEMBER, "进入房间成功 群改名成功 群拉人成功 群踢人失败 ${removeList?.joinToString()}", startTime) + return false + } + if (!groupChangeAnnouncement(newGroupAnnouncement)) { + uploadCommandResult(message, ExecCallbackBean.ERROR_GROUP_CHANGE_ANNOUNCEMENT, "进入房间成功 群改名成功 群拉人成功 群公告失败", startTime) + return false + } + if (!groupChangeRemark(groupRemark)) { + uploadCommandResult(message, ExecCallbackBean.ERROR_GROUP_CHANGE_REMARK, "进入房间成功 群改名成功 群拉人成功 群公告成功 群备注失败", startTime) + return false + } + uploadCommandResult(message, ExecCallbackBean.SUCCESS, "", startTime) + return true } /** - * 推送微盘图片 + * 推送微盘图片(不推荐) + * @see pushFile * @param titleList 待发送姓名列表 * @param objectName 图片名称 * @param extraText 附加留言 可选 */ fun pushMicroDiskImage( + message: WeworkMessageBean, titleList: List, objectName: String, extraText: String? = null ): Boolean { + val startTime = System.currentTimeMillis() goHomeTab("工作台") val node = AccessibilityUtil.scrollAndFindByText(WeworkController.weworkService, getRoot(), "微盘") if (node != null) { @@ -292,16 +341,19 @@ object WeworkOperationImpl { } /** - * 推送微盘文件 + * 推送微盘文件(不推荐) + * @see pushFile * @param titleList 待发送姓名列表 * @param objectName 文件名称 * @param extraText 附加留言 可选 */ fun pushMicroDiskFile( + message: WeworkMessageBean, titleList: List, objectName: String, extraText: String? = null ): Boolean { + val startTime = System.currentTimeMillis() goHomeTab("工作台") val node = AccessibilityUtil.scrollAndFindByText(WeworkController.weworkService, getRoot(), "微盘") if (node != null) { @@ -334,16 +386,18 @@ object WeworkOperationImpl { } /** - * 推送任意小程序 + * 推送任意小程序(不推荐) * @param titleList 待发送姓名列表 * @param objectName 小程序名称 * @param extraText 附加留言 可选 */ fun pushMicroprogram( + message: WeworkMessageBean, titleList: List, objectName: String, extraText: String? = null ): Boolean { + val startTime = System.currentTimeMillis() goHomeTab("工作台") val node = AccessibilityUtil.scrollAndFindByText(WeworkController.weworkService, getRoot(), "用过的小程序") if (node != null) { @@ -373,20 +427,24 @@ object WeworkOperationImpl { * @param extraText 附加留言 可选 */ fun pushOffice( + message: WeworkMessageBean, titleList: List, objectName: String, extraText: String? = null ): Boolean { + val startTime = System.currentTimeMillis() goHomeTab("文档") val allButton = AccessibilityUtil.findOneByText(getRoot(), "全部") if (allButton == null) { LogUtils.e("未找到全部按钮") + uploadCommandResult(message, ExecCallbackBean.ERROR_BUTTON, "未找到全部按钮", startTime) return false } AccessibilityUtil.performClick(allButton) val myFileButton = AccessibilityUtil.findOneByText(getRoot(), "共享空间") if (myFileButton == null) { LogUtils.e("未找到共享空间按钮") + uploadCommandResult(message, ExecCallbackBean.ERROR_BUTTON, "未找到共享空间按钮", startTime) return false } AccessibilityUtil.performClick(myFileButton) @@ -400,17 +458,23 @@ object WeworkOperationImpl { val shareFileButton = AccessibilityUtil.findOneByDesc(getRoot(), "转发") AccessibilityUtil.performClick(shareFileButton) if (relaySelectTarget(titleList, extraText)) { + uploadCommandResult(message, ExecCallbackBean.SUCCESS, "", startTime) return true } else { LogUtils.e("微盘文件转发失败: $objectName") + uploadCommandResult(message, ExecCallbackBean.ERROR_RELAY, "微盘文件转发失败: $objectName", startTime) + return false } } else { LogUtils.e("文档未搜索到相关文件: $objectName") + uploadCommandResult(message, ExecCallbackBean.ERROR_TARGET, "文档未搜索到相关文件: $objectName", startTime) + return false } } else { LogUtils.e("未找到文档搜索按钮") + uploadCommandResult(message, ExecCallbackBean.ERROR_BUTTON, "未找到文档搜索按钮", startTime) + return false } - return false } /** @@ -423,15 +487,17 @@ object WeworkOperationImpl { * @param extraText 附加留言 可选 */ fun pushFile( + message: WeworkMessageBean, titleList: List, objectName: String, fileUrl: String, fileType: String, extraText: String? = null ): Boolean { + val startTime = System.currentTimeMillis() LogUtils.i("下载开始 $fileUrl") val execute = OkGo.get(fileUrl).execute() - LogUtils.i("下载成功 $fileUrl") + LogUtils.i("下载完成 $fileUrl") val body = execute.body() if (body != null) { val df = SimpleDateFormat("yyyy-MM-dd") @@ -447,16 +513,23 @@ object WeworkOperationImpl { if (relaySelectTarget(titleList, extraText)) { val stayButton = AccessibilityUtil.findOneByText(getRoot(), "留在企业微信") AccessibilityUtil.performClick(stayButton) + uploadCommandResult(message, ExecCallbackBean.SUCCESS, "", startTime) return true } else { LogUtils.e("文件转发失败: $objectName") + uploadCommandResult(message, ExecCallbackBean.ERROR_RELAY, "文件转发失败: $objectName", startTime) + return false } } else { LogUtils.e("文件存储本地失败 $filePath") - error("文件存储本地失败 $filePath") + uploadCommandResult(message, ExecCallbackBean.ERROR_FILE_STORAGE, "文件存储本地失败 $filePath", startTime) + return false } + } else { + LogUtils.e("文件下载失败") + uploadCommandResult(message, ExecCallbackBean.ERROR_FILE_DOWNLOAD, "文件下载失败 $fileUrl", startTime) + return false } - return false } /** @@ -465,8 +538,10 @@ object WeworkOperationImpl { * @param friend 待添加用户列表 */ fun addFriendByPhone( + message: WeworkMessageBean, friend: WeworkMessageBean.Friend ): Boolean { + val startTime = System.currentTimeMillis() goHome() val list = AccessibilityUtil.findOneByClazz(getRoot(), Views.RecyclerView, Views.ListView, Views.ViewGroup) if (list != null) { @@ -552,33 +627,54 @@ object WeworkOperationImpl { } if (AccessibilityUtil.findTextAndClick(getRoot(), "发送添加邀请", "发送申请")) { LogUtils.d("发送添加邀请成功: " + friend.phone) + uploadCommandResult(message, ExecCallbackBean.SUCCESS, "", startTime) + return true + } else { + LogUtils.e("未找到发送邀请按钮") + uploadCommandResult(message, ExecCallbackBean.ERROR_BUTTON, "未找到发送邀请按钮", startTime) + return false } } else { if (AccessibilityUtil.findOnceByText(getRoot(), "发消息", exact = true) != null) { - LogUtils.e("已经添加联系人,请勿重复添加") + LogUtils.e("已经添加联系人,请勿重复添加" + friend.phone) + uploadCommandResult(message, ExecCallbackBean.ERROR_REPEAT, "已经添加联系人,请勿重复添加" + friend.phone, startTime) + return false } else { LogUtils.e("未找到添加为联系人") + uploadCommandResult(message, ExecCallbackBean.ERROR_BUTTON, "未找到添加为联系人", startTime) + return false } } } else { LogUtils.e("未找到标签") + uploadCommandResult(message, ExecCallbackBean.ERROR_BUTTON, "未找到标签", startTime) + return false } } else { LogUtils.e("未找到查找手机选项") + uploadCommandResult(message, ExecCallbackBean.ERROR_BUTTON, "未找到查找手机选项", startTime) + return false } } else { LogUtils.e("未找到添加客户按钮") + uploadCommandResult(message, ExecCallbackBean.ERROR_BUTTON, "未找到添加客户按钮", startTime) + return false } } else { LogUtils.e("未找到添加客户列表") + uploadCommandResult(message, ExecCallbackBean.ERROR_BUTTON, "未找到添加客户列表", startTime) + return false } - return true } else { LogUtils.e("未找到搜索按钮") + uploadCommandResult(message, ExecCallbackBean.ERROR_BUTTON, "未找到搜索按钮", startTime) + return false } + } else { + LogUtils.e("未找到聊天列表") + uploadCommandResult(message, ExecCallbackBean.ERROR_BUTTON, "未找到聊天列表", startTime) + return false } - LogUtils.e("未找到聊天列表") - return false } /** @@ -590,11 +686,13 @@ object WeworkOperationImpl { * @param textType 原始消息的消息类型 */ fun showGroupInfo( + message: WeworkMessageBean, titleList: MutableList, receivedName: String, originalContent: String, textType: Int ): Boolean { + val startTime = System.currentTimeMillis() for (groupName in titleList) { if (WeworkRoomUtil.intoRoom(groupName) && WeworkRoomUtil.intoGroupManager()) { val groupInfo = WeworkGetImpl.getGroupInfoDetail() @@ -604,9 +702,12 @@ object WeworkOperationImpl { groupInfo.originalContent = originalContent groupInfo.textType = textType WeworkController.weworkService.webSocketManager.send(groupInfo) + uploadCommandResult(message, ExecCallbackBean.SUCCESS, "", startTime) + return true } } - return true + uploadCommandResult(message, ExecCallbackBean.ERROR_TARGET, "", startTime) + return false } /** @@ -632,7 +733,7 @@ object WeworkOperationImpl { AccessibilityUtil.findTextInput(getRoot(), trimTitle) sleep(Constant.CHANGE_PAGE_INTERVAL) val selectListView = AccessibilityUtil.findOneByClazz(getRoot(), Views.ListView, Views.RecyclerView, Views.ViewGroup, minChildCount = 2) - val regex = "^$trimTitle(-.*)?(…)?(\\(.*?\\))?" + if (needTrim) "" else "$" + val regex = "^$trimTitle" + if (needTrim) ".*?" else "(-.*)?(…)?(\\(.*?\\))?$" val matchSelect = AccessibilityUtil.findOneByTextRegex( selectListView, regex, @@ -709,7 +810,8 @@ object WeworkOperationImpl { /** * 修改群名称 */ - private fun groupRename(groupName: String): Boolean { + private fun groupRename(groupName: String?): Boolean { + if (groupName == null) return true if (WeworkRoomUtil.intoGroupManager()) { val textView = AccessibilityUtil.findOneByText(getRoot(), "全部群成员", "微信用户创建") ?: return false @@ -747,7 +849,7 @@ object WeworkOperationImpl { * 默认不附带历史记录 */ private fun groupAddMember( - selectList: List? = null, + selectList: List?, showMessageHistory: Boolean = false ): Boolean { if (selectList.isNullOrEmpty()) return true @@ -759,10 +861,8 @@ object WeworkOperationImpl { LogUtils.v("tvEmptySize: $tvEmptySize") if (tvEmptySize == 0) { return true - } else if (tvEmptySize == 1) { - AccessibilityUtil.performClick(gridView.getChild(gridView.childCount - 1)) - } else if (tvEmptySize == 2) { - AccessibilityUtil.performClick(gridView.getChild(gridView.childCount - 2)) + } else if (tvEmptySize == 1 || tvEmptySize == 2) { + AccessibilityUtil.performClick(gridView.getChild(gridView.childCount - tvEmptySize)) } } else { LogUtils.e("未找到添加成员按钮") @@ -782,7 +882,7 @@ object WeworkOperationImpl { AccessibilityUtil.findTextInput(getRoot(), trimTitle) sleep(Constant.POP_WINDOW_INTERVAL) val selectListView = AccessibilityUtil.findOneByClazz(getRoot(), Views.ListView, Views.RecyclerView, Views.ViewGroup, minChildCount = 2, firstChildClazz = Views.TextView) - val regex = "^$trimTitle(-.*)?(…)?(\\(.*?\\))?" + if (needTrim) "" else "$" + val regex = "^$trimTitle" + if (needTrim) ".*?" else "(-.*)?(…)?(\\(.*?\\))?$" val matchSelect = AccessibilityUtil.findOneByTextRegex( selectListView, regex, @@ -790,6 +890,7 @@ object WeworkOperationImpl { root = false ) if (selectListView != null && matchSelect != null) { + var flag = false for (i in 0 until selectListView.childCount) { val item = selectListView.getChild(i) val searchResult = AccessibilityUtil.findOnceByTextRegex(item, regex) @@ -797,9 +898,14 @@ object WeworkOperationImpl { item.refresh() val imageView = AccessibilityUtil.findOneByClazz(item, Views.ImageView, root = false) - AccessibilityUtil.performClick(imageView) + if (AccessibilityUtil.performClick(imageView)) { + flag = true + } } } + if (!flag) { + LogUtils.e("拉人失败 找不到: $select") + } } val textView = AccessibilityUtil.findOnceByClazz(getRoot(), Views.TextView) if (textView != null && textView.text.isNullOrBlank()) { @@ -810,6 +916,7 @@ object WeworkOperationImpl { LogUtils.d("找到搜索结果: $select") } else { LogUtils.e("未搜索到结果") + return false } } if (showMessageHistory) { @@ -838,7 +945,7 @@ object WeworkOperationImpl { /** * 移除群成员/踢人 */ - private fun groupRemoveMember(removeList: List): Boolean { + private fun groupRemoveMember(removeList: List?): Boolean { if (removeList.isNullOrEmpty()) return true if (WeworkRoomUtil.intoGroupManager()) { val gridView = AccessibilityUtil.findOneByClazz(getRoot(), Views.GridView) @@ -869,7 +976,7 @@ object WeworkOperationImpl { AccessibilityUtil.findTextInput(getRoot(), trimTitle) sleep(Constant.POP_WINDOW_INTERVAL) val selectListView = AccessibilityUtil.findOneByClazz(getRoot(), Views.ListView, Views.RecyclerView, Views.ViewGroup, minChildCount = 2, firstChildClazz = Views.RelativeLayout) - val regex = "^$trimTitle(-.*)?(…)?(\\(.*?\\))?" + if (needTrim) "" else "$" + val regex = "^$trimTitle" + if (needTrim) ".*?" else "(-.*)?(…)?(\\(.*?\\))?$" val matchSelect = AccessibilityUtil.findOneByTextRegex( selectListView, regex, @@ -897,6 +1004,7 @@ object WeworkOperationImpl { LogUtils.d("找到搜索结果: $select") } else { LogUtils.e("未搜索到结果") + return false } } val confirmButton = @@ -924,7 +1032,7 @@ object WeworkOperationImpl { * 注:首次为发布 后续为编辑 * 注2:外部群为edittext 内部群为webview(只能追加文本) */ - private fun groupChangeAnnouncement(groupAnnouncement: String? = null): Boolean { + private fun groupChangeAnnouncement(groupAnnouncement: String?): Boolean { if (groupAnnouncement == null) return true if (WeworkRoomUtil.intoGroupManager()) { val textView = AccessibilityUtil.findOneByText(getRoot(), "群公告", exact = true) @@ -971,7 +1079,7 @@ object WeworkOperationImpl { * 注:首次为发布 后续为编辑 * 注2:外部群为edittext 内部群为webview(只能追加文本) */ - private fun groupChangeRemark(groupRemark: String? = null): Boolean { + private fun groupChangeRemark(groupRemark: String?): Boolean { if (groupRemark == null) return true if (WeworkRoomUtil.intoGroupManager()) { val textView = AccessibilityUtil.findOneByText(getRoot(), "备注", exact = true) @@ -1165,7 +1273,7 @@ object WeworkOperationImpl { .replace(".jpg", "") LogUtils.v("fileTime: $fileTime") if (fileTime.isNotBlank()) { - if (fileTime.toLong() > currentTime) { + if (fileTime.toLongOrNull() ?: 0 > currentTime) { LogUtils.d("找到最新保存二维码图片: $fileTime") try { val bitmap = ImageUtils.bytes2Bitmap(file.readBytes()) diff --git a/app/src/main/java/org/yameida/worktool/utils/WebSocketManager.java b/app/src/main/java/org/yameida/worktool/utils/WebSocketManager.java index 3725403..94c6d5b 100644 --- a/app/src/main/java/org/yameida/worktool/utils/WebSocketManager.java +++ b/app/src/main/java/org/yameida/worktool/utils/WebSocketManager.java @@ -8,6 +8,7 @@ import com.hjq.toast.ToastUtils; import org.yameida.worktool.model.WeworkMessageBean; import org.yameida.worktool.model.WeworkMessageListBean; +import org.yameida.worktool.service.WeworkController; import java.text.SimpleDateFormat; import java.util.Date; @@ -140,6 +141,9 @@ public class WebSocketManager { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 Log.e(url, "心跳检测" + df.format(new Date()));// new Date()为获取当前系统时间 if (!connecting && (socket == null || !socket.send(HEARTBEAT))) { + //断开链接后关闭新消息接收 + WeworkController.INSTANCE.setEnableLoopRunning(false); + //断开链接后进入重连 reConnect(); } ToastUtils.show("机器人运行中 请勿人工操作手机~"); diff --git a/app/src/main/java/org/yameida/worktool/utils/WeworkRoomUtil.kt b/app/src/main/java/org/yameida/worktool/utils/WeworkRoomUtil.kt index 94aedaf..681a8af 100644 --- a/app/src/main/java/org/yameida/worktool/utils/WeworkRoomUtil.kt +++ b/app/src/main/java/org/yameida/worktool/utils/WeworkRoomUtil.kt @@ -104,7 +104,7 @@ object WeworkRoomUtil { sleep(Constant.CHANGE_PAGE_INTERVAL) //消息页搜索结果列表 val selectListView = findOneByClazz(getRoot(), Views.ListView) - val regex = "^$trimTitle(-.*)?(…)?(\\(.*?\\))?" + if (needTrim) "" else "$" + val regex = "^$trimTitle" + if (needTrim) ".*?" else "(-.*)?(…)?(\\(.*?\\))?$" val searchResult = AccessibilityUtil.findOneByTextRegex( selectListView, regex, diff --git a/app/src/main/res/layout/activity_listen.xml b/app/src/main/res/layout/activity_listen.xml index 2db304b..d43d529 100644 --- a/app/src/main/res/layout/activity_listen.xml +++ b/app/src/main/res/layout/activity_listen.xml @@ -293,7 +293,7 @@