当前位置:   article > 正文

uniapp 实现 app 聊天页面功能_uniapp聊天界面

uniapp聊天界面

写在前面

最近在写一个 app ,其中有一个功能是实现简单的聊天界面功能,虽然听起来是一个很简单的功能,但是在开发的时候会遇到各种问题,经过上网查找解决了自己的问题,下面我记录一下。

 下面是最终的效果图

 下面是具体的代码

  1. <template>
  2. <view class="chat">
  3. <!-- 顶部标题 -->
  4. <view class="topTabbar">
  5. <!-- 返回图标 -->
  6. <u-icon class="icon" name="arrow-left" size="20px" color="#000" @click="goback()"></u-icon>
  7. <!-- 标题 -->
  8. <view class="text">匿名</view>
  9. </view>
  10. <scroll-view :style="{height: `${windowHeight-inputHeight - 180}rpx`}"
  11. id="scrollview"
  12. scroll-y="true"
  13. :scroll-top="scrollTop"
  14. class="scroll-view"
  15. >
  16. <!-- 聊天主体 -->
  17. <view id="msglistview" class="chat-body">
  18. <!-- 聊天记录 -->
  19. <view v-for="(item,index) in msgList" :key="index">
  20. <!-- 自己发的消息 -->
  21. <view class="item self" v-if="item.userContent != ''" >
  22. <!-- 文字内容 -->
  23. <view class="content right">
  24. {{item.userContent}}
  25. </view>
  26. <!-- 头像 -->
  27. <image class="avatar" :src="item.image">
  28. </image>
  29. </view>
  30. <!-- 机器人发的消息 -->
  31. <view class="item Ai" v-if="item.botContent != ''">
  32. <!-- 头像 -->
  33. <image class="avatar" :src="item.image">
  34. </image>
  35. <!-- 文字内容 -->
  36. <view class="content left">
  37. {{item.botContent}}
  38. </view>
  39. </view>
  40. </view>
  41. </view>
  42. </scroll-view>
  43. <!-- 底部消息发送栏 -->
  44. <!-- 用来占位,防止聊天消息被发送框遮挡 -->
  45. <view class="chat-bottom" :style="{height: `${inputHeight}rpx`}">
  46. <view class="send-msg" :style="{bottom:`${keyboardHeight - 60}rpx`}">
  47. <view class="uni-textarea">
  48. <textarea v-model="chatMsg"
  49. maxlength="300"
  50. confirm-type="send"
  51. @confirm="handleSend"
  52. placeholder="快来聊天吧~"
  53. :show-confirm-bar="false"
  54. :adjust-position="false"
  55. @linechange="sendHeight"
  56. @focus="focus" @blur="blur"
  57. auto-height></textarea>
  58. </view>
  59. <button @click="handleSend" class="send-btn">发送</button>
  60. </view>
  61. </view>
  62. </view>
  63. </template>
  64. <script>
  65. export default{
  66. data() {
  67. return {
  68. //键盘高度
  69. keyboardHeight:0,
  70. //底部消息发送高度
  71. bottomHeight: 0,
  72. //滚动距离
  73. scrollTop: 0,
  74. userId:'',
  75. //发送的消息
  76. chatMsg:"",
  77. msgList:[
  78. {
  79. botContent: "你好啊,很高兴你可以关注我,请问我有什么可以帮助你的吗?",
  80. userContent: "",
  81. image:"/static/common/unname1.jpeg"
  82. },
  83. {
  84. botContent: "",
  85. userContent: "你好呀,非常高兴认识你",
  86. image:"/static/common/unname2.jpg"
  87. },
  88. ]
  89. }
  90. },
  91. updated(){
  92. //页面更新时调用聊天消息定位到最底部
  93. this.scrollToBottom();
  94. },
  95. computed: {
  96. windowHeight() {
  97. return this.rpxTopx(uni.getSystemInfoSync().windowHeight)
  98. },
  99. // 键盘弹起来的高度+发送框高度
  100. inputHeight(){
  101. return this.bottomHeight+this.keyboardHeight
  102. }
  103. },
  104. onLoad(){
  105. uni.onKeyboardHeightChange(res => {
  106. //这里正常来讲代码直接写
  107. //this.keyboardHeight=this.rpxTopx(res.height)就行了
  108. //但是之前界面ui设计聊天框的高度有点高,为了不让键盘和聊天输入框之间距离差太大所以我改动了一下。
  109. this.keyboardHeight = this.rpxTopx(res.height)
  110. if(this.keyboardHeight<0)this.keyboardHeight = 0;
  111. })
  112. },
  113. onUnload(){
  114. // uni.offKeyboardHeightChange()
  115. },
  116. methods: {
  117. goback() {
  118. uni.switchTab({
  119. url: "/pages/tutorship/tutorship"
  120. })
  121. },
  122. focus(){
  123. this.scrollToBottom()
  124. },
  125. blur(){
  126. this.scrollToBottom()
  127. },
  128. // px转换成rpx
  129. rpxTopx(px){
  130. let deviceWidth = uni.getSystemInfoSync().windowWidth
  131. let rpx = ( 750 / deviceWidth ) * Number(px)
  132. return Math.floor(rpx)
  133. },
  134. // 监视聊天发送栏高度
  135. sendHeight(){
  136. setTimeout(()=>{
  137. let query = uni.createSelectorQuery();
  138. query.select('.send-msg').boundingClientRect()
  139. query.exec(res =>{
  140. this.bottomHeight = this.rpxTopx(res[0].height)
  141. })
  142. },10)
  143. },
  144. // 滚动至聊天底部
  145. scrollToBottom(e){
  146. setTimeout(()=>{
  147. let query = uni.createSelectorQuery().in(this);
  148. query.select('#scrollview').boundingClientRect();
  149. query.select('#msglistview').boundingClientRect();
  150. query.exec((res) =>{
  151. if(res[1].height > res[0].height){
  152. this.scrollTop = this.rpxTopx(res[1].height - res[0].height)
  153. }
  154. })
  155. },15)
  156. },
  157. // 发送消息
  158. handleSend() {
  159. //如果消息不为空
  160. if(!this.chatMsg||!/^\s+$/.test(this.chatMsg)){
  161. let obj = {
  162. botContent: "",
  163. userContent: this.chatMsg,
  164. image:"/static/common/unname2.jpg"
  165. }
  166. this.msgList.push(obj);
  167. this.chatMsg = '';
  168. this.scrollToBottom()
  169. }else {
  170. this.$modal.showToast('不能发送空白消息')
  171. }
  172. },
  173. }
  174. }
  175. </script>
  176. <style lang="scss" scoped>
  177. $chatContentbgc: #C2DCFF;
  178. $sendBtnbgc: #4F7DF5;
  179. view,button,text,input,textarea {
  180. margin: 0;
  181. padding: 0;
  182. box-sizing: border-box;
  183. }
  184. /* 聊天消息 */
  185. .chat {
  186. .topTabbar {
  187. width: 100%;
  188. height: 90rpx;
  189. line-height: 90rpx;
  190. display: flex;
  191. margin-top: 80rpx;
  192. justify-content: space-between;
  193. .icon {
  194. margin-left: 20rpx;
  195. }
  196. .text {
  197. margin: auto;
  198. font-size: 16px;
  199. font-weight: 700;
  200. }
  201. .button {
  202. width: 10%;
  203. margin: auto 20rpx auto 0rpx;
  204. }
  205. }
  206. .scroll-view {
  207. ::-webkit-scrollbar {
  208. display: none;
  209. width: 0 !important;
  210. height: 0 !important;
  211. -webkit-appearance: none;
  212. background: transparent;
  213. color: transparent;
  214. }
  215. // background-color: orange;
  216. background-color: #F6F6F6;
  217. .chat-body {
  218. display: flex;
  219. flex-direction: column;
  220. padding-top: 23rpx;
  221. // background-color:skyblue;
  222. .self {
  223. justify-content: flex-end;
  224. }
  225. .item {
  226. display: flex;
  227. padding: 23rpx 30rpx;
  228. // background-color: greenyellow;
  229. .right {
  230. background-color: $chatContentbgc;
  231. }
  232. .left {
  233. background-color: #FFFFFF;
  234. }
  235. // 聊天消息的三角形
  236. .right::after {
  237. position: absolute;
  238. display: inline-block;
  239. content: '';
  240. width: 0;
  241. height: 0;
  242. left: 100%;
  243. top: 10px;
  244. border: 12rpx solid transparent;
  245. border-left: 12rpx solid $chatContentbgc;
  246. }
  247. .left::after {
  248. position: absolute;
  249. display: inline-block;
  250. content: '';
  251. width: 0;
  252. height: 0;
  253. top: 10px;
  254. right: 100%;
  255. border: 12rpx solid transparent;
  256. border-right: 12rpx solid #FFFFFF;
  257. }
  258. .content {
  259. position: relative;
  260. max-width: 486rpx;
  261. border-radius: 8rpx;
  262. word-wrap: break-word;
  263. padding: 24rpx 24rpx;
  264. margin: 0 24rpx;
  265. border-radius: 5px;
  266. font-size: 32rpx;
  267. font-family: PingFang SC;
  268. font-weight: 500;
  269. color: #333333;
  270. line-height: 42rpx;
  271. }
  272. .avatar {
  273. display: flex;
  274. justify-content: center;
  275. width: 78rpx;
  276. height: 78rpx;
  277. background: $sendBtnbgc;
  278. border-radius: 50rpx;
  279. overflow: hidden;
  280. image {
  281. align-self: center;
  282. }
  283. }
  284. }
  285. }
  286. }
  287. /* 底部聊天发送栏 */
  288. .chat-bottom {
  289. width: 100%;
  290. height: 100rpx;
  291. background: #F4F5F7;
  292. transition: all 0.1s ease;
  293. .send-msg {
  294. display: flex;
  295. align-items: flex-end;
  296. padding: 16rpx 30rpx;
  297. width: 100%;
  298. min-height: 177rpx;
  299. position: fixed;
  300. bottom: 0;
  301. background: #fff;
  302. transition: all 0.1s ease;
  303. }
  304. .uni-textarea {
  305. padding-bottom: 70rpx;
  306. textarea {
  307. width: 537rpx;
  308. min-height: 75rpx;
  309. max-height: 500rpx;
  310. background: #f1f1f1;
  311. border-radius: 40rpx;
  312. font-size: 32rpx;
  313. font-family: PingFang SC;
  314. color: #333333;
  315. line-height: 74rpx;
  316. padding: 5rpx 8rpx;
  317. text-indent: 30rpx;
  318. }
  319. }
  320. .send-btn {
  321. display: flex;
  322. align-items: center;
  323. justify-content: center;
  324. margin-bottom: 76rpx;
  325. margin-left: 25rpx;
  326. width: 120rpx;
  327. height: 75rpx;
  328. background: #ed5a65;
  329. border-radius: 50rpx;
  330. font-size: 28rpx;
  331. font-family: PingFang SC;
  332. font-weight: 500;
  333. color: #FFFFFF;
  334. line-height: 28rpx;
  335. }
  336. }
  337. }
  338. </style>

下面是当时上网找的解决方法,供大家参考。下面这篇文章讲解了具体的实现过程,我就不多赘述了,这里只展示我自己的代码,方便自己以后查看。【uni-app】uni-app实现聊天页面功能(小程序)——布局篇_uniapp聊天界面_Hanachann的博客-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/514632
推荐阅读
相关标签
  

闽ICP备14008679号