/* * @description: api 登陆接口相关 * @Author: CP * @Date: 2020-09-17 16:23:02 * @FilePath: \construction_management\web\api\login_api.go */ package api import ( "fmt" "github.com/kataras/iris/v12" "go.mod/services" "go.mod/web/utils" ) type LoginApi struct { //框架-web应用上下文环境 Ctx iris.Context // 需要用的service ServiceProjectAccount services.ProjectAccountService ServiceLogin services.LoginService } // @Summary 登录 // @Description 登录接口 // @Tags 登录/登出 // @Accept json // @Produce json // @Param account body string true "项目账号" default(caipin) // @Param password body string true "密码" default(123456) // @Success 200 {object} viewmodels.ProjectAccount "{code:0成功,data:viewmodels.ProjectAccount,msg:}" // @Failure 400 {string} string "{code:-1参数类错误,msg:错误信息}" // @Router /api/login [post] func (c *LoginApi) Post() { //验证规则 LoginData, err := c.ServiceLogin.ValidRule(c.Ctx) if err != nil { ErrMsg := utils.FormValidError(err) c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg}) return } //验证登陆用户 err, token := c.ServiceLogin.ValidProjectAccount(LoginData, c.Ctx.ResponseWriter()) if err != nil { c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)}) return } result := map[string]interface{}{ "token": token, } c.Ctx.JSON(iris.Map{ "code": 0, "msg": "", "result": result, }) } // @Summary 登出 // @Tags 登录/登出 // @Accept json // @Produce json // @Success 200 {string} string "{code:0,msg:string}" // @Failure 400 {string} string "{code:-1,msg:string}" // @Router /api/login/out [post] func (c *LoginApi) PostOut() { c.ServiceLogin.Out(c.Ctx) c.Ctx.JSON(iris.Map{"code": 0, "msg": ""}) } // func (c *LoginApi) Get() { // token := jwt.NewTokenWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ // // 根据需求,可以存一些必要的数据 // "userName": "JabinGP", // "userId": "1", // "admin": true, // // 签发人 // "iss": "iris", // // 签发时间 // "iat": time.Now().Unix(), // // 设定过期时间,便于测试,设置1分钟过期 // "exp": time.Now().Add(1 * time.Minute * time.Duration(1)).Unix(), // }) // // 使用设置的秘钥,签名生成jwt字符串 // tokenString, _ := token.SignedString([]byte(conf.SignSecret)) // // 返回 // c.Ctx.JSON(tokenString) // } // @Summary 登录 // @Description 登录接口 // @Tags 创建账号 // @Accept json // @Produce json // @Param account body string true "项目账号" default(caipin) // @Param password body string true "密码" default(123456) // @Success 200 {object} viewmodels.ProjectAccount "{code:0成功,data:viewmodels.ProjectAccount,msg:}" // @Failure 400 {string} string "{code:-1参数类错误,msg:错误信息}" // @Router /api/login/create/acount [post] func (c *LoginApi) PostCreateAccount() { //验证规则 LoginData, err := c.ServiceLogin.ValidRule(c.Ctx) if err != nil { ErrMsg := utils.FormValidError(err) c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg}) return } err = c.ServiceLogin.CrtateAccount(LoginData) if err != nil { c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)}) return } c.Ctx.JSON(iris.Map{ "code": 0, "msg": "", }) }