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 144
| var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var index_default = { async fetch(request, env) { const corsHeaders = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type, Authorization, x-ijt', };
if (request.method === 'OPTIONS') { return new Response(null, { headers: corsHeaders }); }
const url = new URL(request.url); const pathname = url.pathname;
try { if (pathname === '/blog/get-shuoshuo-list' && request.method === 'GET') { if (!env.DB) { return new Response(JSON.stringify({ error: 'D1数据库绑定缺失,请检查wrangler.toml配置' }), { status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }); } const { results } = await env.DB.prepare( `SELECT author, avatar, date, content, key, tags FROM shuoshuo ORDER BY date DESC` ).all(); const journals = results.map(row => { let tags = undefined; if (row.tags) { try { tags = JSON.parse(row.tags); if (!Array.isArray(tags)) tags = [row.tags]; } catch { tags = row.tags.split(',').map(t => t.trim()); } } const obj = { author: row.author, avatar: row.avatar, date: formatDate(row.date), content: row.content, key: row.key, tags: tags }; Object.keys(obj).forEach(k => obj[k] === undefined && delete obj[k]); return obj; }); return new Response(JSON.stringify(journals), { headers: { ...corsHeaders, 'Content-Type': 'application/json' } }); }
if (pathname === '/blog/create-shuoshuo' && request.method === 'POST') { const SECRET_KEY = '[替换成你的密钥,也可以留空]'; if (!SECRET_KEY || request.headers.get('Authorization') !== `Bearer ${SECRET_KEY}`) { return new Response(JSON.stringify({ error: '密钥错误' }), { status: 401, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }); } if (!env.DB) { return new Response(JSON.stringify({ error: 'D1数据库绑定缺失,请检查wrangler.toml配置' }), { status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }); } let data; try { data = await request.json(); } catch { return new Response(JSON.stringify({ error: '请求体需为JSON格式' }), { status: 400, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }); }
if (!data.date) { data.date = Math.floor(Date.now() / 1000); } const dateInt = typeof data.date === 'number' ? data.date : Date.parse(data.date); if (!Number.isInteger(dateInt)) { return new Response(JSON.stringify({ error: 'date必须为时间戳整数' }), { status: 400, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }); } let tagsValue = null; if (Array.isArray(data.tags)) { tagsValue = JSON.stringify(data.tags); } else if (typeof data.tags === 'string') { tagsValue = JSON.stringify([data.tags]); }
const { success } = await env.DB.prepare( `INSERT INTO shuoshuo (author, avatar, date, content, key, tags) VALUES (?, ?, ?, ?, ?, ?)` ).bind( data.author || null, data.avatar || null, dateInt, data.content, data.key || null, tagsValue ).run();
if (!success) { throw new Error('说说创建失败'); }
return new Response(JSON.stringify({ message: '说说创建成功' }), { headers: { ...corsHeaders, 'Content-Type': 'application/json' } }); } } catch (err) { return new Response(JSON.stringify({ error: err.message }), { status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }); } }, };
function formatDate(ts) { if (ts.toString().length === 10) ts = ts * 1000; const d = new Date(ts + 8 * 60 * 60 * 1000); const pad = n => n.toString().padStart(2, '0'); return `${d.getUTCFullYear()}-${pad(d.getUTCMonth()+1)}-${pad(d.getUTCDate())} ${pad(d.getUTCHours())}:${pad(d.getUTCMinutes())}:${pad(d.getUTCSeconds())}`; }
export { index_default as default };
|