01. uni-app 普通模式可以支持 app、小程序、H5(H5端不支持语音功能);
02. nvue 因全局变量模式问题暂不支持;
1、支持文字、图片、语音三种模式;
2、语音连续播放、暂停
3、历史消息加载
4、录音
5、图片选择、图片预览
2019-05-07 IM 组件大幅度优化升级
1. 底层优化消息展示及消息输入组件; 2. 演示程序真正连接 websocket 服务 3. 完整演示好友列表、聊天功能;
演示说明
演示代码使用 vuex 保证全局只连接一次 websocket 服务,功能比较全面;
如何获取完整演示代码?
请联系您购买时的客服QQ邀请您加入付费用户群,在群文件内下载;
因底层支持不够暂不支持 weex 版本,请使用混合开发模式利用 uni-app 普通模式实现 app 端的即时通讯功能 (:
.wxml
<graceIMMsg
msgs="{{msgs}}"
userid="{{imuserid}}">
</graceIMMsg>
<!-- 消息提交区 -->
<graceIMFooter
bind:sendTextMsg="sendTextMsg"
bind:sendImgMsg="sendImgMsg"
bind:sendVoiceMsg="sendVoiceMsg">
</graceIMFooter>.wxss
page{background:#F6F7F8;}.js
var msgPage = 1, _self;
Page({
data: {
msgs : [],
imuserid : 1002
},
onLoad: function (options) {
_self = this;
//发个系统消息
var msg = {
msg: "欢迎使用 graceUI IM ... 下拉加载历史消息 ^_^", // 消息内容
ctype: 4, // 消息类型 [ 1. 文本类型 2. 图片类型 3. 语音类型 4.系统通知 ]
date: this.getNowDate()
}
// 调用接收消息函数 展示消息
this.receiveMsg(msg);
},
// 下拉加载历史
onPullDownRefresh:function(){
this.getHistoryMsg();
},
// 加载历史消息函数
getHistoryMsg : function(){
// 这里可以连接服务器获取历史消息
// 模拟实现
if(msgPage > 1){
wx.stopPullDownRefresh();
wx.showToast({title: '已经加载全部历史', icon:"none"});
return false;
}
setTimeout(function(){
var historyMsg = [{
id: 100,
name: "上官婉",
face: "https://graceui.oss-cn-beijing.aliyuncs.com/faces/1.png",
ctype: 1,
msg: "有道说此生所求不过翻云覆雨厮守",
date: _self.getNowDate()
}, {
id: 100,
name: "公孙燕",
face: "https://graceui.oss-cn-beijing.aliyuncs.com/faces/2.png",
ctype: 2,
msg: "https://graceui.oss-cn-beijing.aliyuncs.com/swiperimgs/1.png",
date: _self.getNowDate()
}];
for(var i = 0; i < historyMsg.length; i++){
_self.data.msgs.unshift(historyMsg[i]);
}
_self.setData({msgs : _self.data.msgs});
console.log(_self.data.msgs)
wx.stopPullDownRefresh();
msgPage++;
}, 1000);
},
//接收消息函数
receiveMsg: function (msg) {
this.data.msgs.push(msg);
this.setData({ msgs: this.data.msgs });
// 滚动条处理
setTimeout(function () { wx.pageScrollTo({ scrollTop: 999999 }); }, 500);
},
// 发送文本消息
sendTextMsg : function(e){
var content = e.detail;
// 此处已经获取到提交的文本,向 socket 服务器发送消息即可
// 服务器会回复一个消息到当前链接
// 模拟接收到一个消息
var msg = {
id : 1000, //用户id
name : "graceUI 网友", // 昵称
face: "https://graceui.oss-cn-beijing.aliyuncs.com/faces/1.png", // 用户头像
msg : content, // 消息内容
ctype: 1, // 消息类型 [ 1. 文本类型 2. 图片类型 3. 语音类型 4. 系统通知 ]
date: this.getNowDate()
}
// 调用接收消息函数 展示消息
this.receiveMsg(msg);
},
// 发送图片消息
sendImgMsg : function(e){
var imgUrl = e.detail;
// 此处已经获取到选择的临时图片,上传图片[ 与服务器交互完成 ]
// 服务器会回复一个消息到当前链接
// 模拟接收到一个消息
var msg = {
id: 1000, //用户id
name: "graceUI 网友", // 昵称
face: "https://graceui.oss-cn-beijing.aliyuncs.com/faces/2.png", // 用户头像
msg: imgUrl, // 图片文件路径
ctype: 2, // 消息类型 [ 1. 文本类型 2. 图片类型 3. 语音类型 4. 系统通知 ]
date: this.getNowDate()
}
// 调用接收消息函数 展示消息
this.receiveMsg(msg);
},
// 发送语音消息
sendVoiceMsg: function (e) {
var voiceData = e.detail; // 数值格式 [音频文件临时路径 , 录音长度]
// 此处已经获取到录音文件临时地址,上传音频[ 与服务器交互完成 ]
// 服务器会回复一个消息到当前链接
// 模拟接收到一个消息
var msg = {
id: 1002, //用户id
name: "graceUI 网友", // 昵称
face: "https://graceui.oss-cn-beijing.aliyuncs.com/faces/1.png", // 用户头像
msg: voiceData[0], // 语音文件路径
len: voiceData[1], // 语音长度
ctype: 3, // 消息类型 [ 1. 文本类型 2. 图片类型 3. 语音类型 4. 系统通知 ]
date: this.getNowDate() // 消息时间
}
// 调用接收消息函数 展示消息
this.receiveMsg(msg);
},
// 获取当前日期
getNowDate : function () {
var date = new Date();
var sign1 = "-";
var sign2 = ":";
var year = date.getFullYear() // 年
var month = date.getMonth() + 1; // 月
var day = date.getDate(); // 日
var hour = date.getHours(); // 时
var minutes = date.getMinutes(); // 分
var seconds = date.getSeconds() //秒
if(month >= 1 && month <= 9) {month = "0" + month;}
if (day >= 0 && day <= 9) {day = "0" + day;}
if (hour >= 0 && hour <= 9) {hour = "0" + hour;}
if (minutes >= 0 && minutes <= 9){minutes = "0" + minutes;}
if (seconds >= 0 && seconds <= 9){seconds = "0" + seconds;}
var currentdate = year + sign1 + month + sign1 + day + " " + hour + sign2 + minutes + sign2 + seconds;
return currentdate;
}
});.json
{
"usingComponents":{
"graceIMMsg" : "../../graceUI/components/graceIMMsg",
"graceIMFooter": "../../graceUI/components/graceIMFooter"
},
"enablePullDownRefresh": true,
"backgroundTextStyle": "dark"
}