oak-general-business/es/components/sessionMessage/list/index.js

300 lines
9.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { DATA_SUBSCRIBER_KEYS } from '../../../config/constants';
import { generateNewId } from 'oak-domain/lib/utils/uuid';
export default OakComponent({
entity: 'sessionMessage',
projection: {
id: 1,
text: 1,
type: 1,
userId: 1,
wechatUserId: 1,
applicationId: 1,
createTime: 1,
$$createAt$$: 1,
aaoe: 1,
extraFile$entity: {
$entity: 'extraFile',
data: {
id: 1,
tag1: 1,
origin: 1,
bucket: 1,
objectId: 1,
filename: 1,
extra1: 1,
extra2: 1,
extension: 1,
type: 1,
entity: 1,
entityId: 1,
applicationId: 1,
uploadState: 1,
sort: 1,
},
// filter: {
// tag1: {
// $in: ['image'],
// },
// },
},
sessionId: 1,
session: {
id: 1,
entity: 1,
entityId: 1,
},
},
isList: true,
lifetimes: {
async ready() {
const { sessionId } = this.props;
this.createItem();
this.getSessionInfo();
if (sessionId) {
const unSub = await this.subDataEvents([
`${DATA_SUBSCRIBER_KEYS.sessionMessageList}-${sessionId}`,
]);
this.setState({
unSub,
});
}
},
detached() {
const { unSub } = this.state;
unSub && unSub();
},
},
listeners: {
num(prev, next) {
if (prev.num !== next.num) {
this.pageScroll('comment');
}
},
sessionId(prev, next) {
if (this.state.oakFullpath) {
if (prev.sessionId !== next.sessionId) {
if (next.sessionId) {
const { sessionMessageId } = this.state;
this.getSessionInfo();
// 如果sessionId变了需要重新刷新下
this.refresh();
this.removeItem(sessionMessageId);
this.createItem();
this.pageScroll('comment');
}
}
}
},
},
formData({ data, features }) {
const sessionMessages = data?.filter((ele) => ele.$$createAt$$ !== 1);
// 获取用户最后一条sessionMessage
const userLastMessage = this.getUserLastMessage();
return {
sessionMessages,
num: data?.length,
userLastMessage,
};
},
properties: {
sessionId: '',
isEntity: false,
dialog: false,
entity: '',
entityId: '',
entityDisplay: (data) => [], // user端指示如何显示entity对象名称
entityProjection: null, // user端指示需要取哪些entity的属性来显示entityDisplay
},
filters: [
{
filter() {
const { sessionId } = this.props;
return {
sessionId,
};
},
},
],
sorters: [
{
sorter: {
$attr: {
$$createAt$$: 1,
},
$direction: 'desc',
},
},
],
data: {
buttonHidden: true,
unSub: undefined,
},
methods: {
getUserLastMessage() {
const { sessionId } = this.props;
const [lastMessage] = this.features.cache.get('sessionMessage', {
data: {
id: 1,
sessionId: 1,
text: 1,
type: 1,
userId: 1,
wechatUserId: 1,
applicationId: 1,
createTime: 1,
$$createAt$$: 1,
aaoe: 1,
},
filter: {
sessionId,
aaoe: false,
},
sorter: [
{
$attr: {
$$createAt$$: 1,
},
$direction: 'desc',
},
],
count: 1,
});
return lastMessage;
},
setContent(text) {
const { sessionMessageId } = this.state;
this.updateItem({
text,
type: 'text',
}, sessionMessageId);
},
async getSessionInfo() {
const { sessionId } = this.props;
if (!sessionId) {
return;
}
const { data: [session], } = await this.features.cache.refresh('session', {
data: {
id: 1,
entity: 1,
entityId: 1,
userId: 1,
user: {
id: 1,
name: 1,
nickname: 1,
mobile$user: {
$entity: 'mobile',
data: {
id: 1,
mobile: 1,
userId: 1,
},
},
extraFile$entity: {
$entity: 'extraFile',
data: {
id: 1,
tag1: 1,
origin: 1,
bucket: 1,
objectId: 1,
filename: 1,
extra1: 1,
extension: 1,
type: 1,
entity: 1,
entityId: 1,
},
filter: {
tag1: {
$in: ['avatar'],
},
},
},
},
},
filter: {
id: sessionId,
},
});
},
pageScroll(id) {
const doc = window.document.getElementById(id);
setTimeout(() => doc.scrollTo(0, 10000), 500);
},
async createItem() {
const { text, wechatUserId } = this.state;
const { sessionId, isEntity } = this.props;
const userId = this.features.token.getUserId();
const applicationId = this.features.application.getApplicationId();
const sessionMessageId = this.addItem({
applicationId,
userId,
wechatUserId,
sessionId: sessionId,
aaoe: isEntity,
});
this.setState({
sessionMessageId,
});
},
async sendMessage() {
const { sessionMessageId } = this.state;
this.updateItem({
createTime: Date.now(),
type: 'text',
}, sessionMessageId);
await this.execute(undefined, false);
this.pageScroll('comment');
this.createItem();
},
async customUpload(file) {
const { sessionMessageId, userLastMessage } = this.state;
const { sessionId, isEntity } = this.props;
const { name, size, type, originFileObj } = file;
let applicationId = this.features.application.getApplicationId();
const extension = name.substring(name.lastIndexOf('.') + 1);
const filename = name.substring(0, name.lastIndexOf('.'));
let origin = null;
//需要获取用户方回复的applicationId判断用户是否从微信公众号或小程序发起客服消息
if (isEntity && userLastMessage?.wechatUserId) {
applicationId = userLastMessage?.applicationId;
origin = 'wechat';
}
const extraFile = {
applicationId,
origin,
type: 'image',
tag1: 'image',
filename,
fileType: type,
size,
extension,
objectId: generateNewId(),
id: generateNewId(),
uploadState: 'uploading',
sort: 1000,
extra2: {
isPermanent: false,
},
entity: 'sessionMessage',
entityId: sessionMessageId,
};
await this.features.extraFile.autoUpload(extraFile, originFileObj);
try {
this.updateItem({
createTime: Date.now(),
type: 'image',
}, sessionMessageId);
await this.execute(undefined, false);
this.pageScroll('comment');
this.createItem();
}
catch (err) {
throw err;
}
},
},
});