webui: Add profile reset defaults
- Add a reset action for profile values in the admin page - Restore reset values to the auto-created user defaults instead of the bootstrap user - Remind users to re-login after saving or resetting profile changes Signed-off-by: Sean Du <do4suki@gmail.com>
This commit is contained in:
@@ -136,8 +136,8 @@ func addUser(dbSession *xorm.Session, phone, password string, isDefault bool) (g
|
|||||||
awardID := 1 // 音乃木坂学生
|
awardID := 1 // 音乃木坂学生
|
||||||
backgroundID := 1 // 初始背景
|
backgroundID := 1 // 初始背景
|
||||||
unitID := 31 // 初始高坂穂乃果
|
unitID := 31 // 初始高坂穂乃果
|
||||||
userName := "音乃木坂学生"
|
userName := usermodel.DefaultAutoUserName
|
||||||
userDesc := "你好。"
|
userDesc := usermodel.DefaultAutoUserDesc
|
||||||
// 是否初始化
|
// 是否初始化
|
||||||
if isDefault {
|
if isDefault {
|
||||||
// 初始化默认用户
|
// 初始化默认用户
|
||||||
|
|||||||
@@ -135,7 +135,55 @@ func updateProfile(ctx *gin.Context) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func resetProfile(ctx *gin.Context) {
|
||||||
|
pref := usermodel.UserPref{}
|
||||||
|
exists, err := db.UserEng.Table(new(usermodel.UserPref)).
|
||||||
|
Where("user_id = ?", ctx.GetInt("userid")).Get(&pref)
|
||||||
|
if err != nil || !exists {
|
||||||
|
ctx.JSON(http.StatusOK, webuischema.Msg{
|
||||||
|
Code: 1,
|
||||||
|
Message: "用户资料读取失败!",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
pref.ResetProfileDefaults()
|
||||||
|
pref.UpdateTime = time.Now().Unix()
|
||||||
|
|
||||||
|
_, err = db.UserEng.Table(new(usermodel.UserPref)).
|
||||||
|
Where("user_id = ?", ctx.GetInt("userid")).
|
||||||
|
Cols(
|
||||||
|
"user_name",
|
||||||
|
"user_level",
|
||||||
|
"user_desc",
|
||||||
|
"invite_code",
|
||||||
|
"user_exp",
|
||||||
|
"next_exp",
|
||||||
|
"game_coin",
|
||||||
|
"sns_coin",
|
||||||
|
"energy_max",
|
||||||
|
"over_max_energy",
|
||||||
|
"profile_version",
|
||||||
|
"update_time",
|
||||||
|
).
|
||||||
|
Update(&pref)
|
||||||
|
if err != nil {
|
||||||
|
ctx.JSON(http.StatusOK, webuischema.Msg{
|
||||||
|
Code: 1,
|
||||||
|
Message: "重置失败!",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.JSON(http.StatusOK, webuischema.Msg{
|
||||||
|
Code: 0,
|
||||||
|
Message: "已恢复默认值!",
|
||||||
|
Redirect: "/admin/profile",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
router.AddHandler("admin", "GET", "/profile", middleware.WebAuth, profilePage)
|
router.AddHandler("admin", "GET", "/profile", middleware.WebAuth, profilePage)
|
||||||
router.AddHandler("admin", "POST", "/profile", middleware.WebAuth, updateProfile)
|
router.AddHandler("admin", "POST", "/profile", middleware.WebAuth, updateProfile)
|
||||||
|
router.AddHandler("admin", "POST", "/profile/reset", middleware.WebAuth, resetProfile)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import (
|
|||||||
const CurrentUserPrefProfileVersion = 1
|
const CurrentUserPrefProfileVersion = 1
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
DefaultAutoUserName = "音乃木坂学生"
|
||||||
|
DefaultAutoUserDesc = "你好。"
|
||||||
DefaultUserLevel = 1028
|
DefaultUserLevel = 1028
|
||||||
DefaultUserExp = 1089696
|
DefaultUserExp = 1089696
|
||||||
DefaultUserNextExp = 1207185
|
DefaultUserNextExp = 1207185
|
||||||
@@ -65,6 +67,22 @@ func (u *UserPref) ApplyProfileDefaults() {
|
|||||||
u.ProfileVersion = CurrentUserPrefProfileVersion
|
u.ProfileVersion = CurrentUserPrefProfileVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *UserPref) ResetProfileDefaults() {
|
||||||
|
u.UserName = DefaultAutoUserName
|
||||||
|
u.UserDesc = DefaultAutoUserDesc
|
||||||
|
u.UserLevel = DefaultUserLevel
|
||||||
|
u.UserExp = DefaultUserExp
|
||||||
|
u.NextExp = DefaultUserNextExp
|
||||||
|
u.GameCoin = DefaultUserGameCoin
|
||||||
|
u.SnsCoin = DefaultUserSnsCoin
|
||||||
|
u.EnergyMax = DefaultUserEnergyMax
|
||||||
|
u.OverMaxEnergy = DefaultUserOverMaxEnergy
|
||||||
|
if u.UserID > 0 {
|
||||||
|
u.InviteCode = strconv.Itoa(u.UserID)
|
||||||
|
}
|
||||||
|
u.ProfileVersion = CurrentUserPrefProfileVersion
|
||||||
|
}
|
||||||
|
|
||||||
func (u UserPref) NeedsProfileMigration() bool {
|
func (u UserPref) NeedsProfileMigration() bool {
|
||||||
return u.ProfileVersion < CurrentUserPrefProfileVersion
|
return u.ProfileVersion < CurrentUserPrefProfileVersion
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,25 @@
|
|||||||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
|
||||||
<title>SIF 小助手</title>
|
<title>SIF 小助手</title>
|
||||||
{{ template "admin/nav/css.html" . }}
|
{{ template "admin/nav/css.html" . }}
|
||||||
|
<style>
|
||||||
|
.profile-action-row {
|
||||||
|
display: flex;
|
||||||
|
gap: 12px;
|
||||||
|
align-items: center;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.profile-action-row .layui-btn+.layui-btn {
|
||||||
|
margin-left: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media screen and (max-width: 767.98px) {
|
||||||
|
.profile-action-row {
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: stretch;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body class="layui-layout-body">
|
<body class="layui-layout-body">
|
||||||
@@ -90,7 +109,10 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="layui-form-item">
|
<div class="layui-form-item">
|
||||||
<div class="layui-input-block">
|
<div class="layui-input-block">
|
||||||
<button type="button" class="layui-btn" id="save-profile">保存</button>
|
<div class="profile-action-row">
|
||||||
|
<button type="button" class="layui-btn" id="save-profile">保存</button>
|
||||||
|
<button type="button" class="layui-btn layui-btn-primary" id="reset-profile">重置默认值</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
@@ -105,11 +127,35 @@
|
|||||||
|
|
||||||
$("#save-profile").click(function () {
|
$("#save-profile").click(function () {
|
||||||
$.post("/admin/profile", $("form").serialize(), function (res) {
|
$.post("/admin/profile", $("form").serialize(), function (res) {
|
||||||
layer.msg(res.message || "请求完成");
|
var message = res.message || "请求完成";
|
||||||
|
if (res.code === 0) {
|
||||||
|
message += " 请重新登录游戏生效。";
|
||||||
|
}
|
||||||
|
layer.msg(message);
|
||||||
}).fail(function () {
|
}).fail(function () {
|
||||||
layer.msg("请求失败,请稍后再试");
|
layer.msg("请求失败,请稍后再试");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$("#reset-profile").click(function () {
|
||||||
|
layer.confirm("将数值项和用户 ID 恢复为默认安全值,是否继续?", function (index) {
|
||||||
|
$.post("/admin/profile/reset", function (res) {
|
||||||
|
var message = res.message || "请求完成";
|
||||||
|
if (res.code === 0) {
|
||||||
|
message += " 请重新登录游戏生效。";
|
||||||
|
}
|
||||||
|
layer.msg(message);
|
||||||
|
if (res.code === 0) {
|
||||||
|
setTimeout(function () {
|
||||||
|
window.location.reload();
|
||||||
|
}, 1800);
|
||||||
|
}
|
||||||
|
}).fail(function () {
|
||||||
|
layer.msg("请求失败,请稍后再试");
|
||||||
|
});
|
||||||
|
layer.close(index);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
Reference in New Issue
Block a user