consumerm模块完成度90%,完善消费者和商家端数据库表,商品、聊天、订单数据对接好了supabase,和商家端对接了聊天功能,安卓端编译通过了css样式,剩余几个页面在处理函数规范问题
This commit is contained in:
@@ -146,24 +146,24 @@ const currentUserId = ref('')
|
||||
const messages = ref<any[]>([])
|
||||
|
||||
const emojiList = ['😊', '😂', '👍', '👌', '❤️', '🌹', '🙏', '🎉', '😡', '😭', '🤔', '👋', '🤝', '💊', '🏥']
|
||||
let realtimeChannel: any = null
|
||||
let realtimeChannel: any | null = null
|
||||
|
||||
onLoad((options: any) => {
|
||||
// 获取状态栏高度
|
||||
const sys = uni.getSystemInfoSync()
|
||||
statusBarHeight.value = sys.statusBarHeight || 0
|
||||
statusBarHeight.value = sys.statusBarHeight ?? 0
|
||||
|
||||
// 获取参数
|
||||
if (options.merchantId) {
|
||||
merchantId.value = options.merchantId
|
||||
merchantName.value = options.merchantName || '商家'
|
||||
merchantLogo.value = options.merchantLogo || ''
|
||||
console.log('开始聊天,商家ID:', merchantId.value)
|
||||
} else {
|
||||
// 测试模式或默认客服
|
||||
// uni.showToast({ title: '参数缺失', icon: 'none' })
|
||||
merchantName.value = '平台客服'
|
||||
}
|
||||
const optObj = (options instanceof UTSJSONObject) ? (options as UTSJSONObject) : (JSON.parse(JSON.stringify(options ?? {})) as UTSJSONObject)
|
||||
const mid = optObj.getString('merchantId') ?? ''
|
||||
if (mid !== '') {
|
||||
merchantId.value = mid
|
||||
merchantName.value = optObj.getString('merchantName') ?? '商家'
|
||||
merchantLogo.value = optObj.getString('merchantLogo') ?? ''
|
||||
console.log('开始聊天,商家ID:', merchantId.value)
|
||||
} else {
|
||||
merchantName.value = '平台客服'
|
||||
}
|
||||
|
||||
// 获取当前用户
|
||||
const uid = supabaseService.getCurrentUserId()
|
||||
@@ -210,31 +210,35 @@ const loadMoreHistory = () => {
|
||||
|
||||
// 开启实时订阅
|
||||
const startRealtimeSubscription = () => {
|
||||
if (!currentUserId.value) return
|
||||
if (currentUserId.value == '') return
|
||||
|
||||
console.log('开启消息监听...')
|
||||
const filterObj = ({
|
||||
event: 'INSERT',
|
||||
schema: 'public',
|
||||
table: 'ml_chat_messages'
|
||||
} as UTSJSONObject)
|
||||
realtimeChannel = supa.channel(`chat_${currentUserId.value}`)
|
||||
.on(
|
||||
'postgres_changes',
|
||||
{
|
||||
event: 'INSERT',
|
||||
schema: 'public',
|
||||
table: 'ml_chat_messages',
|
||||
filter: `receiver_id=eq.${currentUserId.value}`
|
||||
},
|
||||
(payload) => {
|
||||
filterObj,
|
||||
(payload: any) => {
|
||||
console.log('收到新消息:', payload)
|
||||
const newMsg = payload.new as any
|
||||
const payloadObj = (payload instanceof UTSJSONObject) ? (payload as UTSJSONObject) : (JSON.parse(JSON.stringify(payload ?? {})) as UTSJSONObject)
|
||||
const newAny = payloadObj.get('new')
|
||||
if (newAny == null) return
|
||||
const newMsg = (newAny instanceof UTSJSONObject) ? (newAny as UTSJSONObject) : (JSON.parse(JSON.stringify(newAny)) as UTSJSONObject)
|
||||
// 只有来自当前聊天的商家的消息才显示,或者如果是全局客服模式
|
||||
if (newMsg.sender_id === merchantId.value || !merchantId.value) {
|
||||
const senderId = newMsg.getString('sender_id') ?? ''
|
||||
if (senderId === merchantId.value || merchantId.value == '') {
|
||||
const formatted = formatMessage({
|
||||
id: newMsg.id,
|
||||
content: newMsg.content,
|
||||
msg_type: newMsg.msg_type,
|
||||
sender_id: newMsg.sender_id,
|
||||
receiver_id: newMsg.receiver_id,
|
||||
id: newMsg.getString('id') ?? '',
|
||||
content: newMsg.getString('content') ?? '',
|
||||
msg_type: newMsg.getString('msg_type') ?? '',
|
||||
sender_id: senderId,
|
||||
receiver_id: newMsg.getString('receiver_id') ?? '',
|
||||
is_from_user: false, // 收到的一定不是自己发的
|
||||
created_at: newMsg.created_at
|
||||
created_at: newMsg.getString('created_at') ?? ''
|
||||
} as ChatMessage)
|
||||
|
||||
messages.value.push(formatted)
|
||||
@@ -271,7 +275,7 @@ const formatMessage = (m: ChatMessage): any => {
|
||||
|
||||
const sendMessage = async () => {
|
||||
const text = inputMessage.value.trim()
|
||||
if (!text) return
|
||||
if (text == '') return
|
||||
|
||||
// 乐观更新 UI
|
||||
const tempId = 'temp_' + Date.now()
|
||||
|
||||
Reference in New Issue
Block a user