当前位置: 首页 > news >正文

微信小程序实现聊天界面,发送功能

在这里插入图片描述

.wxml

<scroll-view scroll-y="true" style="height: {{windowHeight}}px;"><view wx:for="{{chatList}}" wx:for-index="index" wx:for-item="item" style="padding-top:{{index==0?30:0}}rpx"><!-- 左边:对方用户 --><view style="display: flex; align-items: flex-start; margin-bottom: 10px;padding: 10rpx 20rpx;" wx:if="{{item.id != userInfo.id}}"><view><image src="{{item.url}}" style="width: 35px; height: 35px; border-radius: 10px;"></image></view><view style="padding: 15rpx 20rpx;border-radius: 0 10px 10px 10px;background-color: #ffffff; margin-left: 10rpx; display: inline-block;"><text>{{item.content}}</text></view></view><!-- 右边:当前用户 --><view style="display: flex; align-items: flex-start; justify-content: flex-end;padding: 10rpx 20rpx;" wx:else><view style="padding: 15rpx 20rpx; background-color: #7e66f6; border-radius: 10px 0 10px 10px; margin-right: 10rpx;color: #ffffff;"><text>{{item.content}}</text></view><view><image src="{{item.url}}" style="width: 35px; height: 35px; border-radius: 10px;"></image></view></view></view><!-- 底部输入框及发送按钮 --><view style="position: fixed; bottom: {{bottomJP}}px; width: 100%; background-color: #fff; padding:  0 10rpx; box-shadow: 0 -2px 4px rgba(202, 202, 202, 0.1);height: 130rpx;display: flex;justify-content: space-around;align-items: center;padding-bottom: 30rpx;"><image src="/images/yuyin.png" style="width: 30px; height: 30px; border-radius: 10px;"></image><input style="height: 20rpx;width: 60%; padding: 15rpx; border: 1px solid rgb(247, 245, 245); border-radius: 10rpx;" 	confirm-type="send" adjust-position="{{false}}" bindfocus="getTelIptHeight" bindblur="getTelIptHeight" bindconfirm="sendContent" value="{{inputValue}}"/><image src="/images/biaoqing.png" style="width: 30px; height: 30px; border-radius: 10px;"></image><image src="/images/tupian.png" style="width: 30px; height: 30px; border-radius: 10px;margin-right: 20rpx;"></image><!-- <button style="width: 150rpx; height: 80rpx; background-color: #007bff; color: #fff; border: none; border-radius: 10px; margin-left: 10rpx;">发送</button> --></view>
</scroll-view>

.js

const app = getApp()
Page({/*** 页面的初始数据*/data: {tips: null,windowHeight: 0,windowWidtth: 0,bottomJP: 0,userInfo: {},inputValue: '',chatList: [{createTime: '2024-07-24 09:10:00',url: '/images/baochang.png',content: '哈哈哈',id: 8},{createTime: '2024-07-24 11:15:30',url: '/images/baochang.png',content: '你好啊',id: 9},{createTime: '2024-07-24 11:15:30',url: '/images/baochang.png',content: '你好啊',id: 9},]},getTelIptHeight(e) {console.log("height---------", e);if (e.type == 'blur') {this.setData({bottomJP: 0})} else {this.setData({bottomJP: e.detail.height})}},onLoad(options) {let that = thisthat.setData({userInfo: wx.getStorageSync("userInfo")})wx.getSystemInfo({success: function (res) {console.log(res)that.setData({windowHeight: res.windowHeight,windowWidtth: res.windowWidth,});}});wx.setNavigationBarTitle({title: '动态获取用户昵称',})},send() {let info = {senderId: wx.getStorageSync("userInfo").id,recipientId: 100,chatContent: '你好啊~',chatContentType: 'text'}let that = thisapp.globalData.ws.send({data: JSON.stringify(info),success: (res) => {console.log(res)that.setData({tips: "发送信息成功"})}})},sendContent(e) {let message = {createTime: '2024-07-24 11:15:30',url: '/images/baochang.png',content: e.detail.value,id: 9}let list = this.data.chatListlist.push(message)this.setData({chatList: list,inputValue: ''})}
})

.wxss

page {background-color: #f4f5f7;
}

.json

{"usingComponents": {},"navigationBarTitleText": ""
}
http://www.lryc.cn/news/408945.html

相关文章:

  • 【强化学习的数学原理】课程笔记--5(值函数近似,策略梯度方法)
  • 前端Long类型精度丢失:后端处理策略
  • C++ | Leetcode C++题解之第300题最长递增子序列
  • springboo 整合 redis
  • dpdk编译安装以及接收udp报文(基于ubuntu)
  • 【计算机网络】OSPF单区域实验
  • Java聚合快递小程序对接云洋系统程序app源码
  • 【React】详解组件通信:从基础到进阶的全面指南
  • 【vluhub】zabbix漏洞
  • openGauss触发器详解
  • 抄作业-跟着《React通关秘籍》捣鼓React-playground-上集
  • 80后最后的书信 年代
  • 软考-软件设计师(4)-计算机网络与安全:OSI七层、子网划分、网络安全控制技术、网络安全协议、网络安全威胁、对称与非对称加密等高频考点
  • Unity横板动作游戏 -为什么我又开始学习Unity,而不是Godot。
  • 什么是NIO
  • PHP switch 替代品 match
  • FastAPI(七十四)实战开发《在线课程学习系统》接口开发-- 删除留言
  • 面试重点---快速排序
  • [MIT6.5840]MapReduce
  • 【系统架构设计师】计算机组成与体系结构 ⑯ ( 奇偶校验码 | CRC 循环冗余码 | 海明码 | 模 2 除法 )
  • springboot,service 层统一异常抛出时,throws Exception写在接口上还是实现类上
  • 深度学习高效性网络
  • PyQt ERROR:ModuleNotFoundError: No module named ‘matplotlib‘
  • Flutter Geolocator插件使用指南:获取和监听地理位置
  • 网站基本布局CSS
  • ssm框架整合,异常处理器和拦截器(纯注解开发)
  • 古籍双层PDF制作教程:保姆级古籍数字化教程
  • Git 删除 远端的分支
  • PrgogressBar实现原理分析
  • 【HarmonyOS】HarmonyOS NEXT学习日记:七、页面与组件的生命周期