提交 ec300eb0 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

create and edit data def

上级 0010cf55
package model
import constant "github.com/easysoft/zendata/src/utils/const"
import (
"time"
)
var CommonPrefix = "zd_"
var (
CommonPrefix = "zd_"
Models = []interface{}{&Def{}}
)
type ReqData struct {
Action string `json:"action"`
Id int `json:"id"`
Data interface{} `json:"data"`
}
......@@ -15,13 +21,11 @@ type ResData struct {
Data interface{} `json:"data"`
}
type Data struct {
Seq string `gorm:"column:seq" json:"seq"`
Name string `gorm:"column:name" json:"name"`
Path string `gorm:"column:path" json:"path"`
Desc string `gorm:"column:desc" json:"desc"`
}
type Model struct {
Id uint `gorm:"column:id;primary_key" json:"id" `
CreatedAt time.Time `gorm:"column:createTime" json:"createTime"`
UpdatedAt time.Time `gorm:"column:updateTime" json:"updateTime"`
func (*Data) TableName() string {
return constant.TablePrefix + "data"
Disabled bool `gorm:"column:disabled;default:false" json:"disabled"`
Deleted bool `gorm:"column:deleted;default:false" json:"deleted"`
}
package model
import (
constant "github.com/easysoft/zendata/src/utils/const"
)
type Def struct {
Name string `gorm:"column:name" json:"name"`
Path string `gorm:"column:path" json:"path"`
Desc string `gorm:"column:desc" json:"desc"`
Folder string `gorm:"-" json:"folder"`
Model
}
func (*Def) TableName() string {
return constant.TablePrefix + "def"
}
......@@ -3,6 +3,8 @@ package server
import (
"encoding/json"
"github.com/easysoft/zendata/src/model"
defServer "github.com/easysoft/zendata/src/server/def"
commonUtils "github.com/easysoft/zendata/src/utils/common"
"io"
"io/ioutil"
"net/http"
......@@ -25,7 +27,21 @@ func AdminHandler(writer http.ResponseWriter, req *http.Request) {
ret := model.ResData{ Code: 1, Msg: "success"}
if reqData.Action == "listDef" {
ret.Data = ListData()
ret.Data = defServer.List()
} else if reqData.Action == "getDef" {
def := defServer.Get(reqData.Id)
def.Folder = commonUtils.GetFolder(def.Path)
ret.Data = def
} else if reqData.Action == "saveDef" {
def := convertDef(reqData.Data)
if def.Id == 0 {
defServer.Create(&def)
} else {
defServer.Update(&def)
}
ret.Data = def
}
jsonStr, _ := json.Marshal(ret)
......
......@@ -26,3 +26,10 @@ func writeRes(ret model.ResData, writer http.ResponseWriter) {
func errRes(msg string) model.ResData {
return model.ResData{ Code: 0, Msg: msg }
}
func convertDef(data interface{}) (def model.Def) {
bytes, _ := json.Marshal(data)
json.Unmarshal(bytes, &def)
return
}
package server
import (
"github.com/easysoft/zendata/src/model"
"github.com/easysoft/zendata/src/utils/vari"
)
func ListData() (defs []model.Data) {
vari.GormDB.Find(&defs)
return
}
package defServer
import (
"github.com/easysoft/zendata/src/model"
commonUtils "github.com/easysoft/zendata/src/utils/common"
constant "github.com/easysoft/zendata/src/utils/const"
"github.com/easysoft/zendata/src/utils/vari"
)
func List() (defs []model.Def) {
vari.GormDB.Find(&defs)
return
}
func Get(id int) (def model.Def) {
vari.GormDB.Where("id=?", id).First(&def)
return
}
func Create(def *model.Def) (err error) {
def.Folder = commonUtils.AddPathSep(def.Folder)
def.Path = def.Folder + constant.PthSep + def.Name
err = vari.GormDB.Save(def).Error
return
}
func Update(def *model.Def) (err error) {
def.Folder = commonUtils.AddPathSep(def.Folder)
def.Path = def.Folder + constant.PthSep + def.Name
err = vari.GormDB.Save(def).Error
return
}
......@@ -208,4 +208,16 @@ func RandNum64(length int64) int64 {
seedInt := rand.Int63n(length)
return seedInt
}
\ No newline at end of file
}
func AddPathSep(pth string) string {
if strings.Index(pth, constant.PthSep) != 0 {
pth = constant.PthSep + pth
}
return pth
}
func GetFolder(pth string) string {
idx := strings.LastIndex(pth, constant.PthSep)
return pth[:idx]
}
......@@ -13,8 +13,8 @@ import (
stdinUtils "github.com/easysoft/zendata/src/utils/stdin"
"github.com/easysoft/zendata/src/utils/vari"
"github.com/fatih/color"
"github.com/jinzhu/gorm"
"gopkg.in/ini.v1"
"log"
"os"
"os/user"
"path/filepath"
......@@ -37,15 +37,27 @@ func InitDB() (db *sql.DB, err error) {
}
// init db tables
ddlStr := fmt.Sprintf(`CREATE TABLE %s (
seq CHAR (5) PRIMARY KEY UNIQUE,
name VARCHAR(100) DEFAULT '',
path VARCHAR(500) DEFAULT ''
);`, (&model.Data{}).TableName())
_, err = db.Exec(ddlStr)
if err != nil {
log.Println(i118Utils.I118Prt.Sprintf("fail_to_create_table", "biz_data", err.Error()))
return
//ddlStr := fmt.Sprintf(`CREATE TABLE %s (
// seq CHAR (5) PRIMARY KEY UNIQUE,
// name VARCHAR(100) DEFAULT '',
// path VARCHAR(500) DEFAULT ''
// );`, (&model.Def{}).TableName())
//_, err = db.Exec(ddlStr)
//if err != nil {
// log.Println(i118Utils.I118Prt.Sprintf("fail_to_create_table", "biz_data", err.Error()))
// return
//}
return
}
func InitGormDB() (gormDb *gorm.DB, err error) {
gormDb, err = gorm.Open(constant.SqliteDriver, constant.SqliteData)
gormDb = gormDb.Debug()
for _, model := range model.Models {
if err := gormDb.Set("gorm:table_options", "").AutoMigrate(model).Error; err != nil {
return nil, fmt.Errorf("auto migrate table %+v failure %s", model, err.Error())
}
}
return
......@@ -240,7 +252,7 @@ func addZdToPathLinux(home string) {
}
func isDataInit(db *sql.DB) bool {
sql := "select * from " + (&model.Data{}).TableName()
sql := "select * from " + (&model.Def{}).TableName()
_, err := db.Query(sql)
if err == nil {
......
......@@ -16,7 +16,6 @@ import (
stringUtils "github.com/easysoft/zendata/src/utils/string"
"github.com/easysoft/zendata/src/utils/vari"
"github.com/fatih/color"
"github.com/jinzhu/gorm"
"io/ioutil"
"net/http"
"os"
......@@ -134,7 +133,7 @@ func main() {
vari.DB, _ = configUtils.InitDB()
defer vari.DB.Close()
vari.GormDB, _ = gorm.Open(constant.SqliteDriver, constant.SqliteData)
vari.GormDB, _ = configUtils.InitGormDB()
defer vari.GormDB.Close()
switch os.Args[1] {
......
......@@ -15,11 +15,19 @@ export function listDef () {
data: {'action': 'listDef'}
})
}
export function getDef (id) {
const data = {'action': 'getDef', id: id}
console.log(data)
return request({
url: api.admin,
method: 'post',
data: data
})
}
export function saveDef (data) {
return request({
url: api.admin,
method: 'post',
data: {'action': 'createDef', 'data': data}
data: {'action': 'saveDef', 'data': data}
})
}
......@@ -8,6 +8,22 @@ h1, h2, h3, h4, h5, h6 {
font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, 'PingFang SC', 'Source Han Sans CN', 'Source Han Sans', 'Source Han Serif', 'Hiragino Sans GB', 'WenQuanYi Micro Hei', 'Microsoft YaHei', sans-serif;
}
.head {
display: flex;
.title {
flex: 1;
margin-bottom: 15px;
padding: 3px 5px;
line-height: 32px;
font-weight: bolder;
font-size: 16px;
}
.buttons {
width: 100px;
text-align: right;
}
}
a.common {
color: rgba(0, 0, 0, 0.65) !important;
}
......
......@@ -48,7 +48,9 @@ export default {
methods: {
handleClick (e) {
console.log('handleClick', e, this.$route.path)
if (this.$route.path.indexOf(e.key) < 0) this.$router.push('/data/' + e.key + '/index');
if (e.key == 'mine' && this.$route.path != '/data/mine/index') {
this.$router.push('/data/mine/index');
}
},
titleClick (e) {
console.log('titleClick', e)
......
......@@ -13,6 +13,14 @@ import SubMenu from "ant-design-vue/lib/menu";
import MenuItem from "ant-design-vue/lib/menu";
import 'ant-design-vue/lib/menu/style';
import FormModel from "ant-design-vue/lib/form-model";
import Input from "ant-design-vue/lib/input";
import Select from "ant-design-vue/lib/select";
import 'ant-design-vue/lib/form/style';
import 'ant-design-vue/lib/form-model/style';
import 'ant-design-vue/lib/input/style';
import 'ant-design-vue/lib/select/style';
import Table from "ant-design-vue/lib/table";
import 'ant-design-vue/lib/table/style';
......@@ -31,6 +39,9 @@ Vue.use(ConfigProvider)
Vue.use(Menu)
Vue.use(SubMenu)
Vue.use(MenuItem)
Vue.use(FormModel)
Vue.use(Input)
Vue.use(Select)
Vue.use(Button)
Vue.use(Table)
Vue.use(Tag)
......
......@@ -27,11 +27,16 @@ const routes = [
redirect: '/data/mine/index',
children: [
{
path: 'list',
path: 'mine-list',
alias: "index",
name: 'list',
component: () => import('../views/data/mine/List')
},
{
path: 'edit/:id',
name: 'mine-edit',
component: () => import('../views/data/mine/Edit')
},
],
},
{
......@@ -49,7 +54,7 @@ const routes = [
{
path: 'list',
alias: "index",
name: 'list',
name: 'excel-list',
component: () => import('../views/data/buildin/excel/List')
},
],
......@@ -63,7 +68,7 @@ const routes = [
{
path: 'list',
alias: "index",
name: 'list',
name: 'yaml-list',
component: () => import('../views/data/buildin/yaml/List')
},
],
......
const labelCol = { lg: { span: 6 }, sm: { span: 6 } }
const wrapperCol = { lg: { span: 10 }, sm: { span: 10 } }
export { labelCol as labelCol, wrapperCol as wrapperCol }
<template>
<div>
<div class="head">
<div class="title">
测试数据<span v-if="id!=0">编辑</span><span v-if="id==0">新建</span>
</div>
<div class="buttons">
<a-button type="primary" @click="back()">返回</a-button>
</div>
</div>
<div>
<a-form-model ref="editForm" :model="model" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-form-model-item label="名称" prop="name">
<a-input v-model="model.name" />
</a-form-model-item>
<a-form-model-item label="文件夹" prop="folder">
<a-input v-model="model.folder" />
</a-form-model-item>
<!--<a-form-model-item label="类型">
<a-select v-model="model.region" placeholder="please select your zone">
<a-select-option value="shanghai">
Zone one
</a-select-option>
<a-select-option value="beijing">
Zone two
</a-select-option>
</a-select>
</a-form-model-item>-->
<a-form-model-item label="描述" prop="desc">
<a-input v-model="model.desc" type="textarea" rows="3" />
</a-form-model-item>
<a-form-model-item :wrapper-col="{ span: 14, offset: 6 }">
<a-button @click="save" type="primary">
保存
</a-button>
<a-button @click="reset" style="margin-left: 10px;">
重置
</a-button>
</a-form-model-item>
</a-form-model>
</div>
</div>
</template>
<script>
import { getDef, saveDef } from "../../../api/manage";
import { labelCol, wrapperCol } from "../../../utils/const";
export default {
name: 'Mine',
data() {
return {
labelCol: labelCol,
wrapperCol: wrapperCol,
rules: {
name: [
{ required: true, message: '名称不能为空', trigger: 'change' },
],
folder: [
{ required: true, message: '目录不能为空', trigger: 'change' },
],
},
id: 0,
model: { folder: '/users' }
};
},
computed: {
},
created () {
this.id = parseInt(this.$route.params.id)
console.log(this.id)
if (this.id == 0) return
getDef(this.id).then(json => {
console.log('getDef', json)
this.model = json.data
})
},
mounted () {
},
methods: {
save() {
this.$refs.editForm.validate(valid => {
console.log(valid, this.model)
if (!valid) {
console.log('validation fail')
return
}
saveDef(this.model).then(json => {
console.log('saveDef', json)
this.back()
})
})
},
reset () {
this.$refs.editForm.resetFields()
},
back() {
this.$router.push({path: '/data/mine/index'});
},
}
}
</script>
......
<template>
<div>
<a-table :columns="columns" :data-source="defs" rowKey="seq">
<div class="head">
<div class="title">测试数据列表</div>
<div class="buttons">
<a-button type="primary" @click="create()">新建</a-button>
</div>
</div>
<a-table :columns="columns" :data-source="defs" rowKey="id">
<a slot="name" slot-scope="text">{{ text }}</a>
<span slot="customTitle">名称</span>
......@@ -51,18 +58,16 @@ export default {
console.log('listDefs', res)
this.defs = res.data
})
// const def = {name: "myDef"}
// saveDef(def).then(res => {
// console.log('saveDef', res)
// this.defs = res
// })
},
mounted () {
},
methods: {
create() {
this.$router.push({path: '/data/mine/edit/0'});
},
edit(record) {
console.log(record)
this.$router.push({path: `/data/mine/edit/${record.id}`});
},
remove(record) {
console.log(record)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册