微信小程序实现聊天界面发送功能(示例代码)

 更新时间:2024年07月30日 11:48:07   作者:令人作呕的溏心蛋  
这篇文章主要介绍了微信小程序实现聊天界面发送功能,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

.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 = this
    that.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 = this
    app.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.chatList
    list.push(message)
    this.setData({
      chatList: list,
      inputValue: ''
    })
  }
})

.wxss

page {
  background-color: #f4f5f7;
}

.json

{
  "usingComponents": {},
  "navigationBarTitleText": ""
}

到此这篇关于微信小程序实现聊天界面,发送功能的文章就介绍到这了,更多相关小程序聊天发送功能内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • js 采用delete实现继承示例代码

    js 采用delete实现继承示例代码

    这篇文章主要介绍了js如何采用delete实现所谓的继承,下面有个不错的示例,大家可以参考下
    2014-05-05
  • JavaScript实现文本目标字符替换和一键全部替换

    JavaScript实现文本目标字符替换和一键全部替换

    这篇文章主要介绍了JavaScript实现文本目标字符替换和一键全部替换,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-06-06
  • JS 事件延迟执行说明分析

    JS 事件延迟执行说明分析

    为了避免用户鼠标无意识划过,而触发事件。浪费客户端资源。
    2010-05-05
  • JavaScript如何将base64图片转化为URL格式

    JavaScript如何将base64图片转化为URL格式

    这篇文章主要给大家介绍了关于JavaScript如何将base64图片转化为URL格式的相关资料,Base64是一种编码方式,而不是真正的加密方式,即使算Base64也仅用作一个简单的加密来保护某些数据,而真正的加密通常都比较繁琐,需要的朋友可以参考下
    2023-07-07
  • 详解webpack 最简打包结果分析

    详解webpack 最简打包结果分析

    这篇文章主要介绍了详解webpack 最简打包结果分析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-02-02
  • postMessage的两种使用方式

    postMessage的两种使用方式

    这篇文章主要介绍了使用postMessage实现iframe跨域通信,第一种使用postMessage在irfame中实现跨域数据传递,第二种使用postMessage在window.open()中的使用,本文结合示例代码给大家详细讲解,需要的朋友跟随小编一起看看吧
    2022-03-03
  • JS实现固定在右下角可展开收缩DIV层的方法

    JS实现固定在右下角可展开收缩DIV层的方法

    这篇文章主要介绍了JS实现固定在右下角可展开收缩DIV层的方法,右下角的div层可实现收缩与展开的功能,非常具有实用价值,需要的朋友可以参考下
    2015-02-02
  • JavaScript如何实现图片懒加载(lazyload) 提高用户体验(增强版)

    JavaScript如何实现图片懒加载(lazyload) 提高用户体验(增强版)

    这篇文章主要介绍了JavaScript如何实现图片懒加载(lazyload) 提高用户体验(增强版)的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-11-11
  • javascript实现禁止鼠标滚轮事件

    javascript实现禁止鼠标滚轮事件

    这篇文章主要介绍了javascript实现禁止鼠标滚轮事件的相关资料,需要的朋友可以参考下
    2015-07-07
  • 详解使用Next.js构建服务端渲染应用

    详解使用Next.js构建服务端渲染应用

    这篇文章主要介绍了详解使用Next.js构建服务端渲染应用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07

最新评论