| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- "use strict";
- const common_vendor = require("../../common/vendor.js");
- const common_assets = require("../../common/assets.js");
- const _sfc_main = {
- data() {
- return {
- inputMessage: "",
- chatMessages: [
- {
- sender: "ai",
- content: "您好!我是农小禹,您的智能农业助手🌱 我可以帮您解答农业种植、病虫害防治、农产品管理等方面的问题。有什么可以帮助您的吗?",
- time: this.getFormattedTime(new Date(Date.now() - 36e5)),
- // 1小时前
- timestamp: Date.now() - 36e5,
- isWelcome: true
- },
- {
- sender: "user",
- content: "南方现在适合种什么蔬菜?",
- time: this.getFormattedTime(new Date(Date.now() - 6e4)),
- // 1分钟前
- timestamp: Date.now() - 6e4
- },
- {
- sender: "ai",
- content: "您可以考虑种植以下蔬菜:\n\n1. 空心菜:耐热耐湿,生长快速\n2. 丝瓜:适应性强,产量高\n3. 茄子:耐热性好,病虫害较少\n4. 辣椒:喜温喜光,南方气候适宜\n5. 秋葵:抗病性强,营养价值高\n\n记得注意排水和通风,南方雨水多,做好防涝措施。建议早晚浇水,避开中午高温时段。",
- time: this.getFormattedTime(/* @__PURE__ */ new Date()),
- timestamp: Date.now()
- }
- ],
- scrollTop: 0,
- inputHeight: 120,
- // 单位px
- scrollTimer: null,
- isProcessing: false,
- // 是否正在处理请求
- suggestedQuestions: [
- "水稻插秧后如何管理?",
- "果树夏季修剪技巧?",
- "如何防治蔬菜常见病虫害?",
- "农药使用注意事项?",
- "有机肥和化肥怎么搭配使用?"
- ],
- statusBarHeight: 20,
- // 默认值,会在mounted中获取真实值
- safeAreaBottom: 34,
- // 默认值,会在mounted中获取真实值
- isIOS: false,
- // 是否是iOS设备
- lastMessageDate: ""
- // 上一条消息的日期,用于判断是否显示日期分割线
- };
- },
- // 设置页面标题
- onNavigationBarButtonTap(e) {
- common_vendor.index.__f__("log", "at pages/ai-chat/index.vue:150", "导航栏按钮点击:", e);
- },
- mounted() {
- const systemInfo = common_vendor.index.getSystemInfoSync();
- this.statusBarHeight = systemInfo.statusBarHeight || 20;
- this.isIOS = systemInfo.platform === "ios";
- this.safeAreaBottom = systemInfo.safeAreaInsets ? systemInfo.safeAreaInsets.bottom || 0 : 0;
- this.initMessages();
- this.$nextTick(() => {
- this.scrollToBottom();
- });
- },
- methods: {
- onInputFocus() {
- this.$nextTick(() => {
- this.scrollToBottom();
- });
- },
- scrollToBottom() {
- this.$nextTick(() => {
- const query = common_vendor.index.createSelectorQuery().in(this);
- query.select(".chat-list").boundingClientRect((data) => {
- if (data) {
- this.scrollTop = data.height + 1e3;
- if (this.isH5) {
- setTimeout(() => {
- const scrollEl = document.querySelector(".chat-container");
- if (scrollEl) {
- scrollEl.scrollTop = scrollEl.scrollHeight;
- }
- }, 100);
- }
- }
- }).exec();
- });
- },
- onScroll(e) {
- },
- getCurrentTime() {
- return this.getFormattedTime(/* @__PURE__ */ new Date());
- },
- getFormattedTime(date) {
- const hours = date.getHours().toString().padStart(2, "0");
- const minutes = date.getMinutes().toString().padStart(2, "0");
- return `${hours}:${minutes}`;
- },
- submitQuestion() {
- if (!this.inputMessage.trim() || this.isProcessing)
- return;
- this.isProcessing = true;
- this.chatMessages.push({
- sender: "user",
- content: this.inputMessage,
- time: this.getCurrentTime()
- });
- const userQuestion = this.inputMessage;
- this.inputMessage = "";
- this.scrollToBottom();
- this.chatMessages.push({
- sender: "ai",
- content: "正在思考...",
- time: this.getCurrentTime(),
- isTyping: true
- });
- setTimeout(() => {
- this.chatMessages.pop();
- let aiResponse = "感谢您的提问!作为您的农技助理,我很高兴能帮助您解决农业相关问题。您询问的内容我已收到,我们团队正在研究适合的解决方案。";
- if (userQuestion.includes("水稻")) {
- aiResponse = "水稻种植需要注意水肥管理和病虫害防治。目前南方地区水稻插秧时间已到,建议您选择抗病性强的品种,如'中优84'。插秧后7天开始浅水促根,分蘖期保持3-5cm水层。";
- } else if (userQuestion.includes("蔬菜")) {
- aiResponse = "夏季蔬菜种植需注意遮阳和水分管理。建议种植耐热品种如空心菜、苋菜、茄子等。可以采用遮阳网减少强光照射,早晚浇水避开高温时段。";
- } else if (userQuestion.includes("果树")) {
- aiResponse = "果树现在应注意夏季修剪和病虫害防治。柑橘类果树可以进行夏季修剪,去除徒长枝和内膛枝。同时注意柑橘黄龙病和炭疽病的预防,建议定期喷施药剂保护。";
- }
- this.chatMessages.push({
- sender: "ai",
- content: aiResponse,
- time: this.getCurrentTime()
- });
- this.scrollToBottom();
- this.isProcessing = false;
- }, 2e3);
- },
- useQuestion(question) {
- this.inputMessage = question;
- },
- containsKeywords(text) {
- const keywords = ["水稻", "小麦", "玉米", "病虫害", "农药", "化肥", "有机肥", "种植技术"];
- return keywords.some((keyword) => text.includes(keyword));
- },
- formatMessage(text) {
- return text.replace(/\n/g, "<br>");
- },
- initMessages() {
- this.chatMessages.forEach((msg) => {
- if (!msg.timestamp) {
- msg.timestamp = (/* @__PURE__ */ new Date()).getTime();
- }
- });
- this.chatMessages.sort((a, b) => a.timestamp - b.timestamp);
- },
- showDateSeparator(index) {
- if (index === 0)
- return true;
- const currentMsg = this.chatMessages[index];
- const prevMsg = this.chatMessages[index - 1];
- return this.isDifferentDay(currentMsg.timestamp, prevMsg.timestamp) || currentMsg.timestamp - prevMsg.timestamp > 30 * 60 * 1e3;
- },
- isDifferentDay(timestamp1, timestamp2) {
- const date1 = new Date(timestamp1);
- const date2 = new Date(timestamp2);
- return date1.getDate() !== date2.getDate() || date1.getMonth() !== date2.getMonth() || date1.getFullYear() !== date2.getFullYear();
- },
- formatDateSeparator(timestamp) {
- const now = /* @__PURE__ */ new Date();
- const msgDate = new Date(timestamp);
- if (this.isSameDay(msgDate, now)) {
- return "今天 " + this.getFormattedTime(msgDate);
- }
- const yesterday = new Date(now);
- yesterday.setDate(now.getDate() - 1);
- if (this.isSameDay(msgDate, yesterday)) {
- return "昨天 " + this.getFormattedTime(msgDate);
- }
- const oneWeekAgo = new Date(now);
- oneWeekAgo.setDate(now.getDate() - 7);
- if (msgDate >= oneWeekAgo) {
- const weekdays = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
- return weekdays[msgDate.getDay()] + " " + this.getFormattedTime(msgDate);
- }
- return msgDate.getFullYear() + "年" + (msgDate.getMonth() + 1) + "月" + msgDate.getDate() + "日 " + this.getFormattedTime(msgDate);
- },
- isSameDay(date1, date2) {
- return date1.getDate() === date2.getDate() && date1.getMonth() === date2.getMonth() && date1.getFullYear() === date2.getFullYear();
- }
- }
- };
- function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
- return common_vendor.e({
- a: common_vendor.f($data.chatMessages, (message, index, i0) => {
- return common_vendor.e({
- a: message.sender === "ai"
- }, message.sender === "ai" ? common_vendor.e({
- b: common_assets._imports_0$1,
- c: message.isTyping
- }, message.isTyping ? {} : {
- d: common_vendor.t(message.content),
- e: $options.containsKeywords(message.content) ? 1 : ""
- }, {
- f: message.isTyping ? 1 : "",
- g: message.isWelcome || index === 0 ? 1 : "",
- h: common_vendor.t(message.time)
- }) : {
- i: common_vendor.t(message.time),
- j: common_vendor.t(message.content),
- k: common_assets._imports_1
- }, {
- l: index,
- m: message.sender === "ai" ? 1 : "",
- n: message.sender === "user" ? 1 : ""
- });
- }),
- b: $data.scrollTop,
- c: common_vendor.o((...args) => $options.onScroll && $options.onScroll(...args)),
- d: `calc(100vh - ${$data.inputHeight}px)`,
- e: $data.chatMessages.length <= 3 && !$data.inputMessage
- }, $data.chatMessages.length <= 3 && !$data.inputMessage ? {
- f: common_vendor.f($data.suggestedQuestions, (question, index, i0) => {
- return {
- a: common_vendor.t(question),
- b: index,
- c: common_vendor.o(($event) => $options.useQuestion(question), index)
- };
- })
- } : {}, {
- g: common_vendor.o((...args) => $options.submitQuestion && $options.submitQuestion(...args)),
- h: common_vendor.o((...args) => $options.onInputFocus && $options.onInputFocus(...args)),
- i: $data.isProcessing,
- j: $data.inputMessage,
- k: common_vendor.o(($event) => $data.inputMessage = $event.detail.value),
- l: $data.inputMessage.trim() && !$data.isProcessing ? "/static/icons/chat.png" : "/static/icons/chat_off.png",
- m: !$data.inputMessage.trim() || $data.isProcessing ? 1 : "",
- n: common_vendor.o((...args) => $options.submitQuestion && $options.submitQuestion(...args)),
- o: `${$data.isIOS ? $data.safeAreaBottom : 20}rpx`
- });
- }
- const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
- wx.createPage(MiniProgramPage);
- //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/ai-chat/index.js.map
|