110 lines
3.1 KiB
Go
110 lines
3.1 KiB
Go
package controller
|
||
|
||
import (
|
||
"yitao/config"
|
||
"yitao/ecode"
|
||
"yitao/util"
|
||
"yitao/validate"
|
||
|
||
"github.com/iris-contrib/middleware/jwt"
|
||
"github.com/kataras/iris/v12/mvc"
|
||
)
|
||
|
||
type UserController struct {
|
||
BaseController
|
||
}
|
||
|
||
func (con *UserController) BeforeActivation(b mvc.BeforeActivation) {
|
||
// Login Api
|
||
{
|
||
b.Handle("POST", "/auth/login", "AuthLogin")
|
||
b.Handle("POST", "/auth/signup", "AuthSignup")
|
||
}
|
||
}
|
||
|
||
// @Summary login api
|
||
// @Description the 用户登录接口
|
||
// @Tags auth api
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param loginParam body validate.LoginParam true "登录参数"
|
||
// @Success 200 {object} map[string]interface{} "{"jwt": "jwt.jwt.jwt"}"
|
||
// @Failure 400 {object} map[string]interface{} "{"msg": "错误信息","code":0}"
|
||
// @Router /api/user/auth/login [post]
|
||
func (con *UserController) AuthLogin() mvc.Result {
|
||
login_param := new(validate.LoginParam)
|
||
e := validate.ReadJSON(con.Ctx, login_param)
|
||
if e.Error() {
|
||
return e.Response()
|
||
}
|
||
|
||
e = con.Service.Captcha.Validate(login_param.CaptchaKey, login_param.CaptchaData)
|
||
if e.Error() {
|
||
return e.Response()
|
||
}
|
||
|
||
user, e := con.Service.User.AuthLogin(login_param.Username, login_param.Password)
|
||
if e.Error() {
|
||
return e.Response()
|
||
}
|
||
|
||
// 写入登录日志
|
||
ip := con.Ctx.Request().RemoteAddr
|
||
// 一个测试ip
|
||
//ip = "109.123.229.220" // 日本 东京
|
||
// 获取 ip 地址
|
||
city, country, _ := util.GetCityAndCountry(ip)
|
||
con.Service.LoginLog.Save(user.ID, ip, "Web", country, city)
|
||
|
||
// 原版 NP 在这里会触发一个对登录地点的检测,但我看数据库它就没成功获取到过国家于城市
|
||
// 所以就先不做异地检测了
|
||
// @TODO 如果有人愿意的话在这做个异地检测
|
||
// 好吧现在好了,但我懒得写了,就这样吧
|
||
|
||
// 开始生成jwt
|
||
conf := config.GetConfig()
|
||
token := jwt.NewTokenWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||
"uid": user.ID,
|
||
"username": user.Username,
|
||
"time": util.GetTime(),
|
||
"is_admin": user.IsAdmin,
|
||
})
|
||
// 使用设置的秘钥,签名生成jwt字符串
|
||
tokenString, _ := token.SignedString([]byte(conf.JwtSecret))
|
||
|
||
return mvc.Response{
|
||
Object: map[string]interface{}{
|
||
"jwt": tokenString,
|
||
},
|
||
}
|
||
}
|
||
|
||
// @Summary 注册API
|
||
// @Description 用户注册接口
|
||
// @Tags auth api
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param signupParam body validate.SignupParam true "注册参数"
|
||
// @Success 200 {object} map[string]interface{} "{"msg": "注册成功","code":1}"
|
||
// @Failure 400 {object} map[string]interface{} "{"msg": "错误信息","code":0}"
|
||
// @Router /api/user/auth/signup [post]
|
||
func (con *UserController) AuthSignup() mvc.Result {
|
||
var e *ecode.Ecode
|
||
signup_param := new(validate.SignupParam)
|
||
e = validate.ReadJSON(con.Ctx, signup_param)
|
||
if e.Error() {
|
||
return e.Response()
|
||
}
|
||
|
||
e = con.Service.Captcha.Validate(signup_param.CaptchaKey, signup_param.CaptchaData)
|
||
if e.Error() {
|
||
return e.Response()
|
||
}
|
||
|
||
_, e = con.Service.User.AuthSignup(signup_param.Username, signup_param.Password)
|
||
if e.Error() {
|
||
return e.Response()
|
||
}
|
||
return ecode.OK().Response()
|
||
}
|