|
|
@@ -388,7 +388,7 @@ export default {
|
|
|
this.setIschat({ notice: false })
|
|
|
this.$store.dispatch('chat/update', { type: 'del', id: res.data.user_id }).catch(() => {})
|
|
|
setTimeout(() => {
|
|
|
- this.loadHistory()
|
|
|
+ this.loadHistory({ bootstrap: true })
|
|
|
}, 500)
|
|
|
} catch (e) {
|
|
|
console.error('[chat] getShopChat failed:', e)
|
|
|
@@ -417,35 +417,127 @@ export default {
|
|
|
this.setIschat({ notice: true, number: 0 })
|
|
|
},
|
|
|
onShow() {
|
|
|
- this.scrollTop = 9999999
|
|
|
+ if (this.msgList.length) {
|
|
|
+ this.scrollToLatest()
|
|
|
+ }
|
|
|
},
|
|
|
methods: {
|
|
|
...mapMutations({
|
|
|
setIschat: 'chat/setIschat'
|
|
|
}),
|
|
|
+ /** 滚到最新消息(强制变更 scrollTop,避免同值不生效) */
|
|
|
+ scrollToLatest() {
|
|
|
+ if (!this.msgList.length) return
|
|
|
+ const last = this.msgList[this.msgList.length - 1]
|
|
|
+ this.scrollAnimation = false
|
|
|
+ this.scrollToView = ''
|
|
|
+ this.scrollTop = 0
|
|
|
+ this.$nextTick(() => {
|
|
|
+ this.scrollTop = 9999999 + (Date.now() % 1000)
|
|
|
+ this.scrollToView = 'msg' + last.id
|
|
|
+ this.$nextTick(() => {
|
|
|
+ this.scrollAnimation = true
|
|
|
+ })
|
|
|
+ })
|
|
|
+ },
|
|
|
getMsgList(id) {
|
|
|
- uni.getStorage({
|
|
|
- key: STORAGE.chatMsgPrefix + id,
|
|
|
- success: res => {
|
|
|
- var list = res.data
|
|
|
- for (let i = 0; i < list.length; i++) {
|
|
|
- if (list[i].type == 'chat' && list[i].message.type == 'img') {
|
|
|
- list[i].message.content = this.setPicSize(list[i].message.content)
|
|
|
- this.msgImgList.push(list[i].message.content.url)
|
|
|
- }
|
|
|
- if (list[i].type == 'chat' && list[i].message.type == 'text') {
|
|
|
- list[i].message.content.text = this.replaceEmoji(list[i].message.content.text)
|
|
|
- }
|
|
|
+ const primary = STORAGE.chatMsgPrefix + id
|
|
|
+ const legacy = 'wanlchat:message_' + id
|
|
|
+ let list = uni.getStorageSync(primary)
|
|
|
+ if (!Array.isArray(list) || !list.length) {
|
|
|
+ list = uni.getStorageSync(legacy)
|
|
|
+ // 旧 key 有数据时迁移到统一前缀
|
|
|
+ if (Array.isArray(list) && list.length) {
|
|
|
+ uni.setStorageSync(primary, list)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!Array.isArray(list) || !list.length) return
|
|
|
+ for (let i = 0; i < list.length; i++) {
|
|
|
+ if (list[i].type == 'chat' && list[i].message && list[i].message.type == 'img') {
|
|
|
+ list[i].message.content = this.setPicSize(list[i].message.content)
|
|
|
+ this.msgImgList.push(list[i].message.content.url)
|
|
|
+ }
|
|
|
+ if (list[i].type == 'chat' && list[i].message && list[i].message.type == 'text') {
|
|
|
+ list[i].message.content.text = this.replaceEmoji(list[i].message.content.text)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ this.msgList = list
|
|
|
+ this.$nextTick(() => {
|
|
|
+ this.scrollToLatest()
|
|
|
+ })
|
|
|
+ },
|
|
|
+ async loadHistory(options) {
|
|
|
+ // scrolltoupper 传入事件对象;进页引导传 { bootstrap: true }
|
|
|
+ const bootstrap = !!(options && options.bootstrap)
|
|
|
+ if (this.isHistoryLoading || this.reload || !this.to_id) return
|
|
|
+ this.isHistoryLoading = true
|
|
|
+ this.scrollAnimation = false
|
|
|
+ const hadLocal = this.msgList.length > 0
|
|
|
+ // 上拉加载更多时锚定原第一条;首次进页要滚到最新,不能锚到顶部
|
|
|
+ const view_id = (!bootstrap && hadLocal) ? this.msgList[0].id : null
|
|
|
+ try {
|
|
|
+ const res = await getChatHistory({ to_id: this.to_id, page: this.current_page })
|
|
|
+ const page = res && res.data
|
|
|
+ let list = []
|
|
|
+ let currentPage = 1
|
|
|
+ let lastPage = 1
|
|
|
+ if (Array.isArray(page)) {
|
|
|
+ list = page
|
|
|
+ } else if (page && typeof page === 'object') {
|
|
|
+ list = page.data || page.rows || page.list || []
|
|
|
+ currentPage = Number(page.current_page) || 1
|
|
|
+ lastPage = Number(page.last_page) || currentPage
|
|
|
+ }
|
|
|
+ if (!Array.isArray(list)) list = []
|
|
|
+
|
|
|
+ // 先拷贝一份用于本地缓存(避免把表情 HTML 写回)
|
|
|
+ const forStore = currentPage == 1 ? JSON.parse(JSON.stringify(list)) : null
|
|
|
+
|
|
|
+ for (let i = 0; i < list.length; i++) {
|
|
|
+ if (list[i].type == 'chat' && list[i].message && list[i].message.type == 'img') {
|
|
|
+ list[i].message.content = this.setPicSize(list[i].message.content)
|
|
|
+ this.msgImgList.unshift(list[i].message.content.url)
|
|
|
}
|
|
|
- this.msgList = list
|
|
|
- this.$nextTick(() => {
|
|
|
- this.scrollTop = 9999
|
|
|
+ if (list[i].type == 'chat' && list[i].message && list[i].message.type == 'text') {
|
|
|
+ list[i].message.content.text = this.replaceEmoji(list[i].message.content.text)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ list.sort(function (a, b) {
|
|
|
+ return (a.updatetime || a.createtime || 0) - (b.updatetime || b.createtime || 0)
|
|
|
+ })
|
|
|
+
|
|
|
+ if (currentPage == 1) {
|
|
|
+ // 本地已有且服务端本页为空时,保留本地,避免被清空
|
|
|
+ if (list.length || !hadLocal) {
|
|
|
+ this.msgList = list
|
|
|
+ }
|
|
|
+ if (forStore && forStore.length && this.to_id) {
|
|
|
+ uni.setStorageSync(STORAGE.chatMsgPrefix + this.to_id, forStore)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ this.msgList = list.concat(this.msgList)
|
|
|
+ }
|
|
|
+
|
|
|
+ this.$nextTick(() => {
|
|
|
+ if (bootstrap || !view_id) {
|
|
|
+ this.scrollToLatest()
|
|
|
+ } else {
|
|
|
+ this.scrollToView = 'msg' + view_id
|
|
|
this.$nextTick(() => {
|
|
|
this.scrollAnimation = true
|
|
|
})
|
|
|
- })
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ if (currentPage >= lastPage || list.length === 0) {
|
|
|
+ this.reload = true
|
|
|
+ } else {
|
|
|
+ this.current_page = currentPage + 1
|
|
|
}
|
|
|
- })
|
|
|
+ } catch (e) {
|
|
|
+ console.error('[chat] loadHistory failed:', e)
|
|
|
+ }
|
|
|
+ this.isHistoryLoading = false
|
|
|
},
|
|
|
onChat(msg) {
|
|
|
if (msg.type == 'system') {
|
|
|
@@ -463,7 +555,6 @@ export default {
|
|
|
},
|
|
|
onSend(msg) {
|
|
|
if (msg.form.id == this.to_id) {
|
|
|
- // 新增:保存到本地緩存
|
|
|
setChat(msg, 'receive')
|
|
|
if (msg.type == 'system') {
|
|
|
if (msg.msg.type == 'text') this.addSystemTextMsg(msg)
|
|
|
@@ -566,43 +657,6 @@ export default {
|
|
|
tabSelect(e) {
|
|
|
this.TabCur = e.currentTarget.dataset.id
|
|
|
},
|
|
|
- async loadHistory() {
|
|
|
- if (this.isHistoryLoading || this.reload) return
|
|
|
- this.isHistoryLoading = true
|
|
|
- this.scrollAnimation = false
|
|
|
- let view_id = this.msgList[0].id
|
|
|
- try {
|
|
|
- const res = await getChatHistory({ to_id: this.to_id, page: this.current_page })
|
|
|
- let list = res.data.data
|
|
|
- for (let i = 0; i < list.length; i++) {
|
|
|
- if (list[i].type == 'chat' && list[i].message.type == 'img') {
|
|
|
- list[i].message.content = this.setPicSize(list[i].message.content)
|
|
|
- this.msgImgList.unshift(list[i].message.content.url)
|
|
|
- }
|
|
|
- if (list[i].type == 'chat' && list[i].message.type == 'text') {
|
|
|
- list[i].message.content.text = this.replaceEmoji(list[i].message.content.text)
|
|
|
- }
|
|
|
- }
|
|
|
- list.sort(function (a, b) {
|
|
|
- return a.updatetime - b.updatetime
|
|
|
- })
|
|
|
- this.msgList = res.data.current_page == 1 ? list : list.concat(this.msgList)
|
|
|
- this.$nextTick(() => {
|
|
|
- this.scrollToView = 'msg' + view_id
|
|
|
- this.$nextTick(() => {
|
|
|
- this.scrollAnimation = true
|
|
|
- })
|
|
|
- })
|
|
|
- if (res.data.current_page == res.data.last_page) {
|
|
|
- this.reload = true
|
|
|
- } else {
|
|
|
- this.current_page = res.data.current_page + 1
|
|
|
- }
|
|
|
- } catch (e) {
|
|
|
- console.error('[chat] loadHistory failed:', e)
|
|
|
- }
|
|
|
- this.isHistoryLoading = false
|
|
|
- },
|
|
|
setPicSize(content) {
|
|
|
let maxW = uni.upx2px(350)
|
|
|
let maxH = uni.upx2px(350)
|