"use strict"; var common_vendor = require("../../common/vendor.js"); var config_index = require("../../config/index.js"); var api_index = require("../../api/index.js"); var utils_wsRequest = require("../../utils/wsRequest.js"); require("../../utils/request.js"); const _sfc_main = { setup(__props) { const currentDate = common_vendor.hooks().format("YYYY-MM-DD"); const currentMessage = common_vendor.ref(""); const messageList = common_vendor.reactive([]); const initalMessageLength = common_vendor.ref(0); const backGroundList = common_vendor.reactive([]); const currentBackGroundImage = common_vendor.computed$1(() => { const pageNum = Math.floor((messageList.length - initalMessageLength.value) / 10); const currentIndex = pageNum % backGroundList.length; return backGroundList[currentIndex] || ""; }); const sessionId = common_vendor.ref(""); const aiAvatarId = common_vendor.ref(""); const currentRoleId = common_vendor.ref(""); const userAvatar = common_vendor.ref(""); common_vendor.onLoad(() => { console.log("onLoad"); const { user_info } = common_vendor.index.getStorageSync("userData"); userAvatar.value = (user_info == null ? void 0 : user_info.avatar_id) || config_index.defaultAvatar; common_vendor.index.getStorage({ key: "chatInfo", success: ({ data }) => { const { session_id, avatar_id, role_id, background_ids = "", role_name } = data; common_vendor.index.setNavigationBarTitle({ title: role_name || "" }); if (sessionId) { sessionId.value = session_id; aiAvatarId.value = avatar_id; currentRoleId.value = role_id; backGroundList.push(...background_ids.split(",")); getHistoryMsg(); } } }); }); const getHistoryMsg = () => { api_index.getHistoryMessage({ role_id: currentRoleId.value, start_time: common_vendor.hooks().subtract(7, "days").startOf().format("YYYY-MM-DD HH:mm:ss"), stop_time: common_vendor.hooks().endOf().format("YYYY-MM-DD HH:mm:ss") }).then(({ status, data }) => { if (Number(status) === 200 && Array.isArray(data == null ? void 0 : data.list)) { const msgList = data.list.map(({ create_at, direction, message }) => ({ from: Number(direction) === 0 ? "me" : "ai", avatar: Number(direction) === 0 ? userAvatar.value : aiAvatarId.value, message, date: create_at.split(" ")[0], time: create_at.split(" ")[1] })); messageList.push(...msgList); scrollToLastItem(); } }).catch((err) => { }).finally(initChat); }; const ws = common_vendor.ref(null); const messageIsEnd = common_vendor.ref(false); const initChat = async () => { var _a; try { ws.value = new utils_wsRequest.webSocketClass(config_index.wsUrl, 60); (_a = ws.value) == null ? void 0 : _a.initSocket().then((res) => { common_vendor.index.$on("message", ({ data }) => { if (typeof data === "string") { const { type, code, msg } = JSON.parse(data); if (Number(code) === 200 && msg) { if (messageIsEnd.value) { messageList.push({ isSaved: false, from: "ai", avatar: aiAvatarId.value, message: msg, date: common_vendor.hooks().format("YYYY-MM-DD"), time: common_vendor.hooks().format("HH:mm:ss") }); } else { const [lastMessageItem] = messageList.slice(-1); lastMessageItem.message += msg; } scrollToLastItem(); } if (type === "end") { onSaveMessage(); } messageIsEnd.value = type === "end"; } }); }); } catch (e) { throw new Error(e); } }; const toCall = () => { var _a; (_a = ws.value) == null ? void 0 : _a.closeSocket(); common_vendor.index.navigateTo({ url: "/pages/call/index?avatar_id=" + aiAvatarId.value }); }; const sendMessage = () => { var _a; if (currentMessage.value) { const data = { text: currentMessage.value, audio: "", is_close: false, meta_info: { stream: false, voice_synthesize: false, is_end: false, encoding: "", session_id: sessionId.value } }; (_a = ws.value) == null ? void 0 : _a.sendMsg(data).then((res) => { if (res) { messageList.push({ isSaved: false, from: "me", avatar: userAvatar.value, message: currentMessage.value, date: common_vendor.hooks().format("YYYY-MM-DD"), time: common_vendor.hooks().format("HH:mm:ss") }); currentMessage.value = ""; messageIsEnd.value = true; onSaveMessage(); } }).catch(() => { common_vendor.index.showToast({ title: "\u53D1\u9001\u6D88\u606F\u5931\u8D25", icon: "none" }); }).finally(scrollToLastItem); } else { common_vendor.index.showToast({ title: "\u8BF7\u8F93\u5165\u5185\u5BB9", duration: 2e3 }); } }; const onSaveMessage = () => { const [item] = messageList.slice(-1); if (item.isSaved) { return; } api_index.saveMessage({ role_id: currentRoleId.value, direction: item.from === "me" ? 0 : 1, message: item.message }).then(({ status }) => { item.isSaved = true; }); }; const lastItemId = common_vendor.ref(""); const scrollToItem = common_vendor.ref(""); const scrollToLastItem = () => { const id = (Math.random() * 1e10).toFixed(0); lastItemId.value = id; common_vendor.nextTick(() => { scrollToItem.value = `item_${id}`; }); }; common_vendor.onUnmounted(() => { var _a; console.log("onUnmounted"); (_a = ws.value) == null ? void 0 : _a.closeSocket(); common_vendor.index.$off("message"); }); return (_ctx, _cache) => { return common_vendor.e({ a: common_vendor.unref(currentBackGroundImage) }, common_vendor.unref(currentBackGroundImage) ? { b: `${common_vendor.unref(config_index.filePrefix)}${common_vendor.unref(currentBackGroundImage)}` } : {}, { c: common_vendor.f(common_vendor.unref(messageList), (item, index, i0) => { return { a: common_vendor.t(item.date === common_vendor.unref(currentDate) ? "\u4ECA\u5929" : item.date), b: common_vendor.t(item.time), c: `${common_vendor.unref(config_index.filePrefix)}${item.avatar}`, d: common_vendor.t(item.message), e: item.message, f: `item_${index}`, g: common_vendor.n(item.from === "me" ? "chat_item right" : "chat_item left") }; }), d: `item_${lastItemId.value}`, e: scrollToItem.value, f: common_vendor.o(toCall), g: common_vendor.o(sendMessage), h: currentMessage.value, i: common_vendor.o(($event) => currentMessage.value = $event.detail.value), j: common_vendor.o(sendMessage) }); }; } }; var MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-725f8307"], ["__file", "C:/Users/bzm15/Desktop/ix/mini_web-develop/pages/chatOnline/index.vue"]]); wx.createPage(MiniProgramPage);