first commit
This commit is contained in:
16
utils/date.ts
Normal file
16
utils/date.ts
Normal file
@ -0,0 +1,16 @@
|
||||
export function formatDateTime() {
|
||||
const now = new Date();
|
||||
|
||||
// 获取年份、月份、日期、小时、分钟、秒数
|
||||
const year = now.getFullYear();
|
||||
const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从0开始,需要加1
|
||||
const day = String(now.getDate()).padStart(2, '0');
|
||||
const hours = String(now.getHours()).padStart(2, '0');
|
||||
const minutes = String(now.getMinutes()).padStart(2, '0');
|
||||
const seconds = String(now.getSeconds()).padStart(2, '0');
|
||||
|
||||
// 拼接成需要的格式
|
||||
const formattedDateTime = `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`;
|
||||
|
||||
return formattedDateTime;
|
||||
}
|
7
utils/ldjson.ts
Normal file
7
utils/ldjson.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export function baiduLdjson(json: any) {
|
||||
return `
|
||||
<script type="application/ld+json">
|
||||
${JSON.stringify(json)}
|
||||
</script>
|
||||
`
|
||||
}
|
76
utils/rpc-client.ts
Normal file
76
utils/rpc-client.ts
Normal 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(),
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
69
utils/rpc.ts
Normal file
69
utils/rpc.ts
Normal file
@ -0,0 +1,69 @@
|
||||
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('127.0.0.1', 9504)
|
||||
}
|
||||
|
||||
// 新闻列表
|
||||
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():Promise<result<InewsIndex[]>>{
|
||||
const client = getRpcClient()
|
||||
const result = await client.call('news/index', {})
|
||||
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user