文章首發(fā)于個(gè)人公號(hào):阿拉平平
有開(kāi)發(fā)經(jīng)驗(yàn)的小伙伴肯定知道,要獨(dú)立開(kāi)發(fā)一套管理系統(tǒng)并非易事月匣。從設(shè)計(jì)數(shù)據(jù)庫(kù)钻洒,到開(kāi)發(fā)接口奋姿,再到編寫(xiě)前端頁(yè)面,想想就讓人頭大航唆。如果需求不是很復(fù)雜,我們可以借助低代碼應(yīng)用引擎來(lái)快速開(kāi)發(fā)院刁。
項(xiàng)目介紹
Yao[1] 是一款 Go 語(yǔ)言驅(qū)動(dòng)的低代碼應(yīng)用引擎糯钙,通過(guò)編寫(xiě) JSON 文件即可快速制作 API 接口,數(shù)據(jù)管理系統(tǒng) 退腥,命令行工具等應(yīng)用程序任岸,應(yīng)用可以運(yùn)行在本地、云端和物聯(lián)網(wǎng)設(shè)備上狡刘。
快速安裝
Yao 可以通過(guò)腳本或容器來(lái)安裝享潜,官方推薦后者,所以這里我們使用 Docker 來(lái)部署嗅蔬。
運(yùn)行以下命令創(chuàng)建容器:
# 注意修改掛載的目錄
docker run -d --name yao -v <project root>:/data/app -p 5099:5099 yaoapp/yao:0.9.2-amd64-dev
容器啟動(dòng)后剑按,進(jìn)入容器:
docker exec -it yao bash
在項(xiàng)目目錄中,運(yùn)行初始化命令:
yao init
接著創(chuàng)建數(shù)據(jù)表:
yao migrate
初始化菜單:
yao run flows.setmenu
完成以上步驟后澜术,啟動(dòng)服務(wù):
yao start
服務(wù)啟動(dòng)后艺蝴,用瀏覽器訪問(wèn) https://<server-ip>:5099/xiang/login/admin
,輸入用戶名:xiang@iqka.com
鸟废, 密碼: A123456p+
登錄猜敢。
使用說(shuō)明
接下來(lái),我將介紹 Yao 的用法盒延,其中包含:
- 基本用法:借助測(cè)試數(shù)據(jù)缩擂,了解 Yao 界面上的功能。
- 新建內(nèi)容:新建新的內(nèi)容添寺,包括數(shù)據(jù)胯盯、接口和界面。
基本用法
Yao 在初始化后會(huì)有些測(cè)試數(shù)據(jù)计露,在界面中可以看到表單信息:
我們可以查看陨闹、編輯數(shù)據(jù):
Yao 還有張用戶表,支持增刪改查:
在菜單界面可以配置左側(cè)顯示的內(nèi)容:
新建內(nèi)容
Yao 界面上的功能大致如此薄坏,接下來(lái)我們建下自己的內(nèi)容趋厉。這里我打算實(shí)現(xiàn)一個(gè)簡(jiǎn)單的圖書(shū)管理功能。
先從數(shù)據(jù)開(kāi)始胶坠。我們回到項(xiàng)目目錄君账,在 models
下新建一個(gè) book.mod.json
文件,內(nèi)容如下:
{
"name": "Book",
"table": {
"name": "book",
"comment": "Book"
},
"columns": [{
"label": "ID",
"name": "id",
"type": "ID",
"comment": "ID"
},
{
"label": "SN",
"name": "sn",
"type": "string",
"unique": true
},
{
"label": "Name",
"name": "name",
"type": "string",
"index": true
},
{
"label": "Kind",
"name": "kind",
"type": "enum",
"option": ["科幻", "名著"],
"default": "科幻",
"index": true
},
{
"label": "Description",
"name": "desc",
"type": "string",
"comment": "Description"
},
{
"label": "Score",
"name": "score",
"type": "integer",
"comment": "Score"
}
],
"values": [{
"sn": "100001",
"name": "水滸傳",
"kind": "名著",
"desc": "三個(gè)女人和一百零五個(gè)男人的故事",
"score": 9
},
{
"sn": "100002",
"name": "三體",
"kind": "科幻",
"desc": "不要回答沈善!不要回答乡数!不要回答椭蹄!",
"score": 9
}
],
"option": {
"timestamps": true,
"soft_deletes": true
}
}
然后在項(xiàng)目目錄中運(yùn)行以下命令:
yao migrate -n book
需要注意的是,該命令的結(jié)果不會(huì)在前臺(tái)顯示净赴,而是寫(xiě)到 logs/application.log
中绳矩。
針對(duì)這種情況,我們可以先查詢下數(shù)據(jù)玖翅,如果數(shù)據(jù)能夠正常顯示翼馆,則說(shuō)明數(shù)據(jù)表已創(chuàng)建:
yao run models.book.get '::{}'
Run: models.book.get
args[0]: {}
--------------------------------------
models.book.get Response
--------------------------------------
[
{
"created_at": "2022-07-27 05:41:32",
"deleted_at": null,
"desc": "三個(gè)女人和一百零五個(gè)男人的故事",
"id": 1,
"kind": "名著",
"name": "水滸傳",
"score": 9,
"sn": "100001",
"updated_at": null
},
{
"created_at": "2022-07-27 05:41:32",
"deleted_at": null,
"desc": "不要回答!不要回答金度!不要回答应媚!",
"id": 2,
"kind": "科幻",
"name": "三體",
"score": 9,
"sn": "100002",
"updated_at": null
}
]
--------------------------------------
?DONE?
再編寫(xiě)接口。在 apis
下新建一個(gè) book.http.json
文件猜极,內(nèi)容如下:
{
"name": "書(shū)籍",
"version": "1.0.0",
"description": "書(shū)籍接口",
"group": "book",
"guard": "bearer-jwt",
"paths": [{
"path": "/search",
"method": "GET",
"guard": "-",
"process": "models.book.Paginate",
"in": [":query-param", "$query.page", "$query.pagesize"],
"out": {
"status": 200,
"type": "application/json"
}
},
{
"path": "/save",
"method": "POST",
"guard": "-",
"process": "models.book.Save",
"in": [":payload"],
"out": {
"status": 200,
"type": "application/json"
}
}
]
}
在這個(gè)文件中中姜,我定義了兩個(gè)接口:/search
和 /save
,用于查詢和創(chuàng)建跟伏。我們先用接口創(chuàng)建新的數(shù)據(jù):
curl -X POST http://127.0.0.1:5099/api/book/save \
-H 'Content-Type: application/json' \
-d '{ "sn": "100003", "name": "三國(guó)演義", "kind": "名著", "desc": "東漢末年分三國(guó)", "score": 9 }'
查詢剛剛創(chuàng)建的數(shù)據(jù)丢胚,如果結(jié)果返回正常,說(shuō)明接口功能無(wú)誤受扳。
curl 'http://127.0.0.1:5099/api/book/search?where.name.match=三國(guó)演義&page=1&pagesize=1'
最后編寫(xiě)界面嗜桌。在 tables
目錄下新建一個(gè) book.tab.json
文件,內(nèi)容如下:
{
"name": "Book",
"version": "1.0.0",
"decription": "Book",
"bind": {
"model": "book"
},
"apis": {},
"columns": {
"ID": {
"label": "ID",
"view": {
"type": "label",
"props": {
"value": ":id"
}
}
},
"SN": {
"label": "SN",
"view": {
"type": "label",
"props": {
"value": ":sn"
}
},
"edit": {
"type": "input",
"props": {
"value": ":sn"
}
}
},
"Name": {
"label": "Name",
"view": {
"type": "label",
"props": {
"value": ":name"
}
},
"edit": {
"type": "input",
"props": {
"value": ":name"
}
}
},
"Kind": {
"label": "Kind",
"view": {
"type": "label",
"props": {
"value": ":kind"
}
},
"edit": {
"type": "select",
"props": {
"value": ":kind",
"options": [{
"label": "科幻",
"value": "科幻"
},
{
"label": "名著",
"value": "名著"
}
]
}
}
},
"Score": {
"label": "Score",
"view": {
"type": "label",
"props": {
"value": ":score"
}
},
"edit": {
"type": "input",
"props": {
"value": ":score"
}
}
},
"Description": {
"label": "Description",
"view": {
"type": "label",
"props": {
"value": ":desc"
}
},
"edit": {
"type": "textArea",
"props": {
"value": ":desc",
"rows": 4
}
}
}
},
"filters": {
"Keywords": {
"@": "f.Keywords",
"in": ["where.name.match"]
}
},
"list": {
"primary": "id",
"layout": {
"columns": [{
"name": "SN",
"width": 100
},
{
"name": "Name",
"width": 200
},
{
"name": "Score",
"width": 300
},
{
"name": "Kind"
}
],
"filters": [{
"name": "Keywords"
}]
},
"actions": {
"create": {
"type": "button",
"props": {
"label": "添加書(shū)籍",
"icon": "fas fa-plus"
}
},
"pagination": {
"props": {
"showTotal": true
}
}
},
"option": {
"operation": {
"unfold": true
}
}
},
"edit": {
"primary": "id",
"layout": {
"fieldset": [{
"columns": [{
"name": "SN",
"width": 6
},
{
"name": "Name",
"width": 6
},
{
"name": "Kind",
"width": 6
},
{
"name": "Score",
"width": 6
},
{
"name": "Description",
"width": 24
}
]
}]
},
"actions": {
"cancel": {},
"save": {
"type": "button",
"props": {
"label": "Save"
}
},
"delete": {
"type": "button",
"props": {
"label": "Delete"
}
}
}
}
}
回到菜單界面辞色,把建好的書(shū)籍界面添加進(jìn)去:
重新登錄系統(tǒng)骨宠,可以看到書(shū)籍界面:
寫(xiě)在最后
可以看到,我們用 Yao 添加新內(nèi)容時(shí)相满,基本都是在和 JSON 打交道层亿,沒(méi)有涉及到代碼。所以對(duì)于需求不復(fù)雜的系統(tǒng)立美,使用低代碼引擎來(lái)開(kāi)發(fā)或許是個(gè)不錯(cuò)的選擇匿又。
References
[1] Yao: https://github.com/YaoApp/yao