This commit is contained in:
aozhiwei 2020-12-19 11:59:30 +08:00
parent dfb42790d0
commit 444edafd54
6 changed files with 109 additions and 17 deletions

View File

@ -17,13 +17,25 @@ func (this *App_) Init() {
G.MetaMgr = new(MetaMgr).Init()
G.HttpServer = new(f5.HttpServer).Init("httpserver", 1000 * 60)
G.UserMgr = new(UserMgr).Init()
G.DBEngine = new(DBEngine).Init()
G.HttpServer.Start(G.MetaMgr.GetServer(1).GetListenPort());
}
func (this *App_) UnInit() {
G.DBEngine.UnInit()
G.UserMgr.UnInit()
G.HttpServer.UnInit()
G.MetaMgr.UnInit()
this.App_.UnInit()
}
func (this *App_) RegisterAuthedHandler(
pattern string,
handle func(*f5.Context, *AuthedUser)) {
G.HttpServer.RegisterRestHandle(pattern,
func (c *f5.Context) {
user := c.Get("user").(AuthedUser)
handle(c, &user)
})
}

1
server/admin/auth.go Normal file
View File

@ -0,0 +1 @@
package main

21
server/admin/dbengine.go Normal file
View File

@ -0,0 +1,21 @@
package main
import (
"q5"
)
type DBEngine struct {
}
func (this *DBEngine) Init() *DBEngine {
return this
}
func (this *DBEngine) UnInit() {
}
func (this *DBEngine) NewAdminConn() *q5.Mysql {
conn := q5.NewMysql("127.0.0.1", 3306, "root", "keji178", "admindb")
return conn
}

View File

@ -8,6 +8,7 @@ type GlobalVar struct {
MetaMgr *MetaMgr
UserMgr *UserMgr
HttpServer *f5.HttpServer
DBEngine *DBEngine
}
var G *GlobalVar = &GlobalVar{}

11
server/admin/types.go Normal file
View File

@ -0,0 +1,11 @@
package main
/*type AuthedUser struct {
accountId string
registerTime int64
}*/
type AuthedUser interface {
GetAccountId() string
GetRegisterTime() int64
}

View File

@ -1,7 +1,6 @@
package main
import (
"net/http"
"q5"
"f5"
)
@ -11,8 +10,13 @@ type UserMgr struct {
func (this *UserMgr) Init() *UserMgr {
G.HttpServer.RegisterRestHandle("/user/login", this._userLogin)
G.HttpServer.RegisterRestHandle("/user/logout", this._userLogout)
G.HttpServer.RegisterRestHandle("/user/info", this._userInfo)
G.HttpServer.DefineGroup("auth", this.auth)
G.HttpServer.UseGroupBegin("auth")
App.RegisterAuthedHandler("/user/info", this._userInfo)
App.RegisterAuthedHandler("/user/logout", this._userLogout)
G.HttpServer.UseGroupEnd()
return this
}
@ -20,28 +24,70 @@ func (this *UserMgr) UnInit() {
}
func (this *UserMgr) _userLogin(w *http.ResponseWriter, r *http.Request) {
username := q5.Request(r, "username").GetString()
password := q5.Request(r, "password").GetString()
func (this *UserMgr) auth(c *f5.Context) {
//bearer := c.Header("authorization")
type AuthedUser struct {
accountId string
registerTime int64
}
c.Abort()
}
func (this *UserMgr) _userLogin(c *f5.Context) {
url := "https://mp.kingsome.cn/api/user/login"
content := q5.NewMxoObject()
content.SetXValue("username", q5.NewXString(username))
content.SetXValue("password", q5.NewXString(password))
content.SetXValue("username", c.Request("username"))
content.SetXValue("password", c.Request("password"))
respStr, err := q5.HttpPostContent(url,
"application/json;charset=utf-8",
content.ToJsonStr())
if err == nil {
f5.SysLog().Debug("respStr:%s", respStr)
q5.Response(w, respStr)
respObj := q5.NewXoFromJsonStr(respStr)
if respObj == nil || !respObj.IsObject() {
c.ResponseErr(500, "服务器内部错误")
return
}
errCode := respObj.GetSimpleStr("errcode", "")
userInfo := respObj.At("userinfo")
if errCode != "0" || userInfo == nil || !userInfo.IsObject() {
c.Response(respStr)
} else {
userName := userInfo.GetSimpleStr("username", "")
fullName := userInfo.GetSimpleStr("fullname", "")
avatarUrl := userInfo.GetSimpleStr("avatar_url", "")
nowUnix := f5.App.NowUnix()
conn := G.DBEngine.NewAdminConn()
row := conn.QueryRow("SELECT username FROM `user` WHERE username='?'", userName)
var idx int64
if err := row.Scan(&idx); err == nil {
//新用户
conn.Exec("INSERT `user`(username, fullname, avatar_url, createtime, " +
" modifytime) " +
"VALUES('?', '?', '?', ?, ?);",
userName,
fullName,
avatarUrl,
nowUnix,
nowUnix)
} else {
//老用户
conn.Exec("UPDATE `user` SET fullname='?', avatar_url='?', " +
" modifytime=? " +
"WHERE username='?';",
fullName,
avatarUrl,
nowUnix,
userName)
}
c.Response(respStr)
}
} else {
q5.ResponseOk(w)
c.ResponseErr(500, "服务器内部错误")
}
}
func (this *UserMgr) _userInfo(w *http.ResponseWriter, r *http.Request) {
token := q5.Request(r, "token").GetString()
func (this *UserMgr) _userInfo(c *f5.Context, user *AuthedUser) {
url := "https://mp.kingsome.cn/api/user/info"
params := q5.NewMxoObject()
respStr, err := q5.HttpGetEx(url,
@ -49,17 +95,17 @@ func (this *UserMgr) _userInfo(w *http.ResponseWriter, r *http.Request) {
func (setOpt func(int32, *q5.XValue, *q5.XValue)) {
setOpt(q5.HTTP_OPT_ADD_HEADER,
q5.NewXString("authorization"),
q5.NewXString("Bearer " + token))
q5.NewXString("Bearer " + c.Request("token").GetString()))
})
if err == nil {
f5.SysLog().Debug("respStr:%s", respStr)
q5.Response(w, respStr)
c.Response(respStr)
} else {
f5.SysLog().Debug("respStr:%s", err)
q5.ResponseOk(w)
c.ResponseOk()
}
}
func (this *UserMgr) _userLogout(w *http.ResponseWriter, r *http.Request) {
func (this *UserMgr) _userLogout(c *f5.Context, user *AuthedUser) {
}