index.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. import IComponentServer from "../IComponentServer";
  2. import { useStore } from "vuex";
  3. import store from "@/store";
  4. /**
  5. * class TUIConversationServer
  6. *
  7. * TUIConversation 逻辑主体
  8. */
  9. export default class TUIConversationServer extends IComponentServer {
  10. public TUICore: any;
  11. public store = store.state.timStore;
  12. constructor(TUICore: any) {
  13. super();
  14. this.TUICore = uni.$TUIKit;
  15. uni.setStorageSync(`TIM_${uni.$chat_SDKAppID}_isTUIKit`, true);
  16. // #ifdef APP-PLUS
  17. if (uni.$aegis) {
  18. uni.$aegis.reportEvent({
  19. name: 'platform',
  20. ext1: 'platform-APP-npm',
  21. ext2: 'uniTuikitExternalVue3',
  22. ext3: `${uni.$chat_SDKAppID}`,
  23. })
  24. }
  25. // #endif
  26. // #ifdef MP-WEIXIN
  27. if (uni.$aegis) {
  28. uni.$aegis.reportEvent({
  29. name: 'platform',
  30. ext1: 'platform-MP-WEIXIN',
  31. ext2: 'uniTuikitExternalVue3',
  32. ext3: `${uni.$chat_SDKAppID}`,
  33. })
  34. }
  35. // #endif
  36. // #ifdef H5
  37. if (uni.$aegis) {
  38. uni.$aegis.reportEvent({
  39. name: 'platform',
  40. ext1: 'platform-H5-npm',
  41. ext2: 'uniTuikitExternalVue3',
  42. ext3: `${uni.$chat_SDKAppID}`,
  43. })
  44. }
  45. // #endif
  46. //uni.showLoading({ title: '加载中' });
  47. this.bindTIMEvent();
  48. if (uni.$chat_isSDKReady) {
  49. this.getConversationList();
  50. }
  51. }
  52. /**
  53. * /////////////////////////////////////////////////////////////////////////////////
  54. * //
  55. * // TIM 事件监听注册接口
  56. * //
  57. * /////////////////////////////////////////////////////////////////////////////////
  58. */
  59. private bindTIMEvent() {
  60. this.TUICore.on(
  61. uni.$TIM.EVENT.CONVERSATION_LIST_UPDATED,
  62. this.handleConversationListUpdate,
  63. this
  64. );
  65. }
  66. private unbindTIMEvent() {
  67. this.TUICore.off(
  68. uni.$TIM.EVENT.CONVERSATION_LIST_UPDATED,
  69. this.handleConversationListUpdate
  70. );
  71. }
  72. public handleConversationListUpdate(res: any) {
  73. uni.hideLoading();
  74. if (res.data.length === 0) {
  75. // uni.showToast({
  76. // title: "暂无回话哦~",
  77. // });
  78. }
  79. uni.$emit('refreshMsgCount');
  80. let imUnread=0;
  81. res.data.forEach(function(element) {
  82. //更新会话订单ID
  83. // var json;
  84. // if(element.lastMessage.cloudCustomData!=null){
  85. // json=JSON.parse(element.lastMessage.cloudCustomData);
  86. // }
  87. // else if(element.customData!=null){
  88. // json=JSON.parse(element.customData);
  89. // }
  90. imUnread+=element.unreadCount;
  91. uni.setStorageSync("imUnread",imUnread);
  92. console.log("收到会话消息");
  93. console.log(element);
  94. // if(json.type=="startFollow"){
  95. // //开始随访
  96. // json.imType=2;
  97. // console.log(json)
  98. // uni.$TUIKit.TUIConversationServer.setConversationValue(element.conversationID,JSON.stringify(json))
  99. // .then((res) => {
  100. // })
  101. // .catch((err) => {
  102. // });
  103. // }
  104. // else if(json.type=="follow"){
  105. // //随访消息
  106. // json.imType=2;
  107. // uni.$TUIKit.TUIConversationServer.setConversationValue(element.conversationID,JSON.stringify(json))
  108. // .then((res) => {
  109. // // uni.$TUIKit.TUIConversationServer.getConversationProfile(element.conversationID)
  110. // // .then((res) => {
  111. // // console.log(res)
  112. // // })
  113. // // .catch((err) => {
  114. // // });
  115. // })
  116. // .catch((err) => {
  117. // });
  118. // }
  119. // else if(json.type=="finishFollow"){
  120. // //结束随访
  121. // json.imType=0;
  122. // uni.$TUIKit.TUIConversationServer.setConversationValue(element.conversationID,JSON.stringify(json))
  123. // .then((res) => {
  124. // // uni.$TUIKit.TUIConversationServer.getConversationProfile(element.conversationID)
  125. // // .then((res) => {
  126. // // console.log(res)
  127. // // })
  128. // // .catch((err) => {
  129. // // });
  130. // })
  131. // .catch((err) => {
  132. // });
  133. // }
  134. // else if(json.type=="startInquiry"){
  135. // //开始问诊
  136. // json.imType=1
  137. // uni.$TUIKit.TUIConversationServer.setConversationValue(element.conversationID,JSON.stringify(json))
  138. // .then((res) => {
  139. // // uni.$TUIKit.TUIConversationServer.getConversationProfile(element.conversationID)
  140. // // .then((res) => {
  141. // // console.log(res)
  142. // // })
  143. // // .catch((err) => {
  144. // // });
  145. // })
  146. // .catch((err) => {
  147. // });
  148. // }
  149. // else if(json.type=="inquiry"){
  150. // //问诊消息
  151. // json.imType=1;
  152. // uni.$TUIKit.TUIConversationServer.setConversationValue(element.conversationID,JSON.stringify(json))
  153. // .then((res) => {
  154. // // uni.$TUIKit.TUIConversationServer.getConversationProfile(element.conversationID)
  155. // // .then((res) => {
  156. // // console.log(res)
  157. // // })
  158. // // .catch((err) => {
  159. // // });
  160. // })
  161. // .catch((err) => {
  162. // });
  163. // }
  164. // else if(json.type=="finishInquiry"){
  165. // //结束问诊
  166. // json.imType=0;
  167. // uni.$TUIKit.TUIConversationServer.setConversationValue(element.conversationID,JSON.stringify(json))
  168. // .then((res) => {
  169. // // uni.$TUIKit.TUIConversationServer.getConversationProfile(element.conversationID)
  170. // // .then((res) => {
  171. // // console.log(res)
  172. // // })
  173. // // .catch((err) => {
  174. // // });
  175. // })
  176. // .catch((err) => {
  177. // });
  178. // }
  179. // else if(json.type=="startDrugReport"){
  180. // //开始随访
  181. // json.imType=2;
  182. // console.log(json)
  183. // uni.$TUIKit.TUIConversationServer.setConversationValue(element.conversationID,JSON.stringify(json))
  184. // .then((res) => {
  185. // })
  186. // .catch((err) => {
  187. // });
  188. // }
  189. // else if(json.type=="drugReport"){
  190. // //随访消息
  191. // json.imType=2;
  192. // uni.$TUIKit.TUIConversationServer.setConversationValue(element.conversationID,JSON.stringify(json))
  193. // .then((res) => {
  194. // // uni.$TUIKit.TUIConversationServer.getConversationProfile(element.conversationID)
  195. // // .then((res) => {
  196. // // console.log(res)
  197. // // })
  198. // // .catch((err) => {
  199. // // });
  200. // })
  201. // .catch((err) => {
  202. // });
  203. // }
  204. // else if(json.type=="finishDrugReport"){
  205. // //结束随访
  206. // json.imType=0;
  207. // uni.$TUIKit.TUIConversationServer.setConversationValue(element.conversationID,JSON.stringify(json))
  208. // .then((res) => {
  209. // // uni.$TUIKit.TUIConversationServer.getConversationProfile(element.conversationID)
  210. // // .then((res) => {
  211. // // console.log(res)
  212. // // })
  213. // // .catch((err) => {
  214. // // });
  215. // })
  216. // .catch((err) => {
  217. // });
  218. // }
  219. });
  220. store.commit("timStore/setConversationList", res.data);
  221. }
  222. /**
  223. * 组件销毁
  224. *
  225. */
  226. public destroyed() {
  227. this.unbindTIMEvent();
  228. }
  229. /*
  230. * 获取 conversationList
  231. *
  232. * @returns {Promise}
  233. */
  234. private async getConversationList() {
  235. return new Promise<void>(async (resolve, reject) => {
  236. try {
  237. const imResponse = await uni.$TUIKit.getConversationList();
  238. store.commit("timStore/setConversationList", imResponse.data.conversationList);
  239. uni.hideLoading();
  240. resolve(imResponse);
  241. } catch (error) {
  242. reject(error);
  243. }
  244. })
  245. }
  246. /**
  247. * 获取 conversationList
  248. *
  249. * @param {string} conversationID 会话ID
  250. * @returns {Promise}
  251. */
  252. public async getConversationProfile(conversationID: string) {
  253. return new Promise<void>(async (resolve, reject) => {
  254. try {
  255. const imResponse = await this.TUICore.getConversationProfile(
  256. conversationID
  257. );
  258. resolve(imResponse);
  259. } catch (error) {
  260. reject(error);
  261. }
  262. });
  263. }
  264. /**
  265. * 设置自定义值
  266. */
  267. public async setConversationValue(conversationID: string,data: Object) {
  268. return new Promise<void>(async (resolve, reject) => {
  269. try {
  270. const imResponse = await this.TUICore.setConversationCustomData(
  271. {
  272. conversationIDList: [conversationID],
  273. customData: data
  274. }
  275. );
  276. resolve(imResponse);
  277. } catch (error) {
  278. reject(error);
  279. }
  280. });
  281. }
  282. /**
  283. * 删除会话
  284. *
  285. * @param {string} conversationID 会话ID
  286. * @returns {Promise}
  287. */
  288. public async deleteConversation(conversationID: string) {
  289. return new Promise<void>(async (resolve, reject) => {
  290. try {
  291. const imResponse: any = await this.TUICore.deleteConversation(
  292. conversationID
  293. );
  294. resolve(imResponse);
  295. } catch (error) {
  296. reject(error);
  297. }
  298. });
  299. }
  300. /**
  301. * 置顶会话
  302. *
  303. * @param {Object} options 置顶参数
  304. * @returns {Promise}
  305. */
  306. public async pinConversation(options: any) {
  307. return new Promise<void>(async (resolve, reject) => {
  308. try {
  309. const imResponse: any = await this.TUICore.pinConversation(options);
  310. resolve(imResponse);
  311. } catch (error) {
  312. reject(error);
  313. }
  314. });
  315. }
  316. /**
  317. * C2C消息免打扰
  318. *
  319. * @param {Object} options 消息免打扰参数
  320. * @returns {Promise}
  321. */
  322. public async muteConversation(options: any) {
  323. return new Promise<void>(async (resolve, reject) => {
  324. try {
  325. const imResponse: any = await this.TUICore.setMessageRemindType(
  326. options
  327. );
  328. resolve(imResponse);
  329. } catch (error) {
  330. reject(error);
  331. }
  332. });
  333. }
  334. /**
  335. * 设置已读
  336. *
  337. * @param {string} conversationID 会话ID
  338. * @returns {Promise}
  339. */
  340. public async setMessageRead(conversationID: string) {
  341. return new Promise<void>(async (resolve, reject) => {
  342. try {
  343. const imResponse: any = await this.TUICore.setMessageRead({
  344. conversationID,
  345. });
  346. resolve(imResponse);
  347. } catch (error) {
  348. reject(error);
  349. }
  350. });
  351. }
  352. }