Skip to content

Commit

Permalink
文件类型消息管理继承FileManager基类,统一管理发送、显示及重发
Browse files Browse the repository at this point in the history
  • Loading branch information
liubiao committed Sep 7, 2018
1 parent ad64dea commit 01b3a22
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 120 deletions.
14 changes: 4 additions & 10 deletions pages/chat/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Page({
console.log(friend);
this.initData();
wx.setNavigationBarTitle({
title: friend.friendName
title: friend.friendName || ''
});
this.imOperator = new IMOperator(this, friend);
this.UI = new UI(this);
Expand Down Expand Up @@ -92,10 +92,10 @@ Page({
},

//模拟上传文件,注意这里的cbOk回调函数传入的参数应该是上传文件成功时返回的文件url,这里因为模拟,我直接用的savedFilePath
simulateUploadFile: function ({savedFilePath, duration, itemIndex}, cbOk) {
simulateUploadFile: function ({savedFilePath, duration, itemIndex, success, fail}) {
setTimeout(() => {
let urlFromServerWhenUploadSuccess = savedFilePath;
cbOk && cbOk(urlFromServerWhenUploadSuccess);
success && success(urlFromServerWhenUploadSuccess);
}, 1000);
},
extraButton: function () {
Expand Down Expand Up @@ -158,12 +158,6 @@ Page({
const itemIndex = parseInt(e.currentTarget.dataset.resendIndex);
const item = this.data.chatItems[itemIndex];
this.UI.updateDataWhenStartSending(item, false, false);
this.sendMsg(this.imOperator.createChatItemContent({
type: item.type,
content: item.content,
duration: item.voiceDuration
}), itemIndex, (msg) => {
this.UI.updateListViewBySort();
});
this.msgManager.resend({...item, itemIndex});
},
});
90 changes: 90 additions & 0 deletions pages/chat/msg-type/base/file-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import FileSaveManager from "../../file-save-manager";

export default class FileManager {

constructor(page) {
this._page = page;
}

/**
* 接收到消息时,通过UI类的管理进行渲染
* @param msg 接收到的消息,这个对象应是由 im-operator.js 中的createNormalChatItem()方法生成的。
*/
showMsg({msg}) {
const url = msg.content;
const localFilePath = FileSaveManager.get(msg);
if (!localFilePath) {
wx.downloadFile({
url,
success: res => {
// console.log('下载成功', res);
FileSaveManager.saveFileRule({
tempFilePath: res.tempFilePath,
success: (savedFilePath) => {
msg.content = savedFilePath;
this._page.UI && this._page.UI.updateViewWhenReceive(msg);
FileSaveManager.set(msg, savedFilePath);
},
fail: res => {
// console.log('存储失败', res);
this._page.UI && this._page.UI.updateViewWhenReceive(msg);
}
}
)
}
});
} else {
msg.content = localFilePath;
this._page.UI.updateViewWhenReceive(msg);
}
}

/**
* 发送文件类型消息
* @param type 消息类型
* @param content 由输入组件接收到的临时文件路径
* @param duration 由输入组件接收到的录音时间
*/
sendOneMsg({type, content, duration}) {
FileSaveManager.saveFileRule({
tempFilePath: content,
success: (savedFilePath) => {
this._sendFileMsg({content: savedFilePath, duration, type});
}, fail: res => {
this._sendFileMsg({content, type, duration});
}
});
}

_sendFileMsg({content, duration, type}) {
const temp = this._page.imOperator.createNormalChatItem({
type,
content,
duration
});
this._page.UI.showItemForMoment(temp, (itemIndex) => {
this.uploadFileAndSend({content, duration, itemIndex, type})
});
}

uploadFileAndSend({content, duration, type, itemIndex}) {
this._page.simulateUploadFile({
savedFilePath: content, duration, itemIndex,
success: (content) => {
this._page.sendMsg({
content: this._page.imOperator.createChatItemContent({type, content, duration}),
itemIndex,
success: (msg) => {
FileSaveManager.set(msg, content);
}
});
}, fail: () => {

}
});
}

resend({}) {
//文件的重发在商业版中已经实现,开源版中需要你自行实现
}
}
56 changes: 3 additions & 53 deletions pages/chat/msg-type/image-manager.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,13 @@
import {saveFileRule} from "../../../utils/file";
import IMOperator from "../im-operator";
import FileManager from "../file-manager";
import FileManager from "./base/file-manager";

export default class ImageManager {
export default class ImageManager extends FileManager {
constructor(page) {
this._page = page;
super(page);
this._page.imageClickEvent = function (e) {
wx.previewImage({
current: e.currentTarget.dataset.url, // 当前显示图片的http链接
urls: [e.currentTarget.dataset.url] // 需要预览的图片http链接列表
})
}
}

sendOneMsg(tempFilePath) {
saveFileRule(tempFilePath, (savedFilePath) => {
const temp = this._page.imOperator.createNormalChatItem({
type: IMOperator.ImageType,
content: savedFilePath
});
this._page.UI.showItemForMoment(temp, (itemIndex) => {
this._page.simulateUploadFile({savedFilePath, itemIndex}, (content) => {
this._page.sendMsg(this._page.imOperator.createChatItemContent({
type: IMOperator.ImageType,
content
}), itemIndex, (msg) => {
FileManager.set(msg, savedFilePath)
});
});
});
});
}

/**
* 接收到消息时,通过UI类的管理进行渲染
* @param msg 接收到的消息,这个对象应是由 im-operator.js 中的createNormalChatItem()方法生成的。
*/
showMsg({msg}) {
const url = msg.content;
const localImagePath = FileManager.get(msg);
console.log('本地图片路径', localImagePath);
if (!localImagePath) {
wx.downloadFile({
url,
success: res => {
saveFileRule(res.tempFilePath, (savedFilePath) => {
const temp = this._page.imOperator.createNormalChatItem({
type: IMOperator.ImageType,
content: savedFilePath
});
this._page.UI.updateViewWhenReceive(temp);
//以消息的content为key,建立消息和本地存储路径的映射关系
FileManager.set(msg, savedFilePath);
});
}
});
} else {
this._page.UI.updateViewWhenReceive(msg);
}
}

}
60 changes: 3 additions & 57 deletions pages/chat/msg-type/voice-manager.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {isVoiceRecordUseLatestVersion} from "../../../modules/chat-input/chat-input";
import {saveFileRule} from "../../../utils/file";
import IMOperator from "../im-operator";
import FileManager from "../file-manager";
import FileManager from "./base/file-manager";

export default class VoiceManager {
export default class VoiceManager extends FileManager{
constructor(page) {
this._page = page;
super(page);
this.isLatestVersion = isVoiceRecordUseLatestVersion();
//判断是否需要使用高版本语音播放接口
if (this.isLatestVersion) {
Expand All @@ -19,32 +18,6 @@ export default class VoiceManager {
}
}

/**
* 发送语音消息
* @param tempFilePath 由输入组件接收到的临时文件路径
* @param duration 由输入组件接收到的录音时间
*/
sendOneMsg(tempFilePath, duration) {
saveFileRule(tempFilePath, (savedFilePath) => {
const temp = this._page.imOperator.createNormalChatItem({
type: IMOperator.VoiceType,
content: savedFilePath,
duration
});
this._page.UI.showItemForMoment(temp, (itemIndex) => {
this._page.simulateUploadFile({savedFilePath, duration, itemIndex}, (content) => {
this._page.sendMsg(this._page.imOperator.createChatItemContent({
type: IMOperator.VoiceType,
content: content,
duration
}), itemIndex, (msg) => {
FileManager.set(msg, savedFilePath);
});
});
});
});
}

/**
* 停止播放所有语音
*/
Expand All @@ -64,33 +37,6 @@ export default class VoiceManager {
}
}

/**
* 接收到消息时,通过UI类的管理进行渲染
* @param msg 接收到的消息,这个对象应是由 im-operator.js 中的createNormalChatItem()方法生成的。
*/
showMsg({msg}) {
const url = msg.content;
const localVoicePath = FileManager.get(msg);
console.log('本地语音路径', localVoicePath);
if (!localVoicePath) {
wx.downloadFile({
url,
success: res => {
saveFileRule(res.tempFilePath, (savedFilePath) => {
const temp = this._page.imOperator.createNormalChatItem({
type: IMOperator.VoiceType,
content: savedFilePath
});
this._page.UI.updateViewWhenReceive(temp);
FileManager.set(msg, savedFilePath);
});
}
});
} else {
this._page.UI.updateViewWhenReceive(msg);
}
}

/**
* 停止播放 兼容低版本语音播放
* @private
Expand Down

0 comments on commit 01b3a22

Please sign in to comment.