Skip to content

Commit 1505dcd

Browse files
committed
mp:优化前端消息发送的缓存逻辑
1 parent 519909e commit 1505dcd

File tree

2 files changed

+52
-35
lines changed

2 files changed

+52
-35
lines changed

src/views/mp/components/wx-msg/main.vue

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,6 @@ import { getUser } from "@/api/mp/user";
177177
this.scrollToBottom()
178178
// 重置 objData 状态
179179
this.$refs['replySelect'].deleteObj(); // 重置,避免 tab 的数据未清理
180-
this.objData = {
181-
type: 'text',
182-
content: '',
183-
}
184180
}).catch(() => {
185181
this.sendLoading = false
186182
})

src/views/mp/components/wx-reply/main.vue

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@
8585
<el-tab-pane name="video">
8686
<span slot="label"><i class="el-icon-share"></i> 视频</span>
8787
<el-row>
88-
<el-input v-model="objData.title" placeholder="请输入标题"></el-input>
88+
<el-input v-model="objData.title" placeholder="请输入标题" @input="inputContent" />
8989
<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" />
9191
<div style="margin: 20px 0;"></div>
9292
<div style="text-align: center;">
9393
<wx-video-player v-if="objData.url" :url="objData.url" />
@@ -149,9 +149,9 @@
149149
</el-col>
150150
</el-row>
151151
<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" />
153153
<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" />
155155
</el-tab-pane>
156156
</el-tabs>
157157
</template>
@@ -309,27 +309,20 @@
309309
310310
// 从 tempObj 临时缓存中,获取对应的数据,并设置回 objData
311311
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)
316312
if (tempObjItem) {
317-
console.log(this.tempObj)
318313
this.objData.content = tempObjItem.content ? tempObjItem.content : null
319314
this.objData.mediaId = tempObjItem.mediaId ? tempObjItem.mediaId : null
320315
this.objData.url = tempObjItem.url ? tempObjItem.url : null
321316
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
325319
return;
326320
}
327321
// 如果获取不到,需要把 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', '');
333326
},
334327
/**
335328
* 选择素材,将设置设置到 objData 变量
@@ -348,11 +341,11 @@
348341
this.objData.thumbMediaId = item.mediaId
349342
tempObjItem.thumbMediaUrl = item.url
350343
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 || ''
356349
} else if (this.objData.type === 'image'
357350
|| this.objData.type === 'voice') {
358351
tempObjItem.mediaId = item.mediaId
@@ -368,11 +361,13 @@
368361
this.objData.url = item.url;
369362
tempObjItem.name = item.name
370363
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 || ''
376371
}
377372
// 最终设置到临时缓存
378373
this.tempObj.set(this.objData.type, tempObjItem)
@@ -398,8 +393,34 @@
398393
this.dialogThumbVisible = false
399394
},
400395
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));
403424
},
404425
getPage(page, params) {
405426
this.tableLoading = true
@@ -426,8 +447,8 @@
426447
* why?不确定为什么 v-model="objData.content" 不能自动缓存,所以通过这样的方式
427448
*/
428449
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));
431452
}
432453
}
433454
};

0 commit comments

Comments
 (0)