当前位置:   article > 正文

一个微信小程序聊天静态页面(基于Vant Weapp)_微信静态页面

微信静态页面

首先需要引用一下vant组件

  1. {
  2. "enablePullDownRefresh": false,
  3. "usingComponents": {
  4. "van-image": "/vantdist/image/index",
  5. "van-field": "/vantdist/field/index",
  6. "van-cell-group": "/vantdist/cell-group/index",
  7. "van-button": "/vantdist/button/index",
  8. "van-icon": "/vantdist/icon/index",
  9. "van-row": "/vantdist/row/index",
  10. "van-col": "/vantdist/col/index"
  11. }
  12. }

这里是HTML/wxml

  1. <view class="container">
  2. <!-- <header id="top-header">
  3. <view style="width:100%;">
  4. <view>
  5. <view>顶部1</view>
  6. <view>顶部</view>
  7. <view>顶部</view>
  8. </view>
  9. </view>
  10. </header>-->
  11. <scroll-view id="main-content" scroll-y="{{true}}" scroll-top="{{scrollTop}}">
  12. <view>
  13. <view style="position: relative;">
  14. <!-- 一条聊天记录 -->
  15. <view id="msglistview" class="chat-body">
  16. <!-- 聊天记录 -->
  17. <view>
  18. <!-- 自己发的消息 -->
  19. <block wx:for="{{msgList}}">
  20. <view class="item self" wx:if="{{item.mine}}">
  21. <!-- 文字内容 -->
  22. <view style="display:flex;flex-direction: column;">
  23. <view class="content right-time">{{item.date +" "+item.name}}</view>
  24. <view>
  25. <view class="content right">
  26. <rich-text
  27. mode="tag"
  28. style="text-align:left"
  29. nodes="{{_this.imgTag2richTag(item.text.text)}}"
  30. user-select
  31. data-html="{{item.text.text}}"
  32. bind:tap="onClick_RichCard"
  33. ></rich-text>
  34. </view>
  35. </view>
  36. </view>
  37. <!-- 头像 -->
  38. <view class="avatar">
  39. <van-image width="78rpx" height="78rpx" fit="fill" src="{{item.img}}" />
  40. </view>
  41. </view>
  42. <!-- 客服发的消息 -->
  43. <view class="item kf" wx:else>
  44. <!-- 头像 -->
  45. <view class="avatar">
  46. <van-image width="78rpx" height="78rpx" fit="fill" src="{{item.img}}" />
  47. </view>
  48. <!-- 文字内容 -->
  49. <view style="display:flex;flex-direction: column;">
  50. <view class="content left-time">{{(item.date)+" "+item.name}}</view>
  51. <view>
  52. <view class="content left">
  53. <rich-text
  54. mode="tag"
  55. style="text-align:left"
  56. nodes="{{_this.imgTag2richTag(item.text.text)}}"
  57. user-select
  58. data-html="{{item.text.text}}"
  59. bind:tap="onClick_RichCard"
  60. ></rich-text>
  61. </view>
  62. </view>
  63. </view>
  64. </view>
  65. <!-- <view class="item Ai">
  66. <view class="avatar">
  67. <van-image width="78rpx" height="78rpx" fit="fill" src="{{item.img}}"/>
  68. </view>
  69. <view>
  70. <view class="content left-time">{{`${item.date}  ${item.name}`}}</view>
  71. <view class="content left">
  72. <van-image
  73. width="100%"
  74. height="10em"
  75. fit="heightFix"
  76. src="https://img.yzcdn.cn/vant/cat.jpeg"
  77. />
  78. </view>
  79. </view>
  80. </view>-->
  81. </block>
  82. </view>
  83. </view>
  84. </view>
  85. </view>
  86. </scroll-view>
  87. <footer id="bottom-footer">
  88. <view style="width:100%;">
  89. <view>
  90. <van-cell-group>
  91. <van-field
  92. model:value="{{ message }}"
  93. type="textarea"
  94. placeholder="请输入留言"
  95. autosize
  96. border="{{ false }}"
  97. >
  98. <view slot="button" style="display:flex">
  99. <van-icon name="add-o" size="1.4em" disable-default-padding="{{false}}" />
  100. <view style="margin-left:20rpx">
  101. <van-button
  102. slot="button"
  103. size="small"
  104. type="primary"
  105. bind:click="onClick_sendButton"
  106. >发送</van-button>
  107. </view>
  108. </view>
  109. <!-- <van-icon slot="button" name="add-o" /> -->
  110. <!-- <van-button slot="button" icon="add-o" round hairline /> -->
  111. </van-field>
  112. </van-cell-group>
  113. </view>
  114. </view>
  115. </footer>
  116. </view>
  117. <wxs module="_this">
  118. function imgTag2richTag(tag) {
  119. var mycontent = tag;
  120. var reg = getRegExp("<img", "g");
  121. mycontent = mycontent.replace(reg, '<img style="width:auto;height:10em"');
  122. var reg = getRegExp("<image", "g");
  123. mycontent = mycontent.replace(reg, '<image style="width:auto;height:10em"');
  124. return mycontent;
  125. }
  126. module.exports.imgTag2richTag = imgTag2richTag;
  127. </wxs>

这里是js

  1. // subpages/kf-message/kf-message.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. scrollTop: 0,
  8. message: "",
  9. msgList: [{
  10. date: "2020/04/25 21:19:07",
  11. text: {
  12. text: "师傅你是做什么工作的?"
  13. },
  14. mine: true,
  15. name: "你",
  16. img: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
  17. }, {
  18. date: "2020/04/25 21:19:07",
  19. text: {
  20. text: "<img src='https://img.yzcdn.cn/vant/cat.jpeg'/>"
  21. },
  22. mine: false,
  23. name: "客服一号",
  24. img: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
  25. },
  26. {
  27. date: "2020/04/25 21:19:07",
  28. text: {
  29. text: "<img src='https://img.yzcdn.cn/vant/cat.jpeg'/>"
  30. },
  31. mine: true,
  32. name: "你",
  33. img: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
  34. },
  35. {
  36. date: "2020/04/25 21:19:07",
  37. text: {
  38. text: "您好,请给好评!"
  39. },
  40. mine: false,
  41. name: "客服一号",
  42. img: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
  43. },
  44. {
  45. date: "2020/04/25 21:19:07",
  46. text: {
  47. text: "您好,请给好评!您好,请给好评!您好,请给好评!您好,请给好评!您好,请给好评!您好,请给好评!您好,请给好评!您好,请给好评!您好,请给好评!"
  48. },
  49. mine: false,
  50. name: "客服一号",
  51. img: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
  52. }
  53. ]
  54. },
  55. /**
  56. * 生命周期函数--监听页面加载
  57. */
  58. onLoad(options) {
  59. },
  60. /**
  61. * 生命周期函数--监听页面初次渲染完成
  62. */
  63. onReady() {
  64. },
  65. /**
  66. * 生命周期函数--监听页面显示
  67. */
  68. onShow() {
  69. },
  70. onClick_sendButton: function () {
  71. // let msgSry=this.data.message.split('\n');
  72. // let message = msgSry.join("<br/>")
  73. let message = this.data.message.replaceAll("\n", "<br/>")
  74. let msgItem = {
  75. date: Date.now(),
  76. text: {
  77. text: message
  78. },
  79. mine: true,
  80. name: "你",
  81. img: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
  82. }
  83. this.data.msgList.push(msgItem);
  84. this.setData({
  85. msgList: this.data.msgList,
  86. message: ""
  87. })
  88. this.resetSorollBar();
  89. },
  90. // 点击卡片之后会预览图片
  91. onClick_RichCard: function (e) {
  92. // 将图片里的 src 提取出来,变成一个数组。
  93. const imgTags = e.currentTarget.dataset.html.match(/<img.*?>/g);
  94. const urls = [];
  95. if (imgTags) {
  96. imgTags.forEach((tag) => {
  97. let url = tag.match(/src="(.*?)"/) || tag.match(/src='(.*?)'/);
  98. if (url) {
  99. urls.push(url[1]);
  100. }
  101. });
  102. }
  103. if (urls.length <= 0) return;
  104. wx.previewImage({
  105. urls: urls,
  106. });
  107. },
  108. resetSorollBar() {
  109. let selQuery = wx.createSelectorQuery();
  110. selQuery.select("#main-content").boundingClientRect(res => {
  111. console.log('res', res)
  112. this.setData({
  113. scrollTop: res.height
  114. })
  115. }).exec();
  116. },
  117. /**
  118. * 生命周期函数--监听页面隐藏
  119. */
  120. onHide() {
  121. },
  122. /**
  123. * 生命周期函数--监听页面卸载
  124. */
  125. onUnload() {
  126. },
  127. /**
  128. * 页面相关事件处理函数--监听用户下拉动作
  129. */
  130. onPullDownRefresh() {
  131. },
  132. /**
  133. * 页面上拉触底事件的处理函数
  134. */
  135. onReachBottom() {
  136. },
  137. /**
  138. * 用户点击右上角分享
  139. */
  140. onShareAppMessage() {
  141. }
  142. })

这里是wxss/css

  1. .container {
  2. display: flex;
  3. flex-direction: column;
  4. width: 100%;
  5. flex-grow: 1;
  6. height: 100vh;
  7. flex-basis: 0;
  8. }
  9. #top-header {
  10. background-color: #f8f8f8;
  11. color: #333;
  12. }
  13. #main-content {
  14. background-color: #fff;
  15. color: #333;
  16. flex: 1;
  17. overflow-y: scroll;
  18. position: relative;
  19. }
  20. #bottom-footer {
  21. background-color: #f8f8f8;
  22. color: #333;
  23. }
  24. .chat-body {
  25. display: flex;
  26. flex-direction: column;
  27. padding-top: 23rpx;
  28. }
  29. .chat-body .self {
  30. justify-content: flex-end;
  31. }
  32. .chat-body .item {
  33. display: flex;
  34. padding: 23rpx 30rpx;
  35. }
  36. .chat-body .item .content {
  37. position: relative;
  38. max-width: 486rpx;
  39. border-radius: 8rpx;
  40. white-space: normal;
  41. word-break: break-all;
  42. padding: 20rpx 20rpx;
  43. margin: 0 24rpx;
  44. border-radius: 5px;
  45. font-size: 32rpx;
  46. font-family: PingFang SC;
  47. font-weight: 500;
  48. color: #333333;
  49. line-height: 42rpx;
  50. overflow: inherit;
  51. }
  52. .chat-body .item .right-time {
  53. background-color: none;
  54. padding: 0 10rpx;
  55. font-size: 12px;
  56. color: #646566;
  57. float: right;
  58. text-align: right;
  59. }
  60. .chat-body .item .right {
  61. background-color: #C2DCFF;
  62. display: flex;
  63. justify-content: flex-end;
  64. }
  65. .chat-body .item .right::after {
  66. position: absolute;
  67. display: inline-block;
  68. content: '';
  69. width: 0;
  70. height: 0;
  71. left: 100%;
  72. top: 10px;
  73. border: 12rpx solid transparent;
  74. border-left: 12rpx solid #C2DCFF;
  75. }
  76. .chat-body .item .left-time {
  77. background-color: none;
  78. padding: 0 10rpx;
  79. font-size: 12px;
  80. color: #646566
  81. }
  82. .chat-body .item .left {
  83. background-color: #F7F8FA;
  84. width: auto;
  85. }
  86. .chat-body .item .left::after {
  87. position: absolute;
  88. display: inline-block;
  89. content: '';
  90. width: 0;
  91. height: 0;
  92. top: 10px;
  93. right: 100%;
  94. border: 12rpx solid transparent;
  95. border-right: 12rpx solid #F7F8FA;
  96. }
  97. .chat-body .item .avatar {
  98. display: flex;
  99. justify-content: center;
  100. width: 78rpx;
  101. height: 78rpx;
  102. background: #4F7DF5;
  103. border-radius: 8rpx;
  104. overflow: hidden;
  105. flex: 0 0 78rpx;
  106. margin-top: 10rpx;
  107. }
  108. .chat-body .item .avatar image {
  109. align-self: center;
  110. }

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

闽ICP备14008679号