注册与登录bug修复
This commit is contained in:
@@ -19,17 +19,19 @@ func V1_user_api(r *gin.RouterGroup) {
|
||||
//返回前端的数据
|
||||
|
||||
//转换传进来的数据
|
||||
|
||||
var jsonData map[string]interface{}
|
||||
if err := ctx.ShouldBindJSON(&jsonData); err == nil {
|
||||
//转换传进来的数据
|
||||
var jsonData Add_user_from
|
||||
data, _ := ctx.Get("data")
|
||||
if err := mapstructure.Decode(data, &jsonData); err == nil {
|
||||
//转换字段
|
||||
newUser := models.User{
|
||||
Name: jsonData["username"].(string),
|
||||
Email: jsonData["useremail"].(string),
|
||||
Pass: jsonData["userpass"].(string), // 实际应替换为哈希值
|
||||
Name: jsonData.Username,
|
||||
Email: jsonData.Useremail,
|
||||
Pass: jsonData.Userpass, // 实际应替换为哈希值
|
||||
Date: time.Now(),
|
||||
// Date 字段无需赋值,数据库会自动填充默认值
|
||||
}
|
||||
//fmt.Println(newUser)
|
||||
//对用户的密码进行哈希替换
|
||||
newUser.Pass = models.Hash_user_pass(newUser.Pass)
|
||||
|
||||
@@ -42,9 +44,13 @@ func V1_user_api(r *gin.RouterGroup) {
|
||||
Return_json(ctx, "user_name_dup", nil)
|
||||
} else {
|
||||
//fmt.Println("用户不存在")
|
||||
dd := models.DB.Create(&newUser) // 传入指针
|
||||
fmt.Println(dd)
|
||||
models.DB.Create(&newUser) // 传入指针
|
||||
|
||||
//创建info
|
||||
var user_info models.User_info
|
||||
user_info.AvatarPath = models.User_configs["def_avatar_path"].(string)
|
||||
user_info.UserID = newUser.ID
|
||||
models.DB.Create(&user_info) // 传入指针
|
||||
|
||||
Return_json(ctx, "api_ok", nil)
|
||||
}
|
||||
@@ -77,8 +83,8 @@ func V1_user_api(r *gin.RouterGroup) {
|
||||
user.Name = newUser.Name
|
||||
if models.DB.Where(&user).First(&user).Error == nil {
|
||||
// 有数据
|
||||
fmt.Println(user)
|
||||
fmt.Println(newUser)
|
||||
//fmt.Println(user)
|
||||
//fmt.Println(newUser)
|
||||
|
||||
if user.Pass == newUser.Pass {
|
||||
//成功登录
|
||||
|
||||
@@ -5,3 +5,9 @@ type Login_from struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type Add_user_from struct {
|
||||
Useremail string `json:"useremail"`
|
||||
Username string `json:"username"`
|
||||
Userpass string `json:"userpass"`
|
||||
}
|
||||
|
||||
Vendored
+1
@@ -65,6 +65,7 @@ function post_json(path, json, callback) {
|
||||
})
|
||||
.catch((error) => {
|
||||
re_data["statusCode"] = -1;
|
||||
re_data["error"]=error;
|
||||
callback(re_data);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@
|
||||
if (c.statusCode == 200) {
|
||||
if (c.data.err_code == 0) {
|
||||
//save_json("cookie", c.data.return.cookie)
|
||||
banner_alert('success', "登录成功",9500)
|
||||
banner_alert('success', "登录成功",950)
|
||||
save_json("user_info", c.data.return.user_info)
|
||||
setTimeout(() => {
|
||||
location.href = '/'
|
||||
|
||||
@@ -218,39 +218,32 @@
|
||||
|
||||
if (from_data_check) {
|
||||
//console.log("ok");
|
||||
const url = '/api/v1/user/add';
|
||||
const sumt_data = {
|
||||
data: {
|
||||
|
||||
post_json("/user/add", {
|
||||
username: username_dom.value,
|
||||
useremail: email_dom.value,
|
||||
userpass: password_dom.value
|
||||
},
|
||||
}, (c) => {
|
||||
if (c.statusCode == 200) {
|
||||
|
||||
};
|
||||
try {
|
||||
const response = axios.post(url, sumt_data, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
switch (c.data.err_code) {
|
||||
case 0:
|
||||
banner_alert('success', "注册成功", 950)
|
||||
setTimeout(() => {
|
||||
location.href = '/sign-in'
|
||||
}, 1000);
|
||||
break;
|
||||
case 1:
|
||||
banner_alert('warning', "用户名已存在", 1000)
|
||||
break;
|
||||
}
|
||||
}).then(response => {
|
||||
console.log('提交成功:', response.data); // 正确打印服务器数据
|
||||
if (response.data.err_code == 1) {
|
||||
username_dom.classList.add("is-invalid");
|
||||
document.getElementById("name_input_err").innerHTML = "用户名已存在";
|
||||
} else if (response.data.err_code == 0) {
|
||||
const myModal = new bootstrap.Modal('#modal-success');
|
||||
myModal.show();
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
if (error.response) {
|
||||
// 服务器返回了错误状态码(如 4xx, 5xx)
|
||||
console.error('服务器错误:', error.response.data);
|
||||
|
||||
} else {
|
||||
console.error('请求未完成:', error.message);
|
||||
banner_alert('danger', "网络连接错误:" + c.error, 10000)
|
||||
}
|
||||
}
|
||||
//console.log(sumt_data);
|
||||
})
|
||||
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user