im-operator.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import {dealChatTime} from "../../utils/time";
  2. const webSocket = require('../../utils/websocket.js');
  3. /**
  4. * 这个类是IM模拟类,作为示例仅供参考。
  5. */
  6. export default class IMOperator {
  7. static VoiceType = 'voice'; //语音
  8. static TextType = 'text'; //文本
  9. static ImageType = 'image'; //图片
  10. static CustomType = 'custom';
  11. constructor(page, opts) {
  12. this._opts = opts;
  13. this._latestTImestamp = 0;//最新消息的时间戳
  14. this._myHeadUrl = this._opts.myheadurl; //我的头像
  15. this._otherHeadUrl = this._opts.headUrl; //好友的头像
  16. }
  17. getFriendId() {
  18. return this._opts.to_id;
  19. }
  20. onSimulateReceiveMsg(cbOk) {
  21. }
  22. onSimulateSendMsg({content, success, fail}) {
  23. console.log(content,'里面');
  24. let _that = this;
  25. let type;
  26. if (content.type){
  27. switch (content.type) {
  28. case 'text':
  29. type = 'alone'
  30. break;
  31. case 'image':
  32. console.log(1111);
  33. type = 'images';
  34. break;
  35. }
  36. }
  37. getApp().Imsocket().sendSocketMessage({
  38. msg: JSON.stringify({ to_id: content.friendId, form_id: content.userId, type: type, content: content.content}),
  39. success: function (res) {
  40. console.log(res, '单独发送消息成功');
  41. const item = _that.createNormalChatItem(content);
  42. _that._latestTImestamp = item.timestamp;
  43. success && success(item);
  44. }, fail: function (res) {
  45. console.log('单独发送消息失败')
  46. }
  47. })
  48. //这里content即为要发送的数据
  49. //这里的content是一个对象了,不再是一个JSON格式的字符串。这样可以在发送消息的底层统一处理。
  50. // getApp().getIMHandler().sendMsg({
  51. // content,
  52. // success: (content) => {
  53. // //这个content格式一样,也是一个对象
  54. // const item = this.createNormalChatItem(content);
  55. // this._latestTImestamp = item.timestamp;
  56. // success && success(item);
  57. // },
  58. // fail
  59. // });
  60. }
  61. //创建要发送的消息
  62. createChatItemContent({type = IMOperator.TextType, content = '', duration} = {}) {
  63. if (!content.replace(/^\s*|\s*$/g, '')) return;
  64. return {
  65. content,
  66. type,
  67. conversationId: 0,//会话id,目前未用到
  68. userId: getApp().UserId(),
  69. friendId: this.getFriendId(),//好友id
  70. duration
  71. };
  72. }
  73. //我对别人说的
  74. createNormalChatItem({type = IMOperator.TextType, content = '', isMy = true, duration} = {}) {
  75. if (!content) return;
  76. const currentTimestamp = Date.now();
  77. const time = dealChatTime(currentTimestamp, this._latestTImestamp);
  78. let obj = {
  79. msgId: 0,//消息id
  80. friendId: this.getFriendId(),//好友id
  81. isMy: isMy,//我发送的消息?
  82. showTime: time.ifShowTime,//是否显示该次发送时间
  83. time: time.timeStr,//发送时间 如 09:15,
  84. timestamp: currentTimestamp,//该条数据的时间戳,一般用于排序
  85. type: type,//内容的类型,目前有这几种类型: text/voice/image/custom | 文本/语音/图片/自定义
  86. content: content,// 显示的内容,根据不同的类型,在这里填充不同的信息。
  87. headUrl: isMy ? this._myHeadUrl : this._otherHeadUrl,//显示的头像,自己或好友的。
  88. sendStatus: 'success',//发送状态,目前有这几种状态:sending/success/failed | 发送中/发送成功/发送失败
  89. voiceDuration: duration,//语音时长 单位秒
  90. isPlaying: false,//语音是否正在播放
  91. };
  92. obj.saveKey = obj.friendId + '_' + obj.msgId;//saveKey是存储文件时的key
  93. return obj;
  94. }
  95. static createCustomChatItem() {
  96. return {
  97. timestamp: Date.now(),
  98. type: IMOperator.CustomType,
  99. content: '会话已关闭'
  100. }
  101. }
  102. }