chat.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // 店铺客服聊天模块
  2. import Vue from 'vue'
  3. import appSettings from '@/core/config/app-settings'
  4. export default {
  5. namespaced: true,
  6. state: {
  7. list: [],
  8. ischat: {
  9. notice: true,
  10. number: 0
  11. }
  12. },
  13. mutations: {
  14. setIschat(state, payload) {
  15. for (let i in payload) {
  16. for (let j in state.ischat) {
  17. if (i === j) {
  18. state.ischat[j] = payload[i]
  19. }
  20. }
  21. }
  22. }
  23. },
  24. actions: {
  25. async get({ state, dispatch }) {
  26. try {
  27. const res = await uni.request({
  28. url: '/wanlshop/chat/lists',
  29. method: 'POST'
  30. })
  31. if (res && res.data) {
  32. state.list = res.data
  33. let count = 0
  34. res.data.forEach(item => { count += item.count })
  35. dispatch('storage', { type: 'statis', number: count })
  36. }
  37. } catch (e) {
  38. console.error('[chat] get failed:', e)
  39. }
  40. },
  41. async del({ state, dispatch }, index) {
  42. let list = state.list
  43. dispatch('storage', { type: 'del', number: list[index].count })
  44. uni.removeStorage({
  45. key: 'wanlchat:message_' + list[index].user_id,
  46. success: () => {
  47. console.log('[chat] 删除消息成功')
  48. }
  49. })
  50. try {
  51. await uni.request({
  52. url: '/wanlshop/chat/del',
  53. method: 'POST',
  54. data: { id: list[index].user_id }
  55. })
  56. } catch (e) {
  57. console.error('[chat] del failed:', e)
  58. }
  59. Vue.delete(state.list, index)
  60. },
  61. async empty({ state, dispatch }) {
  62. uni.showModal({
  63. content: '確定將所有數據標為已讀嗎?',
  64. success: res => {
  65. if (res.confirm) {
  66. state.list.forEach(item => { item.count = 0 })
  67. dispatch('storage', { type: 'empty' })
  68. uni.request({
  69. url: '/wanlshop/chat/read',
  70. method: 'POST',
  71. success: () => {
  72. uni.showToast({ title: '已全部標為已讀', icon: 'none' })
  73. }
  74. })
  75. }
  76. }
  77. })
  78. },
  79. async update({ state, dispatch }, { type, data, id, shop }) {
  80. if (type == 'del') {
  81. let counts = 0
  82. state.list.some(chat => {
  83. if (chat.user_id == id) {
  84. counts = chat.count
  85. chat.count = 0
  86. return true
  87. }
  88. })
  89. dispatch('storage', { type: 'del', number: counts })
  90. try {
  91. await uni.request({
  92. url: '/wanlshop/chat/clear',
  93. method: 'POST',
  94. data: { id: id }
  95. })
  96. } catch (e) {
  97. console.error('[chat] update clear failed:', e)
  98. }
  99. } else if (type == 'chat' || type == 'send') {
  100. let content = ''
  101. let createtime = data.createtime
  102. let chat_id = 0
  103. if (data.message.type == 'text') {
  104. content = data.message.content.text
  105. } else if (data.message.type == 'img') {
  106. content = '[Image Message]'
  107. } else if (data.message.type == 'voice') {
  108. content = '[Voice Message]'
  109. } else if (data.message.type == 'goods') {
  110. content = '[Goods Message]'
  111. } else {
  112. content = '[Unknown Type Message]'
  113. }
  114. if (type == 'chat') {
  115. chat_id = data.form.id
  116. dispatch('storage', { type: 'chat' })
  117. } else if (type == 'send') {
  118. chat_id = data.to_id
  119. }
  120. let result = state.list.some(chat => {
  121. if (chat.user_id == chat_id) {
  122. if (type == 'chat') {
  123. chat.count += 1
  124. }
  125. chat.createtime = createtime
  126. chat.content = content
  127. return true
  128. }
  129. })
  130. if (!result) {
  131. Vue.set(state.list, 0, {
  132. id: shop.id,
  133. user_id: shop.user_id,
  134. name: shop.name,
  135. avatar: shop.avatar,
  136. content: content,
  137. count: 1,
  138. createtime: createtime
  139. })
  140. }
  141. }
  142. },
  143. async storage({ state, rootState }, { type, number }) {
  144. if (type == 'statis') {
  145. rootState.statistics.notice.chat = number
  146. } else if (type == 'order') {
  147. rootState.statistics.notice.order += 1
  148. } else if (type == 'notice') {
  149. rootState.statistics.notice.notice += 1
  150. } else if (type == 'chat') {
  151. rootState.statistics.notice.chat += 1
  152. } else if (type == 'del') {
  153. rootState.statistics.notice.chat -= number
  154. } else if (type == 'empty') {
  155. rootState.statistics.notice.chat = 0
  156. rootState.statistics.notice.order = 0
  157. rootState.statistics.notice.notice = 0
  158. }
  159. uni.setStorageSync('wanlshop:statis', rootState.statistics)
  160. }
  161. }
  162. }