|
|
@@ -1,6 +1,12 @@
|
|
|
// 店铺客服聊天模块
|
|
|
import Vue from 'vue'
|
|
|
-import appSettings from '@/core/config/app-settings'
|
|
|
+import {
|
|
|
+ getChatLists,
|
|
|
+ delChatSession,
|
|
|
+ clearChatUnread,
|
|
|
+ readAllChat
|
|
|
+} from '@/features/mall/gateway/im'
|
|
|
+import { STORAGE } from '@/core/constants/storage-keys'
|
|
|
|
|
|
export default {
|
|
|
namespaced: true,
|
|
|
@@ -25,35 +31,27 @@ export default {
|
|
|
actions: {
|
|
|
async get({ state, dispatch }) {
|
|
|
try {
|
|
|
- const res = await uni.request({
|
|
|
- url: '/wanlshop/chat/lists',
|
|
|
- method: 'POST'
|
|
|
- })
|
|
|
- if (res && res.data) {
|
|
|
- state.list = res.data
|
|
|
- let count = 0
|
|
|
- res.data.forEach(item => { count += item.count })
|
|
|
- dispatch('storage', { type: 'statis', number: count })
|
|
|
- }
|
|
|
+ const res = await getChatLists()
|
|
|
+ const rows = Array.isArray(res.data) ? res.data : (res.data && res.data.rows) || []
|
|
|
+ state.list = rows
|
|
|
+ let count = 0
|
|
|
+ rows.forEach(item => { count += Number(item.count) || 0 })
|
|
|
+ dispatch('storage', { type: 'statis', number: count })
|
|
|
} catch (e) {
|
|
|
console.error('[chat] get failed:', e)
|
|
|
}
|
|
|
},
|
|
|
async del({ state, dispatch }, index) {
|
|
|
- let list = state.list
|
|
|
- dispatch('storage', { type: 'del', number: list[index].count })
|
|
|
+ const list = state.list
|
|
|
+ const row = list[index]
|
|
|
+ if (!row) return
|
|
|
+ dispatch('storage', { type: 'del', number: row.count })
|
|
|
uni.removeStorage({
|
|
|
- key: 'wanlchat:message_' + list[index].user_id,
|
|
|
- success: () => {
|
|
|
- console.log('[chat] 删除消息成功')
|
|
|
- }
|
|
|
+ key: STORAGE.chatMsgPrefix + row.user_id,
|
|
|
+ success: () => {}
|
|
|
})
|
|
|
try {
|
|
|
- await uni.request({
|
|
|
- url: '/wanlshop/chat/del',
|
|
|
- method: 'POST',
|
|
|
- data: { id: list[index].user_id }
|
|
|
- })
|
|
|
+ await delChatSession({ id: row.user_id })
|
|
|
} catch (e) {
|
|
|
console.error('[chat] del failed:', e)
|
|
|
}
|
|
|
@@ -62,17 +60,15 @@ export default {
|
|
|
async empty({ state, dispatch }) {
|
|
|
uni.showModal({
|
|
|
content: '確定將所有數據標為已讀嗎?',
|
|
|
- success: res => {
|
|
|
- if (res.confirm) {
|
|
|
- state.list.forEach(item => { item.count = 0 })
|
|
|
- dispatch('storage', { type: 'empty' })
|
|
|
- uni.request({
|
|
|
- url: '/wanlshop/chat/read',
|
|
|
- method: 'POST',
|
|
|
- success: () => {
|
|
|
- uni.showToast({ title: '已全部標為已讀', icon: 'none' })
|
|
|
- }
|
|
|
- })
|
|
|
+ success: async res => {
|
|
|
+ if (!res.confirm) return
|
|
|
+ state.list.forEach(item => { item.count = 0 })
|
|
|
+ dispatch('storage', { type: 'empty' })
|
|
|
+ try {
|
|
|
+ await readAllChat()
|
|
|
+ uni.showToast({ title: '已全部標為已讀', icon: 'none' })
|
|
|
+ } catch (e) {
|
|
|
+ console.error('[chat] read failed:', e)
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
@@ -89,17 +85,13 @@ export default {
|
|
|
})
|
|
|
dispatch('storage', { type: 'del', number: counts })
|
|
|
try {
|
|
|
- await uni.request({
|
|
|
- url: '/wanlshop/chat/clear',
|
|
|
- method: 'POST',
|
|
|
- data: { id: id }
|
|
|
- })
|
|
|
+ await clearChatUnread({ id })
|
|
|
} catch (e) {
|
|
|
console.error('[chat] update clear failed:', e)
|
|
|
}
|
|
|
} else if (type == 'chat' || type == 'send') {
|
|
|
let content = ''
|
|
|
- let createtime = data.createtime
|
|
|
+ const createtime = data.createtime
|
|
|
let chat_id = 0
|
|
|
if (data.message.type == 'text') {
|
|
|
content = data.message.content.text
|
|
|
@@ -118,7 +110,7 @@ export default {
|
|
|
} else if (type == 'send') {
|
|
|
chat_id = data.to_id
|
|
|
}
|
|
|
- let result = state.list.some(chat => {
|
|
|
+ const result = state.list.some(chat => {
|
|
|
if (chat.user_id == chat_id) {
|
|
|
if (type == 'chat') {
|
|
|
chat.count += 1
|
|
|
@@ -128,20 +120,20 @@ export default {
|
|
|
return true
|
|
|
}
|
|
|
})
|
|
|
- if (!result) {
|
|
|
- Vue.set(state.list, 0, {
|
|
|
+ if (!result && shop) {
|
|
|
+ state.list.unshift({
|
|
|
id: shop.id,
|
|
|
user_id: shop.user_id,
|
|
|
name: shop.name,
|
|
|
avatar: shop.avatar,
|
|
|
content: content,
|
|
|
- count: 1,
|
|
|
+ count: type == 'chat' ? 1 : 0,
|
|
|
createtime: createtime
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
- async storage({ state, rootState }, { type, number }) {
|
|
|
+ async storage({ rootState }, { type, number }) {
|
|
|
if (type == 'statis') {
|
|
|
rootState.statistics.notice.chat = number
|
|
|
} else if (type == 'order') {
|
|
|
@@ -157,7 +149,7 @@ export default {
|
|
|
rootState.statistics.notice.order = 0
|
|
|
rootState.statistics.notice.notice = 0
|
|
|
}
|
|
|
- uni.setStorageSync('wanlshop:statis', rootState.statistics)
|
|
|
+ uni.setStorageSync(STORAGE.stats, rootState.statistics)
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
+}
|