forked from unmagic/wechat-im
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
文件类型消息管理继承FileManager基类,统一管理发送、显示及重发
- Loading branch information
liubiao
committed
Sep 7, 2018
1 parent
ad64dea
commit 01b3a22
Showing
4 changed files
with
100 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({}) { | ||
//文件的重发在商业版中已经实现,开源版中需要你自行实现 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters