1
0
mirror of https://github.com/fumiama/paper-manager.git synced 2026-06-16 07:12:17 +08:00

finish register

This commit is contained in:
源文雨
2023-03-19 21:07:22 +08:00
parent bee5caaadc
commit f3757deecf
6 changed files with 92 additions and 26 deletions

View File

@@ -3,6 +3,7 @@ package backend
import (
"encoding/json"
"net/http"
"strings"
"github.com/fumiama/paper-manager/backend/utils"
)
@@ -75,6 +76,39 @@ func init() {
writeresult(w, codeSuccess, nil, messageOk, typeSuccess)
}}
apimap["/api/register"] = &apihandler{"POST", func(w http.ResponseWriter, r *http.Request) {
type registerbody struct {
Username string `json:"username"`
Mobile string `json:"mobile"`
Password string `json:"password"`
}
if r.Header.Get("Authorization") != "" {
writeresult(w, codeError, nil, errInvalidToken.Error(), typeError)
return
}
var body registerbody
defer r.Body.Close()
err := json.NewDecoder(r.Body).Decode(&body)
if err != nil {
writeresult(w, codeError, nil, err.Error(), typeError)
return
}
ip := r.RemoteAddr
i := strings.LastIndex(ip, ":")
if i >= 0 {
ip = ip[:i]
}
err = register(ip, body.Username, body.Mobile, body.Password)
if err != nil {
writeresult(w, codeError, nil, err.Error(), typeError)
return
}
type message struct {
M string `json:"msg"`
}
writeresult(w, codeSuccess, &message{M: "成功, 请耐心等待通知"}, messageOk, typeSuccess)
}}
apimap["/api/getUsersCount"] = &apihandler{"GET", func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
n, err := getUsersCount(token)