wangzhibo
4 天以前 10595d8632f959d0954d1939243196f4eed02372
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<template>
    <cl-page fullscreen background-color="#fff">
        <view class="page">
            <view class="container">
                <scroll-view
                    :scroll-top="scroller.top"
                    scroll-y
                    :scroll-with-animation="scroller.animation"
                    class="scroller"
                    @tap="tools.close()"
                    @scroll="onScroll"
                >
                    <msg-list :ref="setRefs('msgList')" />
                </scroll-view>
            </view>
 
            <view class="footer">
                <view class="wrap">
                    <!-- 输入 -->
                    <view class="inner">
                        <cl-input
                            v-model="content"
                            round
                            :height="76"
                            placeholder="输入你想要咨询的问题"
                            :confirm-hold="true"
                            :adjust-position="true"
                            :cursor-spacing="10"
                            auto-blur
                            @focus="onFocus"
                            @confirm="onConfirm"
                        />
                    </view>
 
                    <image src="/uni_modules/cool-cs/static/emoji.png" @tap="tools.open('emoji')" />
                    <image src="/uni_modules/cool-cs/static/plus.png" @tap="tools.open('fn')" />
                </view>
 
                <!-- 工具 -->
                <tools />
            </view>
        </view>
    </cl-page>
</template>
 
<script setup lang="ts">
import { useSocket, useTools, useScroller, useSession, useMessage } from "../hooks";
import { onReady } from "@dcloudio/uni-app";
import { useCool } from "/@/cool";
import { ref } from "vue";
import Tools from "../components/tools/index.vue";
import MsgList from "../components/msg-list.vue";
 
const { service, refs, setRefs } = useCool();
const socket = useSocket();
const session = useSession();
const message = useMessage();
const scroller = useScroller();
const tools = useTools();
 
// 咨询内容
const content = ref("");
 
// 监听滚动
function onScroll(e: { detail: { scrollTop: number } }) {
    refs.msgList?.onScroll(e.detail.scrollTop);
}
 
// 聚焦
function onFocus() {
    scroller.scrollToBottom();
}
 
// 确认
async function onConfirm() {
    const data = content.value;
 
    if (data) {
        content.value = "";
 
        socket.send({
            type: "text",
            data,
        });
    }
}
 
onReady(() => {
    console.info("service.cs", service.cs);
    service.cs.session.create().then(async (res) => {
        // 设置会话
        session.set(res);
 
        // 连接 socket
        socket.connect();
 
        // 获取消息列表
        await message.refresh({
            sessionId: res.id,
        });
 
        // 滚动到底
        scroller.scrollToBottom(false);
    });
});
</script>
 
<style lang="scss" scoped>
.page {
    display: flex;
    flex-direction: column;
    height: 100%;
 
    .container {
        flex: 1;
        overflow: hidden;
        background-color: $cl-color-bg;
 
        .scroller {
            height: 100%;
        }
    }
 
    .footer {
        .wrap {
            display: flex;
            align-items: center;
            padding: 24rpx 36rpx;
            background-color: #fff;
 
            .inner {
                flex: 1;
            }
 
            image {
                height: 46rpx;
                width: 46rpx;
                margin-left: 24rpx;
            }
        }
    }
}
</style>