Save user preferences

Signed-off-by: Yuan Si <do4suki@gmail.com>
This commit is contained in:
2023-04-18 01:16:21 +08:00
parent 9461544a15
commit 8b671523d9
11 changed files with 260 additions and 42 deletions
+47 -2
View File
@@ -5,10 +5,13 @@ import (
"encoding/json"
"fmt"
"honoka-chan/encrypt"
"honoka-chan/model"
"honoka-chan/tools"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/tidwall/gjson"
)
type NotificationResp struct {
@@ -37,12 +40,54 @@ func SetNotificationTokenHandler(ctx *gin.Context) {
}
func ChangeNaviHandler(ctx *gin.Context) {
notifResp := NotificationResp{
req := gjson.Parse(ctx.PostForm("request_data"))
pref := tools.UserPref{
UnitOwningUserID: int(req.Get("unit_owning_user_id").Int()),
}
_, err := UserEng.Table("user_preference_m").Where("user_id = ?", ctx.GetString("userid")).Update(&pref)
CheckErr(err)
naviResp := model.UserNaviChangeResp{
ResponseData: []interface{}{},
ReleaseInfo: []interface{}{},
StatusCode: 200,
}
resp, err := json.Marshal(notifResp)
resp, err := json.Marshal(naviResp)
CheckErr(err)
nonce := ctx.GetInt("nonce")
nonce++
ctx.Header("user_id", ctx.GetString("userid"))
ctx.Header("authorize", fmt.Sprintf("consumerKey=lovelive_test&timeStamp=%d&version=1.1&token=%s&nonce=%d&user_id=%s&requestTimeStamp=%d", time.Now().Unix(), ctx.GetString("token"), nonce, ctx.GetString("userid"), ctx.GetInt64("req_time")))
ctx.Header("X-Message-Sign", base64.StdEncoding.EncodeToString(encrypt.RSA_Sign_SHA1(resp, "privatekey.pem")))
ctx.String(http.StatusOK, string(resp))
}
func ChangeNameHandler(ctx *gin.Context) {
req := gjson.Parse(ctx.PostForm("request_data"))
var oldName string
exists, err := UserEng.Table("user_preference_m").Where("user_id = ?", ctx.GetString("userid")).Cols("user_name").Get(&oldName)
CheckErr(err)
if !exists {
ctx.String(http.StatusForbidden, ErrorMsg)
return
}
pref := tools.UserPref{
UserName: req.Get("name").String(),
}
_, err = UserEng.Table("user_preference_m").Where("user_id = ?", ctx.GetString("userid")).Update(&pref)
CheckErr(err)
nameResp := model.UserNameChangeResp{
ResponseData: model.UserNameChangeRes{
BeforeName: oldName,
AfterName: req.Get("name").String(),
ServerTimestamp: time.Now().Unix(),
},
ReleaseInfo: []interface{}{},
StatusCode: 200,
}
resp, err := json.Marshal(nameResp)
CheckErr(err)
nonce := ctx.GetInt("nonce")