cool admin api
1.0.0
Node版後台基礎架構基於Egg.js(阿里出品)
獨有cool-admin.com發布的npm組件
環境Node.js>=8.9.0 Redis mysql
新建並匯入資料庫,修改資料庫連線訊息
推薦使用yarn
git clone https: //github.com/apgzs/cool-admin-api.git
cd cool - admin - api
yarn
yarn dev
http : //localhost:7001或npm
git clone https: //github.com/apgzs/cool-admin-api.git
cd cool - admin - api
npm install
npm run dev
http : //localhost:7001##影片教學:
cool-admin後端簡單入門影片(快速寫6個api介面):https://www.bilibili.com/video/BV1SE411j74K
cool-admin前後端搭配使用:https://www.bilibili.com/video/av90478011/
cool-admin前端crud內部訓練教學:https://www.bilibili.com/video/av89512654/
資料模型必須放在app/entities/*下,否則typeorm無法識別,如:
import { Entity , Column , Index } from 'typeorm' ;
import { BaseEntity } from 'egg-cool-entity' ;
/**
* 系统角色
*/
@ Entity ( { name : 'sys_role' } )
export default class SysRole extends BaseEntity {
// 名称
@ Index ( { unique : true } )
@ Column ( )
name : string ;
// 角色标签
@ Index ( { unique : true } )
@ Column ( { nullable : true } )
label : string ;
// 备注
@ Column ( { nullable : true } )
remark : string ;
}新建完成運行程式碼,就可以看到資料庫新建了一張sys_role表,如不需要自動建立config資料夾下修改typeorm的設定文件
有了資料表之後,如果希望透過介面對資料表進行操作,我們就必須在controller資料夾下新建對應的控制器,如:
import { BaseController } from 'egg-cool-controller' ;
import { Context } from 'egg' ;
import routerDecorator from 'egg-cool-router' ;
import { Brackets } from 'typeorm' ;
/**
* 系统-角色
*/
@ routerDecorator . prefix ( '/admin/sys/role' , [ 'add' , 'delete' , 'update' , 'info' , 'list' , 'page' ] )
export default class SysRoleController extends BaseController {
constructor ( ctx : Context ) {
super ( ctx ) ;
this . setEntity ( this . ctx . repo . sys . Role ) ;
this . setPageOption ( {
keyWordLikeFields : [ 'name' , 'label' ] ,
where : new Brackets ( qb => {
qb . where ( 'id !=:id' , { id : 1 } ) ;
} ) ,
} ) ; //分页配置(可选)
this . setService ( this . service . sys . role ) ; //设置自定义的service(可选)
}
}這樣我們就完成了6個介面的寫,對應的介面如下:
/admin/sys/role/add新增/admin/sys/role/delete刪除/admin/sys/role/update更新/admin/sys/role/info單一訊息/admin/sys/role/list清單訊息/admin/sys/role/page分頁查詢(包含模糊查詢、欄位全符合等) | 參數 | 類型 | 說明 |
|---|---|---|
| keyWordLikeFields | 陣列 | 模糊查詢需要匹配的字段,如[ 'name','phone' ] ,這樣就可以模糊查詢姓名、手机兩個字段了 |
| where | TypeORM Brackets對象 | 固定where條件設置,詳見typeorm |
| fieldEq | 陣列 | 動態條件全匹配,如需要篩選用戶狀態status ,就可以設定成['status'] ,此時介面就可以接受status的值並且對資料有過濾效果 |
| addOrderBy | 物件 | 排序條件可傳多個,如{ sortNum:asc, createTime:desc } |
有些業務場景,我們不希望每次請求介面都需要操作資料庫,如:今日推薦、上個月排行榜等,資料儲存在redis ,註:快取註解只在service層有效
import { BaseService } from 'egg-cool-service' ;
import { Cache } from 'egg-cool-cache' ;
/**
* 业务-排行榜服务类
*/
export default class BusRankService extends BaseService {
/**
* 上个月榜单
*/
@ Cache ( { ttl : 1000 } ) // 表示缓存
async rankList ( ) {
return [ '程序猿1号' , '程序猿2号' , '程序猿3号' ] ;
}
} | 參數 | 類型 | 說明 |
|---|---|---|
| resolver | 陣列 | 方法參數取得,生成key用, resolver: (args => {return args[0];}),這樣就可以獲得方法的第一個參數作為緩存key |
| ttl | 數位 | 快取過期時間,單位:秒 |
| url | 字串 | 請求url包含該前綴才緩存,如/api/*請求時緩存, /admin/*請求時不緩存 |
egg.js原生的路由寫法過於繁瑣, cool-admin的路由支援BaseController還有其他原生支援具體參考egg.js路由
除了單表的簡單操作,真實的業務往往需要對資料庫做一些複雜的操作。這時候我們可以在service自訂SQL,如
async page ( query ) {
const { keyWord , status } = query ;
const sql = `
SELECT
a.*,
GROUP_CONCAT(c.name) AS roleName
FROM
sys_user a
LEFT JOIN sys_user_role b ON a.id = b.userId
LEFT JOIN sys_role c ON b.roleId = c.id
WHERE 1 = 1
${ this . setSql ( status , 'and a.status = ?' , [ status ] ) }
${ this . setSql ( keyWord , 'and (a.name LIKE ? or a.username LIKE ?)' , [ `% ${ keyWord } %` , `% ${ keyWord } %` ] ) }
${ this . setSql ( true , 'and a.id != ?' , [ 1 ] ) }
GROUP BY a.id` ;
return this . sqlRenderPage ( sql , query ) ;
}| 參數 | 類型 | 說明 |
|---|---|---|
| condition | 布林型 | 只有滿足改條件才會拼接上對應的sql和參數 |
| sql | 字串 | 需要拼接的參數 |
| params | 陣列 | 相對應的參數 |