一、准备
1.准备
2.utils.js - - 获取时间的配置文件
function formatTime(date) {
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
function formatNumber(n) {
n = n.toString()
return n[1] ? n : '0' + n
}
module.exports = {
formatTime: formatTime
}
1.index.js
var util=require("../../utils/util.js")
const db=wx.cloud.database()
const chatList=db.collection('chatList')
Page({
data: {
InputBottom: 0,
chatList:[
{
photoUrl:'https://ossweb-img.qq.com/images/lol/web201310/skin/big107000.jpg',
messageData:'内容1',
time:'2018年3月23日 13:24',
isMe:true,
},
{
photoUrl:'https://ossweb-img.qq.com/images/lol/web201310/skin/big107000.jpg',
messageData:'内容2',
time:'2018年3月23日 13:24',
isMe:false,
}
],
message:'',
},
onLoad(){
var that=this
chatList.get({
success:function(res){
var length=res.data.length
var message=res.data[length-1]
console.log("sabdabsn"+message)
that.setData({
chatList:message
})
}
})
},
InputFocus(e) {
this.setData({
InputBottom: e.detail.height
})
},
InputBlur(e) {
this.setData({
InputBottom: 0
})
},
message(e){
this.setData({
message:e.detail.value
})
},
cleanInput() {
var setMessage = { sendInfo: this.data.message }
this.setData(setMessage)
},
send(){
var index = this.data.chatList.length;
var inputmessage=this.data.message.trim();
var nowtime=util.formatTime(new Date())
this.setData({
[`chatList[${index}].messageData`]:inputmessage,
[`chatList[${index}].photoUrl`]:'https://ossweb-img.qq.com/images/lol/web201310/skin/big107000.jpg',
[`chatList[${index}].time`]:nowtime,
[`chatList[${index}].isMe`]:true,
});
const chatListData=this.data.chatList;
chatList.add({
data: {
...chatListData
},
success: function(res) {
console.log(res)
}
})
},
})
2.index.wxml
<!-- <cu-custom bgColor="bg-gradual-pink" isBack="{{true}}"><view slot="backText">返回</view><view slot="content">聊天</view></cu-custom> -->
<!-- 使用wx:for="{{chatList}}"进行循环,chatList是在index.js的data中定义的数组-->
<view class="cu-chat" wx:for="{{chatList}}" wx:key="unique">
<!-- 自己发送的信息 -->
<view class="cu-item self" wx:if="{{item.isMe}}">
<!-- 发送的内容 -->
<view class="main">
<view class="content bg-green shadow">
<text>{{item.messageData}}</text>
</view>
</view>
<!-- 自己的头像 -->
<view class="cu-avatar radius" style="background-image:url({{item.photoUrl}});"></view>
<!-- 发送的时间 -->
<view class="date">{{item.time}}</view>
</view>
<!-- 别人发送的信息 -->
<view class="cu-item" wx:if="{{!item.isMe}}">
<!-- 对方的头像 -->
<view class="cu-avatar radius" style="background-image:url({{item.photoUrl}});"></view>
<!-- 发送的内容 -->
<view class="main">
<view class="content shadow">
<text>{{item.messageData}}</text>
</view>
</view>
<!-- 发送的时间 -->
<view class="date ">{{item.time}}</view>
</view>
</view>
<!-- 底部 -->
<form bindreset="cleanInput">
<view class="cu-bar foot input {{InputBottom!=0?'cur':''}}" style="bottom:{{InputBottom}}px">
<!--cuIcon-sound声音小图标-->
<view class="action">
<text class="cuIcon-sound text-grey"></text>
</view>
<!-- 输入框,bindinput="message"绑定多行文本输入框到index.js的data中的message属性中 -->
<input class="solid-bottom" placeholder="输入消息" bindfocus="InputFocus" bindblur="InputBlur" adjust-position="{{false}}" focus="{{false}}" maxlength="300" cursor-spacing="10"
bindinput="message">
</input>
<!-- cuIcon-emojifill表情小图标 -->
<view class="action">
<text class="cuIcon-emojifill text-grey"></text>
</view>
<!-- 发送按钮,bindtap="send" -->
<button class="cu-btn bg-green shadow" bindtap="send" formType="reset">发送</button>
</view>
</form>
3.index.wxss
page{
padding-bottom: 100rpx;
}