Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Jul 31, 2020
1 parent 2af17a0 commit 4492d53
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
10 changes: 5 additions & 5 deletions core/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,23 +239,23 @@ func (mysql *Mysql) GetUserByPass(pass string) *User {
}

// PageList 通过分页获取用户记录
func (mysql *Mysql) PageList(curPage int, pageSize int) *PageQuery {
func (mysql *Mysql) PageList(curPage int, pageSize int) (*PageQuery, error) {
var (
total int
dataList []*User
)

db := mysql.GetDB()
if db == nil {
return nil
return nil, errors.New("连接mysql失败")
}
defer db.Close()
offset := (curPage - 1) * pageSize
querySQL := fmt.Sprintf("SELECT * FROM users LIMIT %d, %d", offset, pageSize)
rows, err := db.Query(querySQL)
if err != nil {
fmt.Println(err)
return nil
return nil, err
}
defer rows.Close()
for rows.Next() {
Expand All @@ -270,7 +270,7 @@ func (mysql *Mysql) PageList(curPage int, pageSize int) *PageQuery {
)
if err := rows.Scan(&id, &username, &originPass, &passShow, &quota, &download, &upload); err != nil {
fmt.Println(err)
return nil
return nil, err
}
dataList = append(dataList, &User{ID: id, Username: username, Password: passShow, Download: download, Upload: upload, Quota: quota})
}
Expand All @@ -281,7 +281,7 @@ func (mysql *Mysql) PageList(curPage int, pageSize int) *PageQuery {
Total: total,
DataList: dataList,
PageNum: (total + pageSize - 1) / pageSize,
}
}, nil
}

// GetData 获取用户记录
Expand Down
6 changes: 3 additions & 3 deletions web/controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func PageUserList(curPage int, pageSize int) *ResponseBody {
responseBody := ResponseBody{Msg: "success"}
defer TimeCost(time.Now(), &responseBody)
mysql := core.GetMysql()
pageData := mysql.PageList(curPage, pageSize)
if pageData == nil {
responseBody.Msg = "连接mysql失败!"
pageData, err := mysql.PageList(curPage, pageSize)
if err != nil {
responseBody.Msg = err.Error()
return &responseBody
}
domain, port := trojan.GetDomainAndPort()
Expand Down

0 comments on commit 4492d53

Please sign in to comment.