Shiro/Yohaku 个人状态报错
TLDR
mx-space 支持博主点击头像右下角发布个人状态
官方功能描述如下:
但是按照官方提供方法配置完后,发布状态时肯定会失败,控制台报错大致是:
95900-30e790acc8113b00.js:1
GET https://bambooo.top/api/v3/fn/shiro/status?lang=zh 404 (Not Found)
也就是找不到 status 这个函数
修复方法
云函数复制我的粘贴过去保存
interface Status {
emoji: string
icon?: string
desc?: string
ttl: number
untilAt: number
}
function assetAuth(ctx: Context) {
const body = ctx.req.body
const authKey = ctx.secret.key
if (ctx.isAuthenticated) return
if (body.key !== authKey) {
throw new Error('Unauthorized')
}
}
export default async function handler(ctx: Context) {
const method = ctx.req.method.toLowerCase()
switch (method) {
case 'get': {
return GET(ctx)
}
case 'post': {
assetAuth(ctx)
return POST(ctx)
}
case 'delete': {
assetAuth(ctx)
return DELETE(ctx)
}
case 'options': {
return null
}
default: {
throw new Error('Method Not Allowed')
}
}
}
const cacheKey = 'shiro:status'
async function DELETE(ctx: Context) {
await ctx.storage.cache.del(cacheKey)
ctx.broadcast('shiro#status', null)
}
async function POST(ctx: Context) {
const body = ctx.req.body
const { emoji, icon, desc } = body as Status
const ttl = body.ttl || 86400
const status = {
emoji,
icon,
desc,
ttl,
untilAt: Date.now() + ttl * 1000,
} as Status
await ctx.storage.cache.set(cacheKey, JSON.stringify(status), ttl)
ctx.broadcast('shiro#status', status)
return null
}
async function GET(ctx: Context) {
const status = await ctx.storage.cache.get(cacheKey)
return status
}
