customerService.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { queryCustomer, createChat, queryChatList, queryChatMessage, queryChatMessageBack, addChatMessage } from '../../api/customerService'
  2. import { CHANGE_CUSTOMER_SERVICE_INFO, CHANGE_WS_INFO, CHANGE_WSINFO_INFO, CHANGE_CHAT_LIST, CHANGE_ON_FN } from './type'
  3. import { getUserId } from '../../utils'
  4. export default {
  5. namespaced: true,
  6. state: () => ({
  7. customerServiceInfo: [ { letter: '我的客服', data: [] } ],
  8. chatListData: [],
  9. wsHandleInfo: '',
  10. wsHandle: '',
  11. onOpen: '',
  12. onMessage: '',
  13. onClose: '',
  14. onError: ''
  15. }),
  16. mutations: {
  17. [CHANGE_CUSTOMER_SERVICE_INFO](state, customerServiceData) {
  18. state.customerServiceInfo = customerServiceData
  19. },
  20. [CHANGE_CHAT_LIST](state, chatListData) {
  21. state.chatListData = chatListData
  22. },
  23. [CHANGE_WSINFO_INFO](state, wsHandleInfo) {
  24. state.wsHandleInfo = wsHandleInfo
  25. },
  26. [CHANGE_WS_INFO](state, wsHandle) {
  27. state.wsHandle = wsHandle
  28. },
  29. [CHANGE_ON_FN](state, ref) {
  30. ref.onOpen && (state.onOpen = ref.onOpen)
  31. ref.onMessage && (state.onMessage = ref.onMessage)
  32. ref.onClose && (state.onClose = ref.onClose)
  33. ref.onError && (state.onError = ref.onError)
  34. }
  35. },
  36. actions: {
  37. // 获取聊天列表数据. data为true:目前代表是从我的客服页面进来调用的
  38. async getChatList({ dispatch, commit, rootState }, data) {
  39. uni.showLoading({
  40. title: '加载中'
  41. })
  42. const result = await queryChatList({
  43. userId: getUserId()
  44. })
  45. uni.hideLoading()
  46. commit(CHANGE_CHAT_LIST, result.data || [])
  47. // if (data) {
  48. // if (rootState.customerService.chatListData.length !== 0) {
  49. // uni.showModal({
  50. // title: '提示',
  51. // content: '已经与客服取得联系,是否继续与客服进行沟通?',
  52. // success(res) {
  53. // if (res.confirm) {
  54. // uni.redirectTo({
  55. // url: `/user/sever/chat/chat-detail?chat=${rootState.customerService.chatListData[0].toUserId}&name=${rootState.customerService.chatListData[0].toUsername}&avatar=${rootState.customerService.chatListData[0].toAvatarImage}`
  56. // })
  57. // } else {
  58. // dispatch('getCustomerServiceList')
  59. // }
  60. // }
  61. // })
  62. // } else {
  63. // dispatch('getCustomerServiceList')
  64. // }
  65. // }
  66. },
  67. // 获取客服列表数据
  68. async getCustomerServiceList({ commit, rootState }) {
  69. uni.showLoading({
  70. title: '加载中'
  71. })
  72. const { data } = await queryCustomer({})
  73. const tempInfo = {
  74. letter: '我的客服',
  75. data: data.map((item) => ({
  76. friendId: item.userId,
  77. displayName: item.name,
  78. avatar: item.url
  79. }))
  80. }
  81. console.log(data, tempInfo)
  82. uni.hideLoading()
  83. commit(CHANGE_CUSTOMER_SERVICE_INFO, [ tempInfo ])
  84. },
  85. // 在客服列表页面(我的客服)与客服创建聊天
  86. async createChat({ dispatch, rootState }, data) {
  87. try {
  88. uni.showLoading()
  89. // const res = await createChat(Object.assign(data, {
  90. // chatId: '',
  91. // userType: 'APP',
  92. // userId: getUserId()
  93. // }))
  94. // uni.redirectTo({ url: `/user/sever/chat/chat-detail?chat=${res.data.chatId}&name=${res.data.name}` }) // 进入聊天室(创建ws)
  95. uni.redirectTo({
  96. url: `/user/sever/chat/chat-detail?chat=${data.friendId}&name=${data.chatName}&avatar=${data.avatar}`
  97. })
  98. } catch (error) {
  99. uni.showToast({
  100. title: '联系客服失败',
  101. icon: 'none'
  102. })
  103. } finally {
  104. uni.hideLoading()
  105. }
  106. },
  107. // (全局触发,且针对)聊天列表页面,创建聊天,存储wsHandle。考虑在不同页面断线重连。
  108. joinCustomerServiceChat({ commit, rootState, state }, { ref, wsHandle, wsHandleInfo }) {
  109. if (ref) commit(CHANGE_ON_FN, ref)
  110. if (wsHandle) {
  111. commit('CHANGE_WS_INFO', wsHandle)
  112. rootState.customerService.onOpen && wsHandle.onOpen(rootState.customerService.onOpen)
  113. rootState.customerService.onMessage && wsHandle.onMessage(rootState.customerService.onMessage)
  114. rootState.customerService.onClose && wsHandle.onClose(rootState.customerService.onClose)
  115. rootState.customerService.onError && wsHandle.onError(rootState.customerService.onError)
  116. // console.log('顺序1')
  117. } else {
  118. rootState.customerService.onOpen && rootState.customerService.wsHandle.onOpen(rootState.customerService.onOpen)
  119. rootState.customerService.onMessage && rootState.customerService.wsHandle.onMessage(rootState.customerService.onMessage)
  120. rootState.customerService.onClose && rootState.customerService.wsHandle.onClose(rootState.customerService.onClose)
  121. rootState.customerService.onError && rootState.customerService.wsHandle.onError(rootState.customerService.onError)
  122. }
  123. // if (wsHandleInfo) {
  124. // commit('CHANGE_WSINFO_INFO', wsHandleInfo)
  125. // ref.onOpenInfo && wsHandleInfo.onOpen(ref.onOpenInfo)
  126. // ref.onMessageInfo && wsHandleInfo.onMessage(ref.onMessageInfo)
  127. // ref.onCloseInfo && wsHandleInfo.onClose(ref.onCloseInfo)
  128. // ref.onErrorInfo && wsHandleInfo.onError(ref.onErrorInfo)
  129. // }
  130. },
  131. // 查消息
  132. async queryChatMessage({ dispatch, rootState }, data) {
  133. try {
  134. uni.showLoading()
  135. const res = await queryChatMessage(data)
  136. return res.data || []
  137. } catch (error) {
  138. uni.showToast({
  139. title: '获取历史消息失败',
  140. icon: 'none'
  141. })
  142. return Promise.reject()
  143. } finally {
  144. uni.hideLoading()
  145. }
  146. },
  147. // 发消息对象存储
  148. async addChatMessage({ dispatch, rootState }, data) {
  149. try {
  150. const res = await addChatMessage(data)
  151. } catch (error) {
  152. uni.showToast({
  153. title: '发送消息失败',
  154. icon: 'none'
  155. })
  156. return Promise.reject()
  157. } finally {
  158. }
  159. }
  160. // // 查消息(往后查)
  161. // async queryChatMessageBack({ dispatch, rootState }, data) {
  162. // try {
  163. // uni.showLoading()
  164. // const res = await queryChatMessageBack(data)
  165. // return res.data.items || []
  166. // } catch (error) {
  167. // uni.showToast({
  168. // title: '获取历史消息失败',
  169. // icon: 'none'
  170. // })
  171. // } finally {
  172. // uni.hideLoading()
  173. // }
  174. // }
  175. }
  176. }