This commit is contained in:
toom1996
2025-07-09 17:33:41 +08:00
parent 46ad420309
commit 92e543ecff
64 changed files with 15946 additions and 15 deletions

12
utils/config.ts Normal file
View File

@ -0,0 +1,12 @@
import type { IWebsiteConfig } from "./rpc";
export function getConfig(Astro: any):IWebsiteConfig
{
return Astro.locals.template_config;
}
// 取默认的配置
export function getExtra(extraTag: string = '')
{
return {}
}

5
utils/host.ts Normal file
View File

@ -0,0 +1,5 @@
export async function getHost(request: Request) {
// 获取请求头中的 Host
const host = request.headers.get('host');
return host;
}

76
utils/rpc-client.ts Normal file
View File

@ -0,0 +1,76 @@
import net from 'net'
interface JsonRpcRequest {
jsonrpc: '2.0'
method: string
params?: any
id: number
}
interface JsonRpcResponse {
jsonrpc: '2.0'
id: number
result?: any
error?: { code: number; message: string; data?: any }
context?: any
}
export class JsonRpcClient {
private host: string
private port: number
private timeout: number
constructor(host: string = '127.0.0.1', port: number = 9504, timeout = 1000) {
this.host = host
this.port = port
this.timeout = timeout
}
async call(method: string, params?: any): Promise<any> {
const req: JsonRpcRequest = {
jsonrpc: '2.0',
method,
params,
id: Date.now(),
}
console.log(req)
const message = JSON.stringify(req) + '\n'
return new Promise((resolve, reject) => {
const client = new net.Socket()
let response = ''
client.setEncoding('utf-8')
const timer = setTimeout(() => {
client.destroy()
reject(new Error('RPC call timed out'))
}, this.timeout)
client.connect(this.port, this.host, () => {
client.write(message)
})
client.on('data', (data) => {
response += data.toString()
try {
const res: JsonRpcResponse = JSON.parse(response)
clearTimeout(timer)
client.end()
if (res.error) {
reject(new Error(res.error.message))
} else {
resolve(res.result)
}
} catch (e) {
// wait for more data
}
})
client.on('error', (err) => {
clearTimeout(timer)
reject(err)
})
})
}
}

98
utils/rpc.ts Normal file
View File

@ -0,0 +1,98 @@
import { JsonRpcClient } from './rpc-client.ts'
interface images {
src:string
}
interface result<T> {
code : number
message : string
data : T
}
interface data<T> {
page_title: string
page_module: T
}
function getRpcClient() {
return new JsonRpcClient(import.meta.env.RPC_REMOTE, import.meta.env.RPC_PORT)
}
// 新闻列表
export interface InewsIndex {
id: number
title: string
cover: string
keywords: string
description: string
content: string
created_at: string
updated_at: string
}
export async function newsIndex(websiteId:number = 0):Promise<result<InewsIndex[]>>{
console.log(websiteId)
const client = getRpcClient()
const result = await client.call('news/index', {
id:websiteId
})
return result
}
// 新闻详情
export interface InewsDetail {
title: string
keywords: string
description: string
content: string
created_at: string
updated_at: string
about: {
id: number
title: string
}[]
prevNews: {
id: number
title: string
}
nextNews: {
id: number
title: string
}
}
export async function newsDetail(id:string):Promise<result<InewsDetail>>{
const client = getRpcClient()
const result = await client.call('news/view', {
id:id
})
return result
}
// 网站配置
export interface IWebsiteConfig {
id: number
app_extra_tag: string
app_name: string
app_description: string
app_keywords: string
app_company: string
app_logo: string
app_favicon: string
app_copyright: string
app_icp: string
app_filing: string
app_filing_url: string
}
export async function websiteConfig(host:string):Promise<result<IWebsiteConfig>>{
const client = getRpcClient()
const result = await client.call('website/config', {
host:host
})
return result
}