login_api.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. result := map[string]interface{}{
  46. "token": token,
  47. }
  48. c.Ctx.JSON(iris.Map{
  49. "code": 0,
  50. "msg": "",
  51. "result": result,
  52. })
  53. }
  54. // @Summary 登出
  55. // @Tags 登录/登出
  56. // @Accept json
  57. // @Produce json
  58. // @Success 200 {string} string "{code:0,msg:string}"
  59. // @Failure 400 {string} string "{code:-1,msg:string}"
  60. // @Router /api/login/out [post]
  61. func (c *LoginApi) PostOut() {
  62. c.ServiceLogin.Out(c.Ctx)
  63. c.Ctx.JSON(iris.Map{"code": 0, "msg": ""})
  64. }
  65. // func (c *LoginApi) Get() {
  66. // token := jwt.NewTokenWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
  67. // // 根据需求,可以存一些必要的数据
  68. // "userName": "JabinGP",
  69. // "userId": "1",
  70. // "admin": true,
  71. // // 签发人
  72. // "iss": "iris",
  73. // // 签发时间
  74. // "iat": time.Now().Unix(),
  75. // // 设定过期时间,便于测试,设置1分钟过期
  76. // "exp": time.Now().Add(1 * time.Minute * time.Duration(1)).Unix(),
  77. // })
  78. // // 使用设置的秘钥,签名生成jwt字符串
  79. // tokenString, _ := token.SignedString([]byte(conf.SignSecret))
  80. // // 返回
  81. // c.Ctx.JSON(tokenString)
  82. // }
  83. // @Summary 登录
  84. // @Description 登录接口
  85. // @Tags 创建账号
  86. // @Accept json
  87. // @Produce json
  88. // @Param account body string true "项目账号" default(caipin)
  89. // @Param password body string true "密码" default(123456)
  90. // @Success 200 {object} viewmodels.ProjectAccount "{code:0成功,data:viewmodels.ProjectAccount,msg:}"
  91. // @Failure 400 {string} string "{code:-1参数类错误,msg:错误信息}"
  92. // @Router /api/login/create/acount [post]
  93. func (c *LoginApi) PostCreateAccount() {
  94. //验证规则
  95. LoginData, err := c.ServiceLogin.ValidRule(c.Ctx)
  96. if err != nil {
  97. ErrMsg := utils.FormValidError(err)
  98. c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
  99. return
  100. }
  101. err = c.ServiceLogin.CrtateAccount(LoginData)
  102. if err != nil {
  103. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  104. return
  105. }
  106. c.Ctx.JSON(iris.Map{
  107. "code": 0,
  108. "msg": "",
  109. })
  110. }