login_api.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. "go.mod/web/viewmodels"
  14. )
  15. type LoginApi struct {
  16. //框架-web应用上下文环境
  17. Ctx iris.Context
  18. // 需要用的service
  19. ServiceProjectAccount services.ProjectAccountService
  20. ServiceLogin services.LoginService
  21. ServiceProject services.ProjectService
  22. }
  23. // @Summary 登录
  24. // @Description 登录接口
  25. // @Tags 登录/登出
  26. // @Accept json
  27. // @Produce json
  28. // @Param account body string true "项目账号" default(caipin)
  29. // @Param password body string true "密码" default(123456)
  30. // @Success 200 {object} viewmodels.ProjectAccount "{code:0成功,data:viewmodels.ProjectAccount,msg:}"
  31. // @Failure 400 {string} string "{code:-1参数类错误,msg:错误信息}"
  32. // @Router /api/login [post]
  33. func (c *LoginApi) Post() {
  34. //验证规则
  35. LoginData, err := c.ServiceLogin.ValidRule(c.Ctx)
  36. if err != nil {
  37. ErrMsg := utils.FormValidError(err)
  38. c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
  39. return
  40. }
  41. //验证登陆用户
  42. err, token := c.ServiceLogin.ValidProjectAccount(LoginData, c.Ctx.ResponseWriter())
  43. if err != nil {
  44. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  45. return
  46. }
  47. c.Ctx.JSON(iris.Map{
  48. "code": 0,
  49. "msg": "",
  50. "data": token,
  51. })
  52. }
  53. // @Summary 登出
  54. // @Tags 登录/登出
  55. // @Accept json
  56. // @Produce json
  57. // @Success 200 {string} string "{code:0,msg:string}"
  58. // @Failure 400 {string} string "{code:-1,msg:string}"
  59. // @Router /api/login/out [post]
  60. func (c *LoginApi) PostOut() {
  61. c.ServiceLogin.Out(c.Ctx)
  62. c.Ctx.JSON(iris.Map{"code": 0, "msg": ""})
  63. }
  64. // @Summary 获得项目名称
  65. // @Tags 登录/登出
  66. // @Accept json
  67. // @Produce json
  68. // @Param code query string true "项目编号" default({code:2})
  69. // @Success 200 {object} viewmodels.Project "{code:0成功,data:viewmodels.Project,msg:}"
  70. // @Failure 400 {string} string "{code:-1,msg:string}"
  71. // @Router /api/login/project/name [get]
  72. func (c *LoginApi) GetProjectName() {
  73. // 验证内容
  74. ProjectData := viewmodels.Project{}
  75. err := c.Ctx.ReadForm(&ProjectData)
  76. if err != nil {
  77. c.Ctx.JSON(iris.Map{
  78. "code": -1,
  79. "msg": "ReadJSON转换异常,请检查code",
  80. })
  81. return
  82. } else {
  83. fmt.Println("=========================")
  84. fmt.Println(ProjectData)
  85. // 获得项目信息
  86. dataList := c.ServiceProject.GetName(ProjectData.Code)
  87. c.Ctx.JSON(iris.Map{
  88. "code": 0,
  89. "msg": "",
  90. "data": dataList,
  91. })
  92. }
  93. }
  94. // func (c *LoginApi) Get() {
  95. // token := jwt.NewTokenWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
  96. // // 根据需求,可以存一些必要的数据
  97. // "userName": "JabinGP",
  98. // "userId": "1",
  99. // "admin": true,
  100. // // 签发人
  101. // "iss": "iris",
  102. // // 签发时间
  103. // "iat": time.Now().Unix(),
  104. // // 设定过期时间,便于测试,设置1分钟过期
  105. // "exp": time.Now().Add(1 * time.Minute * time.Duration(1)).Unix(),
  106. // })
  107. // // 使用设置的秘钥,签名生成jwt字符串
  108. // tokenString, _ := token.SignedString([]byte(conf.SignSecret))
  109. // // 返回
  110. // c.Ctx.JSON(tokenString)
  111. // }
  112. // @Summary 登录
  113. // @Description 登录接口
  114. // @Tags 创建账号
  115. // @Accept json
  116. // @Produce json
  117. // @Param account body string true "项目账号" default(caipin)
  118. // @Param password body string true "密码" default(123456)
  119. // @Success 200 {object} viewmodels.ProjectAccount "{code:0成功,data:viewmodels.ProjectAccount,msg:}"
  120. // @Failure 400 {string} string "{code:-1参数类错误,msg:错误信息}"
  121. // @Router /api/login/create/acount [post]
  122. func (c *LoginApi) PostCreateAccount() {
  123. //验证规则
  124. LoginData, err := c.ServiceLogin.ValidRule(c.Ctx)
  125. if err != nil {
  126. ErrMsg := utils.FormValidError(err)
  127. c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
  128. return
  129. }
  130. err = c.ServiceLogin.CrtateAccount(LoginData)
  131. if err != nil {
  132. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  133. return
  134. }
  135. c.Ctx.JSON(iris.Map{
  136. "code": 0,
  137. "msg": "",
  138. })
  139. }