64 lines
2.2 KiB
JavaScript
64 lines
2.2 KiB
JavaScript
const { matchmakerStore, matchStore, appStore } = require('../../stores');
|
|||
|
|
const { matchmakerService } = require('../../services');
|
||
|
|
const { ROUTE, to } = require('../../constants');
|
||
|
|
|
||
|
|
Page({
|
||
|
|
data: { loading: true, actionLoadingId: '', recommendations: [] },
|
||
|
|
|
||
|
|
onShow() { this.load(); },
|
||
|
|
|
||
|
|
async load() {
|
||
|
|
this.setData({ loading: true });
|
||
|
|
try {
|
||
|
|
const result = await matchmakerStore.fetchRecommendations();
|
||
|
|
const items = (result && Array.isArray(result) ? result : matchmakerStore.state.recommendations || [])
|
||
|
|
.map((item) => ({
|
||
|
|
...item,
|
||
|
|
userId: item.userId || item.targetUserId,
|
||
|
|
displayName: item.displayName || '红娘推荐对象',
|
||
|
|
recommendationReasons: item.reason ? [item.reason] : [],
|
||
|
|
}));
|
||
|
|
this.setData({ recommendations: items, loading: false });
|
||
|
|
items.filter((item) => item.status === 'pending_view' && item.id).forEach((item) => {
|
||
|
|
matchmakerService.markRecommendationViewed(item.id).catch(() => {});
|
||
|
|
});
|
||
|
|
} catch (error) {
|
||
|
|
this.setData({ loading: false });
|
||
|
|
appStore.toastError(error, '加载红娘推荐失败');
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
onView(e) {
|
||
|
|
const userId = e.detail && e.detail.userId;
|
||
|
|
if (userId) wx.navigateTo({ url: to.detail(userId) });
|
||
|
|
},
|
||
|
|
|
||
|
|
async onLike(e) {
|
||
|
|
await this.respond(e.detail && e.detail.userId, 'like');
|
||
|
|
},
|
||
|
|
|
||
|
|
async onPass(e) {
|
||
|
|
await this.respond(e.detail && e.detail.userId, 'pass');
|
||
|
|
},
|
||
|
|
|
||
|
|
async respond(userId, action) {
|
||
|
|
if (!userId || this.data.actionLoadingId) return;
|
||
|
|
this.setData({ actionLoadingId: userId });
|
||
|
|
try {
|
||
|
|
const result = action === 'like' ? await matchStore.like(userId) : await matchStore.pass(userId);
|
||
|
|
this.setData({ recommendations: this.data.recommendations.filter((item) => item.userId !== userId) });
|
||
|
|
if (action === 'like' && result && result.mutualMatch && result.matchedConversationId) {
|
||
|
|
wx.navigateTo({ url: to.chat(result.matchedConversationId) });
|
||
|
|
} else {
|
||
|
|
appStore.toastSuccess(action === 'like' ? '已表达喜欢' : '已暂不考虑');
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
appStore.toastError(error);
|
||
|
|
} finally {
|
||
|
|
this.setData({ actionLoadingId: '' });
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
onBack() { wx.navigateBack(); },
|
||
|
|
});
|