|
|
<template>
|
|
|
<view class="chat-window">
|
|
|
<SupplyAuthDialog v-model="supplyAuthVisible" return-path="/packages/supply/index" @dismiss="returnBrowse" />
|
|
|
<view v-if="otherProfile" class="other-profile"><SupplyMemberBrief :profile="otherProfile" :show-name="true" /></view>
|
|
|
<view v-if="supplyDemand && supplyDemand.is_managed" class="managed-tip">{{ tips.chat_tip }}</view>
|
|
|
<view v-if="supplyDemand && supplyDemand.id" class="demand-card" @click="openSupplyDemand(supplyDemand.id)">
|
|
|
<text class="demand-label">正在咨询 · {{ supplyDemandType }}</text>
|
|
|
<text class="demand-title">{{ supplyDemand ? supplyDemand.title : '供需信息 #' + supplyDemandId }}</text>
|
|
|
<text v-if="supplyDemand && (supplyDemand.content || supplyDemand.desc)" class="demand-summary">{{ supplyDemand.content || supplyDemand.desc }}</text>
|
|
|
<text class="demand-link">查看详情 ›</text>
|
|
|
</view>
|
|
|
<scroll-view
|
|
|
:scroll-y="true"
|
|
|
class="message-list"
|
|
|
:scroll-into-view="lastMessageId"
|
|
|
@scrolltoupper="onScrollToUpper"
|
|
|
:scroll-with-animation="false"
|
|
|
:upper-threshold="50">
|
|
|
<!-- 加载更多提示 -->
|
|
|
<view v-if="loading" class="loading-more">
|
|
|
<u-loading mode="circle" size="20"></u-loading>
|
|
|
<text class="loading-text">加载中...</text>
|
|
|
</view>
|
|
|
|
|
|
<view v-for="(message, index) in messages" :key="message.id">
|
|
|
<!-- 时间分割线 -->
|
|
|
<view v-if="shouldShowTimeDivider(message, index)" class="time-divider">
|
|
|
<text class="time-text">{{ formatTime(message.timestamp) }}</text>
|
|
|
</view>
|
|
|
<view v-if="message.supply_demand && message.supply_demand.id" class="message-context" @click="openSupplyDemand(message.supply_demand.id)">
|
|
|
咨询:{{ message.supply_demand ? message.supply_demand.title : '供需信息 #' + message.supply_demand_id }} ›
|
|
|
</view>
|
|
|
<!-- 消息内容 -->
|
|
|
<view :id="'msg-' + message.id"
|
|
|
:class="['message-item', message.from === 'me' ? 'my-message' : 'their-message']">
|
|
|
<u-avatar v-if="message.from !== 'me'" :src="theirAvatar" size="80"></u-avatar>
|
|
|
<view class="message-content">
|
|
|
<text class="message-text">{{ message.content }}</text>
|
|
|
</view>
|
|
|
<u-avatar v-if="message.from === 'me'" :src="myAvatar" size="80"></u-avatar>
|
|
|
</view>
|
|
|
</view>
|
|
|
</scroll-view>
|
|
|
|
|
|
<view class="input-area">
|
|
|
<u-input v-model="newMessage" placeholder="输入消息..." class="input-field" :border="false" />
|
|
|
<u-button type="primary" size="medium" :loading="sending" :disabled="sending || loading" @click="sendMessage" class="send-button">发送</u-button>
|
|
|
</view>
|
|
|
</view>
|
|
|
</template>
|
|
|
|
|
|
<script>
|
|
|
import SupplyAuthDialog from '@/components/SupplyAuthDialog.vue'
|
|
|
import { requireSupplyMember, loadSupplySession } from '@/common/supplyIdentity.js'
|
|
|
import SupplyMemberBrief from '@/components/SupplyMemberBrief.vue'
|
|
|
export default {
|
|
|
components: { SupplyAuthDialog, SupplyMemberBrief },
|
|
|
data() {
|
|
|
return { supplyAuthVisible: false, supplyAuthChecking: false, identityReady: false, myAvatar: '', theirAvatar: '', newMessage: '', messages: [], lastMessageId: '', supplyDemandId: '', dialogueId: '', supplyDemand: null, otherProfile: null, tips: {}, theirUserId: '', loading: false, sending: false, currentPage: 1, hasMore: true, pageSize: 20, refreshTimer: null }
|
|
|
},
|
|
|
computed: {
|
|
|
supplyDemandType() { return ({ 1: '供应', 2: '需求', 3: '投融资' })[this.supplyDemand && Number(this.supplyDemand.type)] || '供需信息' }
|
|
|
},
|
|
|
async onLoad(options) {
|
|
|
this.identityReady = await requireSupplyMember(this)
|
|
|
if (!this.identityReady) return
|
|
|
this.theirUserId = Number(options.userId) || 0
|
|
|
this.dialogueId = Number(options.dialogueId) || ''
|
|
|
this.supplyDemandId = Number(options.supplyDemandId) || ''
|
|
|
this.myAvatar = this.$store.state.vuex_user.headimgurl || ''
|
|
|
const userName = this.decodeRouteName(options.userName)
|
|
|
uni.setNavigationBarTitle({ title: userName ? `与 ${userName} 的对话` : '我的私信' })
|
|
|
if (this.supplyDemandId && !this.dialogueId) this.fetchSupplyDemand()
|
|
|
this.fetchMessages()
|
|
|
},
|
|
|
onShow() { if (this.identityReady) this.fetchMessages(1, false, true); if (!this.identityReady && !this.supplyAuthChecking) this.supplyAuthVisible = true; clearInterval(this.refreshTimer); this.refreshTimer = setInterval(() => { if (this.identityReady) this.fetchMessages(1, false, true) }, 10000) },
|
|
|
onHide() { clearInterval(this.refreshTimer) },
|
|
|
onUnload() { clearInterval(this.refreshTimer) },
|
|
|
methods: {
|
|
|
decodeRouteName(value) {
|
|
|
if (!value) return ''
|
|
|
try { return decodeURIComponent(String(value)) }
|
|
|
catch (error) { return String(value) }
|
|
|
},
|
|
|
returnBrowse() { uni.redirectTo({ url: '/packages/supply/index' }); },
|
|
|
openSupplyDemand(id) { if (Number(id) > 0) uni.navigateTo({ url: `/packages/supply/detail?id=${id}` }) },
|
|
|
fetchSupplyDemand() {
|
|
|
this.$u.api.supplyDemandDetail({ id: this.supplyDemandId }).then(res => {
|
|
|
if (res && res.id) { this.supplyDemand = res; this.otherProfile = res.publisher; this.tips = res.tips || {} }
|
|
|
}).catch(() => { this.supplyDemand = null })
|
|
|
},
|
|
|
contextParams() {
|
|
|
if (this.dialogueId) return { dialogue_id: this.dialogueId }
|
|
|
return { to_user_id: this.theirUserId, ...(this.supplyDemandId ? { supply_demand_id: this.supplyDemandId } : {}) }
|
|
|
},
|
|
|
async fetchMessages(page = 1, append = false, quiet = false) {
|
|
|
if (this.loading || (append && !this.hasMore)) return
|
|
|
this.loading = true
|
|
|
try {
|
|
|
if (quiet) {
|
|
|
const session = await loadSupplySession(this)
|
|
|
if (!session.registered) { this.identityReady = false; this.supplyAuthVisible = true; return }
|
|
|
}
|
|
|
const res = await this.$u.api.supplyDemandMessageList({ ...this.contextParams(), page, page_size: this.pageSize })
|
|
|
if (!res) return
|
|
|
if (res.dialogue_id) this.dialogueId = res.dialogue_id
|
|
|
if (Object.prototype.hasOwnProperty.call(res, 'supplyDemand')) {
|
|
|
this.supplyDemand = res.supplyDemand
|
|
|
this.supplyDemandId = res.supplyDemand ? res.supplyDemand.id : ''
|
|
|
}
|
|
|
if (res.other_profile) {
|
|
|
this.otherProfile = res.other_profile
|
|
|
this.theirUserId = res.other_profile.id
|
|
|
this.theirAvatar = res.other_profile.headimgurl || ''
|
|
|
uni.setNavigationBarTitle({ title: `与 ${res.other_profile.name || '发布人'} 的对话` })
|
|
|
}
|
|
|
this.tips = res.tips || this.tips
|
|
|
const incoming = (res.message && res.message.data || []).map(item => ({
|
|
|
...item, from: String(item.user_id) === String(this.$store.state.vuex_user.id) ? 'me' : 'them', timestamp: item.created_at
|
|
|
}))
|
|
|
const previousLast = this.messages.length ? this.messages[this.messages.length - 1].id : null
|
|
|
if (append || quiet) {
|
|
|
const merged = {}; this.messages.concat(incoming).forEach(item => { merged[item.id] = item })
|
|
|
this.messages = Object.values(merged).sort((a,b) => Number(a.id) - Number(b.id))
|
|
|
} else this.messages = incoming.reverse()
|
|
|
if (!quiet) { this.hasMore = res.message.current_page < res.message.last_page; this.currentPage = res.message.current_page }
|
|
|
const latest = this.messages.length ? this.messages[this.messages.length - 1].id : null
|
|
|
if (!append && (!quiet || latest !== previousLast)) this.scrollToBottom()
|
|
|
} catch (err) { if (!quiet) console.error('读取对接消息失败', err) }
|
|
|
finally { this.loading = false }
|
|
|
},
|
|
|
async sendMessage() {
|
|
|
if (!await requireSupplyMember(this)) return
|
|
|
if (this.sending || this.loading || !this.newMessage.trim()) return
|
|
|
if (Number(this.theirUserId) && String(this.theirUserId) === String(this.$store.state.vuex_user.id)) { this.$u.toast('不能给自己发信息'); return }
|
|
|
const content = this.newMessage.trim(); this.sending = true
|
|
|
try {
|
|
|
const res = await this.$u.api.supplyDemandSendMessage({ ...this.contextParams(), content })
|
|
|
if (!res || !res.id) return
|
|
|
this.dialogueId = res.dialogue_id; this.newMessage = ''
|
|
|
if (res.is_managed) uni.showModal({ title: '提交成功', content: this.tips.sent_tip || '对接信息已提交,请耐心等待回复。', showCancel: false })
|
|
|
await this.fetchMessages(1, false, true)
|
|
|
this.scrollToBottom()
|
|
|
} catch (err) { console.error('发送对接消息失败', err) }
|
|
|
finally { this.sending = false }
|
|
|
},
|
|
|
scrollToBottom() { this.$nextTick(() => { if (this.messages.length) this.lastMessageId = `msg-${this.messages[this.messages.length - 1].id}` }) },
|
|
|
formatTime(value) { if (!value) return ''; return this.$moment(value).format('MM-DD HH:mm') },
|
|
|
shouldShowTimeDivider(message, index) { return index === 0 || Math.abs(new Date(message.timestamp) - new Date(this.messages[index - 1].timestamp)) > 300000 },
|
|
|
onScrollToUpper() { this.fetchMessages(this.currentPage + 1, true) }
|
|
|
}
|
|
|
}
|
|
|
</script>
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
.other-profile { background: #fff; padding: 12rpx 30rpx; flex-shrink: 0; }
|
|
|
.managed-tip { background: #fff8e9; padding: 16rpx 30rpx; color: #947348; font-size: 24rpx; line-height: 1.5; flex-shrink: 0; }
|
|
|
.demand-card { max-height: 230rpx; box-sizing: border-box; overflow: hidden; }
|
|
|
.chat-window {
|
|
|
display: flex;
|
|
|
flex-direction: column;
|
|
|
height: 100vh;
|
|
|
background-color: #f5f5f5;
|
|
|
}
|
|
|
|
|
|
.demand-card { flex-shrink: 0; padding: 24rpx 30rpx; background: #fff; border-bottom: 1rpx solid #eee; }
|
|
|
.demand-label, .demand-title, .demand-summary, .demand-link { display: block; }
|
|
|
.demand-label { color: #947348; font-size: 24rpx; }
|
|
|
.demand-title { color: #303133; font-size: 30rpx; font-weight: bold; margin-top: 10rpx; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
|
.demand-summary { color: #777; font-size: 24rpx; margin-top: 8rpx; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
|
|
|
.demand-link { color: #947348; font-size: 24rpx; margin-top: 10rpx; }
|
|
|
.message-context { color: #947348; font-size: 22rpx; text-align: center; margin: 16rpx 0; }
|
|
|
.message-list {
|
|
|
min-height: 0;
|
|
|
flex: 1;
|
|
|
padding: 20rpx;
|
|
|
overflow-y: auto;
|
|
|
box-sizing: border-box;
|
|
|
}
|
|
|
|
|
|
.message-item {
|
|
|
display: flex;
|
|
|
margin-bottom: 30rpx;
|
|
|
align-items: flex-start;
|
|
|
}
|
|
|
|
|
|
.my-message {
|
|
|
justify-content: flex-end;
|
|
|
|
|
|
.message-content {
|
|
|
background-color: #409eff;
|
|
|
color: #fff;
|
|
|
border-radius: 20rpx 20rpx 4rpx 20rpx;
|
|
|
margin-right: 20rpx;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
.their-message {
|
|
|
justify-content: flex-start;
|
|
|
|
|
|
.message-content {
|
|
|
background-color: #fff;
|
|
|
color: #333;
|
|
|
border-radius: 20rpx 20rpx 20rpx 4rpx;
|
|
|
margin-left: 20rpx;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
.message-content {
|
|
|
padding: 20rpx 25rpx;
|
|
|
max-width: 70%;
|
|
|
font-size: 30rpx;
|
|
|
line-height: 1.6;
|
|
|
display: flex;
|
|
|
flex-direction: column;
|
|
|
}
|
|
|
|
|
|
.message-text {
|
|
|
margin-bottom: 8rpx;
|
|
|
}
|
|
|
|
|
|
.message-time {
|
|
|
font-size: 22rpx;
|
|
|
color: #999;
|
|
|
opacity: 0.8;
|
|
|
}
|
|
|
|
|
|
.time-divider {
|
|
|
display: flex;
|
|
|
justify-content: center;
|
|
|
align-items: center;
|
|
|
margin: 30rpx 0 20rpx 0;
|
|
|
}
|
|
|
|
|
|
.time-text {
|
|
|
background-color: rgba(0, 0, 0, 0.1);
|
|
|
color: #999;
|
|
|
font-size: 24rpx;
|
|
|
padding: 8rpx 16rpx;
|
|
|
border-radius: 20rpx;
|
|
|
text-align: center;
|
|
|
}
|
|
|
|
|
|
.loading-more {
|
|
|
display: flex;
|
|
|
justify-content: center;
|
|
|
align-items: center;
|
|
|
padding: 20rpx 0;
|
|
|
}
|
|
|
|
|
|
.loading-text {
|
|
|
font-size: 24rpx;
|
|
|
color: #999;
|
|
|
margin-left: 10rpx;
|
|
|
}
|
|
|
|
|
|
.input-area {
|
|
|
display: flex;
|
|
|
align-items: center;
|
|
|
padding: 20rpx;
|
|
|
background-color: #fff;
|
|
|
border-top: 1rpx solid #e0e0e0;
|
|
|
}
|
|
|
|
|
|
.input-field {
|
|
|
flex: 1;
|
|
|
margin-right: 20rpx;
|
|
|
background-color: #f5f5f5;
|
|
|
border-radius: 12rpx;
|
|
|
padding: 0 20rpx;
|
|
|
}
|
|
|
|
|
|
.send-button {
|
|
|
height: 70rpx;
|
|
|
line-height: 70rpx;
|
|
|
}
|
|
|
</style> |