Blame view

yudao-ui-admin/src/views/mp/components/wx-msg/main.vue 10.9 KB
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
1
2
3
  <!--
    - Copyright (C) 2018-2019
    - All rights reserved, Designed By www.joolun.com
9e4a978f   YunaiV   mp:解决 wx-msg 的 cs...
4
5
6
    芋道源码:
    ① 移除暂时用不到的 websocket
    ② 代码优化,补充注释,提升阅读性
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
7
8
  -->
  <template>
9e4a978f   YunaiV   mp:解决 wx-msg 的 cs...
9
10
    <div class="msg-main">
      <div class="msg-div" :id="'msg-div' + nowStr">
2b477a4d   YunaiV   mp:实现 wx-msg 的消息列...
11
        <!-- 加载更多 -->
68ef11ee   YunaiV   mp:完善微信公众号的消息
12
13
        <div v-loading="loading"></div>
        <div v-if="!loading">
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
14
15
16
          <div class="el-table__empty-block" v-if="loadMore" @click="loadingMore"><span class="el-table__empty-text">点击加载更多</span></div>
          <div class="el-table__empty-block" v-if="!loadMore"><span class="el-table__empty-text">没有更多了</span></div>
        </div>
2b477a4d   YunaiV   mp:实现 wx-msg 的消息列...
17
        <!-- 消息列表 -->
68ef11ee   YunaiV   mp:完善微信公众号的消息
18
        <div class="execution" v-for="item in list" :key='item.id'>
cc23ca2d   YunaiV   mp:解决 wx-msg 的 cs...
19
          <div class="avue-comment" :class="item.sendFrom === 2 ? 'avue-comment--reverse' : ''">
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
20
            <div class="avatar-div">
9e4a978f   YunaiV   mp:解决 wx-msg 的 cs...
21
22
              <img :src="item.sendFrom === 1 ? user.avatar : mp.avatar" class="avue-comment__avatar">
              <div class="avue-comment__author">{{item.sendFrom === 1 ? user.nickname : mp.nickname }}</div>
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
23
24
25
            </div>
            <div class="avue-comment__main">
              <div class="avue-comment__header">
2b477a4d   YunaiV   mp:实现 wx-msg 的消息列...
26
                <div class="avue-comment__create_time">{{ parseTime(item.createTime) }}</div>
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
27
              </div>
cc23ca2d   YunaiV   mp:解决 wx-msg 的 cs...
28
              <div class="avue-comment__body" :style="item.sendFrom === 2 ? 'background: #6BED72;' : ''">
2b477a4d   YunaiV   mp:实现 wx-msg 的消息列...
29
30
31
                <!-- 【事件】区域 -->
                <div v-if="item.type === 'event' && item.event === 'subscribe'">
                  <el-tag type="success" size="mini">关注</el-tag>
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
32
                </div>
2b477a4d   YunaiV   mp:实现 wx-msg 的消息列...
33
34
                <div v-else-if="item.type === 'event' && item.event === 'unsubscribe'">
                  <el-tag type="danger" size="mini">取消关注</el-tag>
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
35
                </div>
2b477a4d   YunaiV   mp:实现 wx-msg 的消息列...
36
37
                <div v-else-if="item.type === 'event' && item.event === 'CLICK'">
                  <el-tag size="mini">点击菜单</el-tag>【{{ item.eventKey }}】
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
38
                </div>
2b477a4d   YunaiV   mp:实现 wx-msg 的消息列...
39
40
                <div v-else-if="item.type === 'event' && item.event === 'VIEW'">
                  <el-tag size="mini">点击菜单链接</el-tag>【{{ item.eventKey }}】
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
41
                </div>
86002145   YunaiV   mp:优化【菜单】其它类型的处理
42
                <div v-else-if="item.type === 'event' && item.event === 'scancode_waitmsg'">
2b477a4d   YunaiV   mp:实现 wx-msg 的消息列...
43
                  <el-tag size="mini">扫码结果</el-tag>【{{ item.eventKey }}】
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
44
                </div>
86002145   YunaiV   mp:优化【菜单】其它类型的处理
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
                <div v-else-if="item.type === 'event' && item.event === 'scancode_push'">
                  <el-tag size="mini">扫码结果</el-tag>【{{ item.eventKey }}】
                </div>
                <div v-else-if="item.type === 'event' && item.event === 'pic_sysphoto'">
                  <el-tag size="mini">系统拍照发图</el-tag>
                </div>
                <div v-else-if="item.type === 'event' && item.event === 'pic_photo_or_album'">
                  <el-tag size="mini">拍照或者相册</el-tag>
                </div>
                <div v-else-if="item.type === 'event' && item.event === 'pic_weixin'">
                  <el-tag size="mini">微信相册</el-tag>
                </div>
                <div v-else-if="item.type === 'event' && item.event === 'location_select'">
                  <el-tag size="mini">选择地理位置</el-tag>
                </div>
2b477a4d   YunaiV   mp:实现 wx-msg 的消息列...
60
61
62
63
64
65
66
                <div v-else-if="item.type === 'event'">
                  <el-tag type="danger" size="mini">未知事件类型</el-tag>
                </div>
                <!-- 【消息】区域 -->
                <div v-else-if="item.type === 'text'">{{ item.content }}</div>
                <div v-else-if="item.type === 'voice'">
                  <wx-voice-player :url="item.mediaUrl" :content="item.recognition" />
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
67
                </div>
2b477a4d   YunaiV   mp:实现 wx-msg 的消息列...
68
69
70
71
                <div v-else-if="item.type === 'image'">
                  <a target="_blank" :href="item.mediaUrl">
                    <img :src="item.mediaUrl" style="width: 100px">
                  </a>
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
72
                </div>
2b477a4d   YunaiV   mp:实现 wx-msg 的消息列...
73
74
75
                <div v-else-if="item.type === 'video' || item.type === 'shortvideo'" style="text-align: center">
                  <wx-video-player :url="item.mediaUrl" />
                </div>
b0386429   YunaiV   mp:支持 wx-news、wx-...
76
                <div v-else-if="item.type === 'link'" class="avue-card__detail">
2b477a4d   YunaiV   mp:实现 wx-msg 的消息列...
77
78
                  <el-link type="success" :underline="false" target="_blank" :href="item.url">
                    <div class="avue-card__title"><i class="el-icon-link"></i>{{ item.title }}</div>
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
79
                  </el-link>
2b477a4d   YunaiV   mp:实现 wx-msg 的消息列...
80
                  <div class="avue-card__info" style="height: unset">{{item.description}}</div>
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
81
                </div>
9e4a978f   YunaiV   mp:解决 wx-msg 的 cs...
82
                <!-- TODO 芋艿:待完善 -->
b0386429   YunaiV   mp:支持 wx-news、wx-...
83
84
85
                <div v-else-if="item.type === 'location'">
                  <wx-location :label="item.label" :location-y="item.locationY" :location-x="item.locationX" />
                </div>
b0386429   YunaiV   mp:支持 wx-news、wx-...
86
87
88
                <div v-else-if="item.type === 'news'" style="width: 300px"> <!-- TODO 芋艿:待测试;详情页也存在类似的情况 -->
                  <wx-news :articles="item.articles" />
                </div>
6325dd28   YunaiV   mp:前端增加 message 消...
89
90
91
92
                <div v-else-if="item.type === 'music'">
                  <wx-music :title="item.title" :description="item.description" :thumb-media-url="item.thumbMediaUrl"
                    :music-url="item.musicUrl" :hq-music-url="item.hqMusicUrl" />
                </div>
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
93
94
95
96
97
              </div>
            </div>
          </div>
        </div>
      </div>
f8eed5dd   YunaiV   mp:初始化回复组件
98
      <div class="msg-send" v-loading="sendLoading">
188d8802   YunaiV   mp:增加【图片】素材的选择
99
        <wx-reply-select ref="replySelect" :objData="objData" />
f8eed5dd   YunaiV   mp:初始化回复组件
100
101
        <el-button type="success" size="small" class="send-but" @click="sendMsg">发送(S)</el-button>
      </div>
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
102
103
104
105
    </div>
  </template>
  
  <script>
188d8802   YunaiV   mp:增加【图片】素材的选择
106
  import { getMessagePage, sendMessage } from '@/api/mp/message'
f8eed5dd   YunaiV   mp:初始化回复组件
107
    import WxReplySelect from '@/views/mp/components/wx-reply/main.vue'
2b477a4d   YunaiV   mp:实现 wx-msg 的消息列...
108
109
    import WxVideoPlayer from '@/views/mp/components/wx-video-play/main.vue';
    import WxVoicePlayer from '@/views/mp/components/wx-voice-play/main.vue';
b0386429   YunaiV   mp:支持 wx-news、wx-...
110
111
    import WxNews from '@/views/mp/components/wx-news/main.vue';
    import WxLocation from '@/views/mp/components/wx-location/main.vue';
6325dd28   YunaiV   mp:前端增加 message 消...
112
    import WxMusic from '@/views/mp/components/wx-music/main.vue';
2ea90270   xingyu   mp user => mp mpuser
113
  import { getUser } from "@/api/mp/mpuser";
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
114
  
3166cd70   YunaiV   mp:同步 mp 公众号 SQL
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  export default {
    name: "wxMsg",
    components: {
      WxReplySelect,
      WxVideoPlayer,
      WxVoicePlayer,
      WxNews,
      WxLocation,
      WxMusic
    },
    props: {
      userId: {
        type: Number,
        required: true
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
129
      },
3166cd70   YunaiV   mp:同步 mp 公众号 SQL
130
131
132
133
134
135
136
137
138
139
140
    },
    data() {
      return {
        nowStr: new Date().getTime(), // 当前的时间戳,用于每次消息加载后,回到原位置;具体见 :id="'msg-div' + nowStr" 处
        loading: false, // 消息列表是否正在加载中
        loadMore: true, // 是否可以加载更多
        list: [], // 消息列表
        queryParams: {
          pageNo: 1, // 当前页数
          pageSize: 14, // 每页显示多少条
          accountId: undefined,
29cf7704   YunaiV   mp:支持 wx-reply 发送...
141
        },
3166cd70   YunaiV   mp:同步 mp 公众号 SQL
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
        user: { // 由于微信不再提供昵称,直接使用“用户”展示
          nickname: '用户',
          avatar: require("@/assets/images/profile.jpg"),
          accountId: 0, // 公众号账号编号
        },
        mp: {
          nickname: '公众号',
          avatar: require("@/assets/images/wechat.png"),
        },
  
        // ========= 消息发送 =========
        sendLoading: false, // 发送消息是否加载中
        objData: { // 微信发送消息
          type: 'text',
        },
      }
    },
    created() {
      // 获得用户信息
      getUser(this.userId).then(response => {
        this.user.nickname = response.data.nickname && response.data.nickname.length > 0 ?
            response.data.nickname : this.user.nickname;
        this.user.avatar = response.data.avatar && this.user.avatar.length > 0 ?
            response.data.avatar : this.user.avatar;
        this.user.accountId = response.data.accountId;
        // 设置公众号账号编号
        this.queryParams.accountId = response.data.accountId;
        this.objData.accountId = response.data.accountId;
68ef11ee   YunaiV   mp:完善微信公众号的消息
170
  
3166cd70   YunaiV   mp:同步 mp 公众号 SQL
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
        // 加载消息
        console.log(this.queryParams)
        this.refreshChange()
      })
    },
    methods:{
      sendMsg(){
        if (!this.objData) {
          return;
        }
        // 公众号限制:客服消息,公众号只允许发送一条
        if (this.objData.type === 'news'
          && this.objData.articles.length > 1) {
          this.objData.articles = [this.objData.articles[0]]
          this.$message({
            showClose: true,
            message: '图文消息条数限制在 1 条以内,已默认发送第一条',
            type: 'success'
          })
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
190
        }
29cf7704   YunaiV   mp:支持 wx-reply 发送...
191
  
3166cd70   YunaiV   mp:同步 mp 公众号 SQL
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
        // 执行发送
        this.sendLoading = true
        sendMessage(Object.assign({
          userId: this.userId
        }, {
          ...this.objData
        })).then(response => {
          this.sendLoading = false
          // 添加到消息列表,并滚动
          this.list = [...this.list , ...[response.data] ]
          this.scrollToBottom()
          // 重置 objData 状态
          this.$refs['replySelect'].deleteObj(); // 重置,避免 tab 的数据未清理
        }).catch(() => {
          this.sendLoading = false
68ef11ee   YunaiV   mp:完善微信公众号的消息
207
        })
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
208
      },
3166cd70   YunaiV   mp:同步 mp 公众号 SQL
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
      loadingMore() {
        this.queryParams.pageNo++
        this.getPage(this.queryParams)
      },
      getPage(page, params) {
        this.loading = true
        getMessagePage(Object.assign({
          pageNo: page.pageNo,
          pageSize: page.pageSize,
          userId: this.userId,
          accountId: page.accountId,
        }, params)).then(response => {
          // 计算当前的滚动高度
          const msgDiv = document.getElementById('msg-div' + this.nowStr);
          let scrollHeight = 0
          if(msgDiv){
            scrollHeight = msgDiv.scrollHeight
541ee813   YunaiV   mp:前端接入发送消息
226
          }
3166cd70   YunaiV   mp:同步 mp 公众号 SQL
227
228
229
230
231
232
233
  
          // 处理数据
          const data = response.data.list.reverse();
          this.list = [...data, ...this.list]
          this.loading = false
          if (data.length < this.queryParams.pageSize || data.length === 0){
            this.loadMore = false
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
234
          }
3166cd70   YunaiV   mp:同步 mp 公众号 SQL
235
236
          this.queryParams.pageNo = page.pageNo
          this.queryParams.pageSize = page.pageSize
02ccf52d   YunaiV   mp:增加发送【图文】消息
237
  
3166cd70   YunaiV   mp:同步 mp 公众号 SQL
238
239
          // 滚动到原来的位置
          if(this.queryParams.pageNo === 1) { // 定位到消息底部
541ee813   YunaiV   mp:前端接入发送消息
240
            this.scrollToBottom()
3166cd70   YunaiV   mp:同步 mp 公众号 SQL
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
          } else if (data.length !== 0) { // 定位滚动条
            this.$nextTick(() => {
              if (scrollHeight !== 0){
                msgDiv.scrollTop = document.getElementById('msg-div'+this.nowStr).scrollHeight - scrollHeight - 100
              }
            })
          }
        })
      },
      /**
       * 刷新回调
       */
      refreshChange() {
        this.getPage(this.queryParams)
      },
      /** 定位到消息底部 */
      scrollToBottom: function () {
        this.$nextTick(() => {
          let div = document.getElementById('msg-div' + this.nowStr)
          div.scrollTop = div.scrollHeight
        })
      },
    }
  };
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
265
  </script>
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
266
  <style lang="scss" scoped>
9e4a978f   YunaiV   mp:解决 wx-msg 的 cs...
267
268
269
270
271
  /* 因为 joolun 实现依赖 avue 组件,该页面使用了 comment.scss、card.scc  */
  @import './comment.scss';
  @import './card.scss';
  
  .msg-main {
cc23ca2d   YunaiV   mp:解决 wx-msg 的 cs...
272
273
274
    margin-top: -30px;
    padding: 10px;
  }
9e4a978f   YunaiV   mp:解决 wx-msg 的 cs...
275
  .msg-div {
cc23ca2d   YunaiV   mp:解决 wx-msg 的 cs...
276
277
278
    height: 50vh;
    overflow: auto;
    background-color: #eaeaea;
68ef11ee   YunaiV   mp:完善微信公众号的消息
279
280
    margin-left: 10px;
    margin-right: 10px;
cc23ca2d   YunaiV   mp:解决 wx-msg 的 cs...
281
  }
9e4a978f   YunaiV   mp:解决 wx-msg 的 cs...
282
  .msg-send {
cc23ca2d   YunaiV   mp:解决 wx-msg 的 cs...
283
284
    padding: 10px;
  }
9e4a978f   YunaiV   mp:解决 wx-msg 的 cs...
285
  .avatar-div {
cc23ca2d   YunaiV   mp:解决 wx-msg 的 cs...
286
287
288
    text-align: center;
    width: 80px;
  }
9e4a978f   YunaiV   mp:解决 wx-msg 的 cs...
289
  .send-but {
cc23ca2d   YunaiV   mp:解决 wx-msg 的 cs...
290
291
292
    float: right;
    margin-top: 8px!important;
  }
df5e6161   YunaiV   mp:重构 wx-msg 前的提交
293
  </style>