Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
update user api
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Jul 21, 2023
1 parent 5d0eb30 commit 5121ee4
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 24 deletions.
26 changes: 24 additions & 2 deletions extension/turbowarp/src/i18n/source.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@
"exist": "随记便签是否存在 [code]",
"login": "登录 lightnotes 账号",
"close": "关闭验证窗口",
"website": "寻找河流的源头..."
"logout": "退出登录",
"check": "是否登录",
"uget": "获取便签 id [id]",
"utime": "获取便签更新时间 id [id]",
"unew": "新建便签 标题 [title] 内容 [body]",
"uupdate": "更新便签 id [id] 标题 [title] 内容 [body]",
"udelete": "删除便签 id [id]",
"ulist": "获取便签列表 第 [page] 页",
"utotal": "获取便签页数",
"uprev": "第 [page] 页是否有上一页",
"unext": "第 [page] 页是否有下一页",
"website": "Lightnotes 官网"
},
"en": {
"title": "Memorize Lightnotes",
Expand All @@ -15,6 +26,17 @@
"exist": "Presence of a casual sticky note [code]",
"login": "Log in to your lightnotes account",
"close": "Close verification window",
"website": "Finding the source of the river..."
"logout": "Log out",
"check": "Logged in or not",
"uget": "Get note id [id]",
"utime": "Get note update time id [id]",
"unew": "New Note Heading [title] Content [body]",
"uupdate": "Update note id [id] title [title] content [body]",
"udelete": "Delete note id [id]",
"ulist": "Get notes list page [page]",
"utotal": "Get the number of note pages",
"uprev": "Is there a previous page on page [page]",
"unext": "Is there a next page on page [page]",
"website": "Lightnotes' website"
}
}
230 changes: 208 additions & 22 deletions extension/turbowarp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,60 @@ import Extension from './include/plugin'
import axios from 'axios'

let show = false;
let offsetX: number, offsetY: number, isDragging = false;

type Note = {
id: number;
title: string;
body: string;
updated_at: string;
}

type PaginationResponse = {
status: boolean;
total: number;
page: number;
prev_page: boolean;
next_page: boolean;
notes: Note[];
}

function setDisplay(state: boolean): boolean {
show = state;
const el = document.getElementById('notes');
if (el) el.className = show ? 'fade': '';
if (el) {
el.className = show ? 'fade': '';
el.style.zIndex = show ? '9999' : '-1';
}
return show;
}

function setToken(raw: string): void {
const token = raw.split('').map((i: string) => {
return String.fromCharCode(i.charCodeAt(0) + 1)
}).join('');
sessionStorage.setItem('notes', token);
axios.defaults.headers.common['Authorization'] = raw;
}

function getToken(): string {
const raw = sessionStorage.getItem('notes');
if (!raw) return '';
return raw.split('').map((i: string) => {
return String.fromCharCode(i.charCodeAt(0) - 1)
}).join('');
}

async function getNotes(page: number): Promise<{data: PaginationResponse, status: boolean, reason: string }> {
try {
const res = await axios.get(`/user/list?page=${page}`);
if (!res.data.status) return { data: {} as PaginationResponse, status: false, reason: "接收失败!请检查您是否登录" };
return { data: res.data as PaginationResponse, status: true, reason: "" };
} catch (e) {
console.debug(e);
return { data: {} as PaginationResponse, status: false, reason: "接收失败!请检查您的网络环境" };
}
}

new Extension({
id: 'notes',
name: '随记便签 Lightnotes',
Expand All @@ -25,11 +70,7 @@ new Extension({
return "标题或内容不能为空"
}
try {
const res = await axios.post("https://notes.lightxi.com/api/anonymous/send", { title, body }, {
headers: {
'Content-Type': 'application/json'
},
})
const res = await axios.post("/anonymous/send", { title, body });
if (!res.data.status) return "发送失败!请稍后重试";
return res.data.code;
} catch (e) {
Expand All @@ -46,11 +87,7 @@ new Extension({
return "接收失败!取签码不能为空"
}
try {
const res = await axios.get(`https://notes.lightxi.com/api/anonymous/get?code=${code}`, {
headers: {
'Content-Type': 'application/json'
},
});
const res = await axios.get(`/anonymous/get?code=${code}`);
if (!res.data.status) return "接收失败!请检查您的接签码是否正确,匿名便签是否过期";
return JSON.stringify({
title: res.data.title,
Expand All @@ -68,11 +105,7 @@ new Extension({
bind: async function ({ code }): Promise<boolean> {
if (!code) return false
try {
const res = await axios.get(`https://notes.lightxi.com/api/anonymous/get?code=${code}`, {
headers: {
'Content-Type': 'application/json'
}
});
const res = await axios.get(`/anonymous/get?code=${code}`);
return res.data.status;
} catch (e) {
console.debug(e);
Expand All @@ -93,10 +126,157 @@ new Extension({
bind: function (): void {
setDisplay(false);
}
}, {
opcode: "logout",
blockType: "command",
text: "退出登录",
bind: function (): void {
sessionStorage.removeItem('notes');
delete axios.defaults.headers.common['Authorization'];
}
}, {
opcode: "check",
blockType: "Boolean",
text: "是否登录",
bind: async function (): Promise<boolean> {
if (!getToken()) return false;
try {
const res = await axios.post("/user/state");
return res.data.status;
} catch (e) {
console.debug(e);
return false;
}
}
}, {
opcode: "uget",
blockType: "reporter",
text: "获取便签 id [id:number]",
bind: async function ({ id }): Promise<string | Record<string, string>> {
try {
const res = await axios.get(`/user/get?id=${id}`);
if (!res.data.status) return "接收失败!请检查您的便签 id 是否正确";
const note = res.data.note as Note;
return JSON.stringify({
title: note.title,
body: note.body,
});
} catch (e) {
console.debug(e);
return "接收失败!请检查您的网络环境 以及是否登录!";
}
}
}, {
opcode: "utime",
blockType: "reporter",
text: "获取便签更新时间 id [id:number]",
bind: async function ({ id }): Promise<string | number> {
try {
const res = await axios.get(`/user/get?id=${id}`);
if (!res.data.status) return "接收失败!请检查您的便签 id 是否正确";
return (res.data.note as Note).updated_at;
} catch (e) {
console.debug(e);
return "接收失败!请检查您的网络环境 以及是否登录!";
}
}
}, {
opcode: "unew",
blockType: "reporter",
text: "新建便签 标题 [title:string] 内容 [body:string]",
default: { title: "新便签", body: "写点什么" },
bind: async function ({ title, body }): Promise<string | number> {
if (!title || !body) {
return "标题或内容不能为空"
}
try {
const res = await axios.post("/user/save", { title, body });
if (!res.data.status) return "发送失败!请稍后重试";
return res.data.id;
} catch (e) {
console.debug(e);
return "发送失败!请检查您的网络环境 以及是否登录!";
}
}
}, {
opcode: "uupdate",
blockType: "reporter",
text: "更新便签 id [id:number] 标题 [title:string] 内容 [body:string]",
default: { title: "新便签", body: "写点什么" },
bind: async function ({ id, title, body }): Promise<string | number> {
if (!id || !title || !body) {
return "标题或内容不能为空"
}
try {
const res = await axios.post(`/user/update?id=${id}`, { id, title, body });
if (!res.data.status) return "发送失败!请稍后重试";
return res.data.status;
} catch (e) {
console.debug(e);
return "发送失败!请检查您的网络环境 以及是否登录!";
}
}
}, {
opcode: "udelete",
blockType: "reporter",
text: "删除便签 id [id:number]",
bind: async function ({ id }): Promise<string | number> {
if (!id) {
return "id 不能为空"
}
try {
const res = await axios.post(`/user/delete?id=${id}`);
if (!res.data.status) return "发送失败!请稍后重试";
return res.data.status;
} catch (e) {
console.debug(e);
return "发送失败!请检查您的网络环境 以及是否登录!";
}
}
}, {
opcode: "ulist",
blockType: "reporter",
text: "获取便签列表 第 [page:number] 页",
default: { page: "1" },
bind: async function ({ page }): Promise<string | Record<string, string>> {
const resp = await getNotes(page);
if (!resp.status) return resp.reason;
return JSON.stringify(resp.data.notes);
}
}, {
opcode: "utotal",
blockType: "reporter",
text: "获取便签页数",
disableMonitor: true,
bind: async function (): Promise<string | number> {
const resp = await getNotes(1);
if (!resp.status) return resp.reason;
return resp.data.total;
}
}, {
opcode: "uprev",
blockType: "reporter",
text: "第 [page:number] 页是否有上一页",
default: { page: "1" },
bind: async function ({ page }): Promise<string | boolean> {
const resp = await getNotes(page);
if (!resp.status) return resp.reason;
return resp.data.prev_page;
}
}, {
opcode: "unext",
blockType: "reporter",
text: "第 [page:number] 页是否有下一页",
default: { page: "1" },
bind: async function ({ page }): Promise<string | boolean> {
const resp = await getNotes(page);
if (!resp.status) return resp.reason;
return resp.data.next_page;
}
}, {
opcode: "website",
blockType: "command",
text: "寻找河流的源头...",
text: "Lightnotes 官网",
bind: () => window.open("https://notes.lightxi.com"),
}],
i18n: {
Expand Down Expand Up @@ -125,7 +305,7 @@ new Extension({
const container = document.createElement('div');
container.id = 'notes';
container.style.position = 'absolute';
container.style.zIndex = '1024';
container.style.zIndex = '-1';
container.style.overflow = 'hidden';
container.style.top = '50%';
container.style.left = '50%';
Expand All @@ -152,7 +332,7 @@ new Extension({
container.appendChild(close);

const frame = document.createElement('iframe');
frame.src = 'http://localhost:5173/';
frame.src = 'https://notes.lightxi.com/';
frame.style.width = '100%';
frame.style.height = '100%';
frame.style.border = 'none';
Expand All @@ -163,12 +343,18 @@ new Extension({
iframe.postMessage({ type: 'login' }, '*');
}, 1000);
window.addEventListener('message', (e) => {
console.log(e.data);
if (e.data.type === 'login') {

if (e.data.status) {
setToken(e.data.token);
setDisplay(false);
}
}
}, false);
}
container.appendChild(frame);

axios.defaults.baseURL = 'https://notes.lightxi.com/api';
axios.defaults.headers.common['Authorization'] = getToken();
axios.defaults.headers.common['Content-Type'] = 'application/json';
}
}).register()

0 comments on commit 5121ee4

Please sign in to comment.