123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import {dealChatTime} from "../../utils/time";
- const webSocket = require('../../utils/websocket.js');
- 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('单独发送消息失败')
- }
- })
-
-
-
-
-
-
-
-
-
-
-
-
- }
-
- createChatItemContent({type = IMOperator.TextType, content = '', duration} = {}) {
- if (!content.replace(/^\s*|\s*$/g, '')) return;
- return {
- content,
- type,
- conversationId: 0,
- userId: getApp().UserId(),
- friendId: this.getFriendId(),
- 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,
- friendId: this.getFriendId(),
- isMy: isMy,
- showTime: time.ifShowTime,
- time: time.timeStr,
- timestamp: currentTimestamp,
- type: type,
- content: content,
- headUrl: isMy ? this._myHeadUrl : this._otherHeadUrl,
- sendStatus: 'success',
- voiceDuration: duration,
- isPlaying: false,
- };
- obj.saveKey = obj.friendId + '_' + obj.msgId;
- return obj;
- }
- static createCustomChatItem() {
- return {
- timestamp: Date.now(),
- type: IMOperator.CustomType,
- content: '会话已关闭'
- }
- }
- }
|