Commit 15c300e5 by xuzhenhao

修正了跨域问题

1 parent a6b95a8c
......@@ -13,7 +13,7 @@ import (
// "net/url"
// "os"
// "reflect"
// "strconv"
"strconv"
// "strings"
"time"
)
......@@ -120,15 +120,27 @@ func CheckUserNameRepeatApi(c *gin.Context) {
//修改密码接口
func ChangeUserPasswordApi(c *gin.Context) {
userName := c.Request.FormValue("user_name")
token, err := c.Cookie("icesimbaToken")
if err != nil {
log.Println(err)
ReturnRespones(c, "104000", err)
return
}
r, err := CheckRequestToken(token)
if err != nil {
log.Println(err)
ReturnRespones(c, r.(string), err)
return
}
userId, err := strconv.Atoi(r.(string))
userOriginPassword := c.Request.FormValue("user_origin_password")
userNewPassword := c.Request.FormValue("user_new_password")
if userName == "" || userOriginPassword == "" || userNewPassword == "" {
log.Println("userName:", userName, "userOriginPassword:", userOriginPassword, "userNewPassword:", userNewPassword)
if userOriginPassword == "" || userNewPassword == "" {
log.Println("userId:", userId, "userOriginPassword:", userOriginPassword, "userNewPassword:", userNewPassword)
ReturnRespones(c, "104002", "参数错误")
return
}
result, err := ChangeUserPassword(userName, userOriginPassword, userNewPassword)
result, err := ChangeUserPassword(userId, userOriginPassword, userNewPassword)
if err != nil {
log.Println(err)
ReturnRespones(c, result.(string), err.Error())
......
......@@ -111,9 +111,26 @@ func GetUserInfoByUserName(userName string) (result interface{}, err error) {
}
//获取用户信息
func GetUserInfoByUserId(userId int) (result interface{}, err error) {
u := make([]UserInfo, 0)
sql := ` select id,user_name,user_password from icesimba_polysdk_user where id = ? limit 1`
err = db.Engine.Sql(sql, userId).Find(&u)
if err != nil {
return "104001", err
}
if len(u) == 0 {
err = errors.New("不存在的用户id")
return "105003", err
}
return u[0], nil
}
//修改用户密码
func ChangeUserPassword(userName string, originPassword string, newPassword string) (result interface{}, err error) {
r, err := GetUserInfoByUserName(userName)
func ChangeUserPassword(userId int, originPassword string, newPassword string) (result interface{}, err error) {
r, err := GetUserInfoByUserId(userId)
if err != nil {
return r, err
}
......@@ -127,8 +144,8 @@ func ChangeUserPassword(userName string, originPassword string, newPassword stri
return "105002", err
}
password = MD5(newPassword)
sql := ` update icesimba_polysdk_user set user_password = ? where user_name = ?`
_, err = db.Engine.Exec(sql, password, userName)
sql := ` update icesimba_polysdk_user set user_password = ? where id = ?`
_, err = db.Engine.Exec(sql, password, userId)
if err != nil {
return "104001", err
}
......
......@@ -13,6 +13,7 @@ func initRouter() *gin.Engine {
router := gin.Default()
config := cors.DefaultConfig()
config.AllowOrigins = []string{"http://127.0.0.1:8000"}
config.AllowCredentials = true
router.Use(cors.New(config))
router.POST("user/login", UserLoginApi)
router.POST("user/register", UserRegisterApi)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!