坑1、因为我mysql的版本是5.6,所以用docker的方式部署,不知道搞错了什么。连插件build的时候一直提示yarn install。目前无解,我啥都没搞,一直卡在这好久。解决:在另一个机器搭建了非docker版本,build后以插件的方式安装到docker版本去
坑2、mysql、nodjs、yarn版本一定要和官网对应!!否则很多问题出现找不到,解决非常非常困难。因为版本不同,编译的插件其他版本无法使用。
1、安装:
官网已经详细:https://docs-cn.nocobase.com/welcome/getting-started/installation/create-nocobase-app
2、开发简单插件:
1、先创建简单的插件demo
yarn pm create @my-project/plugin-wxs
yarn build @my-project/plugin-wxs
注意、最好删了项目根目录的yarn.lock 然后yarn install一次最好。然后重启就会在插件库出现,yarn install 解决了我不能yarn pm add @my-project/plugin-hello 显示的问题
坑:手动删了一个插件,然后数据库没删,它在加载的时候一直报错,无法显示任何界面,花了我半天时间,最后进数据库去看,发现插件表有数据,删掉重启就恢复了
2、简单demo
1、自定义一个api接口,在server目录写


代码:
import { Plugin } from '@nocobase/server';
const axios = require('axios');
export class PluginWxsServer extends Plugin {
async afterAdd() {}
async beforeLoad() {}
async load() {
// 公开hello表的所有操作
this.app.acl.allow('hello', '*', 'public');
this.app.resource({
name: 'wxserver',
actions: {
async register(ctx, next) {
try {
const response = await axios.get('https://hgks.aizwkj.com/server-api/api/t_8vxfgoykz71:list?filter={%22user_wj_id%22:49}&appends=f_4c7x6l61kqa&except=f_4c7x6l61kqa.f_btzy6xntn88&appends[]=f_4c7x6l61kqa.f_gxstjml35dc');
console.log('服务端请求结果', response.data);
ctx.body = {
status: 'success',
data: response.data
};
} catch (error) {
console.error('API请求错误', error.message);
ctx.body = {
status: 'error',
message: error.message,
details: error.response?.data || {}
};
ctx.status = error.response?.status || 500;
}
next();
},
},
});
this.app.acl.allow('wxserver', 'register', 'public');
}
async install() {}
async afterEnable() {}
async afterDisable() {}
async remove() {}
}
export default PluginWxsServer;
这个是开放权限的,不懂问豆包
this.app.acl.allow('wxserver', 'register', 'public');
访问
192.168.1.126:13001/api/wxserver:register
结果:

记录一下关于数据表的关联:


这样就关联了另一个表的数据了
3、数据库操作
单条查询:通过主键(ID 或 TK)获取单条记录
async function getSingleRecord(ctx) {
const tableRepo = ctx.db.getRepository('tableName');
const { id } = ctx.params; // 从URL参数获取ID,如 /api/tableName/:id
// 方法1:使用 get 方法(推荐,性能更好)
const record = await tableRepo.get(id);
// 方法2:使用 findOne 方法(支持更复杂的筛选条件)
const record2 = await tableRepo.findOne({
filter: { id }
});
ctx.body = record;
}
关联查询:查询主表数据并包含关联表数据
async function queryWithRelations(ctx) {
const tableRepo = ctx.db.getRepository('tableName');
// 示例:查询订单表(orders)并包含关联的用户(user)和商品(products)数据
const orders = await tableRepo.find({
filter: {},
include: [
'user', // 关联用户表
{
association: 'products', // 关联商品表
filter: { price: { $gt: 100 } } // 筛选关联商品价格大于100
}
]
});
ctx.body = orders;
}
demo:读取数据库的token信息,提交给微信小程序url获取文字素材
// 公开hello表的所有操作
this.app.acl.allow('hello', '*', 'public');
this.app.resource({
name: 'wxserver',
actions: {
async register(ctx, next) {
try {
const system = ctx.db.getRepository('system'); // 替换为实际表名
// 使用 find 方法查询所有数据
const records = await system.findOne();
console.error('1 records.wx_token=', records.wx_token);
const url='https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token='+records.wx_token.access_token
const response = await axios.post(url,
{
type: "news",
offset: 0,
count: 20
});
console.error('response=', response,"url=",url);
// 只返回响应数据,而不是整个响应对象
ctx.body = response.data;
} catch (error) {
console.error('API请求错误', error.message);
ctx.body = {
status: 'error',
message: error.message,
details: error.response?.data || {}
};
ctx.status = error.response?.status || 500;
}
next();
},
},
});
this.app.acl.allow('wxserver', 'register', 'public');
}
4355

被折叠的 条评论
为什么被折叠?



