This commit is contained in:
aozhiwei 2020-12-19 15:01:25 +08:00
parent 444edafd54
commit 8ca48912f0
3 changed files with 58 additions and 41 deletions

View File

@ -17,5 +17,6 @@ func (this *DBEngine) UnInit() {
func (this *DBEngine) NewAdminConn() *q5.Mysql {
conn := q5.NewMysql("127.0.0.1", 3306, "root", "keji178", "admindb")
conn.Open()
return conn
}

View File

@ -1,6 +1,8 @@
package main
import (
"encoding/json"
"database/sql"
"q5"
"f5"
)
@ -38,53 +40,67 @@ func (this *UserMgr) _userLogin(c *f5.Context) {
content := q5.NewMxoObject()
content.SetXValue("username", c.Request("username"))
content.SetXValue("password", c.Request("password"))
respStr, err := q5.HttpPostContent(url,
respStr := ""
if err := q5.HttpPostContent(url,
"application/json;charset=utf-8",
content.ToJsonStr())
if err == nil {
f5.SysLog().Debug("respStr:%s", respStr)
respObj := q5.NewXoFromJsonStr(respStr)
if respObj == nil || !respObj.IsObject() {
content.ToJsonStr(),
&respStr); err != nil {
c.ResponseErr(500, "服务器内部错误")
return
}
f5.SysLog().Debug("respStr:%s", respStr)
respObj := struct {
ErrCode *int32 `json:"errcode"`
ErrMsg string `json:"errmsg"`
Token string `json:"token"`
UserInfo *struct {
UserName string `json:"username"`
FullName string `json:"fullname"`
AvatarUrl string `json:"avatar"`
} `json:"userInfo"`
}{}
if err := json.Unmarshal([]byte(respStr), &respObj); err != nil || respObj.ErrCode == nil {
c.ResponseErr(500, "服务器内部错误")
return
}
if *respObj.ErrCode != 0 {
c.Response(respStr)
return
}
userInfo := respObj.UserInfo
nowUnix := f5.App.NowUnix()
conn := G.DBEngine.NewAdminConn()
row := conn.QueryRow("SELECT username FROM `user` WHERE username=?;",
userInfo.UserName)
{
userName := ""
err := row.Scan(&userName)
if err != nil && err != sql.ErrNoRows {
c.ResponseErr(500, "服务器内部错误")
return
}
errCode := respObj.GetSimpleStr("errcode", "")
userInfo := respObj.At("userinfo")
if errCode != "0" || userInfo == nil || !userInfo.IsObject() {
c.Response(respStr)
if err == sql.ErrNoRows {
//新用户
conn.Exec("INSERT `user`(username, fullname, avatar_url, createtime, " +
" modifytime) " +
"VALUES(?, ?, ?, ?, ?);",
userInfo.UserName,
userInfo.FullName,
userInfo.AvatarUrl,
nowUnix,
nowUnix)
} 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)
//老用户
conn.Exec("UPDATE `user` SET fullname=?, avatar_url=?, " +
" modifytime=? " +
"WHERE username=?;",
userInfo.FullName,
userInfo.AvatarUrl,
nowUnix,
userInfo.UserName)
}
} else {
c.ResponseErr(500, "服务器内部错误")
}
c.Response(respStr)
}
func (this *UserMgr) _userInfo(c *f5.Context, user *AuthedUser) {

2
third_party/f5 vendored

@ -1 +1 @@
Subproject commit 60518a8f3ccb28de37ba2b386fd36923b085b965
Subproject commit 72fe2ec77dfada4c3e839718327c63afd2254070