1
0
mirror of https://github.com/fumiama/paper-manager.git synced 2026-06-08 09:34:42 +08:00

finish page/settings

This commit is contained in:
源文雨
2023-03-18 18:27:33 +08:00
parent 78418429c1
commit 7dbf8dc517
6 changed files with 117 additions and 12 deletions

View File

@@ -144,6 +144,39 @@ func init() {
}
writeresult(w, codeSuccess, &message{M: "成功, 已将消息报告给课程组长"}, messageOk, typeSuccess)
}}
apimap["/api/setUserInfo"] = &apihandler{"POST", func(w http.ResponseWriter, r *http.Request) {
type setuserinfobody struct {
Nick string `json:"nick"`
Desc string `json:"desc"`
Avtr string `json:"avtr"`
}
token := r.Header.Get("Authorization")
user := usertokens.Get(token)
if user == nil {
writeresult(w, codeError, nil, errInvalidToken.Error(), typeError)
return
}
var body setuserinfobody
defer r.Body.Close()
err := json.NewDecoder(r.Body).Decode(&body)
if err != nil {
writeresult(w, codeError, nil, err.Error(), typeError)
return
}
err = setUserInfo(*user.ID, &body.Nick, &body.Desc, &body.Avtr)
if err != nil {
writeresult(w, codeError, nil, err.Error(), typeError)
return
}
user.Nick = body.Nick
user.Desc = body.Desc
user.Avtr = body.Avtr
type message struct {
M string `json:"msg"`
}
writeresult(w, codeSuccess, &message{M: "成功"}, messageOk, typeSuccess)
}}
}
// Handler serves all backend /api call

View File

@@ -4,11 +4,13 @@ import (
"crypto/md5"
"encoding/hex"
"errors"
"os"
"strings"
"time"
base14 "github.com/fumiama/go-base16384"
"github.com/fumiama/paper-manager/backend/global"
"github.com/fumiama/paper-manager/backend/utils"
)
const (
@@ -114,3 +116,33 @@ func setUserContact(id int, token, ncont string) error {
}
return global.UserDB.UpdateUserContact(id, ncont)
}
// setUserInfo may change the arguments
func setUserInfo(id int, nick, desc, avtr *string) error {
user, err := global.UserDB.GetUserByID(id)
if err != nil {
return err
}
n, d, a := *nick, *desc, *avtr
if n == "" {
*nick = user.Nick
}
if n == user.Nick {
n = ""
}
if d == "" {
*desc = user.Desc
}
if d == user.Desc {
d = ""
}
if a == "" {
*avtr = user.Avtr
} else if utils.IsNotExist(global.DataFolder + a) {
return os.ErrNotExist
}
if a == user.Avtr {
a = ""
}
return global.UserDB.UpdateUserInfo(id, n, d, a)
}