From b77ef72cc6ec9d2e2e1e318a242d50d0e091cb58 Mon Sep 17 00:00:00 2001 From: shylinux Date: Mon, 27 Jul 2020 09:11:51 +0800 Subject: [PATCH] opt mp --- base/aaa/sess.go | 10 +++++-- base/aaa/user.go | 18 ++++++++++- base/web/serve.go | 21 ++++++++----- core/chat/location.go | 1 + core/chat/river.go | 4 ++- misc/lark/lark.go | 70 +++++++++++++++++++++++++++---------------- misc/lark/lark.shy | 5 ++-- misc/mp/mp.go | 47 ++++++++++++++--------------- misc/mp/mp.shy | 6 ---- misc/wx/wx.go | 19 ++++-------- misc/wx/wx.shy | 19 ++++++++++++ 11 files changed, 138 insertions(+), 82 deletions(-) create mode 100644 misc/wx/wx.shy diff --git a/base/aaa/sess.go b/base/aaa/sess.go index bc4eb279..c06a41e5 100644 --- a/base/aaa/sess.go +++ b/base/aaa/sess.go @@ -27,11 +27,17 @@ func _sess_auth(m *ice.Message, sessid string, username string, userrole string) func _sess_check(m *ice.Message, sessid string) { m.Richs(SESS, nil, sessid, func(value map[string]interface{}) { m.Richs(USER, nil, value[USERNAME], func(value map[string]interface{}) { - m.Option(ice.MSG_USERNICK, value[USERNICK]) + if m.Option(ice.MSG_USERNICK, value[USERNICK]) == "" { + if name := kit.Format(value[USERNAME]); len(name) > 10 { + m.Option(ice.MSG_USERNICK, name[:10]) + } else { + m.Option(ice.MSG_USERNICK, value[USERNAME]) + } + } }) m.Log_AUTH( USERNAME, m.Option(ice.MSG_USERNAME, value[USERNAME]), - USERROLE, m.Option(ice.MSG_USERROLE, kit.Select(UserRole(m, value[USERNAME]), value[USERROLE])), + USERROLE, m.Option(ice.MSG_USERROLE, kit.Select(UserRole(m, value[USERNAME]))), USERNICK, m.Option(ice.MSG_USERROLE), ) }) diff --git a/base/aaa/user.go b/base/aaa/user.go index d7422729..c7d0f27a 100644 --- a/base/aaa/user.go +++ b/base/aaa/user.go @@ -14,8 +14,13 @@ func _user_list(m *ice.Message) { m.Richs(USER, nil, kit.MDB_FOREACH, func(key string, value map[string]interface{}) { m.Push(key, value, []string{kit.MDB_TIME, USERNICK, USERNAME}) }) + m.Sort(USERNAME) } func _user_login(m *ice.Message, name, word string) (ok bool) { + if m.Richs(USER, nil, name, nil) == nil { + _user_create(m, name, "") + } + m.Richs(USER, nil, name, func(key string, value map[string]interface{}) { if value[PASSWORD] == "" { ok, value[PASSWORD] = true, word @@ -67,6 +72,12 @@ func UserRoot(m *ice.Message) { cli.PassWord = cli.UserName _user_create(m, cli.UserName, cli.PassWord) } +func UserNick(m *ice.Message, username interface{}) (nick string) { + m.Richs(USER, nil, kit.Format(username), func(key string, value map[string]interface{}) { + nick = kit.Format(value[USERNICK]) + }) + return +} func UserRole(m *ice.Message, username interface{}) (role string) { if role = VOID; username == cli.UserName { return ROOT @@ -82,6 +93,7 @@ func UserLogin(m *ice.Message, username, password string) bool { if _user_login(m, username, password) { m.Option(ice.MSG_USERNAME, username) m.Option(ice.MSG_USERROLE, UserRole(m, username)) + m.Info("%s: %s", m.Option(ice.MSG_USERROLE), m.Option(ice.MSG_USERNAME)) return true } return false @@ -99,7 +111,11 @@ func init() { mdb.CREATE: {Name: "create username [password]", Help: "创建", Hand: func(m *ice.Message, arg ...string) { _user_create(m, arg[0], kit.Select("", arg, 1)) }}, - mdb.MODIFY: {Name: "create username [key value]...", Help: "创建", Hand: func(m *ice.Message, arg ...string) { + mdb.MODIFY: {Name: "modify username [key value]...", Help: "编辑", Hand: func(m *ice.Message, arg ...string) { + if len(arg) == 2 { + _user_modify(m, m.Option("username"), arg[0], arg[1]) + return + } _user_modify(m, arg[0], arg[1:]...) }}, mdb.SEARCH: {Name: "search type name text arg...", Help: "搜索", Hand: func(m *ice.Message, arg ...string) { diff --git a/base/web/serve.go b/base/web/serve.go index 3ea8a874..bd00f7e8 100644 --- a/base/web/serve.go +++ b/base/web/serve.go @@ -22,6 +22,7 @@ func _serve_login(msg *ice.Message, cmds []string, w http.ResponseWriter, r *htt msg.Option(ice.MSG_USERNAME, "") msg.Option(ice.MSG_USERROLE, "") + msg.Debug("what %v", msg.Option(ice.MSG_SESSID)) if msg.Options(ice.MSG_SESSID) { // 会话认证 aaa.SessCheck(msg, msg.Option(ice.MSG_SESSID)) @@ -63,6 +64,14 @@ func _serve_login(msg *ice.Message, cmds []string, w http.ResponseWriter, r *htt } func _serve_handle(key string, cmd *ice.Command, msg *ice.Message, w http.ResponseWriter, r *http.Request) { defer func() { msg.Cost("%s %v %v", r.URL.Path, msg.Optionv(ice.MSG_CMDS), msg.Format("append")) }() + + // 请求变量 + msg.Option(ice.MSG_SESSID, "") + for _, v := range r.Cookies() { + msg.Option(v.Name, v.Value) + } + + msg.Option(ice.MSG_OUTPUT, "") if u, e := url.Parse(r.Header.Get("Referer")); e == nil { for k, v := range u.Query() { msg.Logs("refer", k, v) @@ -81,13 +90,6 @@ func _serve_handle(key string, cmd *ice.Command, msg *ice.Message, w http.Respon msg.Option(ice.MSG_USERADDR, r.RemoteAddr) } - // 请求变量 - msg.Option(ice.MSG_SESSID, "") - msg.Option(ice.MSG_OUTPUT, "") - for _, v := range r.Cookies() { - msg.Option(v.Name, v.Value) - } - // 解析引擎 switch r.Header.Get("Content-Type") { case "application/json": @@ -119,12 +121,14 @@ func _serve_handle(key string, cmd *ice.Command, msg *ice.Message, w http.Respon } } + msg.Debug("what %v", msg.Option(ice.MSG_SESSID)) // 请求命令 if msg.Option(ice.MSG_USERPOD, msg.Option("pod")); msg.Optionv(ice.MSG_CMDS) == nil { if p := strings.TrimPrefix(msg.Option(ice.MSG_USERURL), key); p != "" { msg.Optionv(ice.MSG_CMDS, strings.Split(p, "/")) } } + msg.Debug("what %v", msg.Option(ice.MSG_SESSID)) // 执行命令 if cmds, ok := _serve_login(msg, kit.Simple(msg.Optionv(ice.MSG_CMDS)), w, r); ok { @@ -185,6 +189,9 @@ func _serve_main(m *ice.Message, w http.ResponseWriter, r *http.Request) bool { m.Event(gdb.SYSTEM_INIT) m.W = nil } else if r.URL.Path == "/" && m.Conf(SERVE, "meta.sso") != "" { + if r.ParseForm(); r.FormValue(ice.MSG_SESSID) != "" { + return true + } if c, e := r.Cookie(ice.MSG_SESSID); e != nil || c.Value == "" { http.Redirect(w, r, m.Conf(SERVE, "meta.sso"), http.StatusTemporaryRedirect) return false diff --git a/core/chat/location.go b/core/chat/location.go index 5fe51d32..83cf1e9c 100644 --- a/core/chat/location.go +++ b/core/chat/location.go @@ -81,6 +81,7 @@ func init() { if len(arg) == 0 { m.Push(key, value, []string{kit.MDB_TIME, kit.MDB_TYPE, kit.MDB_NAME, kit.MDB_TEXT, LONGITUDE, LATITUDE}) + m.Debug(m.Option(ice.MSG_USERUA)) if strings.Contains(m.Option(ice.MSG_USERUA), "MicroMessenger") { return } diff --git a/core/chat/river.go b/core/chat/river.go index f0a59635..2c8f0390 100644 --- a/core/chat/river.go +++ b/core/chat/river.go @@ -124,7 +124,9 @@ func init() { _river_user(m, m.Option(ice.MSG_RIVER), arg...) }}, }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - m.Cmd(aaa.USER, mdb.MODIFY, m.Option(ice.MSG_USERNAME), aaa.USERNICK, m.Option(ice.MSG_USERNICK)) + if m.Option(ice.MSG_USERNICK) != "" { + m.Cmd(aaa.USER, mdb.MODIFY, m.Option(ice.MSG_USERNAME), aaa.USERNICK, m.Option(ice.MSG_USERNICK)) + } if len(arg) > 0 && arg[0] == "storm" { m.Cmdy("/storm", arg[1:]) return diff --git a/misc/lark/lark.go b/misc/lark/lark.go index 3056bf86..35f1408b 100644 --- a/misc/lark/lark.go +++ b/misc/lark/lark.go @@ -288,17 +288,13 @@ var Index = &ice.Context{Name: "lark", Help: "机器人", post(m, "bot", "/open-apis/message/v4/send/", "data", kit.Formats(form)) }}, TALK: {Name: "talk text", Help: "聊天", Hand: func(m *ice.Message, c *ice.Context, key string, arg ...string) { - cmd := kit.Split(arg[0]) - if len(cmd) == 0 { - m.Cmd(HOME) - return - } - // 用户权限 m.Option(ice.MSG_USERNAME, m.Option(OPEN_ID)) + m.Option(ice.MSG_USERNICK, aaa.UserNick(m, m.Option(ice.MSG_USERNAME))) m.Option(ice.MSG_USERROLE, aaa.UserRole(m, m.Option(ice.MSG_USERNAME))) m.Info("%s: %s", m.Option(ice.MSG_USERROLE), m.Option(ice.MSG_USERNAME)) + cmd := kit.Split(arg[0]) if !m.Right(cmd) { // 群组权限 m.Option(ice.MSG_USERNAME, m.Option(OPEN_CHAT_ID)) @@ -312,6 +308,10 @@ var Index = &ice.Context{Name: "lark", Help: "机器人", return } } + if len(cmd) == 0 { + m.Cmd(HOME) + return + } // 执行命令 msg := m.Cmd(cmd) @@ -336,12 +336,38 @@ var Index = &ice.Context{Name: "lark", Help: "机器人", m.Echo(list[rand.Intn(len(list))]) }}, HOME: {Name: "home", Help: "首页", Hand: func(m *ice.Message, c *ice.Context, key string, arg ...string) { - m.Cmd(FORM, CHAT_ID, m.Option(OPEN_CHAT_ID), HOME, "应用列表", - "官网首页", "url", "https://shylinux.com", - "群里誰最帅", "cmd", "rand", - "群里誰最美", "cmd", "rand", - "date", "cmd", "date", - "pwd", "cmd", "pwd", + list := []string{} + name := kit.Select(m.Option(ice.MSG_USERNAME), m.Option(ice.MSG_USERNICK)) + if len(name) > 10 { + name = name[:10] + } + name += "的应用列表" + link := "https://shylinux.com" + text := "" + if len(arg) == 0 { + m.Option("_source", ".") + m.Cmd("web.chat./river").Table(func(index int, val map[string]string, head []string) { + m.Option(ice.MSG_RIVER, val["key"]) + m.Cmd("web.chat./storm").Table(func(index int, value map[string]string, head []string) { + list = append(list, val["name"]+"."+value["name"], "cmd", "home "+val["key"]+" "+value["key"]) + }) + }) + } else { + m.Option(ice.MSG_RIVER, arg[0]) + m.Option(ice.MSG_STORM, arg[1]) + m.Richs(chat.RIVER, nil, arg[0], func(key string, val map[string]interface{}) { + m.Richs(chat.RIVER, kit.Keys(kit.MDB_HASH, arg[0], chat.TOOL), arg[1], func(key string, value map[string]interface{}) { + text = kit.Keys(kit.Value(val, "meta.name"), kit.Value(value, "meta.name")) + }) + }) + + m.Cmd("web.chat./action").Table(func(index int, value map[string]string, head []string) { + list = append(list, value["name"], "cmd", kit.Keys(value["group"], value["index"])) + }) + link = "https://shylinux.com?river=" + arg[0] + "&storm=" + arg[1] + } + m.Cmd(FORM, CHAT_ID, m.Option(OPEN_CHAT_ID), name, text, + "网页应用", "url", link, list, ) }}, FORM: {Name: "form chat_id|open_id|user_id|email user title [text [confirm|value|url arg...]]...", Help: "消息", Hand: func(m *ice.Message, c *ice.Context, key string, arg ...string) { @@ -384,7 +410,9 @@ var Index = &ice.Context{Name: "lark", Help: "机器人", button[arg[i+1]], i = arg[i+2], i+2 default: button["value"], i = map[string]interface{}{ - arg[i+1]: arg[i+2], + arg[i+1]: arg[i+2], + ice.MSG_RIVER: m.Option(ice.MSG_RIVER), + ice.MSG_STORM: m.Option(ice.MSG_STORM), }, i+2 } kit.Value(button, "value.content", content) @@ -438,22 +466,14 @@ var Index = &ice.Context{Name: "lark", Help: "机器人", "/msg": {Name: "/msg", Help: "聊天消息", Hand: func(m *ice.Message, c *ice.Context, key string, arg ...string) { data := m.Optionv(ice.MSG_USERDATA) if kit.Value(data, "action") != nil { + m.Option(ice.MSG_USERUA, "MicroMessenger") + kit.Fetch(kit.Value(data, "action.value"), func(key string, value string) { m.Option(key, value) }) - cmd := kit.Split(m.Option("cmd")) - if len(cmd) == 0 { - return - } - msg := m.Cmd(cmd) - if m.Hand = false; !msg.Hand { - msg = m.Cmd(cli.SYSTEM, cmd) - } - if m.Hand = true; msg.Result() == "" { - msg.Table() - } - m.Cmd(SEND, CHAT_ID, m.Option(OPEN_CHAT_ID), msg.Result()) + m.Cmdy(TALK, m.Option("cmd")) + m.Cmd(SEND, CHAT_ID, m.Option(OPEN_CHAT_ID), m.Result()) return } diff --git a/misc/lark/lark.shy b/misc/lark/lark.shy index 470f9560..5dbbf2f4 100644 --- a/misc/lark/lark.shy +++ b/misc/lark/lark.shy @@ -8,12 +8,11 @@ refer "官网" ` 源码 https://github.com/shylinux/icebergs/blob/master/misc/lark/lark.go ` +chapter "应用" field "ship" web.chat.lark.ship field "group" web.chat.lark.group -# field "date" web.chat.lark.date -# field "meta" web.chat.lark.meta -# +chapter "项目" section "icebergs" field "icebergs_统计" web.code.git.trend args `[ icebergs ]` action `{ height 200 speed 20 }` field "icebergs_源码" web.code.inner args `[ usr/icebergs misc/lark/lark.go 74 ]` diff --git a/misc/mp/mp.go b/misc/mp/mp.go index 6ae5927d..a457ae83 100644 --- a/misc/mp/mp.go +++ b/misc/mp/mp.go @@ -19,7 +19,6 @@ var Index = &ice.Context{Name: "mp", Help: "小程序", "auth", "/sns/jscode2session?grant_type=authorization_code", "weixin", "https://api.weixin.qq.com", "appid", "", "appmm", "", "token", "", - "userrole", kit.Dict(), )}, }, Commands: map[string]*ice.Command{ @@ -34,29 +33,6 @@ var Index = &ice.Context{Name: "mp", Help: "小程序", m.Save("login") }}, - "scan": {Name: "scan", Help: "扫码", List: kit.List( - kit.MDB_INPUT, "text", "name", "location", "cb", "location", - kit.MDB_INPUT, "text", "name", "battery", "cb", "battery", - kit.MDB_INPUT, "text", "name", "paste", "cb", "paste", - kit.MDB_INPUT, "text", "name", "scan", "cb", "scan", - kit.MDB_INPUT, "text", "name", "wifi", "cb", "wifi", - - kit.MDB_INPUT, "text", "name", "album", "cb", "album", - kit.MDB_INPUT, "text", "name", "finger", "cb", "finger", - kit.MDB_INPUT, "text", "name", "vibrate", "cb", "vibrate", - kit.MDB_INPUT, "text", "name", "wifiList", "cb", "wifiList", - kit.MDB_INPUT, "text", "name", "wifiConn", "cb", "wifiConn", - - kit.MDB_INPUT, "textarea", "name", "scan", "cb", "scan", - kit.MDB_INPUT, "textarea", "name", "location", "cb", "location", - kit.MDB_INPUT, "button", "name", "scan", "cb", "scan", - kit.MDB_INPUT, "button", "name", "location", "cb", "location", - kit.MDB_INPUT, "button", "name", "text", - kit.MDB_INPUT, "button", "name", "share", - ), Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - m.Echo(arg[0]) - }}, - "/login/": {Name: "/login/", Help: "登录", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { switch arg[0] { case "code": @@ -140,6 +116,29 @@ var Index = &ice.Context{Name: "mp", Help: "小程序", m.Cmdy(arg) } }}, + + "scan": {Name: "scan", Help: "扫码", List: kit.List( + kit.MDB_INPUT, "text", "name", "location", "cb", "location", + kit.MDB_INPUT, "text", "name", "battery", "cb", "battery", + kit.MDB_INPUT, "text", "name", "paste", "cb", "paste", + kit.MDB_INPUT, "text", "name", "scan", "cb", "scan", + kit.MDB_INPUT, "text", "name", "wifi", "cb", "wifi", + + kit.MDB_INPUT, "text", "name", "album", "cb", "album", + kit.MDB_INPUT, "text", "name", "finger", "cb", "finger", + kit.MDB_INPUT, "text", "name", "vibrate", "cb", "vibrate", + kit.MDB_INPUT, "text", "name", "wifiList", "cb", "wifiList", + kit.MDB_INPUT, "text", "name", "wifiConn", "cb", "wifiConn", + + kit.MDB_INPUT, "textarea", "name", "scan", "cb", "scan", + kit.MDB_INPUT, "textarea", "name", "location", "cb", "location", + kit.MDB_INPUT, "button", "name", "scan", "cb", "scan", + kit.MDB_INPUT, "button", "name", "location", "cb", "location", + kit.MDB_INPUT, "button", "name", "text", + kit.MDB_INPUT, "button", "name", "share", + ), Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + m.Echo(arg[0]) + }}, }, } diff --git a/misc/mp/mp.shy b/misc/mp/mp.shy index 9507214d..b4959b2d 100644 --- a/misc/mp/mp.shy +++ b/misc/mp/mp.shy @@ -7,12 +7,6 @@ refer "" ` ` chapter "应用" -field "user" user -field "qrcode" web.chat.qrcode.qrcode -field "qrcode" web.chat.qrcode.qrcode args `[ insert qrcode hi hello ]` - -section "二维码" -field "qrcode 源码" web.code.inner args `[ usr/icebergs core/code/qrcode.go 1 ]` chapter "项目" section "icebergs" diff --git a/misc/wx/wx.go b/misc/wx/wx.go index 4ddabc70..d7316822 100644 --- a/misc/wx/wx.go +++ b/misc/wx/wx.go @@ -4,6 +4,7 @@ import ( "github.com/shylinux/icebergs" "github.com/shylinux/icebergs/base/aaa" "github.com/shylinux/icebergs/base/cli" + "github.com/shylinux/icebergs/base/mdb" "github.com/shylinux/icebergs/base/web" "github.com/shylinux/icebergs/core/chat" "github.com/shylinux/toolkits" @@ -79,7 +80,6 @@ var Index = &ice.Context{Name: "wx", Help: "公众号", "auth", "/sns/jscode2session?grant_type=authorization_code", "weixin", "https://api.weixin.qq.com", "appid", "", "appmm", "", "token", "", - "userrole", kit.Dict(), "template", kit.Dict( "text", ` @@ -97,10 +97,8 @@ var Index = &ice.Context{Name: "wx", Help: "公众号", }, Commands: map[string]*ice.Command{ ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - m.Load() - m.Confm("login", "meta.userrole", func(key string, value string) { - m.Cmd(aaa.ROLE, value, key) - }) + m.Load("login") + m.Cmd(web.SPIDE, mdb.CREATE, "weixin", m.Conf("login", "meta.weixin")) }}, ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Save("login") @@ -109,13 +107,11 @@ var Index = &ice.Context{Name: "wx", Help: "公众号", "/login/": {Name: "/login/", Help: "认证", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { check := []string{m.Conf("login", "meta.token"), m.Option("timestamp"), m.Option("nonce")} sort.Strings(check) - if b := sha1.Sum([]byte(strings.Join(check, ""))); m.Warn(m.Option("signature") != hex.EncodeToString(b[:]), "error") { + if b := sha1.Sum([]byte(strings.Join(check, ""))); m.Warn(m.Option("signature") != hex.EncodeToString(b[:]), "signature error") { // 验证失败 return } - - if m.Option("echostr") != "" { - // 绑定验证 + if m.Option("echostr") != "" { // 绑定验证 m.Render(m.Option("echostr")) return } @@ -124,10 +120,7 @@ var Index = &ice.Context{Name: "wx", Help: "公众号", parse(m) // 用户登录 - m.Option(ice.MSG_USERNAME, m.Option("FromUserName")) - m.Option(ice.MSG_USERROLE, m.Cmdx(aaa.ROLE, "check", m.Option("FromUserName"))) - m.Info("%s: %s", m.Option(ice.MSG_USERROLE), m.Option(ice.MSG_USERNAME)) - m.Option(ice.MSG_SESSID, m.Cmdx(aaa.USER, "login", m.Option(ice.MSG_USERNAME))) + m.Option(ice.MSG_SESSID, aaa.SessCreate(m, m.Append("FromUserName"), aaa.UserRole(m, m.Append("FromUserName")))) switch m.Option("MsgType") { case "text": diff --git a/misc/wx/wx.shy b/misc/wx/wx.shy new file mode 100644 index 00000000..2b761259 --- /dev/null +++ b/misc/wx/wx.shy @@ -0,0 +1,19 @@ +title "微信公众号" + +refer "" ` +官网 https://weixin.qq.com/ +后台 https://mp.weixin.qq.com/ +文档 https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html +源码 https://github.com/shylinux/icebergs/blob/master/misc/wx/wx.go +` + +chapter "应用" +field "user" user +field "sess" sess + +chapter "项目" +section "icebergs" +field "icebergs_统计" web.code.git.trend args `[ icebergs ]` action `{ height 200 speed 20 }` +field "icebergs_源码" web.code.inner args `[ usr/icebergs misc/wx/wx.go 1 ]` +field "icebergs_项目" web.code.git.spide args `[ icebergs ]` +