| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- // 店铺客服聊天模块
- import Vue from 'vue'
- import appSettings from '@/core/config/app-settings'
- export default {
- namespaced: true,
- state: {
- list: [],
- ischat: {
- notice: true,
- number: 0
- }
- },
- mutations: {
- setIschat(state, payload) {
- for (let i in payload) {
- for (let j in state.ischat) {
- if (i === j) {
- state.ischat[j] = payload[i]
- }
- }
- }
- }
- },
- 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 })
- }
- } 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 })
- uni.removeStorage({
- key: 'wanlchat:message_' + list[index].user_id,
- success: () => {
- console.log('[chat] 删除消息成功')
- }
- })
- try {
- await uni.request({
- url: '/wanlshop/chat/del',
- method: 'POST',
- data: { id: list[index].user_id }
- })
- } catch (e) {
- console.error('[chat] del failed:', e)
- }
- Vue.delete(state.list, index)
- },
- 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' })
- }
- })
- }
- }
- })
- },
- async update({ state, dispatch }, { type, data, id, shop }) {
- if (type == 'del') {
- let counts = 0
- state.list.some(chat => {
- if (chat.user_id == id) {
- counts = chat.count
- chat.count = 0
- return true
- }
- })
- dispatch('storage', { type: 'del', number: counts })
- try {
- await uni.request({
- url: '/wanlshop/chat/clear',
- method: 'POST',
- data: { id: id }
- })
- } catch (e) {
- console.error('[chat] update clear failed:', e)
- }
- } else if (type == 'chat' || type == 'send') {
- let content = ''
- let createtime = data.createtime
- let chat_id = 0
- if (data.message.type == 'text') {
- content = data.message.content.text
- } else if (data.message.type == 'img') {
- content = '[Image Message]'
- } else if (data.message.type == 'voice') {
- content = '[Voice Message]'
- } else if (data.message.type == 'goods') {
- content = '[Goods Message]'
- } else {
- content = '[Unknown Type Message]'
- }
- if (type == 'chat') {
- chat_id = data.form.id
- dispatch('storage', { type: 'chat' })
- } else if (type == 'send') {
- chat_id = data.to_id
- }
- let result = state.list.some(chat => {
- if (chat.user_id == chat_id) {
- if (type == 'chat') {
- chat.count += 1
- }
- chat.createtime = createtime
- chat.content = content
- return true
- }
- })
- if (!result) {
- Vue.set(state.list, 0, {
- id: shop.id,
- user_id: shop.user_id,
- name: shop.name,
- avatar: shop.avatar,
- content: content,
- count: 1,
- createtime: createtime
- })
- }
- }
- },
- async storage({ state, rootState }, { type, number }) {
- if (type == 'statis') {
- rootState.statistics.notice.chat = number
- } else if (type == 'order') {
- rootState.statistics.notice.order += 1
- } else if (type == 'notice') {
- rootState.statistics.notice.notice += 1
- } else if (type == 'chat') {
- rootState.statistics.notice.chat += 1
- } else if (type == 'del') {
- rootState.statistics.notice.chat -= number
- } else if (type == 'empty') {
- rootState.statistics.notice.chat = 0
- rootState.statistics.notice.order = 0
- rootState.statistics.notice.notice = 0
- }
- uni.setStorageSync('wanlshop:statis', rootState.statistics)
- }
- }
- }
|