侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

微信小程序聊天功能的示例代码

微信(小程序)  /  管理员 发布于 5年前   697

效果

初始化滚动条高度

var keyHeight = 0;

数据格式

const CHAT_DATA=[ {  type:0,//0客服1用户  content:'欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎欢迎',  headImg:'../../assets/common/images/headHortrait.jpeg',//头像  creatTime:'2019-01-01',//创建时间  contentType:'text' }, {  type: 0,//0客服1用户  content: '1111111',  headImg: '../../assets/common/images/headHortrait.jpeg',//头像  creatTime: '2019-01-01',//创建时间  contentType: 'text' }, {  type: 1,//0客服1用户  content: '222222',  headImg: '../../assets/common/images/headHortrait.jpeg',//头像  creatTime: '2019-01-01',//创建时间  contentType: 'text' }, {  type: 0,//0客服1用户  content: '333333',  headImg: '../../assets/common/images/headHortrait.jpeg',//头像  creatTime: '2019-01-01',//创建时间  contentType: 'text' }, {  type: 1,//0客服1用户  content: '4444444',  headImg: '../../assets/common/images/headHortrait.jpeg',//头像  creatTime: '2019-01-01',//创建时间  contentType: 'text', }, {  type: 0,//0客服1用户  content: 'http://tmp/wxc79c66d8b0ed19a8.o6zAJs6QE8L8FKq645ts4e3LoKzI.pGakaVHKmbQ3160aa57e2bf33cb576fbabf691cd890b.durationTime=3706.aac',  headImg: '../../assets/common/images/headHortrait.jpeg',//头像  creatTime: '2019-01-01',//创建时间  contentType: 'voice',  duration: '3706', },{  type: 1,//0客服1用户  content: 'http://tmp/wxc79c66d8b0ed19a8.o6zAJs6QE8L8FKq645ts4e3LoKzI.pGakaVHKmbQ3160aa57e2bf33cb576fbabf691cd890b.durationTime=3706.aac',  headImg: '../../assets/common/images/headHortrait.jpeg',//头像  creatTime: '2019-01-01',//创建时间  contentType: 'voice',  duration:'3706' }, {  type: 1,//0客服1用户  content: 'https://img.yzcdn.cn/vant/cat.jpeg',  headImg: '../../assets/common/images/headHortrait.jpeg',//头像  creatTime: '2019-01-01',//创建时间  contentType: 'img' }, {  type: 1,//0客服1用户  content: 'https://img.yzcdn.cn/vant/cat.jpeg',  headImg: '../../assets/common/images/headHortrait.jpeg',//头像  creatTime: '2019-01-01',//创建时间  contentType: 'img' }];

wxml对话框

                        {{item.content}}    {{item.duration}}s                      {{item.duration}}"    {{item.content}}                            

wxml底部输入框

                   按住请说话                       {{toastTitle}}

css

#page{ height: 90%;overflow-y: auto;}.content{ background: white;}.inputRoom { width: 100vw; /* height: 16vw; */ border-top: 1px solid #cdcdcd; position: fixed; bottom: 0; display: flex; align-items: center; z-index: 20; background: white; flex-direction: column;}.bottomRow{ width: 100%; height: 16vw;display: flex;align-items: center;flex-direction: row}.bottomRow .van-row{ width: 100%;}.emoji{ height: 30vw;}input {width: 90%;height: 9.33vw;background-color: #EEF4FA;border-radius: 6rpx;font-size: 28rpx;color: #444;padding: 0 3%;margin-left: 2%;}.leftMsg { font-size: 26rpx; color: #333333;line-height: 6vw;padding: 2vw 2.5vw;background-color: #EEF4FA; border-radius: 10rpx; z-index: 10;}.rightMsg { font-size: 26rpx; color: white; line-height: 6vw; padding: 2vw 2.5vw; background-color: #496DFF; border-radius: 10rpx; z-index: 10;}.chatFrame{ background: white; height: 100%} .icon{ line-height: 8vw;}.head{ display: flex; align-items: center}.headImg{ border-radius: 50%; width: 60rpx;height: 60rpx;}.holdTape{ width: 90%;height: 9.33vw;background-color: #EEF4FA;border-radius: 6rpx;padding: 0 3%;margin-left: 2%;display: flex;align-items: center;justify-content: center;}.recordDailog{ -webkit-transition-duration: 300ms;transition-duration: 300ms;z-index: 1000;position: fixed;top: 50%;left: 50%;width: -webkit-fit-content;width: fit-content;-webkit-transform: translate(-50%,-50%);transform: translate(-50%,-50%);max-width: var(--toast-max-width,70%);}.show{width: var(--toast-default-width,90px);min-height: var(--toast-default-min-height,90px);padding: var(--toast-default-padding,16px);display: flex;-webkit-flex-direction: column;flex-direction: column;-webkit-align-items: center;align-items: center;-webkit-justify-content: center;justify-content: center;box-sizing: initial;color: var(--toast-text-color,#fff);font-size: var(--toast-font-size,14px);line-height: var(--toast-line-height,20px);white-space: pre-wrap;word-wrap: break-word;background-color: var(--toast-background-color,rgba(50,50,51,.88));border-radius: var(--toast-border-radius,4px);}.show image{ width: 24px; height: 24px}image{ /* max-width: 88vw;max-height: 400rpx; */width: 100%}.img{background: none;width: 90%}.contentRight{ display: flex; justify-content: flex-end; padding: 2vw 2vw 2vw 11vw;width: 86%;}.contentLeft{ display: flex; padding: 2vw 11vw 2vw 2vw;width: 86%;}

js

// pages/contact/contact.jsconst { pageFunc} = require('../../utils/util.js');const app = getApp();var windowWidth = wx.getSystemInfoSync().windowWidth;var windowHeight = wx.getSystemInfoSync().windowHeight;var keyHeight = 0;const { CHAT_DATA}=require("../../data/customerService.js");import Toast from '../../components/vant/toast/toast';const recorderManager = wx.getRecorderManager();const innerAudioContext = wx.createInnerAudioContext();const db = wx.cloud.database();/** * 初始化数据 *//** * 计算msg总高度 */function calScrollHeight(that, keyHeight) { var query = wx.createSelectorQuery(); query.select('.scrollMsg').boundingClientRect(function(rect) { }).exec();}Page({ /**  * 页面的初始数据  */ data: {  scrollHeight: '100vh',  inputVal:"",  inputBottom: 0,  chatData:[],  show:true,  showDailog:false,  bottomHeight:"18vw",  sendData:{},  pagination: {   pageSize: 5,   currentPage: 1,   total: 0,  },  showEmoji:false,  toastTitle:"录音中...." }, /**  * 生命周期函数--监听页面加载  */ onLoad: function (options) {  // this.setData({  //  cusHeadIcon: app.globalData.userInfo.avatarUrl,  // });  const {   pagination  } = this.data  this.getData({ param: CHAT_DATA, pagination });   wx.pageScrollTo({    scrollTop: 1000   }) }, getData(params) {  const { chatData } = this.data;  const {   param,   pagination: {    pageSize = 10,    currentPage = 1   },  } = params;  this.setData({   pagination: { pageSize, currentPage }  });  const {   data,   pagination  } = pageFunc(param, currentPage, pageSize);  data.forEach((item) => {   if (item.duration) {    item.duration = Math.ceil(item.duration / 1000)   }  });   this.setData({    'chatData': data.concat(chatData)   }); }, startRecord(){//开始录音 const {show}=this.data  if (show){   this.setData({    show: false,   })  }else{   this.setData({    show: true,   })  }     }, startTalk(e){//开始说话  this.setData({   showDailog:true,  })  const options = {   duration: 60000,   sampleRate: 44100,   numberOfChannels: 1,   encodeBitRate: 192000,   format: 'aac',   frameSize: 50  }  recorderManager.start(options)  recorderManager.onStart((res) => {  }) }, stopRecord(){//停止说话  const that=this  this.setData({   showDailog:false,  })  recorderManager.stop();  recorderManager.onStop((res) => {   const { sendData, chatData } = that.data;   let { tempFilePath, duration, fileSize} = res   sendData.tempFilePathData=res   duration = Math.ceil(duration / 1000)   const data = { content: tempFilePath, duration , fileSize, contentType: 'voice', type: 1};   chatData.push(data);   wx.createSelectorQuery().select('.content').boundingClientRect(function (rect) {    // 使页面滚动到底部    wx.pageScrollTo({     scrollTop: rect.bottom + 5000    })   }).exec();   that.setData({    'tempFilePath': tempFilePath,    sendData,    chatData,    scrollHeight: (windowHeight - 0) + 'px',    toView: 'msg-' + (chatData.length - 1),    inputBottom: '0px'   })  }) }, playRecord(e){//播放语音  const { currentTarget: { dataset: { duration } }}=e;  const { tempFilePath} = this.data  innerAudioContext.autoplay = true;  innerAudioContext.src = tempFilePath ,   innerAudioContext.onPlay(() => {    this.setData({     toastTitle: "播放中....",     showDailog: true,    })   })  innerAudioContext.onEnded((res) => {   this.setData({    toastTitle: "录音中....",    showDailog: false,   })  })  innerAudioContext.onError((res) => {  });  innerAudioContext.play() }, uploadeImg(e){  const { file: { path,size:fileSize} } = e.detail;  const { chatData } = this.data;  const data = { content: path, fileSize, contentType: 'img', type: 1 };  chatData.push(data);  wx.createSelectorQuery().select('.content').boundingClientRect(function (rect) {   // 使页面滚动到底部   wx.pageScrollTo({    scrollTop: rect.bottom + 5000   })  }).exec()  this.setData({   chatData,   scrollHeight: (windowHeight - 0) + 'px',   toView: 'msg-' + (chatData.length - 1),   inputBottom:'0px'  }) }, getEmoji(){//获取表情包  wx.createSelectorQuery().select('.content').boundingClientRect(function (rect) {   // 使页面滚动到底部   wx.pageScrollTo({    scrollTop: rect.bottom + 5000   })  }).exec() this.setData({  showEmoji:true,  bottomHeight:"48vw" }) }, clickEmoji: function (e) {//选择表情包  const {   detail: {    value   },   currentTarget: {    dataset: {     key    }   }  } = e;  this.setData({   [key]: value  }) }, onPreview(e){  const {   currentTarget: {    dataset: {     src    }   }  } = e;  const urls = [src]  wx.previewImage({   current: src,   urls  }) }, /**  * 生命周期函数--监听页面显示  */ onShow: function () { }, /**  * 页面相关事件处理函数--监听用户下拉动作  */ onPullDownRefresh: function () {  let { pagination: { currentPage } } = this.data  this.getData({   param: CHAT_DATA,   pagination: {    pageSize: 5,    currentPage: currentPage + 1,   }  }); }, /**  * 页面上拉触底事件的处理函数  */ onReachBottom: function () { }, /**  * 获取聚焦  */ inputValue(e){  const {detail:{value}}=e  this.setData({   inputVal:value  }) }, focus: function (e) {  const { chatData}=this.data  keyHeight = e.detail.height;  wx.pageScrollTo({   scrollTop: windowHeight - keyHeight  })  this.setData({   toView: 'msg-' + (chatData.length - 1),   inputBottom: keyHeight + 'px',   scrollHeight: (windowHeight - keyHeight) + 'px',   showEmoji:false,   bottomHeight: "18vw"  })  //计算msg高度  // calScrollHeight(this, keyHeight); }, //失去聚焦(软键盘消失) blur: function (e) {  const { chatData } = this.data  this.setData({   scrollHeight: '100vh',   inputBottom: 0  })  this.setData({   toView: 'msg-' + (chatData.length - 1)  }) }, /**  * 发送点击监听  */ sendClick: function (e) {  const { chatData, scrollHeight}=this.data;  wx.createSelectorQuery().select('.content').boundingClientRect(function (rect) {   // 使页面滚动到底部   wx.pageScrollTo({    scrollTop: rect.bottom + 5000   })  }).exec()   chatData.push({   type: 1,   contentType: 'text',   content: e.detail.value,   headImg: '../../assets/common/images/headHortrait.jpeg',  })  this.setData({   chatData,   inputVal:''  }); }, /**  * 退回上一页  */ toBackClick: function () {  wx.navigateBack({}) }})// pages/contact/contact.jsconst { pageFunc} = require('../../utils/util.js');const app = getApp();var windowWidth = wx.getSystemInfoSync().windowWidth;var windowHeight = wx.getSystemInfoSync().windowHeight;var keyHeight = 0;const { CHAT_DATA}=require("../../data/customerService.js");import Toast from '../../components/vant/toast/toast';const recorderManager = wx.getRecorderManager();const innerAudioContext = wx.createInnerAudioContext();const db = wx.cloud.database();/** * 初始化数据 *//** * 计算msg总高度 */function calScrollHeight(that, keyHeight) { var query = wx.createSelectorQuery(); query.select('.scrollMsg').boundingClientRect(function(rect) { }).exec();}Page({ /**  * 页面的初始数据  */ data: {  scrollHeight: '100vh',  inputVal:"",  inputBottom: 0,  chatData:[],  show:true,  showDailog:false,  bottomHeight:"18vw",  sendData:{},  pagination: {   pageSize: 5,   currentPage: 1,   total: 0,  },  showEmoji:false,  toastTitle:"录音中...." }, /**  * 生命周期函数--监听页面加载  */ onLoad: function (options) {  // this.setData({  //  cusHeadIcon: app.globalData.userInfo.avatarUrl,  // });  const {   pagination  } = this.data  this.getData({ param: CHAT_DATA, pagination });   wx.pageScrollTo({    scrollTop: 1000   }) }, getData(params) {  const { chatData } = this.data;  const {   param,   pagination: {    pageSize = 10,    currentPage = 1   },  } = params;  this.setData({   pagination: { pageSize, currentPage }  });  const {   data,   pagination  } = pageFunc(param, currentPage, pageSize);  data.forEach((item) => {   if (item.duration) {    item.duration = Math.ceil(item.duration / 1000)   }  });   this.setData({    'chatData': data.concat(chatData)   }); }, startRecord(){//开始录音 const {show}=this.data  if (show){   this.setData({    show: false,   })  }else{   this.setData({    show: true,   })  }     }, startTalk(e){//开始说话  this.setData({   showDailog:true,  })  const options = {   duration: 60000,   sampleRate: 44100,   numberOfChannels: 1,   encodeBitRate: 192000,   format: 'aac',   frameSize: 50  }  recorderManager.start(options)  recorderManager.onStart((res) => {  }) }, stopRecord(){//停止说话  const that=this  this.setData({   showDailog:false,  })  recorderManager.stop();  recorderManager.onStop((res) => {   const { sendData, chatData } = that.data;   let { tempFilePath, duration, fileSize} = res   sendData.tempFilePathData=res   duration = Math.ceil(duration / 1000)   const data = { content: tempFilePath, duration , fileSize, contentType: 'voice', type: 1};   chatData.push(data);   wx.createSelectorQuery().select('.content').boundingClientRect(function (rect) {    // 使页面滚动到底部    wx.pageScrollTo({     scrollTop: rect.bottom + 5000    })   }).exec();   that.setData({    'tempFilePath': tempFilePath,    sendData,    chatData,    scrollHeight: (windowHeight - 0) + 'px',    toView: 'msg-' + (chatData.length - 1),    inputBottom: '0px'   })  }) }, playRecord(e){//播放语音  const { currentTarget: { dataset: { duration } }}=e;  const { tempFilePath} = this.data  innerAudioContext.autoplay = true;  innerAudioContext.src = tempFilePath ,   innerAudioContext.onPlay(() => {    this.setData({     toastTitle: "播放中....",     showDailog: true,    })   })  innerAudioContext.onEnded((res) => {   this.setData({    toastTitle: "录音中....",    showDailog: false,   })  })  innerAudioContext.onError((res) => {  });  innerAudioContext.play() }, uploadeImg(e){  const { file: { path,size:fileSize} } = e.detail;  const { chatData } = this.data;  const data = { content: path, fileSize, contentType: 'img', type: 1 };  chatData.push(data);  wx.createSelectorQuery().select('.content').boundingClientRect(function (rect) {   // 使页面滚动到底部   wx.pageScrollTo({    scrollTop: rect.bottom + 5000   })  }).exec()  this.setData({   chatData,   scrollHeight: (windowHeight - 0) + 'px',   toView: 'msg-' + (chatData.length - 1),   inputBottom:'0px'  }) }, getEmoji(){//获取表情包  wx.createSelectorQuery().select('.content').boundingClientRect(function (rect) {   // 使页面滚动到底部   wx.pageScrollTo({    scrollTop: rect.bottom + 5000   })  }).exec() this.setData({  showEmoji:true,  bottomHeight:"48vw" }) }, clickEmoji: function (e) {//选择表情包  const {   detail: {    value   },   currentTarget: {    dataset: {     key    }   }  } = e;  this.setData({   [key]: value  }) }, onPreview(e){  const {   currentTarget: {    dataset: {     src    }   }  } = e;  const urls = [src]  wx.previewImage({   current: src,   urls  }) }, /**  * 生命周期函数--监听页面显示  */ onShow: function () { }, /**  * 页面相关事件处理函数--监听用户下拉动作  */ onPullDownRefresh: function () {  let { pagination: { currentPage } } = this.data  this.getData({   param: CHAT_DATA,   pagination: {    pageSize: 5,    currentPage: currentPage + 1,   }  }); }, /**  * 页面上拉触底事件的处理函数  */ onReachBottom: function () { }, /**  * 获取聚焦  */ inputValue(e){  const {detail:{value}}=e  this.setData({   inputVal:value  }) }, focus: function (e) {  const { chatData}=this.data  keyHeight = e.detail.height;  wx.pageScrollTo({   scrollTop: windowHeight - keyHeight  })  this.setData({   toView: 'msg-' + (chatData.length - 1),   inputBottom: keyHeight + 'px',   scrollHeight: (windowHeight - keyHeight) + 'px',   showEmoji:false,   bottomHeight: "18vw"  })  //计算msg高度  // calScrollHeight(this, keyHeight); }, //失去聚焦(软键盘消失) blur: function (e) {  const { chatData } = this.data  this.setData({   scrollHeight: '100vh',   inputBottom: 0  })  this.setData({   toView: 'msg-' + (chatData.length - 1)  }) }, /**  * 发送点击监听  */ sendClick: function (e) {  const { chatData, scrollHeight}=this.data;  wx.createSelectorQuery().select('.content').boundingClientRect(function (rect) {   // 使页面滚动到底部   wx.pageScrollTo({    scrollTop: rect.bottom + 5000   })  }).exec()   chatData.push({   type: 1,   contentType: 'text',   content: e.detail.value,   headImg: '../../assets/common/images/headHortrait.jpeg',  })  this.setData({   chatData,   inputVal:''  }); }, /**  * 退回上一页  */ toBackClick: function () {  wx.navigateBack({}) }})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

您可能感兴趣的文章:

  • 微信小程序websocket实现即时聊天功能
  • 详解微信小程序实现仿微信聊天界面(各种细节处理)
  • 微信小程序实现即时通信聊天功能的实例代码
  • 微信小程序实现聊天对话(文本、图片)功能
  • 微信小程序websocket实现聊天功能
  • 微信小程序WebSocket实现聊天对话功能
  • 微信小程序实时聊天WebSocket
  • 微信小程序 聊天室简单实现


  • 上一条:
    微信开发之网页授权获取用户信息(二)
    下一条:
    在微信小程序中渲染HTML内容3种解决方案及分析与问题解决
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 微信模板消息改版后发送规则记录(微信订阅消息参数值内容限制说明)(1个评论)
    • 微信支付v3对接所需工具及命令(0个评论)
    • 2023年9月1日起:微信小程序必须备案才能上线运营(0个评论)
    • 腾讯官方客服回应了:微信好友上限约10000个!(1个评论)
    • 2023年做微信小程序的老铁注意:新增收费项、微信小程序获取手机号也收费了(2个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-10
    • 2018-01
    • 2020-03
    • 2021-06
    • 2021-10
    • 2022-03
    • 2023-02
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客