|
| 1 | +# 额外功能 |
| 2 | + |
| 3 | +## 我的动态 |
| 4 | + |
| 5 | +Shiro 主题中,有一个可以在顶部显示博主当前正在做的事情的功能,这个功能是通过云函数和 ProcessReporterMac 软件实现的。 |
| 6 | + |
| 7 | +import { ToGitHub } from '@components/ToGitHub'; |
| 8 | + |
| 9 | +<ToGitHub repo="mx-space/ProcessReporterMac" /> |
| 10 | + |
| 11 | +import { Callout, Steps } from 'nextra/components' |
| 12 | + |
| 13 | +<Callout type="info"> |
| 14 | +目前软件仅有 Mac 版本,如果你有兴趣,可以参照此项目来开发 Windows 版本 |
| 15 | +</Callout> |
| 16 | + |
| 17 | +<Steps> |
| 18 | +### 配置云函数 |
| 19 | + |
| 20 | +进入后台,点击左侧菜单栏的「其他 -> 配置与云函数」,然后点击新建按钮,在选项卡中填入以下信息: |
| 21 | + |
| 22 | +- 名称:`update` |
| 23 | +- 引用:`ps` |
| 24 | +- 数据类型:`Function` |
| 25 | +- 请求方式:`POST` |
| 26 | + |
| 27 | +这个地方还需要设置一个密钥,在 Secret 中填入 `key`,在 Value 中填入你自己的密钥。 |
| 28 | + |
| 29 | +<Callout type="warning"> |
| 30 | +这个密钥将用于验证你的软件是否有权限更新博主的动态,所以请务必设置一个复杂的密钥。 |
| 31 | + |
| 32 | +**密钥在后面的步骤中还需要用到,所以请务必记住。** |
| 33 | +</Callout> |
| 34 | + |
| 35 | +上方没有提到的选项都不需要填写,然后在右侧的代码编辑器中填入以下代码: |
| 36 | + |
| 37 | +```js |
| 38 | +export default async function handler(ctx: Context) { |
| 39 | + const { timestamp, process, key, media } = ctx.req.body || {} |
| 40 | + |
| 41 | + const [processName, mediaInfo] = await Promise.all([ |
| 42 | + ctx.storage.cache.get('ps'), |
| 43 | + ctx.storage.cache.get('media').then((JSONString) => { |
| 44 | + if (JSONString) { |
| 45 | + try { |
| 46 | + return JSON.parse(JSONString) |
| 47 | + } catch { |
| 48 | + return undefined |
| 49 | + } |
| 50 | + } |
| 51 | + }), |
| 52 | + ]) |
| 53 | + |
| 54 | + if (!key) { |
| 55 | + return { |
| 56 | + processName, |
| 57 | + mediaInfo, |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + const validKey = (await ctx.secret.key) || 'testing' |
| 62 | + if (key != validKey) |
| 63 | + ctx.throws(401, "You haven't permission to update process info") |
| 64 | + await ctx.storage.cache.set('ps', process, 60) |
| 65 | + const ts = +new Date() |
| 66 | + |
| 67 | + if (process !== processName) |
| 68 | + ctx.broadcast('ps-update', { |
| 69 | + process, |
| 70 | + ts, |
| 71 | + }) |
| 72 | + if (media) { |
| 73 | + await ctx.storage.cache.set('media', JSON.stringify(media), 10) |
| 74 | + |
| 75 | + if (mediaInfo.title !== media.title) ctx.broadcast('media-update', media) |
| 76 | + } |
| 77 | + return { |
| 78 | + ok: 1, |
| 79 | + process, |
| 80 | + timestamp: +new Date(), |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +点击保存按钮,云函数就配置完成了。 |
| 86 | + |
| 87 | +### 配置主题配置 |
| 88 | + |
| 89 | +继续在「配置与云函数」页面,找到「theme -> shiro」配置,点击编辑,进入编辑页面,在代码中找到 `module`,加入 activity 配置,如下:(高亮部分) |
| 90 | + |
| 91 | +```json {14,15,16,17} |
| 92 | +{ |
| 93 | + "module": { |
| 94 | + "donate": { |
| 95 | + "enable": false, |
| 96 | + "link": " https://afdian.net/@Innei ", |
| 97 | + "qrcode": [ |
| 98 | + " https://cdn.jsdelivr.net/gh/Innei/img-bed@master/20191211132347.png ", |
| 99 | + " https://cdn.innei.ren/bed/2023/0424213144.png " |
| 100 | + ] |
| 101 | + }, |
| 102 | + "bilibili": { |
| 103 | + "liveId": 1434499 |
| 104 | + }, |
| 105 | + "activity": { |
| 106 | + "enable": true, |
| 107 | + "endpoint": "/fn/ps/update" |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +### 配置软件 |
| 114 | + |
| 115 | +前往 GitHub 仓库,下载最新的 [Release](https://github.com/mx-space/ProcessReporterMac/releases) 版本,下载后打开 ProcessReporter.dmg 文件,将软件拖入「应用程序」文件夹中,然后打开软件。 |
| 116 | + |
| 117 | +<ToGitHub repo="mx-space/ProcessReporterMac" /> |
| 118 | + |
| 119 | +接下来啊你会发现你的系统菜单栏中多了一个图标,点击图标,然后点击「设置」,在弹出的窗口中填入你的信息: |
| 120 | + |
| 121 | +- Endpoint:`{你的API地址}/ps/update` (请将 `{你的API地址}` 替换为你的 API 地址,如:`https://api.example.com/api/v2`) |
| 122 | +- API Key:填入你刚刚在云函数中设置的密钥 |
| 123 | + |
| 124 | +如果你希望软件在开机时自动启动,可以勾选「Launch at login」选项。 |
| 125 | + |
| 126 | +接着关闭窗口,再次点击菜单栏中的图标,点击「Enable」即可。如果一切正常,刷新一下你的博客你就可以在博客顶部看到你的动态了。 |
| 127 | +</Steps> |
| 128 | + |
| 129 | +<style global jsx>{` |
| 130 | + .nextra-content pre { |
| 131 | + max-height: 55vh; |
| 132 | + } |
| 133 | +`}</style> |
0 commit comments