From cd99ac1da52b956d4cb959c3ca8098a85799474a Mon Sep 17 00:00:00 2001 From: shaoying Date: Thu, 30 Jul 2020 16:56:07 +0800 Subject: [PATCH] add meet.miss --- base/mdb/mdb.go | 238 +++++++++++++++++++++++++++++++-------------- base/web/render.go | 2 +- core/chat/meet.go | 117 ++++++++++++++++++++++ core/wiki/feel.go | 3 + logs.go | 2 +- misc/wx/wx.shy | 5 + 6 files changed, 291 insertions(+), 76 deletions(-) create mode 100644 core/chat/meet.go diff --git a/base/mdb/mdb.go b/base/mdb/mdb.go index 40553126..02189d8f 100644 --- a/base/mdb/mdb.go +++ b/base/mdb/mdb.go @@ -1,6 +1,9 @@ package mdb import ( + "path" + "strings" + ice "github.com/shylinux/icebergs" kit "github.com/shylinux/toolkits" "github.com/shylinux/toolkits/task" @@ -11,45 +14,103 @@ import ( "sort" ) -func _story_catch(m *ice.Message, kind, file string) { - m.Cmdy("web.story", "catch", CSV, file) +func _file_name(m *ice.Message, arg ...string) string { + return kit.Select(path.Join("usr/export", kit.Select(arg[0], arg[0]+":"+arg[1], arg[1] != ""), arg[2]), arg, 3) } -func _list_import(m *ice.Message, prefix, key, file string) { - f, e := os.Open(file) +func _hash_insert(m *ice.Message, prefix, key string, arg ...string) string { + m.Log_INSERT("prefix", prefix, arg[0], arg[1]) + return m.Rich(prefix, key, kit.Dict(arg)) + +} +func _hash_modify(m *ice.Message, prefix, key string, field, value string, arg ...string) { + m.Richs(prefix, key, value, func(key string, value map[string]interface{}) { + for i := 0; i < len(arg)-1; i += 2 { + if arg[i] == field { + continue + } + kit.Value(value, arg[i], arg[i+1]) + } + }) + m.Log_MODIFY("prefix", prefix, field, value, arg) +} +func _hash_delete(m *ice.Message, prefix, chain, field, value string) { + m.Richs(prefix, chain, value, func(key string, val map[string]interface{}) { + m.Log_DELETE("prefix", prefix, field, value, "value", kit.Format(val)) + m.Conf(prefix, kit.Keys(chain, kit.MDB_HASH, key), "") + }) +} +func _hash_export(m *ice.Message, prefix, key, file string) { + f, p, e := kit.Create(kit.Keys(file, JSON)) m.Assert(e) - defer _story_catch(m, CSV, file) defer f.Close() - r := csv.NewReader(f) + en := json.NewEncoder(f) + en.SetIndent("", " ") + en.Encode(m.Confv(prefix, kit.Keys(key, HASH))) + m.Log_EXPORT(kit.MDB_FILE, p) + m.Echo(p) +} +func _hash_import(m *ice.Message, prefix, key, file string) { + f, e := os.Open(kit.Keys(file, JSON)) + m.Assert(e) + defer f.Close() + + list := map[string]interface{}{} + de := json.NewDecoder(f) + de.Decode(&list) count := 0 - head, _ := r.Read() - for { - line, e := r.Read() - if e != nil { - break - } - - data := kit.Dict() - for i, k := range head { - if k == kit.MDB_EXTRA { - data[k] = kit.UnMarshal(line[i]) - } else { - data[k] = line[i] - } - } - + for _, data := range list { // 导入数据 - m.Grow(prefix, key, data) + m.Rich(prefix, key, data) count++ } m.Log_IMPORT(kit.MDB_KEY, kit.Keys(prefix, key), kit.MDB_COUNT, count) + m.Echo(kit.Keys(file, JSON)) +} +func _hash_select(m *ice.Message, prefix, key, field, value string) { + fields := strings.Split(kit.Select("time,name", m.Option("fields")), ",") + m.Richs(prefix, key, value, func(key string, val map[string]interface{}) { + if value == kit.MDB_FOREACH { + m.Push(key, val, fields) + return + } + if field != "" && value != val[field] { + return + } + m.Push("detail", val) + }) +} +func _hash_search(m *ice.Message, prefix, key, field, value string) { + m.Richs(prefix, key, kit.MDB_FOREACH, func(key string, val map[string]interface{}) { + if field != "" && value != val[field] { + return + } + m.Push(key, value) + }) +} + +func _list_insert(m *ice.Message, prefix, key string, arg ...string) int { + m.Log_INSERT("prefix", prefix, arg[0], arg[1]) + return m.Grow(prefix, key, kit.Dict(arg)) +} +func _list_modify(m *ice.Message, prefix, key string, field, value string, arg ...string) { + m.Grows(prefix, key, field, value, func(index int, value map[string]interface{}) { + for i := 0; i < len(arg)-1; i += 2 { + if arg[i] == field { + continue + } + kit.Value(value, arg[i], arg[i+1]) + } + m.Log_MODIFY("prefix", prefix, field, value, kit.Format(arg)) + }) +} +func _list_delete(m *ice.Message, prefix, chain, field, value string) { } func _list_export(m *ice.Message, prefix, key, file string) { f, p, e := kit.Create(kit.Keys(file, CSV)) m.Assert(e) - defer _story_catch(m, CSV, p) defer f.Close() w := csv.NewWriter(f) @@ -76,14 +137,52 @@ func _list_export(m *ice.Message, prefix, key, file string) { count++ }) m.Log_EXPORT(kit.MDB_FILE, p, kit.MDB_COUNT, count) + m.Echo(p) +} +func _list_import(m *ice.Message, prefix, key, file string) { + f, e := os.Open(kit.Keys(file, CSV)) + m.Assert(e) + defer f.Close() + + r := csv.NewReader(f) + + count := 0 + head, _ := r.Read() + for { + line, e := r.Read() + if e != nil { + break + } + + data := kit.Dict() + for i, k := range head { + if k == kit.MDB_EXTRA { + data[k] = kit.UnMarshal(line[i]) + } else { + data[k] = line[i] + } + } + + // 导入数据 + m.Grow(prefix, key, data) + count++ + } + m.Log_IMPORT(kit.MDB_KEY, kit.Keys(prefix, key), kit.MDB_COUNT, count) + m.Echo(kit.Keys(file, CSV)) } func _list_select(m *ice.Message, prefix, key, limit, offend, field, value string) { m.Option("cache.limit", limit) m.Option("cache.offend", offend) + fields := strings.Split(kit.Select("time,name", m.Option("fields")), ",") m.Grows(prefix, key, field, value, func(index int, value map[string]interface{}) { - m.Push("", value) + m.Push("", value, fields) }) } + +func _story_catch(m *ice.Message, kind, file string) { + m.Cmdy("web.story", "catch", kind, file) +} + func _list_search(m *ice.Message, prefix, key, field, value string) { list := []interface{}{} files := map[string]bool{} @@ -112,43 +211,6 @@ func _list_search(m *ice.Message, prefix, key, field, value string) { }) } -func _hash_search(m *ice.Message, prefix, key, field, value string) { - m.Richs(prefix, key, kit.MDB_FOREACH, func(key string, val map[string]interface{}) { - if field != "" && value != val[field] { - return - } - m.Push(key, value) - }) -} -func _hash_import(m *ice.Message, prefix, key, file string) { - f, e := os.Open(file) - m.Assert(e) - defer _story_catch(m, JSON, file) - defer f.Close() - - list := map[string]interface{}{} - de := json.NewDecoder(f) - de.Decode(&list) - - count := 0 - for _, data := range list { - // 导入数据 - m.Rich(prefix, key, data) - count++ - } - m.Log_IMPORT(kit.MDB_KEY, kit.Keys(prefix, key), kit.MDB_COUNT, count) -} -func _hash_export(m *ice.Message, prefix, key, file string) { - f, p, e := kit.Create(kit.Keys(file, JSON)) - m.Assert(e) - defer _story_catch(m, JSON, p) - defer f.Close() - - en := json.NewEncoder(f) - en.SetIndent("", " ") - en.Encode(m.Confv(prefix, kit.Keys(key, HASH))) - m.Log_EXPORT(kit.MDB_FILE, p) -} func _dict_import(m *ice.Message, prefix, key, file string) { f, e := os.Open(file) m.Assert(e) @@ -201,9 +263,9 @@ const ( EXPORT = "export" INSERT = "insert" + MODIFY = "modify" DELETE = "delete" SELECT = "select" - MODIFY = "modify" ) var Index = &ice.Context{Name: "mdb", Help: "数据模块", @@ -211,38 +273,66 @@ var Index = &ice.Context{Name: "mdb", Help: "数据模块", ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {}}, ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {}}, - IMPORT: {Name: "import conf key type file", Help: "导入数据", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + INSERT: {Name: "insert conf key type arg...", Help: "添加", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { switch arg[2] { - case LIST: - _list_import(m, arg[0], arg[1], arg[3]) case HASH: - _hash_import(m, arg[0], arg[1], arg[3]) - case DICT: - _dict_import(m, arg[0], arg[1], arg[3]) + m.Echo(_hash_insert(m, arg[0], arg[1], arg[3:]...)) + case LIST: + m.Echo("%d", _list_insert(m, arg[0], arg[1], arg[3:]...)) + } + }}, + MODIFY: {Name: "modify conf key type field value arg...", Help: "编辑", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + switch arg[2] { + case HASH: + _hash_modify(m, arg[0], arg[1], arg[3], arg[4], arg[5:]...) + case LIST: + _list_modify(m, arg[0], arg[1], arg[3], arg[4], arg[5:]...) + } + }}, + DELETE: {Name: "delete conf key type field value arg...", Help: "删除", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + switch arg[2] { + case HASH: + _hash_delete(m, arg[0], arg[1], arg[3], arg[4]) + case LIST: + _list_delete(m, arg[0], arg[1], arg[3], arg[4]) } }}, EXPORT: {Name: "export conf key type [name]", Help: "导出数据", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - switch file := kit.Select(kit.Select(arg[0], arg[0]+":"+arg[1], arg[1] != ""), arg, 3); arg[2] { - case LIST: - _list_export(m, arg[0], arg[1], file) + switch file := _file_name(m, arg...); arg[2] { case HASH: _hash_export(m, arg[0], arg[1], file) + case LIST: + _list_export(m, arg[0], arg[1], file) case DICT: _dict_export(m, arg[0], arg[1], file) } }}, + IMPORT: {Name: "import conf key type file", Help: "导入数据", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + switch file := _file_name(m, arg...); arg[2] { + case HASH: + _hash_import(m, arg[0], arg[1], file) + case LIST: + _list_import(m, arg[0], arg[1], file) + case DICT: + _dict_import(m, arg[0], arg[1], file) + } + }}, - SELECT: {Name: "select conf key type [limit [offend [key value]]]", Help: "数据查询", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + SELECT: {Name: "select conf key type field value", Help: "数据查询", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { switch arg[2] { case LIST: _list_select(m, arg[0], arg[1], kit.Select("10", arg, 3), kit.Select("0", arg, 4), kit.Select("", arg, 5), kit.Select("", arg, 6)) + case HASH: + _hash_select(m, arg[0], arg[1], arg[3], arg[4]) } }}, }, } func init() { - ice.Index.Register(Index, nil, IMPORT, EXPORT, SELECT, + ice.Index.Register(Index, nil, + IMPORT, EXPORT, SELECT, PLUGIN, ENGINE, SEARCH, RENDER, + INSERT, MODIFY, DELETE, ) } diff --git a/base/web/render.go b/base/web/render.go index f75b3da8..34aa6a71 100644 --- a/base/web/render.go +++ b/base/web/render.go @@ -126,7 +126,7 @@ func init() { m.Echo(`%s`, u, arg[0]) }}, RENDER.IMG: {Hand: func(m *ice.Message, arg ...string) { - m.Echo(``, arg[0]) + m.Echo(``, arg[0], kit.Select("120", arg, 1)) }}, RENDER.Field: {Hand: func(m *ice.Message, arg ...string) { m.Echo(`
%s(%s)
`, arg[0], arg[1]) diff --git a/core/chat/meet.go b/core/chat/meet.go new file mode 100644 index 00000000..4f6dce86 --- /dev/null +++ b/core/chat/meet.go @@ -0,0 +1,117 @@ +package chat + +import ( + "path" + + ice "github.com/shylinux/icebergs" + "github.com/shylinux/icebergs/base/mdb" + "github.com/shylinux/icebergs/base/web" + kit "github.com/shylinux/toolkits" +) + +const MEET = "meet" +const ( + MISS = "miss" + DATE = "date" +) + +var _miss_select = "time,name,性别,年龄,籍贯,学历,职业,照片" +var _miss_insert = kit.List( + "_input", "text", "name", "name", + "_input", "text", "name", "性别", + "_input", "text", "name", "年龄", + "_input", "text", "name", "生日", + "_input", "text", "name", "照片", + "_input", "text", "name", "身高", + "_input", "text", "name", "体重", + "_input", "text", "name", "性格", + "_input", "text", "name", "爱好", + "_input", "text", "name", "籍贯", + "_input", "text", "name", "户口", + "_input", "text", "name", "学历", + "_input", "text", "name", "学校", + "_input", "text", "name", "职业", + "_input", "text", "name", "年薪", + "_input", "text", "name", "资产", +) + +var _date_select = "time,id,name,地点,主题" +var _date_insert = kit.List( + "_input", "text", "name", "name", + "_input", "text", "name", "地点", + "_input", "text", "name", "主题", +) + +func init() { + Index.Register(&ice.Context{Name: MEET, Help: "遇见", + Configs: map[string]*ice.Config{ + MISS: {Name: MISS, Help: "miss", Value: kit.Data(kit.MDB_SHORT, kit.MDB_NAME)}, + DATE: {Name: DATE, Help: "date", Value: kit.Data(kit.MDB_SHORT, kit.MDB_NAME)}, + }, + Commands: map[string]*ice.Command{ + ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + m.Load(MISS, DATE) + }}, + ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + m.Save(MISS, DATE) + }}, + + MISS: {Name: "miss name=auto auto 添加:button 导出:button 导入:button", Help: "miss", Meta: kit.Dict( + "display", "", "insert", _miss_insert, + ), Action: map[string]*ice.Action{ + mdb.INSERT: {Name: "insert [key value]...", Help: "添加", Hand: func(m *ice.Message, arg ...string) { + m.Cmdy(mdb.INSERT, m.Prefix(MISS), "", mdb.HASH, arg) + }}, + mdb.MODIFY: {Name: "modify key value", Help: "编辑", Hand: func(m *ice.Message, arg ...string) { + m.Cmdy(mdb.MODIFY, m.Prefix(MISS), "", mdb.HASH, kit.MDB_NAME, m.Option("name"), arg[0], arg[1]) + }}, + mdb.DELETE: {Name: "delete", Help: "删除", Hand: func(m *ice.Message, arg ...string) { + m.Cmdy(mdb.DELETE, m.Prefix(MISS), "", mdb.HASH, kit.MDB_NAME, m.Option("name")) + }}, + mdb.EXPORT: {Name: "export file", Help: "导出", Hand: func(m *ice.Message, arg ...string) { + m.Cmdy(mdb.EXPORT, m.Prefix(MISS), "", mdb.HASH) + }}, + mdb.IMPORT: {Name: "import file", Help: "导入", Hand: func(m *ice.Message, arg ...string) { + m.Cmdy(mdb.IMPORT, m.Prefix(MISS), "", mdb.HASH) + }}, + }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + m.Option("fields", _miss_select) + if msg := m.Cmd(mdb.SELECT, m.Prefix(MISS), "", mdb.HASH, kit.MDB_NAME, kit.Select(kit.MDB_FOREACH, arg, 0)); len(arg) == 0 { + msg.Table(func(index int, value map[string]string, head []string) { + for _, k := range head { + if k == "照片" { + m.Push("照片", m.Cmdx(mdb.RENDER, web.RENDER.IMG, path.Join("/share/local", value["照片"]))) + } else { + m.Push(k, value[k]) + } + } + }) + m.PushAction("删除") + } + }}, + + DATE: {Name: "date name=auto auto 添加:button 导出:button 导入:button", Help: "date", Meta: kit.Dict( + "display", "", "insert", _date_insert, + ), Action: map[string]*ice.Action{ + mdb.INSERT: {Name: "insert [key value]...", Help: "添加", Hand: func(m *ice.Message, arg ...string) { + m.Cmdy(mdb.INSERT, m.Prefix(DATE), "", mdb.LIST, arg) + }}, + mdb.MODIFY: {Name: "modify key value", Help: "编辑", Hand: func(m *ice.Message, arg ...string) { + m.Cmdy(mdb.MODIFY, m.Prefix(DATE), "", mdb.LIST, kit.MDB_ID, m.Option("id"), arg[0], arg[1]) + }}, + mdb.DELETE: {Name: "delete", Help: "删除", Hand: func(m *ice.Message, arg ...string) { + // m.Cmdy(mdb.DELETE, m.Prefix(DATE), "", mdb.LIST, kit.MDB_NAME, m.Option("name")) + }}, + mdb.EXPORT: {Name: "export file", Help: "导出", Hand: func(m *ice.Message, arg ...string) { + m.Cmdy(mdb.EXPORT, m.Prefix(DATE), "", mdb.LIST) + }}, + mdb.IMPORT: {Name: "import file", Help: "导入", Hand: func(m *ice.Message, arg ...string) { + m.Cmdy(mdb.IMPORT, m.Prefix(DATE), "", mdb.LIST) + }}, + }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + m.Option("fields", _date_select) + m.Cmdy(mdb.SELECT, m.Prefix(DATE), "", mdb.LIST) + }}, + }, + }, nil) +} diff --git a/core/wiki/feel.go b/core/wiki/feel.go index 943c5941..aef98c26 100644 --- a/core/wiki/feel.go +++ b/core/wiki/feel.go @@ -56,6 +56,9 @@ func init() { if !_wiki_list(m, FEEL, kit.Select("./", arg, 0)) { _feel_show(m, arg[0]) } + m.Table(func(index int, value map[string]string, head []string) { + m.Push("show", m.Cmdx(mdb.RENDER, web.RENDER.IMG, path.Join("/share/local", value["path"]))) + }) m.Sort(kit.MDB_TIME, "time_r") }}, }, diff --git a/logs.go b/logs.go index c4763a90..a1387518 100644 --- a/logs.go +++ b/logs.go @@ -21,7 +21,7 @@ func (m *Message) log(level string, str string, arg ...interface{}) *Message { // 日志颜色 prefix, suffix := "", "" switch level { - case LOG_ENABLE, LOG_IMPORT, LOG_CREATE, LOG_INSERT, LOG_EXPORT: + case LOG_ENABLE, LOG_IMPORT, LOG_CREATE, LOG_INSERT, LOG_MODIFY, LOG_EXPORT: prefix, suffix = "\033[36;44m", "\033[0m" case LOG_LISTEN, LOG_SIGNAL, LOG_TIMERS, LOG_EVENTS: diff --git a/misc/wx/wx.shy b/misc/wx/wx.shy index 8bbbd796..ea434f58 100644 --- a/misc/wx/wx.shy +++ b/misc/wx/wx.shy @@ -8,7 +8,12 @@ refer "" ` ` image qrcode `https://weixin.qq.com` + chapter "应用" +field "feel" web.wiki.feel args `[ usr/export/web.chat.meet.miss/feel/ ]` +field "miss" web.chat.meet.miss args `[ ]` +field "date" web.chat.meet.date args `[ ]` + field "user" user field "sess" sess