package com.yidianling.im.modular.service import android.app.Activity import android.content.Context import android.support.v7.app.AppCompatActivity import com.alibaba.android.arouter.facade.annotation.Route import com.netease.nimlib.sdk.NIMClient import com.netease.nimlib.sdk.RequestCallback import com.netease.nimlib.sdk.auth.AuthService import com.netease.nimlib.sdk.auth.LoginInfo import com.netease.nimlib.sdk.msg.MessageBuilder import com.netease.nimlib.sdk.msg.MsgService import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum import com.ydl.ydlcommon.router.YdlCommonOut import com.ydl.ydlcommon.utils.StringUtils import com.yidianling.im.R import com.yidianling.im.api.bean.IMLoginInfo import com.yidianling.im.api.bean.IMRequestCallback import com.yidianling.im.api.service.IImService import com.yidianling.im.bridge.P2PCustomActionHandlerImpl import com.yidianling.im.helper.IMUtil import com.yidianling.im.helper.MsgReceiveHelper import com.yidianling.im.preference.IMCache import com.yidianling.im.router.ImIn import com.yidianling.im.session.SessionHelper import com.yidianling.im.session.extension.CustomAttachSubScriptTime import com.yidianling.im.session.extension.CustomAttachmentTest import com.yidianling.nimbase.common.media.picker.PickImageHelper import com.yidianling.uikit.api.NimUIKit import com.yidianling.uikit.business.session.helper.MessageListPanelHelper /** * Created by haorui on 2019-12-11 . * Des: */ @Route(path = "/im/ImService") class IMServiceImpl : IImService { override fun isHasUnread(): Boolean { return MsgReceiveHelper.isHasUnread } override fun init(context: Context?) { } override fun startP2PSession(context: Activity, toUid: String) { IMUtil.startChat(context as AppCompatActivity, toUid, IMUtil.FLAG_SAVE or IMUtil.FLAG_USE_UM, 0, null) } override fun startP2PXiaoYi(context: Context) { if (!ImIn.isLogin()) { context.startActivity(ImIn.loginWayIntent(context)) return } SessionHelper.startP2PSession(context, -1, "14", null, P2PCustomActionHandlerImpl("14", "客服小壹", "https://static.ydlcdn.com/mobile/images/avatar_girl_app.png")) } override fun imLogin(info: IMLoginInfo) { NimUIKit.setAccount(info.account) try { NIMClient.getService(AuthService::class.java).login(LoginInfo(info.account, info.passWord)) } catch (e: Exception) { e.printStackTrace() } } override fun login(info: IMLoginInfo, callback: IMRequestCallback<IMLoginInfo>?) { NimUIKit.login(LoginInfo(info.account, info.passWord), object : RequestCallback<LoginInfo> { override fun onSuccess(param: LoginInfo?) { if (param != null) { val newParam = IMLoginInfo(param.account, param.token) callback?.onSuccess(newParam) } } override fun onFailed(code: Int) { callback?.onFailed(code) } override fun onException(exception: Throwable?) { callback?.onException(exception) } }) } override fun setAccount(account: String) { NimUIKit.setAccount(account) } override fun setChattingAccountAll() { NIMClient.getService(MsgService::class.java).setChattingAccount(MsgService.MSG_CHATTING_ACCOUNT_ALL, SessionTypeEnum.None) } override fun setChattingAccountNone() { NIMClient.getService(MsgService::class.java).setChattingAccount(MsgService.MSG_CHATTING_ACCOUNT_NONE, SessionTypeEnum.None) } override fun logout() { NIMClient.getService(AuthService::class.java).logout() } override fun clear() { NimUIKit.logout() IMCache.clear() } override fun createTextMessage(sessionId: String?, content: String, callback: IMRequestCallback<Void>) { val message = MessageBuilder.createTextMessage(sessionId, SessionTypeEnum.P2P, content) NIMClient.getService(MsgService::class.java) .sendMessage(message, false) .setCallback(object : RequestCallback<Void> { override fun onSuccess(param: Void?) { callback.onSuccess(param) MessageListPanelHelper.getInstance().notifyAddMessage(message) } override fun onException(exception: Throwable?) { callback.onException(exception) } override fun onFailed(code: Int) { callback.onFailed(code) } }) } override fun sendSubscriptionTimeMessage(sessionId: String?, content: String, callback: IMRequestCallback<Void>) { val customTime = CustomAttachSubScriptTime(content) val message = MessageBuilder.createCustomMessage(sessionId, SessionTypeEnum.P2P, content, customTime) NIMClient.getService(MsgService::class.java) .sendMessage(message, false) .setCallback(object : RequestCallback<Void> { override fun onSuccess(param: Void?) { callback.onSuccess(param) MessageListPanelHelper.getInstance().notifyAddMessage(message) } override fun onException(exception: Throwable?) { callback.onException(exception) } override fun onFailed(code: Int) { callback.onFailed(code) } }) } override fun showSelector(activity: Activity, requestCode: Int) { val option = PickImageHelper.PickImageOption() option.titleResId = R.string.im_input_panel_photo option.multiSelect = true option.multiSelectMaxCount = 9 option.crop = false option.cropOutputImageWidth = 720 option.cropOutputImageHeight = 720 // option.outputPath = StorageUtil.getWritePath(StringUtils.get32UUID() + ".jpg", StorageType.TYPE_TEMP) option.outputPath = YdlCommonOut.getApp().externalCacheDir.absolutePath + "/" + StringUtils.get32UUID() + ".jpg" PickImageHelper.pickImage(activity, requestCode, option) } override fun sendTestResultMessage(uid: String, content: String, title: String?, head: String?, url: String?, id: Int, share_url: String?, callback: IMRequestCallback<Void>) { val customAttachmentTest = CustomAttachmentTest( CustomAttachmentTest.FLAG_RESULT, title, head, url, id, share_url ) val message = MessageBuilder.createCustomMessage(uid, SessionTypeEnum.P2P, "测试结果", customAttachmentTest) NIMClient.getService(MsgService::class.java).sendMessage(message, false).setCallback(object : RequestCallback<Void> { override fun onSuccess(param: Void?) { callback.onSuccess(param) MessageListPanelHelper.getInstance().notifyAddMessage(message) } override fun onFailed(code: Int) { callback.onFailed(code) } override fun onException(exception: Throwable?) { callback.onException(exception) } }) } /** * 跳转私聊界面 */ override fun startChat(context: Activity, toUid: String, flag: Int, canTalk: Int) { //这里虽然是倾述流程进入私聊,但不需要发送自定义消息 IMUtil.startChat(context as AppCompatActivity, toUid, 0x001, canTalk, null, 0, false) } override fun getUnReadByUid(uid: String): Int { return MsgReceiveHelper.getUnNum(uid) } override fun getAllUnReadNum(): Int { return MsgReceiveHelper.getAllUnNum() } }