jiuyiUniapp/jiuyi2/unpackage/dist/dev/mp-weixin/pages/news/question-answer/index.js

354 lines
11 KiB
JavaScript
Raw Normal View History

2024-12-18 15:46:27 +08:00
"use strict";
const common_vendor = require("../../../common/vendor.js");
const common_assets = require("../../../common/assets.js");
const components_public_eventBus = require("../../../components/public/event-bus.js");
const utils_msgtype = require("../../../utils/msgtype.js");
const pages_news_questionAnswer_fn = require("./fn.js");
if (!Array) {
const _easycom_uni_easyinput2 = common_vendor.resolveComponent("uni-easyinput");
_easycom_uni_easyinput2();
}
const _easycom_uni_easyinput = () => "../../../uni_modules/uni-easyinput/components/uni-easyinput/uni-easyinput.js";
if (!Math) {
(common_vendor.unref(newsTempSystem) + common_vendor.unref(newsTemplate) + JyVoice + _easycom_uni_easyinput + JyPlus + emoji)();
}
const newsTempSystem = () => "./components/news-temp-system/index.js";
const newsTemplate = () => "./components/news-temp/index.js";
const emoji = () => "./emoji.js";
const JyVoice = () => "./jy-voice.js";
const JyPlus = () => "./jy-plus.js";
const _sfc_main = {
__name: "index",
setup(__props) {
const {
userinfo
} = common_vendor.useStore().state;
const {
showToastAndRedirect,
format_url
} = common_vendor.inject("util");
const chatParams = common_vendor.reactive({
from: null,
to: null,
chatType: null,
ext: {
from: {
userNickname: null,
userPortrait: null
}
}
});
common_vendor.provide("chatParams", chatParams);
common_vendor.onMounted(() => {
try {
const msg = common_vendor.index.getStorageSync("toUser") || void 0;
if (msg) {
console.log(msg, "msg");
switch (msg.msgType) {
case utils_msgtype.msgType.chatType.SINGLE_CHAT:
chatParams.to = msg.userId;
break;
case utils_msgtype.msgType.chatType.GROUP_CHAT:
chatParams.to = msg.groupid;
break;
default:
break;
}
chatParams.chatType = msg.msgType;
chatParams.from = userinfo.userId;
chatParams.ext.from = {
userNickname: userinfo.userNickname,
userPortrait: userinfo.userPortrait
};
}
} catch (error) {
console.log("uni.getStorageSync('toUser')报错", error);
}
if (!common_vendor.index.WebIM.config.isMultiLoginSessions) {
getApp().globalData.conn.reopen();
}
getHistory(scrollToBottom);
components_public_eventBus.eventBus.on("onMessage", (msg) => {
console.log(123222222, msg, chatParams.to);
switch (msg.chatType) {
case utils_msgtype.msgType.chatType.SINGLE_CHAT:
if (msg.from == chatParams.to) {
list.value.push(msg);
scrollToBottom();
}
break;
case utils_msgtype.msgType.chatType.GROUP_CHAT:
if (msg.to == chatParams.to) {
list.value.push(msg);
scrollToBottom();
}
break;
}
});
});
common_vendor.onUnmounted(() => {
components_public_eventBus.eventBus.off("onMessage");
});
const content = common_vendor.ref("");
const list = common_vendor.ref([]);
const top = common_vendor.ref(0);
const toolStatus = common_vendor.reactive({
// 录音状态
showVoice: false,
// emoji
showEmoji: false,
// 加号
showPlus: false
});
const emojiTap = (val) => {
content.value = content.value + val;
};
const focus = () => {
Object.keys(toolStatus).forEach((item) => {
toolStatus[item] = false;
});
};
const toolClick = (val) => {
if (toolStatus[val]) {
toolStatus[val] = false;
return;
}
Object.keys(toolStatus).forEach((item) => {
toolStatus[item] = false;
});
toolStatus[val] = true;
};
const send = () => {
let type = utils_msgtype.msgType.TEXT;
let option = {
// 消息类型。
type,
// 消息内容。
msg: content.value,
// 消息接收方:单聊为对方用户 ID群聊和聊天室分别为群组 ID 和聊天室 ID。
...chatParams
};
let msg = common_vendor.index.WebIM.message.create(option);
WebIMSend(msg, {
type,
msg: content.value
});
};
const plusClick = (res) => {
let type = res.type;
switch (type) {
case utils_msgtype.msgType.IMAGE:
let option = {
...chatParams,
// 消息类型。
type,
// 图片文件的 URL 地址。
url: res.content
};
let msg = common_vendor.index.WebIM.message.create(option);
WebIMSend(msg, {
type,
url: res.content
});
break;
}
};
const setVoice = (res) => {
let type = res.type;
let filename = `${(/* @__PURE__ */ new Date()).getTime()}.mp3`;
let option = {
...chatParams,
type,
// 图片文件的 URL 地址。
body: {
type,
filename,
url: res.content,
length: Math.ceil(res.length)
}
};
let msg = common_vendor.index.WebIM.message.create(option);
WebIMSend(msg, {
type,
url: res.content,
filename,
length: Math.ceil(res.length)
});
};
const WebIMSend = (msg, obj) => {
common_vendor.index.WebIM.conn.send(msg).then((val) => {
list.value.push({
...chatParams,
time: (/* @__PURE__ */ new Date()).getTime(),
onlineState: 3,
id: val.localMsgId,
...obj
});
console.log(list.value);
scrollToBottom();
}).catch((e) => {
console.log("方法发送该图片消", e);
});
};
const setRed = (res, params) => {
console.log("====================================");
console.log(res, params);
console.log("====================================");
if (res.code == 200) {
const {
data
} = JSON.parse(res.data.message);
const id = data[Object.keys(data)[0]];
let option = {
...chatParams,
id,
customEvent: "custom_event",
time: data.createTime,
onlineState: 3,
// 自定义消息扩展。
ext: {
...params,
id: res.data.id,
time: data.createTime
}
};
option.type = utils_msgtype.msgType.CUSTOM;
list.value.push(option);
}
};
common_vendor.provide("setRed", setRed);
const cursor = common_vendor.ref(-1);
const isLast = common_vendor.ref(false);
const loading = common_vendor.ref(false);
const getHistory = (callback = () => {
}) => {
if (loading.value) {
showToastAndRedirect("加载中");
return;
}
if (!isLast.value) {
loading.value = true;
console.log(111111, chatParams);
pages_news_questionAnswer_fn.getHistoryMsg(chatParams.chatType, chatParams.to, cursor.value).then((res) => {
cursor.value = res.messages[res.messages.length - 1].id;
isLast.value = res.isLast;
list.value = res.messages.reverse().concat(list.value);
callback();
}).catch((e) => {
}).finally(() => {
loading.value = false;
});
}
};
const handleScroll = (e) => {
if (e.detail.scrollTop === 0) {
getHistory();
}
};
common_vendor.onLoad((options) => {
common_vendor.index.setNavigationBarTitle({
title: options.userNickname
});
});
const scrollToBottom = () => {
common_vendor.nextTick$1().then(() => {
common_vendor.index.createSelectorQuery().select("#scroll-content").boundingClientRect((rect) => {
top.value = rect.height + 60;
}).exec();
});
};
const is = common_vendor.ref(false);
const isH = common_vendor.ref({
height: "0px",
duration: "0.25s"
});
const keyboardheightchange = (res) => {
isH.value = res.detail;
common_vendor.nextTick$1(() => {
is.value = res.detail.height > 0 ? true : false;
});
};
return (_ctx, _cache) => {
return common_vendor.e({
a: common_vendor.f(list.value, (item, index, i0) => {
var _a, _b;
return common_vendor.e({
a: ((_a = item == null ? void 0 : item.ext) == null ? void 0 : _a.type) === "system"
}, ((_b = item == null ? void 0 : item.ext) == null ? void 0 : _b.type) === "system" ? {
b: "9bd9708b-0-" + i0,
c: common_vendor.p({
item
})
} : common_vendor.e({
d: item.from === chatParams.from
}, item.from === chatParams.from ? {
e: "9bd9708b-1-" + i0,
f: common_vendor.p({
item
}),
g: common_vendor.unref(format_url)(common_vendor.unref(userinfo).userPortrait, "img")
} : common_vendor.e({
h: item.chatType === common_vendor.unref(utils_msgtype.msgType).chatType.GROUP_CHAT
}, item.chatType === common_vendor.unref(utils_msgtype.msgType).chatType.GROUP_CHAT ? {
i: common_vendor.t(item.ext.from.userNickname)
} : {}, {
j: "9bd9708b-2-" + i0,
k: common_vendor.p({
item
})
})), {
l: index
});
}),
b: top.value,
c: common_vendor.o$1(handleScroll)
}, {}, {
e: common_vendor.unref(common_assets.NewsVoice),
f: common_vendor.o$1(($event) => toolClick("showVoice")),
g: toolStatus.showVoice
}, toolStatus.showVoice ? {
h: common_vendor.o$1(setVoice)
} : {}, {
i: !toolStatus.showVoice
}, !toolStatus.showVoice ? {
j: common_vendor.o$1(focus),
k: common_vendor.o$1(send),
l: common_vendor.o$1(keyboardheightchange),
m: common_vendor.o$1(($event) => content.value = $event),
n: common_vendor.p({
type: "text",
clearable: false,
["adjust-position"]: false,
placeholder: "请输入你的问题",
confirmType: "发送",
modelValue: content.value
})
} : {}, {
o: common_vendor.unref(common_assets.NewsEmoji),
p: common_vendor.o$1(($event) => toolClick("showEmoji")),
q: !content.value
}, !content.value ? {
r: common_vendor.unref(common_assets.NewsPlus),
s: common_vendor.o$1(($event) => toolClick("showPlus"))
} : {
t: common_vendor.o$1(send)
}, {
v: is.value
}, is.value ? {
w: `${isH.value.height}px`,
x: `${isH.value.duration}s`
} : {}, {
y: toolStatus.showPlus
}, toolStatus.showPlus ? {
z: common_vendor.o$1(plusClick)
} : {}, {
A: toolStatus.showEmoji
}, toolStatus.showEmoji ? {
B: common_vendor.o$1(emojiTap)
} : {});
};
}
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-9bd9708b"]]);
wx.createPage(MiniProgramPage);