赞
踩
首先需要引用一下vant组件
- {
- "enablePullDownRefresh": false,
- "usingComponents": {
- "van-image": "/vantdist/image/index",
- "van-field": "/vantdist/field/index",
- "van-cell-group": "/vantdist/cell-group/index",
- "van-button": "/vantdist/button/index",
- "van-icon": "/vantdist/icon/index",
- "van-row": "/vantdist/row/index",
- "van-col": "/vantdist/col/index"
- }
- }
这里是HTML/wxml
- <view class="container">
- <!-- <header id="top-header">
- <view style="width:100%;">
- <view>
- <view>顶部1</view>
- <view>顶部</view>
- <view>顶部</view>
- </view>
- </view>
- </header>-->
- <scroll-view id="main-content" scroll-y="{{true}}" scroll-top="{{scrollTop}}">
- <view>
- <view style="position: relative;">
- <!-- 一条聊天记录 -->
- <view id="msglistview" class="chat-body">
- <!-- 聊天记录 -->
- <view>
- <!-- 自己发的消息 -->
- <block wx:for="{{msgList}}">
- <view class="item self" wx:if="{{item.mine}}">
- <!-- 文字内容 -->
- <view style="display:flex;flex-direction: column;">
- <view class="content right-time">{{item.date +" "+item.name}}</view>
- <view>
- <view class="content right">
- <rich-text
- mode="tag"
- style="text-align:left"
- nodes="{{_this.imgTag2richTag(item.text.text)}}"
- user-select
- data-html="{{item.text.text}}"
- bind:tap="onClick_RichCard"
- ></rich-text>
- </view>
- </view>
- </view>
- <!-- 头像 -->
- <view class="avatar">
- <van-image width="78rpx" height="78rpx" fit="fill" src="{{item.img}}" />
- </view>
- </view>
- <!-- 客服发的消息 -->
- <view class="item kf" wx:else>
- <!-- 头像 -->
- <view class="avatar">
- <van-image width="78rpx" height="78rpx" fit="fill" src="{{item.img}}" />
- </view>
- <!-- 文字内容 -->
- <view style="display:flex;flex-direction: column;">
- <view class="content left-time">{{(item.date)+" "+item.name}}</view>
- <view>
- <view class="content left">
- <rich-text
- mode="tag"
- style="text-align:left"
- nodes="{{_this.imgTag2richTag(item.text.text)}}"
- user-select
- data-html="{{item.text.text}}"
- bind:tap="onClick_RichCard"
- ></rich-text>
- </view>
- </view>
- </view>
- </view>
- <!-- <view class="item Ai">
- <view class="avatar">
- <van-image width="78rpx" height="78rpx" fit="fill" src="{{item.img}}"/>
- </view>
- <view>
- <view class="content left-time">{{`${item.date} ${item.name}`}}</view>
- <view class="content left">
- <van-image
- width="100%"
- height="10em"
- fit="heightFix"
- src="https://img.yzcdn.cn/vant/cat.jpeg"
- />
- </view>
- </view>
- </view>-->
- </block>
- </view>
- </view>
- </view>
- </view>
- </scroll-view>
- <footer id="bottom-footer">
- <view style="width:100%;">
- <view>
- <van-cell-group>
- <van-field
- model:value="{{ message }}"
- type="textarea"
- placeholder="请输入留言"
- autosize
- border="{{ false }}"
- >
- <view slot="button" style="display:flex">
- <van-icon name="add-o" size="1.4em" disable-default-padding="{{false}}" />
- <view style="margin-left:20rpx">
- <van-button
- slot="button"
- size="small"
- type="primary"
- bind:click="onClick_sendButton"
- >发送</van-button>
- </view>
- </view>
- <!-- <van-icon slot="button" name="add-o" /> -->
- <!-- <van-button slot="button" icon="add-o" round hairline /> -->
- </van-field>
- </van-cell-group>
- </view>
- </view>
- </footer>
- </view>
-
- <wxs module="_this">
- function imgTag2richTag(tag) {
- var mycontent = tag;
- var reg = getRegExp("<img", "g");
- mycontent = mycontent.replace(reg, '<img style="width:auto;height:10em"');
- var reg = getRegExp("<image", "g");
- mycontent = mycontent.replace(reg, '<image style="width:auto;height:10em"');
- return mycontent;
- }
- module.exports.imgTag2richTag = imgTag2richTag;
- </wxs>
这里是js
- // subpages/kf-message/kf-message.js
- Page({
-
- /**
- * 页面的初始数据
- */
- data: {
- scrollTop: 0,
- message: "",
- msgList: [{
- date: "2020/04/25 21:19:07",
- text: {
- text: "师傅你是做什么工作的?"
- },
- mine: true,
- name: "你",
- img: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
- }, {
- date: "2020/04/25 21:19:07",
- text: {
- text: "<img src='https://img.yzcdn.cn/vant/cat.jpeg'/>"
- },
- mine: false,
- name: "客服一号",
- img: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
- },
- {
- date: "2020/04/25 21:19:07",
- text: {
- text: "<img src='https://img.yzcdn.cn/vant/cat.jpeg'/>"
- },
- mine: true,
- name: "你",
- img: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
- },
- {
- date: "2020/04/25 21:19:07",
- text: {
- text: "您好,请给好评!"
- },
- mine: false,
- name: "客服一号",
- img: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
- },
- {
- date: "2020/04/25 21:19:07",
- text: {
- text: "您好,请给好评!您好,请给好评!您好,请给好评!您好,请给好评!您好,请给好评!您好,请给好评!您好,请给好评!您好,请给好评!您好,请给好评!"
- },
- mine: false,
- name: "客服一号",
- img: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
- }
- ]
- },
-
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {
-
- },
-
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {
-
- },
-
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
-
- },
-
- onClick_sendButton: function () {
- // let msgSry=this.data.message.split('\n');
- // let message = msgSry.join("<br/>")
-
-
- let message = this.data.message.replaceAll("\n", "<br/>")
-
- let msgItem = {
- date: Date.now(),
- text: {
- text: message
- },
- mine: true,
- name: "你",
- img: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
- }
- this.data.msgList.push(msgItem);
- this.setData({
- msgList: this.data.msgList,
- message: ""
- })
- this.resetSorollBar();
- },
- // 点击卡片之后会预览图片
- onClick_RichCard: function (e) {
- // 将图片里的 src 提取出来,变成一个数组。
- const imgTags = e.currentTarget.dataset.html.match(/<img.*?>/g);
- const urls = [];
- if (imgTags) {
- imgTags.forEach((tag) => {
- let url = tag.match(/src="(.*?)"/) || tag.match(/src='(.*?)'/);
- if (url) {
- urls.push(url[1]);
- }
- });
- }
- if (urls.length <= 0) return;
- wx.previewImage({
- urls: urls,
- });
- },
-
- resetSorollBar() {
- let selQuery = wx.createSelectorQuery();
- selQuery.select("#main-content").boundingClientRect(res => {
- console.log('res', res)
- this.setData({
- scrollTop: res.height
- })
- }).exec();
-
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {
-
- },
-
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {
-
- },
-
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
-
- },
-
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
-
- },
-
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
-
- }
- })
这里是wxss/css
- .container {
- display: flex;
- flex-direction: column;
- width: 100%;
- flex-grow: 1;
- height: 100vh;
- flex-basis: 0;
- }
-
- #top-header {
- background-color: #f8f8f8;
- color: #333;
- }
-
- #main-content {
- background-color: #fff;
- color: #333;
- flex: 1;
- overflow-y: scroll;
- position: relative;
- }
-
- #bottom-footer {
- background-color: #f8f8f8;
- color: #333;
- }
-
-
- .chat-body {
- display: flex;
- flex-direction: column;
- padding-top: 23rpx;
- }
-
- .chat-body .self {
- justify-content: flex-end;
- }
-
- .chat-body .item {
- display: flex;
- padding: 23rpx 30rpx;
- }
-
- .chat-body .item .content {
- position: relative;
- max-width: 486rpx;
- border-radius: 8rpx;
- white-space: normal;
- word-break: break-all;
- padding: 20rpx 20rpx;
- margin: 0 24rpx;
- border-radius: 5px;
- font-size: 32rpx;
- font-family: PingFang SC;
- font-weight: 500;
- color: #333333;
- line-height: 42rpx;
- overflow: inherit;
- }
-
- .chat-body .item .right-time {
- background-color: none;
- padding: 0 10rpx;
- font-size: 12px;
- color: #646566;
- float: right;
- text-align: right;
- }
-
- .chat-body .item .right {
- background-color: #C2DCFF;
- display: flex;
- justify-content: flex-end;
- }
-
- .chat-body .item .right::after {
- position: absolute;
- display: inline-block;
- content: '';
- width: 0;
- height: 0;
- left: 100%;
- top: 10px;
- border: 12rpx solid transparent;
- border-left: 12rpx solid #C2DCFF;
- }
-
- .chat-body .item .left-time {
- background-color: none;
- padding: 0 10rpx;
- font-size: 12px;
- color: #646566
- }
-
- .chat-body .item .left {
- background-color: #F7F8FA;
- width: auto;
- }
-
- .chat-body .item .left::after {
- position: absolute;
- display: inline-block;
- content: '';
- width: 0;
- height: 0;
- top: 10px;
- right: 100%;
- border: 12rpx solid transparent;
- border-right: 12rpx solid #F7F8FA;
- }
-
-
- .chat-body .item .avatar {
- display: flex;
- justify-content: center;
- width: 78rpx;
- height: 78rpx;
- background: #4F7DF5;
- border-radius: 8rpx;
- overflow: hidden;
- flex: 0 0 78rpx;
- margin-top: 10rpx;
- }
-
- .chat-body .item .avatar image {
- align-self: center;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。