im-operator.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import {dealChatTime} from "../../utils/time";
  2. import ImApi from '../../apis/im'
  3. import { fotmatDate } from '../../utils/date'
  4. export default class IMOperator {
  5. static VoiceType = 'voice';
  6. static TextType = 'text';
  7. static ImageType = 'image';
  8. static CustomType = 'custom';
  9. static VrCardType = 'vr'
  10. constructor(page, opts = {}) {
  11. this._opts = opts;
  12. this._latestTImestamp = 0;//最新消息的时间戳
  13. this._myHeadUrl = this._opts.myHeadUrl;
  14. this._otherHeadUrl = this._opts.friendHeadUrl;
  15. }
  16. getFriendId() {
  17. return this._opts.friendId;
  18. }
  19. onSimulateReceiveMsg(cbOk, toId) {
  20. this.listener = (msg) => {
  21. if (!msg) {
  22. return;
  23. }
  24. msg.isMy = msg.fromId === getApp().globalData.userinfo.viewerId;
  25. const item = this.createNormalChatItem(msg);
  26. this._latestTImestamp = item.timestamp;
  27. //这里是收到好友消息的回调函数,建议传入的item是 由 createNormalChatItem 方法生成的。
  28. cbOk && cbOk(item);
  29. }
  30. getApp().getIMHandler().setOnReceiveMessageListener({
  31. listener: this.listener
  32. });
  33. }
  34. removeSimulateReceiveMsg () {
  35. getApp().getIMHandler().removeOnReceiveMessageListener({
  36. listener: this.listener
  37. });
  38. }
  39. async onSimulateSendMsg({content}) {
  40. //这里content即为要发送的数据
  41. //这里的content是一个对象了,不再是一个JSON格式的字符串。这样可以在发送消息的底层统一处理。
  42. try {
  43. content = {
  44. fromId: content.fromId,
  45. fromName: getApp().globalData.userinfo.name || getApp().globalData.userinfo.phone,
  46. toId: content.toId,
  47. toName: 'xu',
  48. type: content.type,
  49. msgType: content.msgType,
  50. content: content.content
  51. }
  52. const {content: contentSendSuccess} = await getApp().getIMHandler().sendMsg({content});
  53. //这个contentSendSuccess格式一样,也是一个对象
  54. const msg = this.createNormalChatItem(contentSendSuccess);
  55. this._latestTImestamp = msg.timestamp;
  56. return Promise.resolve({msg});
  57. } catch (e) {
  58. return Promise.reject(e);
  59. }
  60. }
  61. createChatItemContent({msgType = IMOperator.TextType, content = '', duration, toId, type="TYPE_ONE"} = {}) {
  62. return {
  63. content,
  64. msgType,
  65. type,
  66. toId,
  67. conversationId: 0,//会话id,目前未用到
  68. fromId: getApp().globalData.userinfo.viewerId,
  69. duration
  70. };
  71. }
  72. createNormalChatItem({type = IMOperator.TextType, content = '', isMy = true, duration, msgType, sendTime=fotmatDate(new Date(), 'yyyy-MM-dd hh:mm:ss')} = {}) {
  73. if (!content) return;
  74. const currentTimestamp = Date.now();
  75. const time = dealChatTime(sendTime, this._latestTImestamp);
  76. let obj = {
  77. friendId: this.getFriendId(),//好友id
  78. isMy,//我发送的消息?
  79. showTime: time.ifShowTime,//是否显示该次发送时间
  80. time: time.timeStr,//发送时间 如 09:15,
  81. timestamp: currentTimestamp,//该条数据的时间戳,一般用于排序
  82. type,//内容的类型,目前有这几种类型: text/voice/image/custom | 文本/语音/图片/自定义
  83. content,// 显示的内容,根据不同的类型,在这里填充不同的信息。
  84. headUrl: isMy ? this._myHeadUrl : this._otherHeadUrl,//显示的头像,自己或好友的。
  85. sendStatus: 'success',//发送状态,目前有这几种状态:sending/success/failed | 发送中/发送成功/发送失败
  86. duration: duration || content.duration,//语音时长 单位秒
  87. isPlaying: false,//语音是否正在播放,
  88. msgType,
  89. toId: arguments[0].toId,
  90. fromId: arguments[0].fromId
  91. };
  92. if (type !== IMOperator.TextType) {
  93. obj.saveKey = content;//saveKey是存储文件时的key
  94. }
  95. return obj;
  96. }
  97. static createCustomChatItem() {
  98. return {
  99. timestamp: Date.now(),
  100. type: IMOperator.CustomType,
  101. content: '会话已关闭'
  102. }
  103. }
  104. }