微信小程序?qū)崿F(xiàn)聊天對(duì)話(文本、圖片)功能
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)聊天對(duì)話功能的具體代碼,供大家參考,具體內(nèi)容如下
這是我實(shí)際項(xiàng)目線上的代碼, 或許有些不足 || 和你的需求不符合。
上圖:
to_news.wxml
<!--pages/index/to_news/to_news.wxml--> <view class='tab'> <view class='lan'>{{tabdata.title}}</view> <view class='tent'> <text>{{tabdata.attribute_attribute}}</text> <text class='fl_r '>{{tabdata.num}}</text> </view> <view class='xiahuaxian1'></view> <view> <text class='fabu'>發(fā)布時(shí)間: {{tabdata.time_agree}}</text> </view> </view> <view class='news'> <view class='xiahuaxian1 xiahuaxia'></view> <view class='new_top_txt'>您正在與{{tabdata.nickname}}進(jìn)行溝通</view> <view class="historycon"> <scroll-view scroll-y="true" scroll-top="{{scrollTop}}" class="history" wx:for="{{centendata}}" wx:key=''> <view> <text class='time'>{{item.time}}</text> </view> <block wx:if="{{item.is_show_right ==1}}"> <view class='my_right'> <view class='page_row'> <text wx:if='{{!item.is_img}}' class='new_txt'>{{item.content}}</text> <image wx:if='{{item.is_img}}' src='http://sz800800.cn/Uploads/{{item.content}}' class='new_imgtent'></image> <view wx:if='{{!item.is_img}}' class='sanjiao my'></view> <image class='new_img' wx:if='{{item.show_rignt == "是自己的內(nèi)容,顯示在右邊,右邊渲染 nickname_owner,head_owner"}}' src='{{item.head_owner}}'></image> <image class='new_img' wx:if='{{item.show_rignt == "是自己的內(nèi)容,顯示在右邊,右邊渲染 nickname_open,head_open"}}' src='{{item.head_open}}'></image> </view> </view> </block> <block wx:else> <view class='you_left'> <view class='page_row'> <image class='new_img' wx:if='{{item.show_rignt == "不是自己的內(nèi)容,顯示在左邊,左邊渲染 nickname_owner,head_owner"}}' src='{{item.head_owner}}'></image> <image class='new_img' wx:if='{{item.show_rignt == "不是自己的內(nèi)容,顯示在左邊,左邊渲染 nickname_open,head_open"}}' src='{{item.head_open}}'></image> <view wx:if='{{!item.is_img}}' class='sanjiao you'></view> <text wx:if='{{!item.is_img}}' class='new_txt'>{{item.content}}</text> <image wx:if='{{item.is_img}}' src='http://sz800800.cn/Uploads/{{item.content}}' class='new_imgtent'></image> </view> </view> </block> </scroll-view> </view> </view> <view class='hei' id="hei"></view> <view class="sendmessage"> <input type="emoji" bindinput="bindChange" confirm-type="done" value='{{news_input_val}}' placeholder="" /> <button catchtap="add">發(fā)送</button> <input style='display:none' type="" bindinput="bindChange" confirm-type="done" placeholder="" /> <image bindtap="upimg1" class='jia_img' src='../../../images/jia_img.png'></image> </view>
to_news.js
// pages/index/to_news/to_news.js var app = getApp(); var util = require("../../../utils/util.js") var message = ''; var text = ''; var user = {}; var length; var zx_info_id; var openid_talk; Page({ data: { news: '', scrollTop: 0, message: '', text: text, centendata: '', nickName: '', avatarUrl: '', news_input_val:'', tabdata: '' }, bindChange: function (e) { message = e.detail.value }, //事件處理函數(shù) add: function (e) { var that = this var data = { program_id: app.jtappid, openid: app._openid, zx_info_id: zx_info_id, content: message, openid_talk:openid_talk } util.request('pg.php/ZXinfo/session_submit', 'post', data, '正在加載數(shù)據(jù)', function (res) { if (res.data.state == 1) { var a = true; that.loaddata(a); that.setData({ news_input_val:'' }) message = '' } else { wx.showToast({ title: '網(wǎng)絡(luò)錯(cuò)誤,請(qǐng)稍后', }) } }) }, onLoad: function (options) { openid_talk = options.openid_talk; zx_info_id = options.zx_info_id; console.log(openid_talk) //調(diào)用應(yīng)用實(shí)例的方法獲取全局?jǐn)?shù)據(jù) this.setData({ zx_info_id: zx_info_id, nickName: app.nickName, avatarUrl: app.avatarUrl, }); this.loaddata() }, // 頁(yè)面加載 loaddata: function (a) { var that = this; var is_img = true; var data = { program_id: app.jtappid, openid: app._openid, zx_info_id: zx_info_id, openid_talk: openid_talk } util.request('pg.php/ZXinfo/session_page', 'post', data, '', function (res) { if (res.data.k1) { res.data.k1.time_agree = util.js_date_time(res.data.k1.time_agree) } for (var i = 0; i < res.data.k2.length; i++) { res.data.k2[i].time = util.js_date_time(res.data.k2[i].time) var n = res.data.k2[i].content.charAt(res.data.k2[i].content.length - 1); switch (n) { case 'g': res.data.k2[i].is_img = is_img break; default: } } that.setData({ tabdata: res.data.k1, centendata: res.data.k2.reverse() }) wx.setNavigationBarTitle({ title: that.data.tabdata.nickname }); if (a) { setTimeout(function () { that.bottom() }, 500); } }) setTimeout(function () { if (that.data.centendata.length != length) { length = that.data.centendata.length } that.loaddata() }, 3000); }, // 獲取hei的id節(jié)點(diǎn)然后屏幕焦點(diǎn)調(diào)轉(zhuǎn)到這個(gè)節(jié)點(diǎn) bottom: function () { var query = wx.createSelectorQuery() query.select('#hei').boundingClientRect() query.selectViewport().scrollOffset() query.exec(function (res) { wx.pageScrollTo({ scrollTop: res[0].bottom // #the-id節(jié)點(diǎn)的下邊界坐標(biāo) }) res[1].scrollTop // 顯示區(qū)域的豎直滾動(dòng)位置 }) }, // 選擇圖片并把圖片保存 upimg1: function () { var that = this; wx.chooseImage({ success: function (res) { var data = { program_id: app.jtappid, openid: app._openid, zx_info_id: zx_info_id, } var tempFilePaths = res.tempFilePaths wx.uploadFile({ url: 'pg.php/ZXinfo/session_submit', //提交信息至后臺(tái) filePath: tempFilePaths[0], name: 'content', //文件對(duì)應(yīng)的參數(shù)名字(key) formData: data, //其它的表單信息 success: function (res) { var a = true; that.loaddata(a); message = '' } }) } }) } })
to_news.wxss
/* pages/index/to_news/to_news.wxss */ page { background-color: #f7f7f7; } .tab { padding: 20rpx 20rpx 40rpx 50rpx; height: 20%; background-color: white; } .tab .tent { font-size: 33rpx; margin-bottom: 30rpx; } .jia_img{ height: 80rpx; width: 90rpx; } .new_imgtent{ height: 180rpx; width: 190rpx; } .tab .fabu { font-size: 33rpx; margin-top: 30rpx; margin-bottom: 30rpx; } .xiahuaxia { width: 80%; text-align: center; margin: 0 auto; position: relative; top: 60rpx; } .time { text-align: center; padding: 5rpx 20rpx 5rpx 20rpx; width: 200rpx; font-size: 26rpx; background-color: #E8E8E8; } .new_top_txt { width: 50%; position: relative; top: 38rpx; text-align: center; margin: 0 auto; font-size: 30rpx; color: #787878; background-color: #f7f7f7; } /* 聊天內(nèi)容 */ .news { margin-top: 30rpx; text-align: center; margin-bottom: 150rpx; } .img_null { height: 60rpx; } .l { height: 5rpx; width: 20%; margin-top: 30rpx; color: #000; } /* 聊天 */ .my_right { float: right; position: relative; right: 40rpx; } .you_left { float: left; position: relative; left: 5rpx; } .new_img { width: 100rpx; height: 100rpx; border-radius: 50%; } .new_txt { width: 380rpx; border-radius: 7px; background-color: #95d4ff; padding: 0rpx 30rpx 0rpx 30rpx; } .sanjiao { top: 20rpx; position: relative; width: 0px; height: 0px; border-width: 10px; border-style: solid; } .my { border-color: transparent transparent transparent #95d4ff; } .you { border-color: transparent #95d4ff transparent transparent; } .sendmessage { background-color: white; width: 100%; position: fixed; bottom: 0rpx; display: flex; flex-direction: row; } .sendmessage input { width: 80%; height: 40px; background-color: white; line-height: 40px; font-size: 14px; border: 1px solid #d0d0d0; padding-left: 10px; } .sendmessage button { border: 1px solid white; width: 18%; height: 40px; background: #fff; color: #000; line-height: 40px; font-size: 14px; } .historycon { height: 90%; width: 100%; flex-direction: column; display: flex; margin-top: 100rpx; border-top: 0px; } .hei{ margin-top: 50px; height: 20rpx; } .history { height: 100%; margin-top: 15px; margin: 10px; font-size: 14px; line-height: 40px; word-break: break-all; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序websocket聊天室的實(shí)現(xiàn)示例代碼
- 微信小程序websocket實(shí)現(xiàn)聊天功能
- 微信小程序WebSocket實(shí)現(xiàn)聊天對(duì)話功能
- 微信小程序?qū)崟r(shí)聊天WebSocket
- 詳解微信小程序?qū)崿F(xiàn)仿微信聊天界面(各種細(xì)節(jié)處理)
- 微信小程序?qū)崿F(xiàn)即時(shí)通信聊天功能的實(shí)例代碼
- 使用node.js實(shí)現(xiàn)微信小程序?qū)崟r(shí)聊天功能
- 微信小程序與AspNetCore SignalR聊天實(shí)例代碼
- 微信小程序 聊天室簡(jiǎn)單實(shí)現(xiàn)
- 微信小程序websocket實(shí)現(xiàn)即時(shí)聊天功能
相關(guān)文章
js網(wǎng)頁(yè)側(cè)邊隨頁(yè)面滾動(dòng)廣告效果實(shí)現(xiàn)
其實(shí)這個(gè)效果不是什么難實(shí)現(xiàn)的效果,關(guān)鍵注意幾個(gè)地方就可以了2011-04-04詳細(xì)講解js實(shí)現(xiàn)電梯導(dǎo)航的實(shí)例
對(duì)于某一個(gè)頁(yè)面內(nèi)容繁多,如果我們滾動(dòng)的時(shí)間較長(zhǎng),為了增加用戶體驗(yàn),我們需要實(shí)現(xiàn)點(diǎn)擊某一個(gè)按鈕,然后滾動(dòng)到對(duì)應(yīng)的區(qū)域,滾動(dòng)的時(shí)候,右側(cè)對(duì)應(yīng)的分類實(shí)現(xiàn)高亮,所以本文給大家詳細(xì)介紹講解了js實(shí)現(xiàn)電梯導(dǎo)航,需要的朋友可以參考下2023-10-10分享經(jīng)典的JavaScript開(kāi)發(fā)技巧
這篇文章向大家分享了40多個(gè)經(jīng)典的JavaScript開(kāi)發(fā)技巧,相信讀完這篇文章對(duì)大家開(kāi)發(fā)javascript有很大的幫助作用,感興趣的小伙伴們可以參考一下2015-11-11微信小程序 高德地圖路線規(guī)劃實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了微信小程序 路線規(guī)劃實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08動(dòng)態(tài)添加option及createElement使用示例
動(dòng)態(tài)添加option在某些特殊的情況下還是比較實(shí)用的,本文有個(gè)小示例為大家介紹下createElement使用,感興趣的朋友可以參考下2014-01-01javascript獲取不重復(fù)的隨機(jī)數(shù)的方法比較
js永不重復(fù)隨機(jī)數(shù)實(shí)現(xiàn)代碼比較2008-09-09詳解JavaScript中扁平與樹(shù)形數(shù)據(jù)的轉(zhuǎn)換
這篇文章主要為大家想介紹了JavaScript中實(shí)現(xiàn)扁平與樹(shù)形數(shù)據(jù)相互轉(zhuǎn)換的方法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)JavaScript有一定的幫助,需要的可以參考一下2023-01-01