Commit 858b4e81a5298a00d10c648891b81f1149b65818

Authored by YunaiV
1 parent 7ba6a2e3

mp:优化前端消息发送的缓存逻辑

yudao-module-mp/yudao-module-mp-api/src/main/java/cn/iocoder/yudao/module/mp/enums/ErrorCodeConstants.java
... ... @@ -34,6 +34,9 @@ public interface ErrorCodeConstants {
34 34 // ========== 公众号素材 1006004000============
35 35 ErrorCode MATERIAL_UPLOAD_FAIL = new ErrorCode(1006004000, "上传素材失败,原因:{}");
36 36  
  37 + // ========== 公众号素材 1006005000============
  38 + ErrorCode MESSAGE_SEND_FAIL = new ErrorCode(1006005000, "发送消息失败,原因:{}");
  39 +
37 40 // TODO 要处理下
38 41 ErrorCode MENU_NOT_EXISTS = new ErrorCode(1006001002, "菜单不存在");
39 42  
... ...
yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/service/message/MpMessageServiceImpl.java
... ... @@ -31,6 +31,9 @@ import org.springframework.validation.annotation.Validated;
31 31 import javax.annotation.Resource;
32 32 import javax.validation.Validator;
33 33  
  34 +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
  35 +import static cn.iocoder.yudao.module.mp.enums.ErrorCodeConstants.MESSAGE_SEND_FAIL;
  36 +
34 37 /**
35 38 * 粉丝消息表 Service 实现类
36 39 *
... ... @@ -115,7 +118,7 @@ public class MpMessageServiceImpl implements MpMessageService {
115 118 try {
116 119 mpService.getKefuService().sendKefuMessageWithResponse(wxMessage);
117 120 } catch (WxErrorException e) {
118   - throw new RuntimeException(e);
  121 + throw exception(MESSAGE_SEND_FAIL, e.getError().getErrorMsg());
119 122 }
120 123  
121 124 // 记录消息
... ...
yudao-ui-admin/src/views/mp/components/wx-msg/main.vue
... ... @@ -177,10 +177,6 @@ import { getUser } from "@/api/mp/user";
177 177 this.scrollToBottom()
178 178 // 重置 objData 状态
179 179 this.$refs['replySelect'].deleteObj(); // 重置,避免 tab 的数据未清理
180   - this.objData = {
181   - type: 'text',
182   - content: '',
183   - }
184 180 }).catch(() => {
185 181 this.sendLoading = false
186 182 })
... ...
yudao-ui-admin/src/views/mp/components/wx-reply/main.vue
... ... @@ -85,9 +85,9 @@
85 85 <el-tab-pane name="video">
86 86 <span slot="label"><i class="el-icon-share"></i> 视频</span>
87 87 <el-row>
88   - <el-input v-model="objData.title" placeholder="请输入标题"></el-input>
  88 + <el-input v-model="objData.title" placeholder="请输入标题" @input="inputContent" />
89 89 <div style="margin: 20px 0;"></div>
90   - <el-input v-model="objData.description" placeholder="请输入描述"></el-input>
  90 + <el-input v-model="objData.description" placeholder="请输入描述" @input="inputContent" />
91 91 <div style="margin: 20px 0;"></div>
92 92 <div style="text-align: center;">
93 93 <wx-video-player v-if="objData.url" :url="objData.url" />
... ... @@ -149,9 +149,9 @@
149 149 </el-col>
150 150 </el-row>
151 151 <div style="margin: 20px 0;"></div>
152   - <el-input v-model="objData.musicUrl" placeholder="请输入音乐链接"></el-input>
  152 + <el-input v-model="objData.musicUrl" placeholder="请输入音乐链接" @input="inputContent" />
153 153 <div style="margin: 20px 0;"></div>
154   - <el-input v-model="objData.hqMusicUrl" placeholder="请输入高质量音乐链接"></el-input>
  154 + <el-input v-model="objData.hqMusicUrl" placeholder="请输入高质量音乐链接" @input="inputContent" />
155 155 </el-tab-pane>
156 156 </el-tabs>
157 157 </template>
... ... @@ -309,27 +309,20 @@
309 309  
310 310 // 从 tempObj 临时缓存中,获取对应的数据,并设置回 objData
311 311 let tempObjItem = this.tempObj.get(this.objData.type)
312   - // console.log(this.objData.type)
313   - // console.log(tempObjItem)
314   - // console.log(this.objData)
315   - // console.log(this.tempObj)
316 312 if (tempObjItem) {
317   - console.log(this.tempObj)
318 313 this.objData.content = tempObjItem.content ? tempObjItem.content : null
319 314 this.objData.mediaId = tempObjItem.mediaId ? tempObjItem.mediaId : null
320 315 this.objData.url = tempObjItem.url ? tempObjItem.url : null
321 316 this.objData.name = tempObjItem.url ? tempObjItem.name : null
322   -
323   - // TODO 芋艿:临时注释掉,看看有没用
324   - // this.objData.repDesc = tempObjItem.repDesc ? tempObjItem.repDesc : null
  317 + this.objData.title = tempObjItem.title ? tempObjItem.title : null
  318 + this.objData.description = tempObjItem.description ? tempObjItem.description : null
325 319 return;
326 320 }
327 321 // 如果获取不到,需要把 objData 复原
328   - this.objData.content = undefined;
329   - this.objData.mediaId = undefined;
330   - this.objData.url = undefined;
331   - this.objData.name = undefined;
332   - // this.$delete(this.objData,'repDesc')
  322 + // 必须使用 $set 赋值,不然 input 无法输入内容
  323 + this.$set(this.objData, 'content', '');
  324 + this.$set(this.objData, 'title', '');
  325 + this.$set(this.objData, 'description', '');
333 326 },
334 327 /**
335 328 * 选择素材,将设置设置到 objData 变量
... ... @@ -348,11 +341,11 @@
348 341 this.objData.thumbMediaId = item.mediaId
349 342 tempObjItem.thumbMediaUrl = item.url
350 343 this.objData.thumbMediaUrl = item.url
351   - // title、introduction、musicUrl、hqMusicUrl
352   - tempObjItem.title = this.objData.title
353   - tempObjItem.introduction = this.objData.introduction
354   - tempObjItem.musicUrl = this.objData.musicUrl
355   - tempObjItem.hqMusicUrl = this.objData.hqMusicUrl
  344 + // title、introduction、musicUrl、hqMusicUrl:从 objData 到 tempObjItem,避免上传素材后,被覆盖掉
  345 + tempObjItem.title = this.objData.title || ''
  346 + tempObjItem.introduction = this.objData.introduction || ''
  347 + tempObjItem.musicUrl = this.objData.musicUrl || ''
  348 + tempObjItem.hqMusicUrl = this.objData.hqMusicUrl || ''
356 349 } else if (this.objData.type === 'image'
357 350 || this.objData.type === 'voice') {
358 351 tempObjItem.mediaId = item.mediaId
... ... @@ -368,11 +361,13 @@
368 361 this.objData.url = item.url;
369 362 tempObjItem.name = item.name
370 363 this.objData.name = item.name
371   - // title、introduction
372   - this.objData.title = item.title
373   - tempObjItem.title = this.objData.title
374   - this.objData.description = item.introduction // 消息使用的是 description,素材使用的是 introduction,所以转换下
375   - tempObjItem.description = this.objData.introduction
  364 + // title、introduction:从 item 到 tempObjItem,因为素材里有 title、introduction
  365 + this.objData.title = item.title || ''
  366 + tempObjItem.title = item.title || ''
  367 + this.objData.description = item.introduction || '' // 消息使用的是 description,素材使用的是 introduction,所以转换下
  368 + tempObjItem.description = item.introduction || ''
  369 + } else if (this.objData.type === 'text') {
  370 + this.objData.content = item.content || ''
376 371 }
377 372 // 最终设置到临时缓存
378 373 this.tempObj.set(this.objData.type, tempObjItem)
... ... @@ -398,8 +393,34 @@
398 393 this.dialogThumbVisible = false
399 394 },
400 395 deleteObj() {
401   - // this.$delete(this.objData, 'url'); TODO 芋艿:重新实现清空;还有 reset
402   - this.selectMaterial({}) // 选择一个空的素材
  396 + if (this.objData.type === 'news') {
  397 + this.objData.articles = []
  398 + } else if(this.objData.type === 'image') {
  399 + this.objData.mediaId = null
  400 + this.objData.url = null
  401 + this.objData.name = null
  402 + } else if(this.objData.type === 'voice') {
  403 + this.objData.mediaId = null
  404 + this.objData.url = null
  405 + this.objData.name = null
  406 + } else if(this.objData.type === 'video') {
  407 + this.objData.mediaId = null
  408 + this.objData.url = null
  409 + this.objData.name = null
  410 + this.objData.title = null
  411 + this.objData.description = null
  412 + } else if(this.objData.type === 'music') {
  413 + this.objData.thumbMediaId = null
  414 + this.objData.thumbMediaUrl = null
  415 + this.objData.title = null
  416 + this.objData.description = null
  417 + this.objData.musicUrl = null
  418 + this.objData.hqMusicUrl = null
  419 + } else if(this.objData.type === 'text') {
  420 + this.objData.content = null
  421 + }
  422 + // 覆盖缓存
  423 + this.tempObj.set(this.objData.type, Object.assign({}, this.objData));
403 424 },
404 425 getPage(page, params) {
405 426 this.tableLoading = true
... ... @@ -426,8 +447,8 @@
426 447 * why?不确定为什么 v-model="objData.content" 不能自动缓存,所以通过这样的方式
427 448 */
428 449 inputContent(str) {
429   - let tempObjItem = {...this.objData};
430   - this.tempObj.set(this.objData.type, tempObjItem);
  450 + // 覆盖缓存
  451 + this.tempObj.set(this.objData.type, Object.assign({}, this.objData));
431 452 }
432 453 }
433 454 };
... ...