123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- import IComponentServer from "../IComponentServer";
- import { useStore } from "vuex";
- import store from "@/store";
- /**
- * class TUIConversationServer
- *
- * TUIConversation 逻辑主体
- */
- export default class TUIConversationServer extends IComponentServer {
- public TUICore: any;
- public store = store.state.timStore;
- constructor(TUICore: any) {
- super();
- this.TUICore = uni.$TUIKit;
- uni.setStorageSync(`TIM_${uni.$chat_SDKAppID}_isTUIKit`, true);
- // #ifdef APP-PLUS
- if (uni.$aegis) {
- uni.$aegis.reportEvent({
- name: 'platform',
- ext1: 'platform-APP-npm',
- ext2: 'uniTuikitExternalVue3',
- ext3: `${uni.$chat_SDKAppID}`,
- })
- }
- // #endif
-
- // #ifdef MP-WEIXIN
- if (uni.$aegis) {
- uni.$aegis.reportEvent({
- name: 'platform',
- ext1: 'platform-MP-WEIXIN',
- ext2: 'uniTuikitExternalVue3',
- ext3: `${uni.$chat_SDKAppID}`,
- })
- }
- // #endif
-
- // #ifdef H5
- if (uni.$aegis) {
- uni.$aegis.reportEvent({
- name: 'platform',
- ext1: 'platform-H5-npm',
- ext2: 'uniTuikitExternalVue3',
- ext3: `${uni.$chat_SDKAppID}`,
- })
- }
- // #endif
- //uni.showLoading({ title: '加载中' });
- this.bindTIMEvent();
- if (uni.$chat_isSDKReady) {
- this.getConversationList();
- }
- }
- /**
- * /////////////////////////////////////////////////////////////////////////////////
- * //
- * // TIM 事件监听注册接口
- * //
- * /////////////////////////////////////////////////////////////////////////////////
- */
- private bindTIMEvent() {
- this.TUICore.on(
- uni.$TIM.EVENT.CONVERSATION_LIST_UPDATED,
- this.handleConversationListUpdate,
- this
- );
- }
- private unbindTIMEvent() {
- this.TUICore.off(
- uni.$TIM.EVENT.CONVERSATION_LIST_UPDATED,
- this.handleConversationListUpdate
- );
- }
- public handleConversationListUpdate(res: any) {
- uni.hideLoading();
- if (res.data.length === 0) {
- // uni.showToast({
- // title: "暂无回话哦~",
- // });
- }
- uni.$emit('refreshMsgCount');
- let imUnread=0;
- res.data.forEach(function(element) {
- //更新会话订单ID
- // var json;
- // if(element.lastMessage.cloudCustomData!=null){
- // json=JSON.parse(element.lastMessage.cloudCustomData);
- // }
- // else if(element.customData!=null){
- // json=JSON.parse(element.customData);
- // }
- imUnread+=element.unreadCount;
- uni.setStorageSync("imUnread",imUnread);
- console.log("收到会话消息");
- console.log(element);
- // if(json.type=="startFollow"){
- // //开始随访
- // json.imType=2;
- // console.log(json)
- // uni.$TUIKit.TUIConversationServer.setConversationValue(element.conversationID,JSON.stringify(json))
- // .then((res) => {
- // })
- // .catch((err) => {
-
- // });
- // }
- // else if(json.type=="follow"){
- // //随访消息
- // json.imType=2;
- // uni.$TUIKit.TUIConversationServer.setConversationValue(element.conversationID,JSON.stringify(json))
- // .then((res) => {
- // // uni.$TUIKit.TUIConversationServer.getConversationProfile(element.conversationID)
- // // .then((res) => {
- // // console.log(res)
- // // })
- // // .catch((err) => {
- // // });
- // })
- // .catch((err) => {
-
- // });
- // }
- // else if(json.type=="finishFollow"){
- // //结束随访
- // json.imType=0;
- // uni.$TUIKit.TUIConversationServer.setConversationValue(element.conversationID,JSON.stringify(json))
- // .then((res) => {
- // // uni.$TUIKit.TUIConversationServer.getConversationProfile(element.conversationID)
- // // .then((res) => {
- // // console.log(res)
- // // })
- // // .catch((err) => {
- // // });
- // })
- // .catch((err) => {
-
- // });
- // }
- // else if(json.type=="startInquiry"){
- // //开始问诊
- // json.imType=1
- // uni.$TUIKit.TUIConversationServer.setConversationValue(element.conversationID,JSON.stringify(json))
- // .then((res) => {
- // // uni.$TUIKit.TUIConversationServer.getConversationProfile(element.conversationID)
- // // .then((res) => {
- // // console.log(res)
- // // })
- // // .catch((err) => {
- // // });
- // })
- // .catch((err) => {
-
- // });
-
- // }
- // else if(json.type=="inquiry"){
- // //问诊消息
- // json.imType=1;
- // uni.$TUIKit.TUIConversationServer.setConversationValue(element.conversationID,JSON.stringify(json))
- // .then((res) => {
- // // uni.$TUIKit.TUIConversationServer.getConversationProfile(element.conversationID)
- // // .then((res) => {
- // // console.log(res)
- // // })
- // // .catch((err) => {
- // // });
- // })
- // .catch((err) => {
-
- // });
- // }
- // else if(json.type=="finishInquiry"){
- // //结束问诊
- // json.imType=0;
- // uni.$TUIKit.TUIConversationServer.setConversationValue(element.conversationID,JSON.stringify(json))
- // .then((res) => {
- // // uni.$TUIKit.TUIConversationServer.getConversationProfile(element.conversationID)
- // // .then((res) => {
- // // console.log(res)
- // // })
- // // .catch((err) => {
- // // });
- // })
- // .catch((err) => {
-
- // });
- // }
- // else if(json.type=="startDrugReport"){
- // //开始随访
- // json.imType=2;
- // console.log(json)
- // uni.$TUIKit.TUIConversationServer.setConversationValue(element.conversationID,JSON.stringify(json))
- // .then((res) => {
- // })
- // .catch((err) => {
-
- // });
- // }
- // else if(json.type=="drugReport"){
- // //随访消息
- // json.imType=2;
- // uni.$TUIKit.TUIConversationServer.setConversationValue(element.conversationID,JSON.stringify(json))
- // .then((res) => {
- // // uni.$TUIKit.TUIConversationServer.getConversationProfile(element.conversationID)
- // // .then((res) => {
- // // console.log(res)
- // // })
- // // .catch((err) => {
- // // });
- // })
- // .catch((err) => {
-
- // });
- // }
- // else if(json.type=="finishDrugReport"){
- // //结束随访
- // json.imType=0;
- // uni.$TUIKit.TUIConversationServer.setConversationValue(element.conversationID,JSON.stringify(json))
- // .then((res) => {
- // // uni.$TUIKit.TUIConversationServer.getConversationProfile(element.conversationID)
- // // .then((res) => {
- // // console.log(res)
- // // })
- // // .catch((err) => {
- // // });
- // })
- // .catch((err) => {
-
- // });
- // }
- });
- store.commit("timStore/setConversationList", res.data);
- }
- /**
- * 组件销毁
- *
- */
- public destroyed() {
- this.unbindTIMEvent();
- }
- /*
- * 获取 conversationList
- *
- * @returns {Promise}
- */
- private async getConversationList() {
- return new Promise<void>(async (resolve, reject) => {
- try {
- const imResponse = await uni.$TUIKit.getConversationList();
- store.commit("timStore/setConversationList", imResponse.data.conversationList);
- uni.hideLoading();
- resolve(imResponse);
- } catch (error) {
- reject(error);
- }
- })
- }
- /**
- * 获取 conversationList
- *
- * @param {string} conversationID 会话ID
- * @returns {Promise}
- */
- public async getConversationProfile(conversationID: string) {
- return new Promise<void>(async (resolve, reject) => {
- try {
- const imResponse = await this.TUICore.getConversationProfile(
- conversationID
- );
- resolve(imResponse);
- } catch (error) {
- reject(error);
- }
- });
- }
- /**
- * 设置自定义值
- */
- public async setConversationValue(conversationID: string,data: Object) {
- return new Promise<void>(async (resolve, reject) => {
- try {
- const imResponse = await this.TUICore.setConversationCustomData(
- {
- conversationIDList: [conversationID],
- customData: data
- }
- );
- resolve(imResponse);
- } catch (error) {
- reject(error);
- }
- });
- }
- /**
- * 删除会话
- *
- * @param {string} conversationID 会话ID
- * @returns {Promise}
- */
- public async deleteConversation(conversationID: string) {
- return new Promise<void>(async (resolve, reject) => {
- try {
- const imResponse: any = await this.TUICore.deleteConversation(
- conversationID
- );
- resolve(imResponse);
- } catch (error) {
- reject(error);
- }
- });
- }
- /**
- * 置顶会话
- *
- * @param {Object} options 置顶参数
- * @returns {Promise}
- */
- public async pinConversation(options: any) {
- return new Promise<void>(async (resolve, reject) => {
- try {
- const imResponse: any = await this.TUICore.pinConversation(options);
- resolve(imResponse);
- } catch (error) {
- reject(error);
- }
- });
- }
- /**
- * C2C消息免打扰
- *
- * @param {Object} options 消息免打扰参数
- * @returns {Promise}
- */
- public async muteConversation(options: any) {
- return new Promise<void>(async (resolve, reject) => {
- try {
- const imResponse: any = await this.TUICore.setMessageRemindType(
- options
- );
- resolve(imResponse);
- } catch (error) {
- reject(error);
- }
- });
- }
- /**
- * 设置已读
- *
- * @param {string} conversationID 会话ID
- * @returns {Promise}
- */
- public async setMessageRead(conversationID: string) {
- return new Promise<void>(async (resolve, reject) => {
- try {
- const imResponse: any = await this.TUICore.setMessageRead({
- conversationID,
- });
- resolve(imResponse);
- } catch (error) {
- reject(error);
- }
- });
- }
- }
|