- 数据库 https://blog.csdn.net/dwp_wz/article/details/123806045
- Egg.js获取formData数据
- 安全
- 单元测试
- https://github.com/flarestart/egg-websocket-plugin
- egg-socket https://www.cnblogs.com/kbnet/archive/2019/05/30/10949074.html
开始
在项目文件夹中 npm init egg --type=simple
跨域
配置cors跨域相关问题
npm i egg-cors --save
# config/plugin.js
exports.cors: {
enable: true,
package: 'egg-cors'
}
# config/config.default.js
config.security = {
csrf: {
enable: false
},
domainWhiteList: [ '*' ]
};
config.cors = {
origin: '*',
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS'
};
针对个别 URL 不使用 csrf
config.security = {
csrf: {
ignore: ctx => ['/pay/notice'].indexOf(ctx.request.url) > -1
}
}
POST
header 携带 x-csrf-token
MySQL
- 安装插件
npm i --save egg-mysql
- 在
config/plugin.js
开启插件 - 在
config/config.${env}.js
配置数据库信息
https://eggjs.org/zh-cn/tutorials/mysql.html
MySQL 查询
查询示例
Sequelize
res.data = await db.select('grm_gdgem', {
where: { status: 'draft' },
columns: ['author', 'title'], // get the value of certain columns
orders: [['created_at','desc'], ['id','desc']],
limit: 10,
offset: 0,
});
日期时间查询:
数据库格式 timestamp
-- DATE_FORMAT
DATE_FORMAT(insert_time,'%Y-%m-%d') = '2023-03-03'
DATE_FORMAT(insert_time,'%Y-%m-%d') = '2023-3-3' -- ×
-- TO_DAYS
TO_DAYS(insert_time) = TO_DAYS('2023-3-3') -- ✓
TO_DAYS(insert_time) = TO_DAYS('2023-03-03') -- ✓
TO_DAYS(insert_time) = TO_DAYS('2023-03-03 12:00') -- ✓
TO_DAYS(insert_time) = TO_DAYS('Fri Mar 24 2023 18:49:55 GMT+0800 (中国标准时间)') -- ×
TO_DAYS(insert_time) = TO_DAYS(时间戳) -- ×
数据库格式 datetime (同上也可以用)
日期时间输出:
new Date(+create_time).toLocaleString('zh', { hour12: false });
// 或者
sd (看zaoa实现)
MySQL 更新
db.update 不支持一次更新多条记录
登录jwt
参考:https://github.com/okoala/egg-jwt
https://www.jianshu.com/p/932bf133371f https://w.cnblogs.com/xingguozhiming/p/12047952.html
微信支付
网页支付
https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_7_1.shtml
- 使用 /app/extend/wxpay
- 依赖
"dependencies": {
"MD5": "^1.2.1",
"request": "^2.88.0",
"body-parser": "^1.18.3",
"xml2js": "^0.4.6"
}
- 查看 /app/controller/wxpay.js 中的 demo。
接口文档
微信JSSDK配置
/wx/config?url=
QuickStart
<!-- add docs here for user -->
see [egg docs] [egg] for more detail.
Development
npm i
npm run dev
open http://localhost:7001/
Deploy
npm start
npm stop
npm scripts
- Use
npm run lint
to check code style. - Use
npm test
to run unit test. - Use
npm run autod
to auto detect dependencies upgrade, see autod for more detail.
错误码
5001 | need code | 缺少参数 5003 | need appId | 缺少参数 5002 | invalid code | 参数错误 5006 | | 数据已存在