From 885b787b3dfc7aeef20833c2eec70aa2d3062513 Mon Sep 17 00:00:00 2001 From: shaoying Date: Mon, 28 May 2018 22:34:14 +0800 Subject: [PATCH] mac add web.login --- src/contexts/ctx.go | 40 +++++++++- src/contexts/web/web.go | 125 +++++++++++++++++++++++++------ usr/library/context.js | 5 +- usr/template/common/login.html | 85 +++++++++++++++++++++ usr/template/upload.html | 130 +++++++++++++++++---------------- 5 files changed, 296 insertions(+), 89 deletions(-) create mode 100644 usr/template/common/login.html diff --git a/src/contexts/ctx.go b/src/contexts/ctx.go index 615772f9..8f7cde46 100644 --- a/src/contexts/ctx.go +++ b/src/contexts/ctx.go @@ -2311,7 +2311,7 @@ var Index = &Context{Name: "ctx", Help: "模块中心", "right": &Command{ Name: "right [share|add|del group [cache|config|command item]]", Help: "用户组管理,查看、添加、删除用户组或是接口", - Formats: map[string]int{"add": 0, "del": 0, "cache": 0, "config": 0, "command": 0}, + Formats: map[string]int{"check": 0, "add": 0, "del": 0, "cache": 0, "config": 0, "command": 0}, Hand: func(m *Message, c *Context, key string, arg ...string) { index := m.Target().Index // {{{ if index == nil { @@ -2356,6 +2356,33 @@ var Index = &Context{Name: "ctx", Help: "模块中心", } switch { + case m.Has("check"): + if group != nil { + switch { + case m.Has("cache"): + if _, ok := group.Caches[item]; ok { + m.Echo("ok") + } + case m.Has("config"): + if _, ok := group.Configs[item]; ok { + m.Echo("ok") + } + case m.Has("command"): + if x, ok := group.Commands[item]; ok { + if len(arg) > 2 { + if len(x.Options) > 0 { + for i := 2; i < len(arg)-1; i += 2 { + if x, ok := x.Options[arg[i]]; !ok || x != arg[i+1] { + return + } + } + } + } + m.Echo("ok") + } + } + } + return case m.Has("add"): if group == nil { if _, ok := index[arg[0]]; ok { @@ -2380,11 +2407,18 @@ var Index = &Context{Name: "ctx", Help: "模块中心", group.Configs[item] = x } case m.Has("command"): - if x, ok := current.Commands[item]; ok { + if _, ok := current.Commands[item]; ok { if group.Commands == nil { group.Commands = map[string]*Command{} } - group.Commands[item] = x + // group.Commands[item] = x + options := map[string]string{} + for i := 2; i < len(arg)-1; i += 2 { + options[arg[i]] = arg[i+1] + } + group.Commands[item] = &Command{ + Options: options, + } } } diff --git a/src/contexts/web/web.go b/src/contexts/web/web.go index 34de5e70..662bab99 100644 --- a/src/contexts/web/web.go +++ b/src/contexts/web/web.go @@ -362,16 +362,17 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", "count": &ctx.Cache{Name: "count", Value: "0", Help: "主机协议"}, }, Configs: map[string]*ctx.Config{ - "protocol": &ctx.Config{Name: "protocol", Value: "", Help: "主机协议"}, - "hostname": &ctx.Config{Name: "hostname", Value: "", Help: "主机地址"}, - "port": &ctx.Config{Name: "port", Value: "", Help: "主机端口"}, - "dir": &ctx.Config{Name: "dir", Value: "/", Help: "主机路由"}, - "file": &ctx.Config{Name: "file", Value: "", Help: "主机文件"}, - "query": &ctx.Config{Name: "query", Value: "", Help: "主机参数"}, - "output": &ctx.Config{Name: "output", Value: "stdout", Help: "响应输出"}, - "editor": &ctx.Config{Name: "editor", Value: "vim", Help: "响应编辑器"}, - "upload_tpl": &ctx.Config{Name: "upload_tpl", Value: "usr/template/upload.html", Help: "上传文件路径"}, - "travel_tpl": &ctx.Config{Name: "travel_tpl", Value: "usr/template/travel.html", Help: "上传文件路径"}, + "protocol": &ctx.Config{Name: "protocol", Value: "", Help: "主机协议"}, + "hostname": &ctx.Config{Name: "hostname", Value: "", Help: "主机地址"}, + "port": &ctx.Config{Name: "port", Value: "", Help: "主机端口"}, + "dir": &ctx.Config{Name: "dir", Value: "/", Help: "主机路由"}, + "file": &ctx.Config{Name: "file", Value: "", Help: "主机文件"}, + "query": &ctx.Config{Name: "query", Value: "", Help: "主机参数"}, + "output": &ctx.Config{Name: "output", Value: "stdout", Help: "响应输出"}, + "editor": &ctx.Config{Name: "editor", Value: "vim", Help: "响应编辑器"}, + "upload_tpl": &ctx.Config{Name: "upload_tpl", Value: "upload.html", Help: "上传文件路径"}, + "travel_tpl": &ctx.Config{Name: "travel_tpl", Value: "travel.html", Help: "上传文件路径"}, + "template_dir": &ctx.Config{Name: "template_dir", Value: "usr/template/", Help: "上传文件路径"}, }, Commands: map[string]*ctx.Command{ "serve": &ctx.Command{Name: "serve [directory [address [protocol]]]", Help: "开启应用服务", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { @@ -606,8 +607,38 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", } dir := m.Option("file") + if !m.Options("username") { + m.Option("username", "") + } + + m.Option("right", "") + if !m.Options("sessid") { + m.Option("sessid", "") + m.Option("message", "please login") + } else { + msg := m.Find("aaa").Cmd("login", m.Option("sessid")) + if msg.Result(0) == "error: " { + m.Option("sessid", "") + m.Option("message", "login failure") + } else { + m.Option("username", msg.Result(0)) + msg = m.Spawn(m.Target()) + msg.Cmd("right", "check", msg.Cap("group"), "command", "/upload", "file", dir) + if msg.Result(0) == "ok" { + m.Option("right", "ok") + } else { + m.Option("message", "your do not have the right of", dir) + } + } + } + if m.Option("method") == "POST" { - if m.Options("content") { + if m.Options("shareto") { + msg := m.Spawn(m.Target()) + msg.Cmd("right", "add", m.Option("shareto"), "command", "/upload", "file", m.Option("sharefile")) + m.Append("link", "hello") + return + } else if m.Options("content") { name := path.Join(dir, m.Option("filename")) if _, e := os.Stat(name); e != nil { f, e := os.Create(name) @@ -639,14 +670,6 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", } } - switch m.Option("cmd") { - case "git": - branch := m.Find("nfs").Cmd("git", "-C", dir, "branch") - m.Option("branch", branch.Result(0)) - status := m.Find("nfs").Cmd("git", "-C", dir, "status") - m.Option("status", status.Result(0)) - } - file := m.Option("file") s, e := os.Stat(file) if m.Assert(e); !s.IsDir() { @@ -707,7 +730,54 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", } w.Header().Add("Content-Type", "text/html") - m.Assert(template.Must(template.ParseGlob(m.Conf("upload_tpl"))).Execute(w, m.Meta)) + tmpl := template.Must(template.Must(template.ParseGlob(m.Conf("template_dir") + "/common/*.html")).ParseGlob(m.Conf("template_dir") + "upload.html")) + m.Assert(tmpl) + + tmpl.ExecuteTemplate(w, "head", m.Meta) + if m.Options("right") { + tmpl.ExecuteTemplate(w, "userinfo", m.Meta) + + msg := m.Spawn(m.Target()) + + for k, v := range msg.Target().Index { + for i, j := range v.Commands { + for v, n := range j.Options { + if n == dir { + msg.Add("append", "group", k) + msg.Add("append", "command", i) + msg.Add("append", "argument", v) + msg.Add("append", "value", n) + m.Log("fuck", nil, "why %v", msg.Meta) + } + } + } + } + + tmpl.ExecuteTemplate(w, "share", msg.Meta) + + tmpl.ExecuteTemplate(w, "upload", m.Meta) + } else { + tmpl.ExecuteTemplate(w, "login", m.Meta) + } + + switch m.Option("cmd") { + case "git": + branch := m.Find("nfs").Cmd("git", "-C", dir, "branch") + m.Option("branch", branch.Result(0)) + status := m.Find("nfs").Cmd("git", "-C", dir, "status") + m.Option("status", status.Result(0)) + tmpl.ExecuteTemplate(w, "git", m.Meta) + } + + if m.Options("message") { + tmpl.ExecuteTemplate(w, "message", m.Meta) + } + + tmpl.ExecuteTemplate(w, "list", m.Meta) + if m.Options("right") { + tmpl.ExecuteTemplate(w, "create", m.Meta) + } + tmpl.ExecuteTemplate(w, "tail", m.Meta) delete(m.Meta, "result") delete(m.Meta, "append") // }}} @@ -770,7 +840,10 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", } w.Header().Add("Content-Type", "text/html") - m.Assert(template.Must(template.ParseGlob(m.Conf("travel_tpl"))).ExecuteTemplate(w, "head", m.Meta)) + tmpl := template.Must(template.Must(template.ParseGlob(m.Conf("template_dir") + "/common/*.html")).ParseGlob(m.Conf("template_dir") + "travel.html")) + m.Assert(tmpl) + + m.Assert(tmpl.ExecuteTemplate(w, "head", m.Meta)) if msg := m.Find(module, true); msg != nil { for _, v := range []string{"cache", "config", "command", "module", "domain"} { @@ -789,17 +862,21 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", if len(msg.Meta["append"]) > 0 { msg.Option("current_module", module) msg.Option("current_domain", m.Option("domain")) - m.Log("fuck", nil, "fuck %v", msg.Meta) - m.Assert(template.Must(template.ParseGlob(m.Conf("travel_tpl"))).ExecuteTemplate(w, v, msg.Meta)) + m.Assert(tmpl.ExecuteTemplate(w, v, msg.Meta)) } } } - m.Assert(template.Must(template.ParseGlob(m.Conf("travel_tpl"))).ExecuteTemplate(w, "tail", m.Meta)) + m.Assert(tmpl.ExecuteTemplate(w, "tail", m.Meta)) delete(m.Meta, "result") delete(m.Meta, "append") // }}} }}, + "/login": &ctx.Command{Name: "/login", Help: "文件上传", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + w := m.Data["response"].(http.ResponseWriter) + msg := m.Find("aaa").Cmd("login", m.Option("username"), m.Option("password")) + http.SetCookie(w, &http.Cookie{Name: "sessid", Value: msg.Result(0)}) + }}, "temp": &ctx.Command{Name: "temp", Help: "应用示例", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { msg := m.Spawn(m.Target()) question := []string{} diff --git a/usr/library/context.js b/usr/library/context.js index 7fbdba90..1637f201 100644 --- a/usr/library/context.js +++ b/usr/library/context.js @@ -30,7 +30,7 @@ ctx = { } } } else if (value == undefined) { - return args[key]; + return args[key] || ""; } else { args[key] = value; } @@ -77,6 +77,9 @@ ctx = { console.log("POST: "+url+"?"+arg); xhr.send(arg); },//}}} + Refresh: function() {//{{{ + location.assign(location.href); + },//}}} Cap: function(cap, cb) {//{{{ if (typeof cap == "function") { diff --git a/usr/template/common/login.html b/usr/template/common/login.html new file mode 100644 index 00000000..62b69de9 --- /dev/null +++ b/usr/template/common/login.html @@ -0,0 +1,85 @@ +{{define "login"}} +
login +
+ + + +
+
+ +{{end}} + +{{define "userinfo"}} +
userinfo + welcome {{index .username 0}} + +
+ +{{end}} + +{{define "share"}} +
share +
+ + +
+ + + {{range .append}} + + {{end}} + + + {{range .append}} + + {{end}} + + + {{$meta := .}} + {{if .append}} + {{$first := index .append 0}} + {{range $i, $k := index . $first}} + + {{range $key := index $meta "append"}} + + {{end}} + + {{end}} + {{end}} +
{{.}}
+ {{index $meta $key $i}} +
+
+ +{{end}} diff --git a/usr/template/upload.html b/usr/template/upload.html index 6f60474a..43177bfb 100644 --- a/usr/template/upload.html +++ b/usr/template/upload.html @@ -1,3 +1,4 @@ +{{define "head"}} @@ -35,79 +36,83 @@ } - - -{{if index . "cmd"}} -{{$cmd := index . "cmd" 0}} -{{if eq $cmd "git"}} -
branch -
{{index . "branch"}}
-
- -
status -
{{index . "status"}}
-
-{{end}} {{end}} -
upload -
- -
-
+{{define "git"}} +
branch +
{{index . "branch"}}
+
+
status +
{{index . "status"}}
+
+{{end}} -{{if index . "message" 0}} +{{define "upload"}} +
upload +
+ +
+
+{{end}} + +{{define "message"}}
message
{{index . "message"}}
{{end}} -
- -- {{range .append}} - - {{end}} - - - {{$sort := index . "sort" 0}} - {{$reverse := index . "reverse" 0}} - {{range .append}} - {{if eq $sort .}} - - {{else if eq $reverse .}} - - {{else if eq . "path"}} - {{else}} - +{{define "list"}} +
+
{{.}}{{.}}{{.}}
+ + {{range .append}} + {{end}} - {{end}} - - -{{$meta := .}} -{{if .append}} -{{$first := index .append 0}} -{{range $i, $k := index . $first}} - - {{range $key := index $meta "append"}} - {{if eq $key "name"}} - - {{else if eq $key "path"}} - {{else}} - + + + {{$sort := index . "sort" 0}} + {{$reverse := index . "reverse" 0}} + {{range .append}} + {{if eq $sort .}} + + {{else if eq $reverse .}} + + {{else if eq . "path"}} + {{else}} + + {{end}} {{end}} - {{end}} - -{{end}} -{{end}} -
- {{index $meta $key $i}} - - {{index $meta $key $i}} -
{{.}}{{.}}{{.}}
-
+ + + {{$meta := .}} + {{if .append}} + {{$first := index .append 0}} + {{range $i, $k := index . $first}} + + {{range $key := index $meta "append"}} + {{if eq $key "name"}} + + {{index $meta $key $i}} + + {{else if eq $key "path"}} + {{else}} + + {{index $meta $key $i}} + + {{end}} + {{end}} + + {{end}} + {{end}} + + +{{end}} + +{{define "create"}}
create
@@ -116,7 +121,9 @@
+{{end}} +{{define "tail"}} +{{end}}