login_api.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * @description: api 登陆接口相关
  3. * @Author: CP
  4. * @Date: 2020-09-17 16:23:02
  5. * @FilePath: \construction_management\web\api\login_api.go
  6. */
  7. package api
  8. import (
  9. "fmt"
  10. "github.com/kataras/iris/v12"
  11. "go.mod/services"
  12. "go.mod/web/utils"
  13. )
  14. type LoginApi struct {
  15. //框架-web应用上下文环境
  16. Ctx iris.Context
  17. // 需要用的service
  18. ServiceProjectAccount services.ProjectAccountService
  19. ServiceLogin services.LoginService
  20. }
  21. // @Summary 登录
  22. // @Description 登录接口
  23. // @Tags 登录/登出
  24. // @Accept json
  25. // @Produce json
  26. // @Param account body string true "项目账号" default(caipin)
  27. // @Param password body string true "密码" default(123456)
  28. // @Success 200 {object} viewmodels.ProjectAccount "{code:0成功,data:viewmodels.ProjectAccount,msg:}"
  29. // @Failure 400 {string} string "{code:-1参数类错误,msg:错误信息}"
  30. // @Router /api/login [post]
  31. func (c *LoginApi) Post() {
  32. //验证规则
  33. LoginData, err := c.ServiceLogin.ValidRule(c.Ctx)
  34. if err != nil {
  35. ErrMsg := utils.FormValidError(err)
  36. c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
  37. return
  38. }
  39. //验证登陆用户
  40. err, token := c.ServiceLogin.ValidProjectAccount(LoginData, c.Ctx.ResponseWriter())
  41. if err != nil {
  42. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  43. return
  44. }
  45. c.Ctx.JSON(iris.Map{
  46. "code": 0,
  47. "msg": "",
  48. "result": token,
  49. })
  50. }
  51. // @Summary 登出
  52. // @Tags 登录/登出
  53. // @Accept json
  54. // @Produce json
  55. // @Success 200 {string} string "{code:0,msg:string}"
  56. // @Failure 400 {string} string "{code:-1,msg:string}"
  57. // @Router /api/login/out [post]
  58. func (c *LoginApi) PostOut() {
  59. c.ServiceLogin.Out(c.Ctx)
  60. c.Ctx.JSON(iris.Map{"code": 0, "msg": ""})
  61. }
  62. // func (c *LoginApi) Get() {
  63. // token := jwt.NewTokenWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
  64. // // 根据需求,可以存一些必要的数据
  65. // "userName": "JabinGP",
  66. // "userId": "1",
  67. // "admin": true,
  68. // // 签发人
  69. // "iss": "iris",
  70. // // 签发时间
  71. // "iat": time.Now().Unix(),
  72. // // 设定过期时间,便于测试,设置1分钟过期
  73. // "exp": time.Now().Add(1 * time.Minute * time.Duration(1)).Unix(),
  74. // })
  75. // // 使用设置的秘钥,签名生成jwt字符串
  76. // tokenString, _ := token.SignedString([]byte(conf.SignSecret))
  77. // // 返回
  78. // c.Ctx.JSON(tokenString)
  79. // }
  80. // @Summary 登录
  81. // @Description 登录接口
  82. // @Tags 创建账号
  83. // @Accept json
  84. // @Produce json
  85. // @Param account body string true "项目账号" default(caipin)
  86. // @Param password body string true "密码" default(123456)
  87. // @Success 200 {object} viewmodels.ProjectAccount "{code:0成功,data:viewmodels.ProjectAccount,msg:}"
  88. // @Failure 400 {string} string "{code:-1参数类错误,msg:错误信息}"
  89. // @Router /api/login/create/acount [post]
  90. func (c *LoginApi) PostCreateAccount() {
  91. //验证规则
  92. LoginData, err := c.ServiceLogin.ValidRule(c.Ctx)
  93. if err != nil {
  94. ErrMsg := utils.FormValidError(err)
  95. c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
  96. return
  97. }
  98. err = c.ServiceLogin.CrtateAccount(LoginData)
  99. if err != nil {
  100. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  101. return
  102. }
  103. c.Ctx.JSON(iris.Map{
  104. "code": 0,
  105. "msg": "",
  106. })
  107. }