first commit

This commit is contained in:
zhaoyingbo 2023-08-16 10:55:19 +08:00
commit d3fbb300dc
16 changed files with 10062 additions and 0 deletions

58
.gitignore vendored Normal file
View File

@ -0,0 +1,58 @@
# Logs
logs
*.log
npm-debug.log*
# Runtime data
pids
*.pid
*.seed
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules
jspm_packages
# Optional npm cache directory
.npm
# Optional REPL history
.node_repl_history
# 0x
profile-*
# mac files
.DS_Store
# vim swap files
*.swp
# webstorm
.idea
# vscode
.vscode
*code-workspace
# clinic
profile*
*clinic*
*flamegraph*

23
README.md Normal file
View File

@ -0,0 +1,23 @@
# Getting Started with [Fastify-CLI](https://www.npmjs.com/package/fastify-cli)
This project was bootstrapped with Fastify-CLI.
## Available Scripts
In the project directory, you can run:
### `npm run dev`
To start the app in dev mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
### `npm start`
For production mode
### `npm run test`
Run the test cases.
## Learn More
To learn Fastify, check out the [Fastify documentation](https://www.fastify.io/docs/latest/).

32
app.js Normal file
View File

@ -0,0 +1,32 @@
'use strict'
const path = require('path')
const AutoLoad = require('@fastify/autoload')
const { initSchedule } = require('./schedule')
// Init Scheduler
initSchedule()
// Pass --options via CLI arguments in command to enable these options.
module.exports.options = {}
module.exports = async function (fastify, opts) {
// Place here your custom code!
// Do not touch the following lines
// This loads all plugins defined in plugins
// those should be support plugins that are reused
// through your application
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'plugins'),
options: Object.assign({}, opts)
})
// This loads all plugins defined in routes
// define your routes in one of these
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'routes'),
options: Object.assign({}, opts)
})
}

9762
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

30
package.json Normal file
View File

@ -0,0 +1,30 @@
{
"name": "fastify",
"version": "1.0.0",
"description": "This project was bootstrapped with Fastify-CLI.",
"main": "app.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "tap \"test/**/*.test.js\"",
"start": "fastify start -l info app.js",
"dev": "fastify start -w -l info -P app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@fastify/autoload": "^5.0.0",
"@fastify/sensible": "^5.0.0",
"cross-fetch": "^4.0.0",
"fastify": "^4.0.0",
"fastify-cli": "^5.8.0",
"fastify-plugin": "^4.0.0",
"node-schedule": "^2.1.1",
"pocketbase": "^0.16.0"
},
"devDependencies": {
"tap": "^16.1.0"
}
}

16
plugins/README.md Normal file
View File

@ -0,0 +1,16 @@
# Plugins Folder
Plugins define behavior that is common to all the routes in your
application. Authentication, caching, templates, and all the other cross
cutting concerns should be handled by plugins placed in this folder.
Files in this folder are typically defined through the
[`fastify-plugin`](https://github.com/fastify/fastify-plugin) module,
making them non-encapsulated. They can define decorators and set hooks
that will then be used in the rest of your application.
Check out:
* [The hitchhiker's guide to plugins](https://www.fastify.io/docs/latest/Guides/Plugins-Guide/)
* [Fastify decorators](https://www.fastify.io/docs/latest/Reference/Decorators/).
* [Fastify lifecycle](https://www.fastify.io/docs/latest/Reference/Lifecycle/).

14
plugins/sensible.js Normal file
View File

@ -0,0 +1,14 @@
'use strict'
const fp = require('fastify-plugin')
/**
* This plugins adds some utilities to handle http errors
*
* @see https://github.com/fastify/fastify-sensible
*/
module.exports = fp(async function (fastify, opts) {
fastify.register(require('@fastify/sensible'), {
errorHandler: false
})
})

12
plugins/support.js Normal file
View File

@ -0,0 +1,12 @@
'use strict'
const fp = require('fastify-plugin')
// the use of fastify-plugin is required to be able
// to export the decorators to the outer scope
module.exports = fp(async function (fastify, opts) {
fastify.decorate('someSupport', function () {
return 'hugs'
})
})

13
routes/bot/index.js Normal file
View File

@ -0,0 +1,13 @@
'use strict'
module.exports = async function (fastify, opts) {
// 机器人验证及分发
fastify.post('/', async function (request, reply) {
console.log(JSON.stringify(request.body))
if (request.body.type === 'url_verification') {
console.log('url_verification')
return { challenge: request.body.challenge }
}
return 'OK'
})
}

7
routes/root.js Normal file
View File

@ -0,0 +1,7 @@
'use strict'
module.exports = async function (fastify, opts) {
fastify.get('/', async function (request, reply) {
return 'hello, glade to see you!'
})
}

21
schedule/accessToken.js Normal file
View File

@ -0,0 +1,21 @@
const fetch = require('node-fetch');
const { updateTenantAccessToken } = require('../utils/pb');
exports.resetAccessToken = async () => {
const URL = 'https://open.f.mioffice.cn/open-apis/auth/v3/tenant_access_token/internal'
const app_id = 'cli_a1eff35b43b89063'
const app_secret = 'IFSl8ig5DMwMnFjwPiljCfoEWlgRwDxW'
const res = await fetch(URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
app_id,
app_secret
})
})
const { tenant_access_token } = await res.json()
await updateTenantAccessToken(tenant_access_token)
return tenant_access_token
}

9
schedule/index.js Normal file
View File

@ -0,0 +1,9 @@
const schedule = require('node-schedule');
const { resetAccessToken } = require('./accessToken');
exports.initSchedule = async () => {
// 定时任务每15分钟刷新一次token
schedule.scheduleJob('*/15 * * * *', resetAccessToken);
resetAccessToken()
}

View File

@ -0,0 +1,21 @@
[
{
"_id": "a5cb7b58-0c22-41fb-bc00-af460ef90c1b",
"colId": "history",
"containerId": "",
"name": "https://self.imoaix.cn/bot",
"url": "https://self.imoaix.cn/bot",
"method": "POST",
"sortNum": 0,
"created": "2023-08-15T07:47:05.439Z",
"modified": "2023-08-15T07:47:34.510Z",
"headers": [],
"params": [],
"body": {
"type": "json",
"raw": "{\r\n \"challenge\": \"c14206ad-5b5f-4a77-82b6-b39b7365392b\",\r\n \"token\": \"tV9djUKSjzVnekV7xTg2Od06NFTcsBnj\",\r\n \"type\": \"url_verification\"\r\n}",
"form": []
},
"tests": []
}
]

14
utils/pb.js Normal file
View File

@ -0,0 +1,14 @@
const PocketBase = require('pocketbase/cjs')
require('cross-fetch/polyfill')
const pb = new PocketBase('https://eggpb.imoaix.cn')
module.exports.updateTenantAccessToken = async (value) => {
await pb.collection('config').update('ugel8f0cpk0rut6', { value })
console.log('reset access token success', value)
}
module.exports.getTenantAccessToken = async () => {
const { value } = await pb.collection('config').getOne('ugel8f0cpk0rut6')
return value
}

21
utils/sendCard.js Normal file
View File

@ -0,0 +1,21 @@
const fetch = require('node-fetch');
const {
getTenantAccessToken
} = require('./pb');
module.exports.sendCard = async (receive_id_type, receive_id, msg_type, content) => {
const URL = `https://open.f.mioffice.cn/open-apis/im/v1/messages?receive_id_type=${receive_id_type}`
const tenant_access_token = await getTenantAccessToken();
const header = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${tenant_access_token}`
}
const body = { receive_id, msg_type, content }
const res = await fetch(URL, {
method: 'POST',
headers: header,
body: JSON.stringify(body)
})
const data = await res.json();
console.log('sendCard success', data);
}

9
utils/yiyan.js Normal file
View File

@ -0,0 +1,9 @@
const fetch = require('node-fetch');
module.exports.getYiYan = async () => {
const URL = 'https://v1.hitokoto.cn/?c=i'
const res = await fetch(URL)
const { hitokoto } = await res.json()
console.log('get YiYan success', hitokoto)
return hitokoto
}