Mala-Pro主题对接Crisp
对接Crisp在用户打开聊天框时,自动推送用户的套餐,到期时间,流量重置日期,剩余流量
会同时在用户侧显示,同时推送至Crisp后台供客服查看
并在到期,流量用完时进行提醒
<script type="text/javascript">
window.$crisp = [];
window.CRISP_WEBSITE_ID = "填入网站ID";
(function () {
d = document;
s = d.createElement("script");
s.src = "https://client.crisp.chat/l.js";
s.async = 1;
d.getElementsByTagName("head")[0].appendChild(s);
s.onload = function () {
function updateCrispData() {
if (typeof $crisp === 'undefined') return;
var email = localStorage.getItem('email');
var expiredAt = localStorage.getItem('expired_at');
var planName = localStorage.getItem('plan_name');
var resetDay = localStorage.getItem('reset_day');
var remainingTraffic = localStorage.getItem('remaining_traffic_gb');
if (email && window.$crisp.is("website:available")) {
try {
$crisp.push(["set", "user:email", [email]]);
$crisp.push(["set", "session:data", [[
["plan_name", planName],
["expired_at", expiredAt ? new Date(parseInt(expiredAt) * 1000).toLocaleDateString() : "不限时"],
["reset_day", resetDay ? (resetDay + "号") : "不支持"],
["remaining_traffic", remainingTraffic + "GB"]
]]]);
} catch (e) {
console.error("更新Crisp会话数据时出错:", e);
}
}
}
function showWelcomeMessage() {
var email = localStorage.getItem('email');
if (!email || !window.$crisp.is("website:available")) return;
var resetDay = localStorage.getItem('reset_day');
var resetInfo = resetDay && resetDay !== "0"
? `- 重置日期:每月${resetDay}号\n`
: `- 流量重置:不支持\n`;
var expiredAt = localStorage.getItem('expired_at');
var currentTime = Math.floor(Date.now() / 1000);
var remainingTraffic = localStorage.getItem('remaining_traffic_gb');
// 检查是否已到期或流量已用完
if (expiredAt && parseInt(expiredAt) < currentTime) {
$crisp.push(["do", "message:show", ["text", "您的套餐已到期,请及时续费!"]]);
return;
}
if (!remainingTraffic || parseFloat(remainingTraffic) <= 0) {
$crisp.push(["do", "message:show", ["text", "您的流量已用完,请重置流量!"]]);
return;
}
var expiryInfo = expiredAt
? `- 到期时间:${new Date(parseInt(expiredAt) * 1000).toLocaleDateString()}\n`
: `- 到期时间:不限时\n`;
var welcomeMessage = `您好!您当前的套餐信息:
- 套餐名称:${localStorage.getItem('plan_name')}
- 剩余流量:${localStorage.getItem('remaining_traffic_gb')}GB
${resetInfo}${expiryInfo}
需要帮助吗?`;
$crisp.push(["do", "message:show", ["text", welcomeMessage]]);
}
window.$crisp.push(["on", "chat:opened", showWelcomeMessage]);
setInterval(updateCrispData, 5000);
};
})();
</script>
License:
私有