From 3fe511f14a0d103c3b351e54632d55b14e6935c6 Mon Sep 17 00:00:00 2001 From: shy Date: Sat, 23 Jul 2022 00:03:04 +0800 Subject: [PATCH] add location.go --- base/aaa/totp.go | 2 ++ base/cli/daemon.go | 4 +++ base/ctx/command.go | 5 +-- base/mdb/hash.go | 12 ++++--- base/mdb/mdb.go | 5 +++ core/chat/location.go | 73 +++++++++++++++++++++++------------------- core/chat/location.shy | 8 +++++ 7 files changed, 70 insertions(+), 39 deletions(-) create mode 100644 core/chat/location.shy diff --git a/base/aaa/totp.go b/base/aaa/totp.go index e6c35018..ee67def5 100644 --- a/base/aaa/totp.go +++ b/base/aaa/totp.go @@ -49,6 +49,8 @@ const ( SECRET = "secret" PERIOD = "period" NUMBER = "number" + + TOKEN = "token" ) const TOTP = "totp" diff --git a/base/cli/daemon.go b/base/cli/daemon.go index fccc20b1..e6987ea6 100644 --- a/base/cli/daemon.go +++ b/base/cli/daemon.go @@ -1,6 +1,7 @@ package cli import ( + "io" "os/exec" ice "shylinux.com/x/icebergs" @@ -11,6 +12,9 @@ import ( ) func _daemon_exec(m *ice.Message, cmd *exec.Cmd) { + if r, ok := m.Optionv(CMD_INPUT).(io.Reader); ok { + cmd.Stdin = r + } if w := _system_out(m, CMD_OUTPUT); w != nil { cmd.Stdout = w cmd.Stderr = w diff --git a/base/ctx/command.go b/base/ctx/command.go index 8d916e51..9efdb3d3 100644 --- a/base/ctx/command.go +++ b/base/ctx/command.go @@ -80,8 +80,9 @@ func CmdAction(args ...ice.Any) ice.Actions { } }}, ice.RUN: {Name: "run", Help: "执行", Hand: func(m *ice.Message, arg ...string) { - if len(arg) > 3 && arg[1] == "action" && arg[2] == "config" && arg[3] == "reset" { - m.Cmd("config", "reset", arg[0]) + if len(arg) > 3 && arg[1] == ACTION && arg[2] == CONFIG && arg[3] == "reset" { + m.Cmd(CONFIG, "reset", arg[0]) + return } if m.Right(arg) && !m.PodCmd(arg) { m.Cmdy(arg) diff --git a/base/mdb/hash.go b/base/mdb/hash.go index 7ef294d4..856b7303 100644 --- a/base/mdb/hash.go +++ b/base/mdb/hash.go @@ -4,6 +4,7 @@ import ( "encoding/json" "os" "path" + "strings" ice "shylinux.com/x/icebergs" kit "shylinux.com/x/toolkits" @@ -147,7 +148,7 @@ const HASH = "hash" func AutoConfig(args ...ice.Any) *ice.Action { return &ice.Action{Hand: func(m *ice.Message, arg ...string) { - if cs := m.Target().Configs; cs[m.CommandKey()] == nil { + if cs := m.Target().Configs; cs[m.CommandKey()] == nil && len(args) > 0 { cs[m.CommandKey()] = &ice.Config{Value: kit.Data(args...)} m.Load(m.CommandKey()) } @@ -166,17 +167,14 @@ func AutoConfig(args ...ice.Any) *ice.Action { return } - m.Debug("what %v %v", m.CommandKey(), cs[m.CommandKey()].Actions[INSERT]) if cs[m.CommandKey()].Actions[INSERT] != nil { if cs[m.CommandKey()].Meta[INSERT] == nil { m.Design(INSERT, "添加", append([]ice.Any{ZONE}, inputs...)...) } - m.Debug("what %v %v", m.CommandKey(), cs[m.CommandKey()].Actions[INSERT]) } else if cs[m.CommandKey()].Actions[CREATE] != nil { if cs[m.CommandKey()].Meta[CREATE] == nil { m.Design(CREATE, "创建", inputs...) } - m.Debug("what %v %v", m.CommandKey(), cs[m.CommandKey()].Actions[INSERT]) } }} } @@ -203,6 +201,12 @@ func HashAction(args ...ice.Any) ice.Actions { m.Event(kit.Keys(m.CommandKey(), REMOVE), m.CommandKey(), m.Option(m.Config(SHORT))) }}, MODIFY: {Name: "modify", Help: "编辑", Hand: func(m *ice.Message, arg ...string) { + field := m.Config(FIELD) + for i := 0; i < len(arg); i += 2 { + if !strings.Contains(field, arg[i]) { + arg[i] = kit.Keys("extra", arg[i]) + } + } m.Cmdy(MODIFY, m.PrefixKey(), "", HASH, m.OptionSimple(_key(m)), arg) }}, EXPORT: {Name: "export", Help: "导出", Hand: func(m *ice.Message, arg ...string) { diff --git a/base/mdb/mdb.go b/base/mdb/mdb.go index bb2c6212..753a3e99 100644 --- a/base/mdb/mdb.go +++ b/base/mdb/mdb.go @@ -191,6 +191,11 @@ var Index = &ice.Context{Name: MDB, Help: "数据模块", Commands: ice.Commands } }}, INPUTS: {Name: "inputs key sub type field value", Help: "补全", Hand: func(m *ice.Message, arg ...string) { + switch arg[3] { + case "index": + m.OptionFields(arg[3]) + m.Cmdy("command", "search", "command") + } switch arg[2] { case ZONE: // inputs key sub type zone field value _list_inputs(m, arg[0], _domain_chain(m, kit.Keys(arg[1], kit.KeyHash(arg[3]))), kit.Select(NAME, arg, 4), kit.Select("", arg, 5)) diff --git a/core/chat/location.go b/core/chat/location.go index 304b01bc..0337f7b6 100644 --- a/core/chat/location.go +++ b/core/chat/location.go @@ -1,52 +1,59 @@ package chat import ( - "math" + "sync" ice "shylinux.com/x/icebergs" + "shylinux.com/x/icebergs/base/aaa" + "shylinux.com/x/icebergs/base/ctx" "shylinux.com/x/icebergs/base/mdb" "shylinux.com/x/icebergs/base/web" kit "shylinux.com/x/toolkits" ) -func distance(lat1, long1, lat2, long2 float64) float64 { - lat1 = lat1 * math.Pi / 180 - long1 = long1 * math.Pi / 180 - lat2 = lat2 * math.Pi / 180 - long2 = long2 * math.Pi / 180 - return 2 * 6371 * math.Asin(math.Sqrt(math.Pow(math.Sin(math.Abs(lat1-lat2)/2), 2)+math.Cos(lat1)*math.Cos(lat2)*math.Pow(math.Sin(math.Abs(long1-long2)/2), 2))) -} -func _trans(arg []string, tr ice.Maps) { - for i := 0; i < len(arg)-1; i += 2 { - arg[i] = kit.Select(arg[i], tr[arg[i]]) - } -} - const ( LATITUDE = "latitude" LONGITUDE = "longitude" ) -const ( - GETLOCATION = "getLocation" - OPENLOCATION = "openLocation" -) + const LOCATION = "location" func init() { - Index.Merge(&ice.Context{Configs: ice.Configs{ - LOCATION: {Name: LOCATION, Help: "地理位置", Value: kit.Data( - mdb.SHORT, mdb.TEXT, mdb.FIELD, "time,hash,type,name,text,longitude,latitude", - )}, - }, Commands: ice.Commands{ - LOCATION: {Name: "location hash auto getLocation", Help: "地理位置", Actions: ice.MergeAction(ice.Actions{ - OPENLOCATION: {Name: "location", Help: "地图"}, - GETLOCATION: {Name: "location create", Help: "打卡"}, - mdb.CREATE: {Name: "create type=text name text latitude longitude", Help: "添加"}, - }, mdb.HashAction()), Hand: func(m *ice.Message, arg ...string) { - m.Debug("what %v", m.Cmdx(web.SPIDE_GET, "https://apis.map.qq.com/ws/location/v1/ip?ip=111.206.145.41&key="+m.Config("token"))) - m.Display("/plugin/local/chat/location.js", "token", m.Config("token")) - mdb.HashSelect(m, arg...) - m.PushAction(OPENLOCATION, mdb.REMOVE) + location := sync.Map{} + cache := func(m *ice.Message, key string, load func() string) ice.Any { + if current, ok := location.Load(key); ok { + m.Debug("read cache %v", key) + return current + } + current := load() + location.Store(key, current) + m.Debug("load cache %v %v", key, current) + return current + } + get := func(m *ice.Message, api string, arg ...ice.Any) string { + return kit.Format(cache(m, kit.Join(kit.Simple(api, arg)), func() string { + return m.Cmdx(web.SPIDE_GET, "https://apis.map.qq.com/ws/"+api, mdb.KEY, m.Config(aaa.TOKEN), arg) + })) + } + + Index.MergeCommands(ice.Commands{ + LOCATION: {Name: "location hash auto", Help: "地理位置", Actions: ice.MergeAction(ice.Actions{ + "explore": {Name: "explore", Help: "周边", Hand: func(m *ice.Message, arg ...string) { + m.Echo(get(m, "place/v1/explore", m.OptionSimple("boundary,page_index"))) + }}, + "search": {Name: "search", Help: "搜索", Hand: func(m *ice.Message, arg ...string) { + m.Echo(get(m, "place/v1/search", m.OptionSimple("keyword,boundary,page_index"))) + }}, + "direction": {Name: "direction", Help: "导航", Hand: func(m *ice.Message, arg ...string) { + m.Echo(get(m, "direction/v1/"+m.Option(mdb.TYPE)+ice.PS, m.OptionSimple("from,to"))) + }}, + "district": {Name: "district", Help: "地区", Hand: func(m *ice.Message, arg ...string) { + m.Echo(get(m, "district/v1/getchildren", m.OptionSimple(mdb.ID))) + }}, + }, mdb.HashAction(mdb.FIELD, "time,hash,type,name,text,latitude,longitude,extra"), ctx.CmdAction()), Hand: func(m *ice.Message, arg ...string) { + mdb.HashSelect(m, kit.Slice(arg, 0, 1)...) + m.DisplayLocal("", m.ConfigSimple(aaa.TOKEN)) + m.Option(LOCATION, get(m, "location/v1/ip", aaa.IP, m.Option(ice.MSG_USERIP))) }}, - }}) + }) } diff --git a/core/chat/location.shy b/core/chat/location.shy new file mode 100644 index 00000000..7902be78 --- /dev/null +++ b/core/chat/location.shy @@ -0,0 +1,8 @@ +section "location" +refer ` +后台 https://lbs.qq.com/dev/console/application/mine +后端 https://lbs.qq.com/service/webService/webServiceGuide/webServiceOverview +前端 https://lbs.qq.com/webApi/javascriptGL/glGuide/glBasic +手册 https://lbs.qq.com/webApi/javascriptGL/glDoc/docIndexMap +` +