/* * @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" "go.mod/web/viewmodels" ) type LoginApi struct { //框架-web应用上下文环境 Ctx iris.Context // 需要用的service ServiceProjectAccount services.ProjectAccountService ServiceLogin services.LoginService ServiceProject services.ProjectService } // @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 } c.Ctx.JSON(iris.Map{ "code": 0, "msg": "", "data": token, }) } // @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": ""}) } // @Summary 获得项目名称 // @Tags 登录/登出 // @Accept json // @Produce json // @Param code query string true "项目编号" default({code:2}) // @Success 200 {object} viewmodels.Project "{code:0成功,data:viewmodels.Project,msg:}" // @Failure 400 {string} string "{code:-1,msg:string}" // @Router /api/login/project/name [get] func (c *LoginApi) GetProjectName() { // 验证内容 ProjectData := viewmodels.Project{} err := c.Ctx.ReadForm(&ProjectData) if err != nil { c.Ctx.JSON(iris.Map{ "code": -1, "msg": "ReadJSON转换异常,请检查code", }) return } else { fmt.Println("=========================") fmt.Println(ProjectData) // 获得项目信息 dataList := c.ServiceProject.GetName(ProjectData.Code) c.Ctx.JSON(iris.Map{ "code": 0, "msg": "", "data": dataList, }) } } // 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": "", }) }