Skip to content

Commit

Permalink
Feat: add str length limit
Browse files Browse the repository at this point in the history
  • Loading branch information
LazyCreeper committed Oct 10, 2023
1 parent a853a93 commit 42970c6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ElMessage } from "element-plus";

export const getFileExtension = (url: string) => {
const match = url.match(/\.([a-z0-9]+)(?:[\?#]|$)/i);
if (match) {
Expand Down Expand Up @@ -53,3 +55,12 @@ export const deepEqualObject = (obj1: any, obj2: any) => {
return false;
}
};

export const strLengthLimit = (str: any, num: number) => {
if (typeof str !== "string") return;
if (str.length > num)
throw ElMessage({
type: "error",
message: `输入框内容过长,单个输入框最大不可超过${num}个字符!`
});
};
10 changes: 9 additions & 1 deletion src/views/Cinema.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { WsMessageType } from "@/types/Room";
import { getFileExtension, devLog } from "@/utils/utils";
import { sync } from "@/plugins/sync";
import artplayerPluginDanmuku from "artplayer-plugin-danmuku";
import { strLengthLimit } from "@/utils/utils";
const watchers: WatchStopHandle[] = [];
onBeforeUnmount(() => {
Expand Down Expand Up @@ -60,6 +61,7 @@ const { status, data, send, close } = useWebSocket(
const { state: newToken, execute: reqUpdateRoomPasswordApi } = updateRoomPasswordApi();
const changePassword = async () => {
try {
strLengthLimit(password, 32);
await reqUpdateRoomPasswordApi({
data: {
password: password
Expand Down Expand Up @@ -317,6 +319,9 @@ const pushMovie = async (dir: string) => {
}
try {
for (const key in newMovieInfo.value) {
strLengthLimit(key, 32);
}
await reqPushMovieApi({
params: {
pos: dir
Expand Down Expand Up @@ -385,6 +390,9 @@ const openEditDialog = (item: MovieInfo) => {
const { isLoading: editMovieInfoLoading, execute: reqEditMovieInfoApi } = editMovieInfoApi();
const editMovieInfo = async () => {
try {
for (const key in cMovieInfo.value) {
strLengthLimit(key, 32);
}
await reqEditMovieInfoApi({
data: cMovieInfo.value,
headers: { Authorization: localStorage.token }
Expand Down Expand Up @@ -466,7 +474,6 @@ const swapMovie = async () => {
};
// 设置当前正在播放的影片
const { execute: reqChangeCurrentMovieApi } = changeCurrentMovieApi();
const changeCurrentMovie = async (id: number) => {
try {
Expand Down Expand Up @@ -639,6 +646,7 @@ const sendText = () => {
message: "发送的消息不能为空",
type: "warning"
});
strLengthLimit(sendText_.value, 64);
const msg = JSON.stringify({
Type: 2,
Message: sendText_.value,
Expand Down
6 changes: 5 additions & 1 deletion src/views/CreateRoom.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<script setup lang="ts">
import { ref } from "vue";
import { ElNotification } from "element-plus";
import { ElNotification, ElMessage } from "element-plus";
import { roomStore } from "@/stores/room";
import router from "@/router/index";
import { createRoomApi } from "@/services/apis/room";
import { strLengthLimit } from "@/utils/utils";
const room = roomStore();
const { state: createRoomToken, execute: reqCreateRoomApi } = createRoomApi();
Expand All @@ -27,6 +28,9 @@ const operateRoom = async () => {
return;
}
try {
for (const key in formData.value) {
strLengthLimit(key, 3);
}
await reqCreateRoomApi({
data: formData.value
});
Expand Down
5 changes: 5 additions & 0 deletions src/views/JoinRoom.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { roomStore } from "@/stores/room";
import router from "@/router/index";
import { useRoute } from "vue-router";
import { joinRoomApi } from "@/services/apis/room";
import { strLengthLimit } from "@/utils/utils";
const route = useRoute();
const room = roomStore();
Expand Down Expand Up @@ -44,6 +45,9 @@ const JoinRoom = async () => {
});
return;
}
for (const key in formData.value) {
strLengthLimit(key, 32);
}
try {
await reqJoinRoomApi({
data: formData.value,
Expand Down Expand Up @@ -105,6 +109,7 @@ const JoinRoom = async () => {
<br />
<input class="l-input" type="password" v-model="formData.password" placeholder="房间密码" />
<br />
<div><b>注意:</b>所有输入框最大只可输入32个字符</div>
<div>
<input class="w-auto" type="checkbox" v-model="savePwd" />
<label title="明文保存到本机哦~">&nbsp;记住密码</label>
Expand Down

0 comments on commit 42970c6

Please sign in to comment.