From f6987c5e857f7535eb39923c029b426155fe8c44 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 29 Sep 2022 18:13:57 +0800 Subject: [PATCH] [change] app menu & yao.login.admin process --- data/bindata.go | 62 +++++++++---------- user/process.go | 3 - widgets/app/app.go | 45 +++++++++++++- widgets/app/app_test.go | 4 +- widgets/login/login.go | 5 +- widgets/login/login_test.go | 18 +++--- widgets/login/process.go | 116 ++++++++++++++++++++++++++++++++++++ 7 files changed, 203 insertions(+), 50 deletions(-) create mode 100644 widgets/login/process.go diff --git a/data/bindata.go b/data/bindata.go index 2aa5914f..8bc08df4 100644 --- a/data/bindata.go +++ b/data/bindata.go @@ -208,7 +208,7 @@ func uiIndexHtml() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "ui/index.html", size: 11, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "ui/index.html", size: 11, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -228,7 +228,7 @@ func xgenV09IndexHtml() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xgen/v0.9/index.html", size: 14, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "xgen/v0.9/index.html", size: 14, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -248,12 +248,12 @@ func xgenV10IndexHtml() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xgen/v1.0/index.html", size: 50, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "xgen/v1.0/index.html", size: 50, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } -var _xgenV10UmiJs = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xb2\x81\x00\x05\x0f\x57\x47\x17\xae\xb2\xc4\x22\x85\x82\xc4\x92\x0c\x05\x5b\x05\x25\xfd\xf8\xf8\xca\xc4\xfc\xf8\xc4\x94\xdc\xcc\xbc\xf8\xa2\xfc\xfc\x12\x7d\x25\x6b\xb0\x82\xb4\xfc\x7c\x05\x5b\x85\xe4\xfc\xbc\xe4\xc4\x12\x0d\x25\x74\x55\x4a\x9a\xd6\x5c\xb6\x10\x80\xa4\x1a\xb7\x69\x49\x89\x45\x20\x79\x0c\x63\xac\xb9\xec\x20\x40\x21\x25\x25\xd1\x30\xcd\xd0\x52\x41\x23\x3a\x2d\xb3\x22\x56\x01\xac\x46\x01\xa4\x46\xa1\x28\xb5\x20\x27\x31\x39\x55\x21\xa9\x34\x5d\x93\x0b\x10\x00\x00\xff\xff\xcc\x4c\x1b\x91\xc9\x00\x00\x00") +var _xgenV10UmiJs = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x2a\x4b\x2c\x52\x28\x48\x2c\xc9\x50\xb0\x55\x50\xd2\x8f\x8f\xaf\x4c\xcc\x8f\x4f\x4c\xc9\xcd\xcc\x8b\x2f\xca\xcf\x2f\xd1\x57\xb2\xe6\x02\x29\x48\xcb\xcf\x57\xb0\x55\x48\xce\xcf\x4b\x4e\x2c\xd1\x50\x42\x57\xa5\xa4\x69\xcd\x05\x08\x00\x00\xff\xff\x99\x7c\xcb\xfe\x47\x00\x00\x00") func xgenV10UmiJsBytes() ([]byte, error) { return bindataRead( @@ -268,7 +268,7 @@ func xgenV10UmiJs() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xgen/v1.0/umi.js", size: 201, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "xgen/v1.0/umi.js", size: 71, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -288,7 +288,7 @@ func yaoApisReadmeMd() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/apis/README.md", size: 13, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/apis/README.md", size: 13, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -308,7 +308,7 @@ func yaoApisChartHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/apis/chart.http.json", size: 624, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/apis/chart.http.json", size: 624, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -328,7 +328,7 @@ func yaoApisImportHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/apis/import.http.json", size: 1676, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/apis/import.http.json", size: 1676, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -348,7 +348,7 @@ func yaoApisPageHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/apis/page.http.json", size: 609, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/apis/page.http.json", size: 609, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -368,7 +368,7 @@ func yaoApisStorageHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/apis/storage.http.json", size: 1026, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/apis/storage.http.json", size: 1026, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -388,7 +388,7 @@ func yaoApisTableHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/apis/table.http.json", size: 3213, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/apis/table.http.json", size: 3213, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -408,7 +408,7 @@ func yaoApisUserHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/apis/user.http.json", size: 1453, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/apis/user.http.json", size: 1453, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -428,7 +428,7 @@ func yaoApisXiangHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/apis/xiang.http.json", size: 1239, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/apis/xiang.http.json", size: 1239, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -448,7 +448,7 @@ func yaoDataAppJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/data/app.json", size: 277, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/data/app.json", size: 277, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -468,7 +468,7 @@ func yaoDataIcons404Png() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/data/icons/404.png", size: 9342, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/data/icons/404.png", size: 9342, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -488,7 +488,7 @@ func yaoDataIconsIconIcns() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/data/icons/icon.icns", size: 67465, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/data/icons/icon.icns", size: 67465, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -508,7 +508,7 @@ func yaoDataIconsIconIco() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/data/icons/icon.ico", size: 54993, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/data/icons/icon.ico", size: 54993, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -528,7 +528,7 @@ func yaoDataIconsIconPng() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/data/icons/icon.png", size: 34558, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/data/icons/icon.png", size: 34558, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -548,7 +548,7 @@ func yaoDataIndexHtml() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/data/index.html", size: 282, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/data/index.html", size: 282, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -568,7 +568,7 @@ func yaoFlowsReadmeMd() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/flows/README.md", size: 12, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/flows/README.md", size: 12, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -588,7 +588,7 @@ func yaoFlowsAdminGuardFlowJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/flows/admin/guard.flow.json", size: 559, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/flows/admin/guard.flow.json", size: 559, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -608,7 +608,7 @@ func yaoFlowsMenuFlowJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/flows/menu.flow.json", size: 1045, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/flows/menu.flow.json", size: 1045, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -628,7 +628,7 @@ func yaoFlowsUserFindFlowJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/flows/user/find.flow.json", size: 328, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/flows/user/find.flow.json", size: 328, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -648,7 +648,7 @@ func yaoLangsEnUsJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/langs/en-US.json", size: 66, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/langs/en-US.json", size: 66, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -668,7 +668,7 @@ func yaoModelsReadmeMd() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/models/README.md", size: 15, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/models/README.md", size: 15, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -688,7 +688,7 @@ func yaoModelsMenuModJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/models/menu.mod.json", size: 2378, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/models/menu.mod.json", size: 2378, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -708,7 +708,7 @@ func yaoModelsUserModJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/models/user.mod.json", size: 8826, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/models/user.mod.json", size: 8826, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -728,7 +728,7 @@ func yaoModelsWorkflowModJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/models/workflow.mod.json", size: 3333, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/models/workflow.mod.json", size: 3333, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -748,7 +748,7 @@ func yaoTablesReadmeMd() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/tables/README.md", size: 15, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/tables/README.md", size: 15, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -768,7 +768,7 @@ func yaoTablesMenuTabJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/tables/menu.tab.json", size: 10471, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/tables/menu.tab.json", size: 10471, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -788,7 +788,7 @@ func yaoTablesUserTabJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/tables/user.tab.json", size: 2357, mode: os.FileMode(420), modTime: time.Unix(1664428475, 0)} + info := bindataFileInfo{name: "yao/tables/user.tab.json", size: 2357, mode: os.FileMode(420), modTime: time.Unix(1664445873, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/user/process.go b/user/process.go index 11ec4aca..ba89385f 100644 --- a/user/process.go +++ b/user/process.go @@ -6,13 +6,11 @@ import ( "github.com/yaoapp/kun/exception" "github.com/yaoapp/kun/log" "github.com/yaoapp/kun/maps" - "github.com/yaoapp/kun/utils" ) func init() { gou.RegisterProcessHandler("xiang.user.Captcha", ProcessCaptcha) gou.RegisterProcessHandler("xiang.user.Login", ProcessLogin) - gou.AliasProcess("xiang.user.Login", "yao.admin.Login") } // ProcessLogin xiang.user.Login 用户登录 @@ -41,7 +39,6 @@ func ProcessLogin(process *gou.Process) interface{} { if email != "" { return Auth("email", email, password) } else if mobile != "" { - utils.Dump(mobile, password) return Auth("mobile", mobile, password) } diff --git a/widgets/app/app.go b/widgets/app/app.go index ebac7472..5eb743c0 100644 --- a/widgets/app/app.go +++ b/widgets/app/app.go @@ -43,6 +43,10 @@ func LoadAndExport(cfg config.Config) error { // Load the app DSL func Load(cfg config.Config) error { + if os.Getenv("YAO_LANG") == "" { + os.Setenv("YAO_LANG", "en-us") + } + file := filepath.Join(cfg.Root, "app.json") file, err := filepath.Abs(file) if err != nil { @@ -111,8 +115,9 @@ func exportAPI() error { process = "yao.app.Menu" args := []string{} if Setting.Menu.Process != "" { - process = Setting.Menu.Process - args = Setting.Menu.Args + if Setting.Menu.Args != nil { + args = Setting.Menu.Args + } } path = gou.Path{ @@ -146,7 +151,40 @@ func Export() error { func exportProcess() { gou.RegisterProcessHandler("yao.app.setting", processSetting) gou.RegisterProcessHandler("yao.app.xgen", processXgen) - gou.AliasProcess("flows.xiang.menu", "yao.app.menu") + gou.RegisterProcessHandler("yao.app.menu", processMenu) +} + +func processMenu(process *gou.Process) interface{} { + + if Setting.Menu.Process != "" { + return gou. + NewProcess(Setting.Menu.Process, process.Args...). + WithGlobal(process.Global). + WithSID(process.Sid). + Run() + } + + args := map[string]interface{}{ + "select": []string{"id", "name", "icon", "parent", "path", "blocks", "visible_menu"}, + "withs": map[string]interface{}{ + "children": map[string]interface{}{ + "query": map[string]interface{}{ + "select": []string{"id", "name", "icon", "parent", "path", "blocks", "visible_menu"}, + }, + }, + }, + "wheres": []map[string]interface{}{ + {"column": "status", "value": "enabled"}, + {"column": "parent", "op": "null"}, + }, + "limit": 200, + "orders": []map[string]interface{}{{"column": "rank", "option": "asc"}}, + } + return gou. + NewProcess("models.xiang.menu.get", args). + WithGlobal(process.Global). + WithSID(process.Sid). + Run() } func processSetting(process *gou.Process) interface{} { @@ -191,6 +229,7 @@ func processXgen(process *gou.Process) interface{} { xgenSetting := map[string]interface{}{ "name": Setting.Name, "description": Setting.Description, + "theme": Setting.Theme, "mode": mode, "apiPrefix": "__yao", "token": "localStorage", diff --git a/widgets/app/app_test.go b/widgets/app/app_test.go index 19d22137..3736184e 100644 --- a/widgets/app/app_test.go +++ b/widgets/app/app_test.go @@ -128,8 +128,8 @@ func TestProcessXgen(t *testing.T) { assert.Equal(t, "Another yao application", xgen.Get("description")) assert.Equal(t, "/api/__yao/login/admin/captcha?type=digit", xgen.Get("login.admin.captcha")) assert.Equal(t, "/api/__yao/login/admin", xgen.Get("login.admin.login")) - assert.Equal(t, "/x/Table/pet", xgen.Get("login.entry.admin")) - assert.Equal(t, "/x/Table/dash", xgen.Get("login.entry.user")) + assert.Equal(t, "/x/Chart/dashboard", xgen.Get("login.entry.admin")) + assert.Equal(t, "/x/Table/pet", xgen.Get("login.entry.user")) assert.Equal(t, "/api/__yao/login/user/captcha?type=digit", xgen.Get("login.user.captcha")) assert.Equal(t, "/api/__yao/login/user", xgen.Get("login.user.login")) assert.Equal(t, os.Getenv("YAO_ENV"), xgen.Get("mode")) diff --git a/widgets/login/login.go b/widgets/login/login.go index 78d9284a..3f34503a 100644 --- a/widgets/login/login.go +++ b/widgets/login/login.go @@ -15,7 +15,7 @@ import ( // // API: // GET /api/__yao/login/:id/captcha -> Default process: yao.utils.Captcha :query -// POST /api/__yao/login/:id -> Default process: yao.admin.Login :payload +// POST /api/__yao/login/:id -> Default process: yao.login.Admin :payload // // Logins the loaded login widgets @@ -71,6 +71,7 @@ func LoadFrom(dir string, prefix string) error { // Export export login api func Export() error { + exportProcess() return exportAPI() } @@ -89,7 +90,7 @@ func exportAPI() error { for _, dsl := range Logins { // login action - process := "yao.admin.Login" + process := "yao.login.Admin" args := []string{":payload"} if dsl.Action.Process != "" { process = dsl.Action.Process diff --git a/widgets/login/login_test.go b/widgets/login/login_test.go index 2d003163..ca8ea63a 100644 --- a/widgets/login/login_test.go +++ b/widgets/login/login_test.go @@ -24,11 +24,11 @@ func TestLoad(t *testing.T) { assert.Equal(t, "admin", Logins["admin"].ID) assert.Equal(t, "Admin Login", Logins["admin"].Name) - assert.Equal(t, "yao.admin.Login", Logins["admin"].Action.Process) + assert.Equal(t, "yao.login.Admin", Logins["admin"].Action.Process) assert.Equal(t, []string{":payload"}, Logins["admin"].Action.Args) assert.Equal(t, "yao.utils.Captcha", Logins["admin"].Layout.Captcha) assert.Equal(t, "/images/admin-cover.png", Logins["admin"].Layout.Cover) - assert.Equal(t, "/x/Table/pet", Logins["admin"].Layout.Entry) + assert.Equal(t, "/x/Chart/dashboard", Logins["admin"].Layout.Entry) assert.Equal(t, "https://yaoapps.com", Logins["admin"].Layout.Site) assert.Equal(t, "Make Your Dream With Yao App Engine", Logins["admin"].Layout.Slogan) @@ -38,7 +38,7 @@ func TestLoad(t *testing.T) { assert.Equal(t, []string{":payload"}, Logins["user"].Action.Args) assert.Equal(t, "yao.utils.Captcha", Logins["user"].Layout.Captcha) assert.Equal(t, "/images/user-cover.png", Logins["user"].Layout.Cover) - assert.Equal(t, "/x/Table/dash", Logins["user"].Layout.Entry) + assert.Equal(t, "/x/Table/pet", Logins["user"].Layout.Entry) assert.Equal(t, "https://yaoapps.com/docs", Logins["user"].Layout.Site) assert.Equal(t, "Make Your Dream With Yao App Engine", Logins["user"].Layout.Slogan) } @@ -56,11 +56,11 @@ func TestLoadHK(t *testing.T) { assert.Equal(t, "admin", Logins["admin"].ID) assert.Equal(t, "管理員登錄", Logins["admin"].Name) - assert.Equal(t, "yao.admin.Login", Logins["admin"].Action.Process) + assert.Equal(t, "yao.login.Admin", Logins["admin"].Action.Process) assert.Equal(t, []string{":payload"}, Logins["admin"].Action.Args) assert.Equal(t, "yao.utils.Captcha", Logins["admin"].Layout.Captcha) assert.Equal(t, "/images/admin-cover.png", Logins["admin"].Layout.Cover) - assert.Equal(t, "/x/Table/pet", Logins["admin"].Layout.Entry) + assert.Equal(t, "/x/Chart/dashboard", Logins["admin"].Layout.Entry) assert.Equal(t, "https://yaoapps.com", Logins["admin"].Layout.Site) assert.Equal(t, "夢想讓我們與眾不同", Logins["admin"].Layout.Slogan) @@ -70,7 +70,7 @@ func TestLoadHK(t *testing.T) { assert.Equal(t, []string{":payload"}, Logins["user"].Action.Args) assert.Equal(t, "yao.utils.Captcha", Logins["user"].Layout.Captcha) assert.Equal(t, "/images/user-cover.png", Logins["user"].Layout.Cover) - assert.Equal(t, "/x/Table/dash", Logins["user"].Layout.Entry) + assert.Equal(t, "/x/Table/pet", Logins["user"].Layout.Entry) assert.Equal(t, "https://yaoapps.com/docs", Logins["user"].Layout.Site) assert.Equal(t, "夢想讓我們與眾不同", Logins["user"].Layout.Slogan) } @@ -88,11 +88,11 @@ func TestLoadCN(t *testing.T) { assert.Equal(t, "admin", Logins["admin"].ID) assert.Equal(t, "管理员登录", Logins["admin"].Name) - assert.Equal(t, "yao.admin.Login", Logins["admin"].Action.Process) + assert.Equal(t, "yao.login.Admin", Logins["admin"].Action.Process) assert.Equal(t, []string{":payload"}, Logins["admin"].Action.Args) assert.Equal(t, "yao.utils.Captcha", Logins["admin"].Layout.Captcha) assert.Equal(t, "/images/admin-cover.png", Logins["admin"].Layout.Cover) - assert.Equal(t, "/x/Table/pet", Logins["admin"].Layout.Entry) + assert.Equal(t, "/x/Chart/dashboard", Logins["admin"].Layout.Entry) assert.Equal(t, "https://yaoapps.com", Logins["admin"].Layout.Site) assert.Equal(t, "梦想让我们与众不同", Logins["admin"].Layout.Slogan) @@ -102,7 +102,7 @@ func TestLoadCN(t *testing.T) { assert.Equal(t, []string{":payload"}, Logins["user"].Action.Args) assert.Equal(t, "yao.utils.Captcha", Logins["user"].Layout.Captcha) assert.Equal(t, "/images/user-cover.png", Logins["user"].Layout.Cover) - assert.Equal(t, "/x/Table/dash", Logins["user"].Layout.Entry) + assert.Equal(t, "/x/Table/pet", Logins["user"].Layout.Entry) assert.Equal(t, "https://yaoapps.com/docs", Logins["user"].Layout.Site) assert.Equal(t, "梦想让我们与众不同", Logins["user"].Layout.Slogan) } diff --git a/widgets/login/process.go b/widgets/login/process.go new file mode 100644 index 00000000..035f7f1a --- /dev/null +++ b/widgets/login/process.go @@ -0,0 +1,116 @@ +package login + +import ( + "time" + + "github.com/yaoapp/gou" + "github.com/yaoapp/gou/session" + "github.com/yaoapp/kun/any" + "github.com/yaoapp/kun/exception" + "github.com/yaoapp/kun/log" + "github.com/yaoapp/kun/maps" + "github.com/yaoapp/yao/helper" + "github.com/yaoapp/yao/user" + "golang.org/x/crypto/bcrypt" +) + +var loginTypes = map[string]string{ + "email": "email", + "mobile": "mobile", +} + +// Export process + +func exportProcess() { + gou.RegisterProcessHandler("yao.login.admin", processLoginAdmin) +} + +// processLoginAdmin yao.admin.login 用户登录 +func processLoginAdmin(process *gou.Process) interface{} { + process.ValidateArgNums(1) + payload := process.ArgsMap(0).Dot() + log.With(log.F{"payload": payload}).Debug("processLoginAdmin") + + id := any.Of(payload.Get("captcha.id")).CString() + value := any.Of(payload.Get("captcha.code")).CString() + if id == "" { + exception.New("请输入验证码ID", 400).Ctx(maps.Map{"id": id, "code": value}).Throw() + } + if value == "" { + exception.New("请输入验证码", 400).Ctx(maps.Map{"id": id, "code": value}).Throw() + } + if !user.ValidateCaptcha(id, value) { + log.With(log.F{"id": id, "code": value}).Debug("ProcessLogin") + exception.New("验证码不正确", 403).Ctx(maps.Map{"id": id, "code": value}).Throw() + return nil + } + + email := any.Of(payload.Get("email")).CString() + mobile := any.Of(payload.Get("mobile")).CString() + password := any.Of(payload.Get("password")).CString() + if email != "" { + return auth("email", email, password) + } else if mobile != "" { + return auth("mobile", mobile, password) + } + + exception.New("参数错误", 400).Ctx(payload).Throw() + return nil +} + +func auth(field string, value string, password string) maps.Map { + column, has := loginTypes[field] + if !has { + exception.New("登录方式(%s)尚未支持", 400, field).Throw() + } + + user := gou.Select("xiang.user") + rows, err := user.Get(gou.QueryParam{ + Select: []interface{}{"id", "password", "name", "type", "email", "mobile", "extra"}, + Limit: 1, + Wheres: []gou.QueryWhere{ + {Column: column, Value: value}, + {Column: "status", Value: "enabled"}, + }, + }) + + if err != nil { + exception.New("数据库查询错误", 500, field).Throw() + } + + if len(rows) == 0 { + exception.New("用户不存在(%s)", 404, value).Throw() + } + + row := rows[0] + passwordHash := row.Get("password").(string) + row.Del("password") + + err = bcrypt.CompareHashAndPassword([]byte(passwordHash), []byte(password)) + if err != nil { + exception.New("登录密码错误", 403, value).Throw() + } + + expiresAt := time.Now().Unix() + 3600 + + // token := MakeToken(row, expiresAt) + sid := session.ID() + id := any.Of(row.Get("id")).CInt() + token := helper.JwtMake(id, map[string]interface{}{}, map[string]interface{}{ + "expires_at": expiresAt, + "sid": sid, + "issuer": "xiang", + }) + session.Global().Expire(time.Duration(token.ExpiresAt)*time.Second).ID(sid).Set("user_id", id) + session.Global().ID(sid).Set("user", row) + session.Global().ID(sid).Set("issuer", "xiang") + + // 读取菜单 + menus := gou.NewProcess("yao.app.menu").Run() + return maps.Map{ + "expires_at": token.ExpiresAt, + "token": token.Token, + "user": row, + "menus": menus, + } +}