Egg.js


Links

开始

在项目文件夹中 npm init egg --type=simple

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,
});

未实践

where: {
                a: 1,
                b: [11, 22],
                OR: {
                    sender11: { op: 'like', value: `%111%` },
                    receiver22: { op: 'like', value: `%222%` },
                },
                AND: {
                    orderno: null,
                    id: 1,
                    role: ['admin', 'guest'],
                    OR: {
                        realname: { op: 'like', value: 'simba' },
                        age: { op: '>', value: '20' },
                        nickname: { op: 'like', value: 'ace' }
                    },
                    createtime: [
                        { op: '>', value: '2020-10-13' },
                        { op: '<', value: '2020-11-22' },
                    ]
                },
          }

日期时间输出

new Date(+create_time).toLocaleString('zh', { hour12: false });
// 或者
sd (看zaoa实现)

更新

db.update 不支持一次更新多条记录

Redis

@eggjs/redis 在 egg@^2.26.1 中使用,this.app.redis 为 undefined

Usage


  • 数据库 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

跨域

配置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

登录jwt

参考:https://github.com/okoala/egg-jwt

微信支付

网页支付

https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_7_1.shtml

  1. 使用 /app/extend/wxpay
  2. 依赖
"dependencies": {
    "MD5": "^1.2.1",
    "request": "^2.88.0",
    "body-parser": "^1.18.3",
    "xml2js": "^0.4.6"
}
  1. 查看 /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.

egg

错误码

5001 | need code | 缺少参数 5003 | need appId | 缺少参数 5002 | invalid code | 参数错误 5006 | | 数据已存在

更新时间:2025-04-16 18:13:17