chat.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // 店铺客服聊天模块
  2. import Vue from 'vue'
  3. import {
  4. getChatLists,
  5. delChatSession,
  6. clearChatUnread,
  7. readAllChat
  8. } from '@/features/mall/gateway/im'
  9. import { STORAGE } from '@/core/constants/storage-keys'
  10. export default {
  11. namespaced: true,
  12. state: {
  13. list: [],
  14. ischat: {
  15. notice: true,
  16. number: 0
  17. }
  18. },
  19. mutations: {
  20. setIschat(state, payload) {
  21. for (let i in payload) {
  22. for (let j in state.ischat) {
  23. if (i === j) {
  24. state.ischat[j] = payload[i]
  25. }
  26. }
  27. }
  28. }
  29. },
  30. actions: {
  31. async get({ state, dispatch }) {
  32. try {
  33. const res = await getChatLists()
  34. const rows = Array.isArray(res.data) ? res.data : (res.data && res.data.rows) || []
  35. state.list = rows
  36. let count = 0
  37. rows.forEach(item => { count += Number(item.count) || 0 })
  38. dispatch('storage', { type: 'statis', number: count })
  39. } catch (e) {
  40. console.error('[chat] get failed:', e)
  41. }
  42. },
  43. async del({ state, dispatch }, index) {
  44. const list = state.list
  45. const row = list[index]
  46. if (!row) return
  47. dispatch('storage', { type: 'del', number: row.count })
  48. uni.removeStorage({
  49. key: STORAGE.chatMsgPrefix + row.user_id,
  50. success: () => {}
  51. })
  52. try {
  53. await delChatSession({ id: row.user_id })
  54. } catch (e) {
  55. console.error('[chat] del failed:', e)
  56. }
  57. Vue.delete(state.list, index)
  58. },
  59. async empty({ state, dispatch }) {
  60. const translate = (key, fallback) => {
  61. try {
  62. const app = typeof getApp === 'function' ? getApp() : null
  63. const vm = app && app.$vm
  64. if (vm && typeof vm.$t === 'function') {
  65. const v = vm.$t(key)
  66. if (v && v !== key) return v
  67. }
  68. } catch (_) {}
  69. return fallback
  70. }
  71. uni.showModal({
  72. content: translate('chat.markAllReadConfirm', 'Mark all as read?'),
  73. success: async res => {
  74. if (!res.confirm) return
  75. state.list.forEach(item => { item.count = 0 })
  76. dispatch('storage', { type: 'empty' })
  77. try {
  78. await readAllChat()
  79. uni.showToast({
  80. title: translate('chat.markAllReadSuccess', 'Done'),
  81. icon: 'none'
  82. })
  83. } catch (e) {
  84. console.error('[chat] read failed:', e)
  85. }
  86. }
  87. })
  88. },
  89. async update({ state, dispatch }, { type, data, id, shop }) {
  90. if (type == 'del') {
  91. let counts = 0
  92. state.list.some(chat => {
  93. if (chat.user_id == id) {
  94. counts = chat.count
  95. chat.count = 0
  96. return true
  97. }
  98. })
  99. dispatch('storage', { type: 'del', number: counts })
  100. try {
  101. await clearChatUnread({ id })
  102. } catch (e) {
  103. console.error('[chat] update clear failed:', e)
  104. }
  105. } else if (type == 'chat' || type == 'send') {
  106. let content = ''
  107. const createtime = data.createtime
  108. let chat_id = 0
  109. if (data.message.type == 'text') {
  110. content = data.message.content.text
  111. } else if (data.message.type == 'img') {
  112. content = '[Image Message]'
  113. } else if (data.message.type == 'voice') {
  114. content = '[Voice Message]'
  115. } else if (data.message.type == 'goods') {
  116. content = '[Goods Message]'
  117. } else {
  118. content = '[Unknown Type Message]'
  119. }
  120. if (type == 'chat') {
  121. chat_id = data.form.id
  122. dispatch('storage', { type: 'chat' })
  123. } else if (type == 'send') {
  124. chat_id = data.to_id
  125. }
  126. const result = state.list.some(chat => {
  127. if (chat.user_id == chat_id) {
  128. if (type == 'chat') {
  129. chat.count += 1
  130. }
  131. chat.createtime = createtime
  132. chat.content = content
  133. return true
  134. }
  135. })
  136. if (!result && shop) {
  137. state.list.unshift({
  138. id: shop.id,
  139. user_id: shop.user_id,
  140. name: shop.name,
  141. avatar: shop.avatar,
  142. content: content,
  143. count: type == 'chat' ? 1 : 0,
  144. createtime: createtime
  145. })
  146. }
  147. }
  148. },
  149. async storage({ rootState }, { type, number }) {
  150. if (type == 'statis') {
  151. rootState.statistics.notice.chat = number
  152. } else if (type == 'order') {
  153. rootState.statistics.notice.order += 1
  154. } else if (type == 'notice') {
  155. rootState.statistics.notice.notice += 1
  156. } else if (type == 'chat') {
  157. rootState.statistics.notice.chat += 1
  158. } else if (type == 'del') {
  159. rootState.statistics.notice.chat -= number
  160. } else if (type == 'empty') {
  161. rootState.statistics.notice.chat = 0
  162. rootState.statistics.notice.order = 0
  163. rootState.statistics.notice.notice = 0
  164. }
  165. uni.setStorageSync(STORAGE.stats, rootState.statistics)
  166. }
  167. }
  168. }