51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
import { Button, Text } from '@tarojs/components';
|
|
import type { ReactNode } from 'react';
|
|
|
|
export type ContactCsSessionContext = {
|
|
orderId?: string;
|
|
orderNo?: string;
|
|
from?: string;
|
|
};
|
|
|
|
type ContactCsButtonProps = {
|
|
className?: string;
|
|
children?: ReactNode;
|
|
/** 客服会话来源上下文,便于客服后台识别 */
|
|
session?: ContactCsSessionContext;
|
|
};
|
|
|
|
const isWeapp = process.env.TARO_ENV === 'weapp';
|
|
|
|
/** 组装 session-from(微信限制约 1000 字符) */
|
|
export function buildCsSessionFrom(session?: ContactCsSessionContext): string {
|
|
if (!session) return 'dukang|from=mini-user';
|
|
const parts = ['dukang'];
|
|
if (session.from) parts.push(`from=${session.from}`);
|
|
if (session.orderNo) parts.push(`orderNo=${session.orderNo}`);
|
|
if (session.orderId) parts.push(`orderId=${session.orderId}`);
|
|
return parts.join('|');
|
|
}
|
|
|
|
/**
|
|
* 微信小程序客服入口(open-type=contact)。
|
|
* 非 weapp 环境不渲染,由调用方走电话等兜底。
|
|
*/
|
|
export default function ContactCsButton({
|
|
className = '',
|
|
children = '联系在线客服',
|
|
session,
|
|
}: ContactCsButtonProps) {
|
|
if (!isWeapp) return null;
|
|
|
|
return (
|
|
<Button
|
|
className={className}
|
|
openType="contact"
|
|
sessionFrom={buildCsSessionFrom(session)}
|
|
hoverClass="none"
|
|
>
|
|
{typeof children === 'string' ? <Text>{children}</Text> : children}
|
|
</Button>
|
|
);
|
|
}
|