123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import {dealChatTime} from "../../utils/time";
- const webSocket = require('../../utils/websocket.js');
- /**
- * 这个类是IM模拟类,作为示例仅供参考。
- */
- export default class IMOperator {
- static VoiceType = 'voice'; //语音
- static TextType = 'text'; //文本
- static ImageType = 'image'; //图片
- static CustomType = 'custom';
- constructor(page, opts) {
- this._opts = opts;
- this._latestTImestamp = 0;//最新消息的时间戳
- this._myHeadUrl = this._opts.myheadurl; //我的头像
- this._otherHeadUrl = this._opts.headUrl; //好友的头像
- }
- getFriendId() {
- return this._opts.to_id;
- }
- onSimulateReceiveMsg(cbOk) {
- }
- onSimulateSendMsg({content, success, fail}) {
- console.log(content,'里面');
- let _that = this;
- let type;
- if (content.type){
- switch (content.type) {
- case 'text':
- type = 'alone'
- break;
- case 'image':
- console.log(1111);
- type = 'images';
- break;
- }
- }
-
- getApp().Imsocket().sendSocketMessage({
- msg: JSON.stringify({ to_id: content.friendId, form_id: content.userId, type: type, content: content.content}),
- success: function (res) {
- console.log(res, '单独发送消息成功');
- const item = _that.createNormalChatItem(content);
- _that._latestTImestamp = item.timestamp;
- success && success(item);
- }, fail: function (res) {
- console.log('单独发送消息失败')
- }
- })
- //这里content即为要发送的数据
- //这里的content是一个对象了,不再是一个JSON格式的字符串。这样可以在发送消息的底层统一处理。
- // getApp().getIMHandler().sendMsg({
- // content,
- // success: (content) => {
- // //这个content格式一样,也是一个对象
- // const item = this.createNormalChatItem(content);
- // this._latestTImestamp = item.timestamp;
- // success && success(item);
- // },
- // fail
- // });
- }
- //创建要发送的消息
- createChatItemContent({type = IMOperator.TextType, content = '', duration} = {}) {
- if (!content.replace(/^\s*|\s*$/g, '')) return;
- return {
- content,
- type,
- conversationId: 0,//会话id,目前未用到
- userId: getApp().UserId(),
- friendId: this.getFriendId(),//好友id
- duration
- };
- }
- //我对别人说的
- createNormalChatItem({type = IMOperator.TextType, content = '', isMy = true, duration} = {}) {
- if (!content) return;
- const currentTimestamp = Date.now();
- const time = dealChatTime(currentTimestamp, this._latestTImestamp);
- let obj = {
- msgId: 0,//消息id
- friendId: this.getFriendId(),//好友id
- isMy: isMy,//我发送的消息?
- showTime: time.ifShowTime,//是否显示该次发送时间
- time: time.timeStr,//发送时间 如 09:15,
- timestamp: currentTimestamp,//该条数据的时间戳,一般用于排序
- type: type,//内容的类型,目前有这几种类型: text/voice/image/custom | 文本/语音/图片/自定义
- content: content,// 显示的内容,根据不同的类型,在这里填充不同的信息。
- headUrl: isMy ? this._myHeadUrl : this._otherHeadUrl,//显示的头像,自己或好友的。
- sendStatus: 'success',//发送状态,目前有这几种状态:sending/success/failed | 发送中/发送成功/发送失败
- voiceDuration: duration,//语音时长 单位秒
- isPlaying: false,//语音是否正在播放
- };
- obj.saveKey = obj.friendId + '_' + obj.msgId;//saveKey是存储文件时的key
- return obj;
- }
- static createCustomChatItem() {
- return {
- timestamp: Date.now(),
- type: IMOperator.CustomType,
- content: '会话已关闭'
- }
- }
- }
|