|
|
@@ -0,0 +1,206 @@
|
|
|
+<template>
|
|
|
+ <view class="pages_height">
|
|
|
+ <view class="intelligent_main">
|
|
|
+ <scroll-view
|
|
|
+ :scroll-y="true"
|
|
|
+ :style="{ height: key_height }"
|
|
|
+ :scroll-into-view="controlId ? bottomId : 'msg' + controlId"
|
|
|
+ :scroll-with-animation="true"
|
|
|
+ class="intelligent_chat"
|
|
|
+ @click="other_click"
|
|
|
+ @dragging="other_click"
|
|
|
+ :show-scrollbar="false"
|
|
|
+ >
|
|
|
+ <view
|
|
|
+ v-for="(item, index) in msg_list"
|
|
|
+ :key="index"
|
|
|
+ :class="'intelligent_chat_view' + index"
|
|
|
+ >
|
|
|
+ <view :class="item.left ? 'robot_word' : 'patient_word'">
|
|
|
+ <view v-if="item.type === 'img' && item.data">
|
|
|
+ <view>
|
|
|
+ <image :src="item.data.url" class="img-msg" mode="widthFix" />
|
|
|
+ </view>
|
|
|
+ </view>
|
|
|
+ <view v-if="item.msg">
|
|
|
+ <view>
|
|
|
+ {{ item.msg }}
|
|
|
+ </view>
|
|
|
+ </view>
|
|
|
+ </view>
|
|
|
+ </view>
|
|
|
+ <view :id="'dade' + num" v-if="controlId"></view>
|
|
|
+ <view :id="'msg' + controlId" v-else></view>
|
|
|
+ </scroll-view>
|
|
|
+
|
|
|
+ <view class="intelligent_input" :style="{ bottom: bottom }">
|
|
|
+ <input
|
|
|
+ type="text"
|
|
|
+ placeholder-style="font-size: 30rpx;color: #C8C8C8;"
|
|
|
+ placeholder="请输入…"
|
|
|
+ :adjust-position="false"
|
|
|
+ v-model="value"
|
|
|
+ @confirm.prevent="input_send"
|
|
|
+ :focus="fouse"
|
|
|
+ :hold-keyboard="hold_keyboard"
|
|
|
+ :confirm-hold="hold_keyboard"
|
|
|
+ @focus="i_focus"
|
|
|
+ @blur="blur"
|
|
|
+ confirm-type="发送"
|
|
|
+ />
|
|
|
+ <view class="input_send" @touchend.prevent="input_send">
|
|
|
+ <image
|
|
|
+ src="https://yiben01.oss-cn-hangzhou.aliyuncs.com/img/921c55e21e95bf68a5d32f4aeab37bc64d2317d0_100.png"
|
|
|
+ mode=""
|
|
|
+ ></image>
|
|
|
+ </view>
|
|
|
+ </view>
|
|
|
+ </view>
|
|
|
+ </view>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script lang="ts">
|
|
|
+import { Component } from "vue-property-decorator";
|
|
|
+import AccountMixin from "@/mixins/account";
|
|
|
+import { textToImage } from "@/apis/base";
|
|
|
+
|
|
|
+@Component({})
|
|
|
+export default class HomeView extends AccountMixin {
|
|
|
+ value = "";
|
|
|
+ // 输入框距底部高度(键盘显示隐藏高度变化)
|
|
|
+ bottom = "0px";
|
|
|
+ // 对话框数组
|
|
|
+ msg_list: {
|
|
|
+ left: boolean;
|
|
|
+ msg: string;
|
|
|
+ type?: "text" | "img";
|
|
|
+ data?: { url: string };
|
|
|
+ }[] = [];
|
|
|
+ // 容器高度
|
|
|
+ key_height = "0px";
|
|
|
+ // 容器滚动底部定位id
|
|
|
+ bottomId = "";
|
|
|
+ // 数组长度,用到定位id
|
|
|
+ num = 2;
|
|
|
+ // 输入框聚焦
|
|
|
+ fouse = false;
|
|
|
+ // 键盘不收起
|
|
|
+ hold_keyboard = true;
|
|
|
+ // 防抖
|
|
|
+ boolen = true;
|
|
|
+ // 定位id
|
|
|
+ controlId = false;
|
|
|
+ inputHeight = 0;
|
|
|
+
|
|
|
+ onShow() {
|
|
|
+ this.keyboardHeightChange();
|
|
|
+ this.compute_input_height();
|
|
|
+ const res_s = uni.getSystemInfoSync();
|
|
|
+ this.key_height = `${res_s.windowHeight - this.inputHeight}px`;
|
|
|
+ }
|
|
|
+
|
|
|
+ onUnload() {
|
|
|
+ uni.offKeyboardHeightChange(); //取消监听键盘高度变化事件,避免内存消耗
|
|
|
+ }
|
|
|
+
|
|
|
+ compute_input_height() {
|
|
|
+ const query = uni.createSelectorQuery().in(this); //这样写就只会选择本页面组件的类名box的
|
|
|
+ query
|
|
|
+ .selectAll(".intelligent_input")
|
|
|
+ .boundingClientRect((data) => {
|
|
|
+ // eslint-disable-next-line no-undef
|
|
|
+ this.inputHeight = (data as UniApp.NodeInfo[])[0].height || 0;
|
|
|
+ })
|
|
|
+ .exec();
|
|
|
+ }
|
|
|
+ // 点击键盘以外的地方收起键盘
|
|
|
+ other_click() {
|
|
|
+ uni.hideKeyboard();
|
|
|
+ }
|
|
|
+ // 监听键盘高度事件
|
|
|
+ keyboardHeightChange() {
|
|
|
+ uni.onKeyboardHeightChange((res) => {
|
|
|
+ //监听键盘高度变化
|
|
|
+ const res_keyboard = uni.getSystemInfoSync();
|
|
|
+ const key_height =
|
|
|
+ res.height -
|
|
|
+ (res_keyboard.screenHeight -
|
|
|
+ res_keyboard.windowHeight +
|
|
|
+ (res_keyboard.safeAreaInsets?.bottom || 0));
|
|
|
+ this.bottom = `${res.height ? key_height : 0}px`;
|
|
|
+
|
|
|
+ this.compute_input_height();
|
|
|
+
|
|
|
+ if (res.height) {
|
|
|
+ this.key_height = `${
|
|
|
+ res_keyboard.windowHeight - this.inputHeight - key_height
|
|
|
+ }px`;
|
|
|
+ } else {
|
|
|
+ this.key_height = `${res_keyboard.windowHeight - this.inputHeight}px`;
|
|
|
+ }
|
|
|
+
|
|
|
+ this.num = this.msg_list.length;
|
|
|
+ this.bottomId = "dade" + this.num;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ // 输入框聚焦事件
|
|
|
+ i_focus() {
|
|
|
+ this.fouse = true;
|
|
|
+ this.hold_keyboard = true;
|
|
|
+ // 换id
|
|
|
+ this.controlId = true;
|
|
|
+ }
|
|
|
+ blur() {
|
|
|
+ // 换id
|
|
|
+ this.controlId = false;
|
|
|
+ }
|
|
|
+ // input发送事件
|
|
|
+ async input_send() {
|
|
|
+ if (!this.value.toString()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ let timer;
|
|
|
+ if (timer) clearTimeout(timer);
|
|
|
+ if (this.boolen) {
|
|
|
+ try {
|
|
|
+ this.boolen = false;
|
|
|
+
|
|
|
+ const msg = this.value.trim();
|
|
|
+ // 增加回答,自己的话
|
|
|
+ this.msg_list = [
|
|
|
+ ...this.msg_list,
|
|
|
+ {
|
|
|
+ left: false,
|
|
|
+ msg,
|
|
|
+ },
|
|
|
+ ];
|
|
|
+ this.value = "";
|
|
|
+ const res = await textToImage({
|
|
|
+ q: msg,
|
|
|
+ });
|
|
|
+ if (res.errno === 10000) {
|
|
|
+ this.msg_list = [
|
|
|
+ ...this.msg_list,
|
|
|
+ ...res.data.imgs.map((data) => {
|
|
|
+ return {
|
|
|
+ left: true,
|
|
|
+ msg: "",
|
|
|
+ type: "img",
|
|
|
+ data,
|
|
|
+ } as (typeof this.msg_list)[number];
|
|
|
+ }),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ this.value = "";
|
|
|
+ this.boolen = true;
|
|
|
+ } catch (e) {
|
|
|
+ this.boolen = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+</script>
|
|
|
+
|
|
|
+<style lang="scss">
|
|
|
+@import "./index.scss";
|
|
|
+</style>
|