這篇文章主要介紹了微信小程序如何實現簡單聊天室的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇微信小程序如何實現簡單聊天室文章都會有所收獲,下面我們一起來看看吧。
cha.js
// pages/chat/chat.js
// 獲取小程序實例
let app = getApp();
Page({
/**
* 頁面的初始數據
*/
data: {
nickname:"",
avatar:"",
chatlists:[
{
nickname:"小林",
avatar:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1591617971938&di=30d9f3b49a0d1b27fb4b61ea424f82c9&imgtype=0&src=http%3A%2F%2Fa-ssl.duitang.com%2Fuploads%2Fitem%2F201610%2F07%2F20161007135058_nUxji.jpeg",
content:`你好呀!`
}
],
invalue:""
},
sendMsg:function(){
let _this = this;
let obj = {
nickname:_this.data.nickname,
avatar:_this.data.avatar,
content:_this.data.invalue
};
let arr = _this.data.chatlists;
arr.push(obj)
_this.setData({
chatlists:arr,
invalue:""
});
// 把聊天內容發送到服務器,處理完成后返回,再把返回的數據放到chatlist里面
},
getInput:function(e){
console.log(e.detail.value);
this.setData({invalue:e.detail.value});
},
/**
* 生命周期函數--監聽頁面加載
*/
onLoad: function (options) {
console.log(app.globalData.userInfo.nickName);
this.setData({
nickname:app.globalData.userInfo.nickName,
avatar:app.globalData.userInfo.avatarUrl
});
},
/**
* 生命周期函數--監聽頁面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函數--監聽頁面顯示
*/
onShow: function () {
},
/**
* 生命周期函數--監聽頁面隱藏
*/
onHide: function () {
},
/**
* 生命周期函數--監聽頁面卸載
*/
onUnload: function () {
},
/**
* 頁面相關事件處理函數--監聽用戶下拉動作
*/
onPullDownRefresh: function () {
},
/**
* 頁面上拉觸底事件的處理函數
*/
onReachBottom: function () {
},
/**
* 用戶點擊右上角分享
*/
onShareAppMessage: function () {
}
})chat.wxml
<block wx:for="{{chatlists}}" wx:for-index="ind" wx:for-item="chat" wx:key="ind">
<view class="chat self" wx:if="{{nickname == chat.nickname}}">
<view class="right">
<view class="content">
{{chat.content}}
</view>
</view>
<view class="left">
<image class="avatar" src="{{chat.avatar}}"></image>
</view>
</view>
<view class="chat" wx:else>
<view class="left">
<image class="avatar" src="{{chat.avatar}}"></image>
</view>
<view class="right">
<view class="nickname">{{chat.nickname}}</view>
<view class="content">
{{chat.content}}
</view>
</view>
</view>
</block>
<view class="form">
<view class="weui-cell weui-cell_input">
<input class="weui-input" value="{{invalue}}" bindinput="getInput" placeholder="請輸入聊天內容" />
</view>
<button type="primary" bindtap="sendMsg">發送</button>
</view>chat.css
/* pages/chat/chat.wxss */
.avatar{
width: 130rpx;
height: 130rpx;
border-radius: 50%;
}
.chat{
display: flex;
align-items: center;
margin-top: 15px;
}
.self{
justify-content: flex-end;
margin-top: 15px;
}
.left{
padding: 20rpx;
align-self: flex-start;
}
.self .left{
padding-top: 0;
}
.right{
margin-left: 10px;
}
.right .content{
background-color: #eee;
color: #123;
font-size: 16px;
/* border:1px solid #ddd; */
padding: 10px;
line-height: 26px;
margin-right: 10px;
border-radius: 3px;
position: relative;
min-height: 20px;
}
.right .content::before{
content: " ";
display: block;
width: 0;
height: 0;
border: 12px solid transparent;
border-right-color:#eee;
position: absolute;
top: 10px;
left: -23px;
}
.self .right .content::before{
border: 0;
}
.self .right .content::after{
content: " ";
display: block;
width: 0;
height: 0;
border: 12px solid transparent;
border-left-color:#1ad409;
position: absolute;
top: 10px;
right: -23px;
}
.self .right .content{
background-color: #1ad409;
}
.form{
position: fixed;
bottom: 0;
background-color: #eee;
width: 750rpx;
display: flex;
height: 39px;
align-items: center;
}
.form input{
width: 600rpx;
background-color: #fff;
height: 36px;
line-height: 36px;
padding: 0 5px;
}
button{
width:65rpx;
height:36px;
}關于“微信小程序如何實現簡單聊天室”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“微信小程序如何實現簡單聊天室”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。