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 { func (this *DBEngine) NewAdminConn() *q5.Mysql {
conn := q5.NewMysql("127.0.0.1", 3306, "root", "keji178", "admindb") conn := q5.NewMysql("127.0.0.1", 3306, "root", "keji178", "admindb")
conn.Open()
return conn return conn
} }

View File

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

2
third_party/f5 vendored

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