first commit
This commit is contained in:
commit
a2f9439b49
3
.vscode/extensions.json
vendored
Normal file
3
.vscode/extensions.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"recommendations": ["Vue.volar", "Vue.vscode-typescript-vue-plugin"]
|
||||
}
|
||||
29
README.md
Normal file
29
README.md
Normal file
@ -0,0 +1,29 @@
|
||||
# secert_chat
|
||||
|
||||
This template should help get you started developing with Vue 3 in Vite.
|
||||
|
||||
## Recommended IDE Setup
|
||||
|
||||
[VSCode](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar) (and disable Vetur) + [TypeScript Vue Plugin (Volar)](https://marketplace.visualstudio.com/items?itemName=Vue.vscode-typescript-vue-plugin).
|
||||
|
||||
## Customize configuration
|
||||
|
||||
See [Vite Configuration Reference](https://vitejs.dev/config/).
|
||||
|
||||
## Project Setup
|
||||
|
||||
```sh
|
||||
npm install
|
||||
```
|
||||
|
||||
### Compile and Hot-Reload for Development
|
||||
|
||||
```sh
|
||||
npm run dev
|
||||
```
|
||||
|
||||
### Compile and Minify for Production
|
||||
|
||||
```sh
|
||||
npm run build
|
||||
```
|
||||
13
index.html
Normal file
13
index.html
Normal file
@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<link rel="icon" href="/favicon.ico">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Vite App</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script type="module" src="/src/main.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
8
jsconfig.json
Normal file
8
jsconfig.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"paths": {
|
||||
"@/*": ["./src/*"]
|
||||
}
|
||||
},
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
1168
package-lock.json
generated
Normal file
1168
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
21
package.json
Normal file
21
package.json
Normal file
@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "secert_chat",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite --host",
|
||||
"build": "vite build",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"@icon-park/vue-next": "^1.4.2",
|
||||
"crypto-js": "^4.2.0",
|
||||
"element-plus": "^2.5.6",
|
||||
"vue": "^3.4.15"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vitejs/plugin-vue": "^5.0.3",
|
||||
"vite": "^5.0.11"
|
||||
}
|
||||
}
|
||||
BIN
public/favicon.ico
Normal file
BIN
public/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.2 KiB |
12
src/App.vue
Normal file
12
src/App.vue
Normal file
@ -0,0 +1,12 @@
|
||||
<script setup>
|
||||
|
||||
import Chat from "@/components/chat.vue";
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Chat/>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
|
||||
</style>
|
||||
45
src/Util.js
Normal file
45
src/Util.js
Normal file
@ -0,0 +1,45 @@
|
||||
import CryptoJs from "crypto-js";
|
||||
|
||||
|
||||
export let util = {
|
||||
/**
|
||||
* 传入一个字符串,长度为4到16位,用于生成密钥
|
||||
* @param str 原始字符串
|
||||
* @returns {null}
|
||||
*/
|
||||
generateKey:function (str){
|
||||
if(!str instanceof String || str.length < 4)
|
||||
return null
|
||||
|
||||
let raw
|
||||
|
||||
if(str.length > 16)
|
||||
raw = str.substring(0, 16)
|
||||
else
|
||||
raw = str.repeat(Math.ceil(16 / str.length)).substring(0, 16)
|
||||
|
||||
return CryptoJs.enc.Utf8.parse(raw)
|
||||
},
|
||||
generatePairCode:function (str){
|
||||
return CryptoJs.MD5(str+'salt').toString()
|
||||
},
|
||||
encrypt:function (plainText, key){
|
||||
return CryptoJs.AES.encrypt(plainText, key, {mode: CryptoJs.mode.ECB, padding: CryptoJs.pad.Pkcs7}).toString()
|
||||
},
|
||||
decrypt:function (encrypted, key){
|
||||
return CryptoJs.AES.decrypt(encrypted, key, {mode: CryptoJs.mode.ECB, padding: CryptoJs.pad.Pkcs7}).toString(CryptoJs.enc.Utf8)
|
||||
}
|
||||
}
|
||||
|
||||
export let MessageType = {
|
||||
'INIT': 1,
|
||||
'WAIT': 11,
|
||||
'JOIN': 12,
|
||||
'CANCEL': 13,
|
||||
'DISCONNECT': 20,
|
||||
'CONTENT': 30,
|
||||
'TIMER_CONFIG': 31,
|
||||
'PURGE_CONFIG': 32,
|
||||
'MESSAGE': 33
|
||||
}
|
||||
|
||||
1
src/assets/logo.svg
Normal file
1
src/assets/logo.svg
Normal file
@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 261.76 226.69"><path d="M161.096.001l-30.225 52.351L100.647.001H-.005l130.877 226.688L261.749.001z" fill="#41b883"/><path d="M161.096.001l-30.225 52.351L100.647.001H52.346l78.526 136.01L209.398.001z" fill="#34495e"/></svg>
|
||||
|
After Width: | Height: | Size: 276 B |
47
src/assets/reset.css
Normal file
47
src/assets/reset.css
Normal file
@ -0,0 +1,47 @@
|
||||
/* http://meyerweb.com/eric/tools/css/reset/
|
||||
v2.0 | 20110126
|
||||
License: none (public domain)
|
||||
*/
|
||||
|
||||
html, body, div, span, applet, object, iframe,
|
||||
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
|
||||
a, abbr, acronym, address, big, cite, code,
|
||||
del, dfn, em, img, ins, kbd, q, s, samp,
|
||||
small, strike, strong, sub, sup, tt, var,
|
||||
b, u, i, center,
|
||||
dl, dt, dd, ol, ul, li,
|
||||
fieldset, form, label, legend,
|
||||
table, caption, tbody, tfoot, thead, tr, th, td,
|
||||
article, aside, canvas, details, embed,
|
||||
figure, figcaption, footer, header, hgroup,
|
||||
menu, nav, output, ruby, section, summary,
|
||||
time, mark, audio, video {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
border: 0;
|
||||
font-size: 100%;
|
||||
font: inherit;
|
||||
vertical-align: baseline;
|
||||
}
|
||||
/* HTML5 display-role reset for older browsers */
|
||||
article, aside, details, figcaption, figure,
|
||||
footer, header, hgroup, menu, nav, section {
|
||||
display: block;
|
||||
}
|
||||
body {
|
||||
line-height: 1;
|
||||
}
|
||||
ol, ul {
|
||||
list-style: none;
|
||||
}
|
||||
blockquote, q {
|
||||
quotes: none;
|
||||
}
|
||||
blockquote:before, blockquote:after,
|
||||
q:before, q:after {
|
||||
content: none;
|
||||
}
|
||||
table {
|
||||
border-collapse: collapse;
|
||||
border-spacing: 0;
|
||||
}
|
||||
351
src/components/chat.vue
Normal file
351
src/components/chat.vue
Normal file
@ -0,0 +1,351 @@
|
||||
<script setup>
|
||||
import fire from "@icon-park/vue-next/lib/icons/fire.js";
|
||||
import previewCloseOne from "@icon-park/vue-next/lib/icons/PreviewCloseOne.js";
|
||||
import previewOpen from "@icon-park/vue-next/lib/icons/PreviewOpen.js";
|
||||
import {ref} from "vue";
|
||||
import {MessageType, util} from "@/Util.js";
|
||||
import {ElMessage} from "element-plus";
|
||||
|
||||
let messages = ref({})
|
||||
let message = ref('')
|
||||
let status = ref('未连接') //未连接 等待中 已连接 已断开
|
||||
let raw = ref('')
|
||||
let key = ''
|
||||
let initTimer = ref(0)
|
||||
let initIntervalId = 0
|
||||
|
||||
let purgeOnDisconnect = ref(false)
|
||||
let autoDelete = ref(false)
|
||||
let deleteTime = ref(0)
|
||||
let messageCounter = ref(0)
|
||||
let websocket
|
||||
|
||||
function init(){
|
||||
if(raw.value.trim().length < 4){
|
||||
ElMessage.error("长度不够,请重新输入")
|
||||
return
|
||||
}
|
||||
|
||||
key = util.generateKey(raw.value)
|
||||
let pairCode = util.generatePairCode(key)
|
||||
|
||||
websocket = new WebSocket('wss://sc.lionwebsite.xyz/ws')
|
||||
websocket.onopen = () => {
|
||||
let initMessage = {"type": MessageType.INIT, "pairCode": pairCode}
|
||||
websocket.send(JSON.stringify(initMessage))
|
||||
}
|
||||
|
||||
websocket.onmessage = (event) => {
|
||||
if(event.data.startsWith("{"))
|
||||
switch (JSON.parse(event.data).type){
|
||||
case MessageType.WAIT:
|
||||
initTimer.value = 60
|
||||
status.value = '等待中'
|
||||
initIntervalId = setInterval(() => {
|
||||
if(initTimer.value > 0)
|
||||
initTimer.value--
|
||||
else {
|
||||
status.value = '未连接'
|
||||
websocket.close()
|
||||
initTimer.value = 0
|
||||
ElMessage.error("已超时,请重新发起连接")
|
||||
clearInterval(initIntervalId)
|
||||
}
|
||||
}, 1000)
|
||||
break
|
||||
case MessageType.JOIN:
|
||||
status.value = '已连接'
|
||||
initTimer.value = 0
|
||||
clearInterval(initIntervalId)
|
||||
ElMessage.success("已加入聊天")
|
||||
if (purgeOnDisconnect.value)
|
||||
setPurgeOnDisconnect()
|
||||
if (autoDelete.value)
|
||||
setAutoDelete(deleteTime.value)
|
||||
break
|
||||
case MessageType.DISCONNECT:
|
||||
status.value = '已断开'
|
||||
console.log("已断开")
|
||||
ElMessage.error("对方已断开")
|
||||
if(purgeOnDisconnect.value)
|
||||
messages.value = {}
|
||||
break
|
||||
}
|
||||
else {
|
||||
let data = util.decrypt(event.data, key)
|
||||
data = JSON.parse(data)
|
||||
console.log(data)
|
||||
switch (data.type){
|
||||
case MessageType.PURGE_CONFIG:
|
||||
purgeOnDisconnect.value = data.purgeOnDisconnect
|
||||
if(purgeOnDisconnect.value)
|
||||
messages.value[++messageCounter.value] = {'type': 'config', 'message': '对方已设置断开连接后删除消息'}
|
||||
else
|
||||
messages.value[++messageCounter.value] = {'type': 'config', 'message': '对方已设置断开连接后保留消息'}
|
||||
break
|
||||
case MessageType.TIMER_CONFIG:
|
||||
autoDelete.value = data.autoDelete
|
||||
deleteTime.value = data.deleteTime
|
||||
if(autoDelete.value)
|
||||
messages.value[++messageCounter.value] = {'type': 'config', 'message': `对方已设置${deleteTime.value}秒后删除消息`}
|
||||
else
|
||||
messages.value[++messageCounter.value] = {'type': 'config', 'message': `对方已关闭定时删除消息`}
|
||||
break
|
||||
case MessageType.MESSAGE:
|
||||
messages.value[++messageCounter.value] = {'type': 'other', 'message': data.message}
|
||||
if(autoDelete.value) {
|
||||
let key = messageCounter.value
|
||||
setTimeout(() => {
|
||||
console.log("删除消息:", key)
|
||||
delete messages.value[key]
|
||||
}, deleteTime.value * 1000)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
websocket.onclose = () => {
|
||||
status.value = '已断开'
|
||||
console.log("已断开")
|
||||
ElMessage.error("与服务器失去连接")
|
||||
if(purgeOnDisconnect.value)
|
||||
messages.value = {}
|
||||
messages.value[++messageCounter.value] = {'type': 'config', 'message': '与服务器连接已断开'}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function send(){
|
||||
let data = {'type': MessageType.MESSAGE, 'message': message.value}
|
||||
data = {'type': MessageType.CONTENT, 'content': util.encrypt(JSON.stringify(data), key)}
|
||||
websocket.send(JSON.stringify(data))
|
||||
|
||||
messages.value[++messageCounter.value] = {'type': 'self', 'message': message.value}
|
||||
if(autoDelete.value) {
|
||||
let key = messageCounter.value
|
||||
setTimeout(() => {
|
||||
console.log("删除消息:", key)
|
||||
delete messages.value[key]
|
||||
}, deleteTime.value * 1000)
|
||||
}
|
||||
message.value = ''
|
||||
}
|
||||
|
||||
let isAutoDeleteChange = false
|
||||
function setAutoDelete(){
|
||||
if(isAutoDeleteChange) {
|
||||
isAutoDeleteChange = false
|
||||
autoDelete.value = deleteTime.value > 0
|
||||
let data = {'type': MessageType.TIMER_CONFIG, 'deleteTime': deleteTime.value, 'autoDelete': autoDelete.value}
|
||||
data = {'type': MessageType.CONTENT, 'content': util.encrypt(JSON.stringify(data), key)}
|
||||
websocket.send(JSON.stringify(data))
|
||||
|
||||
if (autoDelete.value)
|
||||
messages.value[++messageCounter.value] = {'type': 'config', 'message': `你设置了${deleteTime.value}秒后删除消息`}
|
||||
else
|
||||
messages.value[++messageCounter.value] = {'type': 'config', 'message': `你关闭了定时删除消息`}
|
||||
}
|
||||
}
|
||||
|
||||
function setPurgeOnDisconnect(){
|
||||
let data = {'type': MessageType.PURGE_CONFIG, 'purgeOnDisconnect': purgeOnDisconnect.value}
|
||||
data = {'type': MessageType.CONTENT, 'content': util.encrypt(JSON.stringify(data), key)}
|
||||
websocket.send(JSON.stringify(data))
|
||||
|
||||
if(purgeOnDisconnect.value)
|
||||
messages.value[++messageCounter.value] = {'type': 'config', 'message': `你设置了断开连接后删除消息`}
|
||||
else
|
||||
messages.value[++messageCounter.value] = {'type': 'config', 'message': `你设置了断开连接后保留消息`}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<el-container>
|
||||
<el-header style="text-align: center; background: cornflowerblue; line-height: 60px; display: block; font-size: 24px">
|
||||
<div v-if="status !== '未连接'">
|
||||
{{status}}
|
||||
<el-popover>
|
||||
<template #reference>
|
||||
<span style="font-size: 16px">
|
||||
<el-icon>
|
||||
<preview-close-one v-if="purgeOnDisconnect" fill="black"></preview-close-one>
|
||||
<preview-open v-else fill="black"></preview-open>
|
||||
</el-icon>
|
||||
|
||||
</span>
|
||||
</template>
|
||||
断开后清空消息 <el-switch v-model="purgeOnDisconnect" @change="setPurgeOnDisconnect()" :disabled="status === '已断开'"/>
|
||||
</el-popover>
|
||||
|
||||
<el-popover @after-leave="setAutoDelete">
|
||||
<template #reference>
|
||||
<span style="font-size: 16px; color: black">
|
||||
<el-icon>
|
||||
<fire theme="outline" :fill="autoDelete ? 'red': 'black'"/>
|
||||
</el-icon>
|
||||
{{autoDelete ? deleteTime + 's': ''}}
|
||||
</span>
|
||||
</template>
|
||||
自动删除消息: <el-input-number v-model="deleteTime" @change="isAutoDeleteChange = true" max="60" min="0" size="small" :disabled="status === '已断开'" />
|
||||
</el-popover>
|
||||
</div>
|
||||
<div v-else>未连接</div>
|
||||
</el-header>
|
||||
<el-main v-if="status === '未连接'" class="login" @keydown.enter="init">
|
||||
<el-input class="inp" v-model="raw" placeholder="请输入四到十六位字符串以生成密钥"/>
|
||||
<span>定时删除消息</span> <el-switch v-model="autoDelete" style="margin-right: 30px"></el-switch><br>
|
||||
<div v-if="autoDelete">
|
||||
<span>删除时间(秒)</span><el-input-number v-model="deleteTime" max="60" min="1" size="small"></el-input-number><br>
|
||||
</div>
|
||||
<span>断开连接后删除消息</span><el-switch v-model="purgeOnDisconnect"></el-switch>
|
||||
<el-button @click="init" color="#333" class="btn">连接</el-button>
|
||||
</el-main>
|
||||
|
||||
<el-main v-if="status === '等待中'">
|
||||
<div v-loading="status === '等待中'"/>
|
||||
<div style="text-align: center; margin-top: 20px">
|
||||
{{initTimer}}
|
||||
</div>
|
||||
</el-main>
|
||||
<el-main class="chat-container" v-if="status === '已连接' || status === '已断开'" @keydown.enter="send">
|
||||
<el-scrollbar class="chat-messages" max-height="78vh" style="min-height: 78vh">
|
||||
<div v-for="message in messages" :class="message.type === 'config' ? message.type: message.type + ' message'">
|
||||
{{message.message}}
|
||||
</div>
|
||||
</el-scrollbar>
|
||||
<div style="width: 100%;">
|
||||
<el-input v-model="message" placeholder="输入消息..." style=" width: 70%" :disabled="status !== '已连接'"/>
|
||||
<el-button class="send-button" style="width: 20%; position: absolute" @click="send" :disabled="status !== '已连接'">发送</el-button>
|
||||
</div>
|
||||
</el-main>
|
||||
</el-container>
|
||||
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.chat-container {
|
||||
width: 100%;
|
||||
margin: 0 auto;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 5px;
|
||||
}
|
||||
|
||||
.chat-messages {
|
||||
padding: 20px;
|
||||
overflow-y: scroll;
|
||||
}
|
||||
|
||||
.el-scrollbar{
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
.message {
|
||||
font-size: 20px;
|
||||
margin-bottom: 10px;
|
||||
padding: 10px 20px 10px 20px;
|
||||
clear: both;
|
||||
}
|
||||
|
||||
.self {
|
||||
float: right;
|
||||
background-color: #4caf50;
|
||||
color: #fff;
|
||||
border-radius: 20px 20px 0 20px;
|
||||
}
|
||||
|
||||
.other {
|
||||
float: left;
|
||||
background-color: #e6e6e6;
|
||||
color: #333;
|
||||
border-radius: 20px 20px 20px 0;
|
||||
}
|
||||
|
||||
.config {
|
||||
width: 80%;
|
||||
margin: 0 auto 10px;
|
||||
padding: 2px 0;
|
||||
background-color: gray;
|
||||
border-radius: 20px;
|
||||
color: #cccccc;
|
||||
text-align: center;
|
||||
font-size: 14px;
|
||||
clear: both;
|
||||
|
||||
}
|
||||
|
||||
.user-input {
|
||||
width: calc(100% - 20px);
|
||||
padding: 8px;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 5px;
|
||||
margin: 10px;
|
||||
}
|
||||
|
||||
.send-button {
|
||||
width: calc(100% - 20px);
|
||||
background-color: #4caf50;
|
||||
color: #fff;
|
||||
border: none;
|
||||
padding: 8px;
|
||||
border-radius: 5px;
|
||||
cursor: pointer;
|
||||
margin: 0 10px;
|
||||
}
|
||||
.login{
|
||||
margin: 10px;
|
||||
box-sizing: border-box;
|
||||
background-color: #ecf0f1;
|
||||
border-radius: 20px;
|
||||
position: relative;
|
||||
}
|
||||
.inp{
|
||||
margin-top: 20px;
|
||||
margin-bottom: 25px;
|
||||
|
||||
}
|
||||
.inp :before{
|
||||
content:"密钥:";
|
||||
width: 50px;
|
||||
}
|
||||
|
||||
.text{
|
||||
font-size: 12px;
|
||||
color: gray;
|
||||
position: absolute;
|
||||
top: 75px;
|
||||
right: 15px;
|
||||
}
|
||||
span{
|
||||
font-size: 12px;
|
||||
color: #444;
|
||||
}
|
||||
.btn{
|
||||
margin-top: 2px;
|
||||
margin-bottom: 10px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.loader-container {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.loader {
|
||||
border: 4px solid #3498db; /* 圆圈的颜色 */
|
||||
border-top: 4px solid #f1f1f1; /* 圆圈的颜色 */
|
||||
border-radius: 50%;
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
animation: spin 1s linear infinite; /* 旋转动画 */
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
0% { transform: rotate(0deg); }
|
||||
100% { transform: rotate(360deg); }
|
||||
}
|
||||
.el-icon svg{
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
}
|
||||
</style>
|
||||
9
src/main.js
Normal file
9
src/main.js
Normal file
@ -0,0 +1,9 @@
|
||||
|
||||
import { createApp } from 'vue'
|
||||
import App from './App.vue'
|
||||
import "element-plus/dist/index.css"
|
||||
import element from 'element-plus'
|
||||
import './assets/reset.css'
|
||||
import '@icon-park/vue-next/styles/index.css';
|
||||
|
||||
createApp(App).use(element).mount('#app')
|
||||
16
vite.config.js
Normal file
16
vite.config.js
Normal file
@ -0,0 +1,16 @@
|
||||
import { fileURLToPath, URL } from 'node:url'
|
||||
|
||||
import { defineConfig } from 'vite'
|
||||
import vue from '@vitejs/plugin-vue'
|
||||
|
||||
// https://vitejs.dev/config/
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
vue(),
|
||||
],
|
||||
resolve: {
|
||||
alias: {
|
||||
'@': fileURLToPath(new URL('./src', import.meta.url))
|
||||
}
|
||||
}
|
||||
})
|
||||
Loading…
Reference in New Issue
Block a user