107 lines
2.3 KiB
Vue
107 lines
2.3 KiB
Vue
<template>
|
|
<div class="demo">
|
|
<div class="bar">
|
|
<input v-model="name" class="field" placeholder="键" />
|
|
<input v-model="value" class="field" placeholder="值" />
|
|
<input v-model.number="ttl" class="field small" type="number" placeholder="毫秒" />
|
|
<button type="button" class="btn" @click="set" v-text="'写入'" />
|
|
<button type="button" class="btn" @click="get" v-text="'读取'" />
|
|
<button type="button" class="btn" @click="remove" v-text="'删除'" />
|
|
</div>
|
|
|
|
<p class="note" v-text="'有效期留空表示会话级 Cookie,关闭浏览器即失效。'" />
|
|
|
|
<ol class="log">
|
|
<li v-for="(item, index) in log" :key="index" v-text="item" />
|
|
</ol>
|
|
</div>
|
|
</template>
|
|
|
|
<script lang="ts" setup>
|
|
import { ref } from "vue";
|
|
import { Cooker } from "timi-web";
|
|
|
|
const name = ref("timi_demo");
|
|
const value = ref("hello cookie");
|
|
const ttl = ref(0);
|
|
const log = ref<string[]>([]);
|
|
|
|
function push(message: string) {
|
|
log.value = [`${new Date().toLocaleTimeString()} ${message}`, ...log.value].slice(0, 6);
|
|
}
|
|
|
|
function set() {
|
|
Cooker.set(name.value, value.value, ttl.value);
|
|
const expires = ttl.value ? `,有效期 ${ttl.value}ms` : ",会话级";
|
|
push(`set("${name.value}", "${value.value}")${expires}`);
|
|
}
|
|
|
|
function get() {
|
|
const result = Cooker.get(name.value);
|
|
push(`get("${name.value}") → ${result === undefined ? "undefined" : result}`);
|
|
}
|
|
|
|
function remove() {
|
|
Cooker.remove(name.value);
|
|
push(`remove("${name.value}")`);
|
|
}
|
|
</script>
|
|
|
|
<style lang="less" scoped>
|
|
.demo {
|
|
.bar {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
gap: .5rem;
|
|
margin-bottom: .5rem;
|
|
}
|
|
|
|
.field {
|
|
width: 9rem;
|
|
padding: .25rem .5rem;
|
|
font-size: .8125rem;
|
|
border: 1px solid var(--vp-c-divider);
|
|
border-radius: .375rem;
|
|
color: var(--vp-c-text-1);
|
|
background: var(--vp-c-bg);
|
|
|
|
&.small {
|
|
width: 6rem;
|
|
}
|
|
}
|
|
|
|
.btn {
|
|
padding: .25rem .75rem;
|
|
font-size: .75rem;
|
|
cursor: pointer;
|
|
border: 1px solid var(--vp-c-divider);
|
|
border-radius: .375rem;
|
|
color: var(--vp-c-text-1);
|
|
background: var(--vp-c-bg-soft);
|
|
|
|
&:hover {
|
|
color: var(--vp-c-brand-1);
|
|
border-color: var(--vp-c-brand-1);
|
|
}
|
|
}
|
|
|
|
.note {
|
|
margin: 0 0 .75rem;
|
|
font-size: .75rem;
|
|
color: var(--vp-c-text-2);
|
|
}
|
|
|
|
.log {
|
|
margin: 0;
|
|
padding-left: 1.25rem;
|
|
font-size: .75rem;
|
|
font-family: monospace;
|
|
color: var(--vp-c-text-2);
|
|
|
|
li {
|
|
margin-bottom: .25rem;
|
|
}
|
|
}
|
|
}
|
|
</style>
|