Skip to content

Commit b0b62eb

Browse files
author
puhui999
committed
【优化】:mall 客服表情包存放到本地使用
1 parent 33cf98e commit b0b62eb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+58
-49
lines changed

.env.local

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,5 @@ VITE_BASE_PATH=/
2929
# 商城H5会员端域名
3030
VITE_MALL_H5_DOMAIN='http://localhost:3000'
3131

32-
# TODO puhui999:这个可以不走 cdn 地址么?
33-
# 客户端静态资源地址 空=默认使用服务端指定的CDN资源地址前缀 | local=本地 | http(s)://xxx.xxx=自定义静态资源地址前缀
34-
VITE_STATIC_URL = https://file.sheepjs.com
35-
3632
# 验证码的开关
3733
VITE_APP_CAPTCHA_ENABLE=false

src/views/mall/promotion/kefu/components/EmojiSelectPopover.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626

2727
<script lang="ts" setup>
2828
defineOptions({ name: 'EmojiSelectPopover' })
29-
import { Emoji, getEmojiList } from './emoji'
29+
import { Emoji, useEmoji } from './emoji'
3030
31+
const { getEmojiList } = useEmoji()
3132
const emojiList = computed(() => getEmojiList())
3233
3334
const emits = defineEmits<{

src/views/mall/promotion/kefu/components/KeFuChatBox.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,14 @@ import { ElScrollbar as ElScrollbarType } from 'element-plus'
8989
import { KeFuMessageApi, KeFuMessageRespVO } from '@/api/mall/promotion/kefu/message'
9090
import { KeFuConversationRespVO } from '@/api/mall/promotion/kefu/conversation'
9191
import EmojiSelectPopover from './EmojiSelectPopover.vue'
92-
import { Emoji, replaceEmoji } from './emoji'
92+
import { Emoji, useEmoji } from './emoji'
9393
import { KeFuMessageContentTypeEnum } from './constants'
9494
import { isEmpty } from '@/utils/is'
9595
import { UserTypeEnum } from '@/utils/constants'
9696
import { createImageViewer } from '@/components/ImageViewer'
9797
9898
defineOptions({ name: 'KeFuMessageBox' })
99+
const { replaceEmoji } = useEmoji()
99100
const messageTool = useMessage()
100101
const message = ref('') // 消息
101102
const messageList = ref<KeFuMessageRespVO[]>([]) // 消息列表
@@ -130,6 +131,7 @@ const handleSendMessage = async () => {
130131
// 1. 校验消息是否为空
131132
if (isEmpty(unref(message.value))) {
132133
messageTool.warning('请输入消息后再发送哦!')
134+
return
133135
}
134136
// 2. 组织发送消息
135137
const msg = {

src/views/mall/promotion/kefu/components/KeFuConversationBox.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@
3535

3636
<script lang="ts" setup>
3737
import { KeFuConversationApi, KeFuConversationRespVO } from '@/api/mall/promotion/kefu/conversation'
38-
import { replaceEmoji } from '@/views/mall/promotion/kefu/components/emoji'
38+
import { useEmoji } from './emoji'
3939
import { formatDate, getNowDateTime } from '@/utils/formatTime'
4040
import { KeFuMessageContentTypeEnum } from '@/views/mall/promotion/kefu/components/constants'
4141
4242
defineOptions({ name: 'KeFuConversationBox' })
43+
const { replaceEmoji } = useEmoji()
4344
const activeConversationIndex = ref(-1) // 选中的会话
4445
const conversationList = ref<KeFuConversationRespVO[]>([]) // 会话列表
4546
const getConversationList = async () => {

src/views/mall/promotion/kefu/components/emoji.ts

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export const emojiList = [
1+
import { isEmpty } from '@/utils/is'
2+
3+
const emojiList = [
24
{ name: '[笑掉牙]', file: 'xiaodiaoya.png' },
35
{ name: '[可爱]', file: 'keai.png' },
46
{ name: '[冷酷]', file: 'lengku.png' },
@@ -54,53 +56,60 @@ export interface Emoji {
5456
url: string
5557
}
5658

57-
export const emojiPage = {}
58-
emojiList.forEach((item, index) => {
59-
if (!emojiPage[Math.floor(index / 30) + 1]) {
60-
emojiPage[Math.floor(index / 30) + 1] = []
59+
export const useEmoji = () => {
60+
const emojiPathList = ref<any[]>([])
61+
// 加载本地图片
62+
const getStaticEmojiPath = async () => {
63+
const pathList = import.meta.glob(
64+
'@/views/mall/promotion/kefu/components/images/*.{png,jpg,jpeg,svg}'
65+
)
66+
for (const path in pathList) {
67+
const imageModule: any = await pathList[path]()
68+
emojiPathList.value.push(imageModule.default)
69+
}
6170
}
62-
emojiPage[Math.floor(index / 30) + 1].push(item)
63-
})
64-
65-
// 后端上传地址
66-
const staticUrl = import.meta.env.VITE_STATIC_URL
67-
// 后缀
68-
const suffix = '/static/img/chat/emoji/'
71+
// 初始化
72+
onMounted(async () => {
73+
if (isEmpty(emojiPathList.value)) {
74+
await getStaticEmojiPath()
75+
}
76+
})
6977

70-
// 处理表情
71-
export function replaceEmoji(data: string) {
72-
let newData = data
73-
if (typeof newData !== 'object') {
74-
const reg = /\[(.+?)\]/g // [] 中括号
75-
const zhEmojiName = newData.match(reg)
76-
if (zhEmojiName) {
77-
zhEmojiName.forEach((item) => {
78-
const emojiFile = selEmojiFile(item)
79-
newData = newData.replace(
80-
item,
81-
`<img class="chat-img" style="width: 24px;height: 24px;margin: 0 3px;" src="${
82-
staticUrl + suffix + emojiFile
83-
}"/>`
84-
)
85-
})
78+
// 处理表情
79+
function replaceEmoji(data: string) {
80+
let newData = data
81+
if (typeof newData !== 'object') {
82+
const reg = /\[(.+?)\]/g // [] 中括号
83+
const zhEmojiName = newData.match(reg)
84+
if (zhEmojiName) {
85+
zhEmojiName.forEach((item) => {
86+
const emojiFile = selEmojiFile(item)
87+
newData = newData.replace(
88+
item,
89+
`<img class="chat-img" style="width: 24px;height: 24px;margin: 0 3px;" src="${emojiFile}"/>`
90+
)
91+
})
92+
}
8693
}
94+
return newData
8795
}
88-
return newData
89-
}
9096

91-
// 获得所有表情
92-
export function getEmojiList(): Emoji[] {
93-
return emojiList.map((item) => ({
94-
url: staticUrl + suffix + item.file,
95-
name: item.name
96-
})) as Emoji[]
97-
}
97+
// 获得所有表情
98+
function getEmojiList(): Emoji[] {
99+
return emojiList.map((item) => ({
100+
url: selEmojiFile(item.name),
101+
name: item.name
102+
})) as Emoji[]
103+
}
98104

99-
function selEmojiFile(name: string) {
100-
for (const index in emojiList) {
101-
if (emojiList[index].name === name) {
102-
return emojiList[index].file
105+
function selEmojiFile(name: string) {
106+
for (const emoji of emojiList) {
107+
if (emoji.name === name) {
108+
return emojiPathList.value.find((item: string) => item.indexOf(emoji.file) > -1)
109+
}
103110
}
111+
return false
104112
}
105-
return false
113+
114+
return { replaceEmoji, getEmojiList }
106115
}
4.14 KB
2.25 KB
4.33 KB
3.7 KB
3.68 KB

0 commit comments

Comments
 (0)