project_setting.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * @description: 项目设置相关--管理员可访问
  3. * @Author: CP
  4. * @Date: 2020-10-09 10:35:38
  5. * @FilePath: \construction_management\web\api\project_setting.go
  6. */
  7. package api
  8. import (
  9. "fmt"
  10. "github.com/iris-contrib/middleware/csrf"
  11. "github.com/kataras/iris/v12"
  12. "go.mod/services"
  13. "go.mod/web/utils"
  14. "go.mod/web/viewmodels"
  15. )
  16. type ProjectSettingApi struct {
  17. //框架-web应用上下文环境
  18. Ctx iris.Context
  19. // 需要用的service
  20. ServiceProjectAccount services.ProjectAccountService
  21. }
  22. // @Summary 获得项目账号列表
  23. // @Tags 项目设置-管理员
  24. // @Description id获得单条信息<br/>name模糊检索
  25. // @Accept json
  26. // @Produce json
  27. // @Security ApiKeyAuth
  28. // @Param id body string false "账号ID"
  29. // @Success 200 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  30. // @Router /api/projectSetting/account [get]
  31. func (c *ProjectSettingApi) GetAccount() {
  32. // 获得项目ID
  33. projectIdInt, err := utils.GetProjectId(c.Ctx)
  34. if err != nil {
  35. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  36. return
  37. }
  38. // 获得账号ID
  39. AccountData := viewmodels.ProjectAccount{}
  40. err = c.Ctx.ReadJSON(&AccountData)
  41. if err != nil {
  42. // 获得所有项目下的账号
  43. dataList := c.ServiceProjectAccount.GetAll(projectIdInt)
  44. c.Ctx.JSON(iris.Map{
  45. "code": 0,
  46. "msg": "",
  47. "data": dataList,
  48. })
  49. } else {
  50. // 获得单个账号ID
  51. id, err := utils.GetDecryptId(AccountData.Id)
  52. if err != nil {
  53. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  54. return
  55. }
  56. accountData := c.ServiceProjectAccount.Get(id, projectIdInt)
  57. c.Ctx.JSON(iris.Map{
  58. "code": 0,
  59. "msg": "",
  60. "data": accountData,
  61. })
  62. }
  63. }
  64. // @Summary 检索账号信息
  65. // @Tags 项目设置-管理员
  66. // @Description 检索字段:账号 姓名 单位 手机 前匹配
  67. // @Accept json
  68. // @Produce json
  69. // @Security ApiKeyAuth
  70. // @Param name body string true "检索内容"
  71. // @Success 200 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  72. // @Router /api/projectSetting/account/search [get]
  73. func (c *ProjectSettingApi) GetAccountSearch() {
  74. // 获得项目ID
  75. projectIdInt, err := utils.GetProjectId(c.Ctx)
  76. if err != nil {
  77. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  78. return
  79. }
  80. c.Ctx.Header("X-CSRF-Token", csrf.Token(c.Ctx))
  81. fmt.Println(c.Ctx.GetHeader("X-CSRF-Token"))
  82. fmt.Println(csrf.Token(c.Ctx))
  83. // 获得检索关键字
  84. AccountData := viewmodels.ProjectAccount{}
  85. err = c.Ctx.ReadJSON(&AccountData)
  86. if err != nil {
  87. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("JSON转换异常, error=%s", err)})
  88. return
  89. }
  90. // 检索
  91. dataList := c.ServiceProjectAccount.Search(AccountData.Name, projectIdInt)
  92. c.Ctx.JSON(iris.Map{
  93. "code": 0, "msg": "",
  94. "data": dataList,
  95. })
  96. }
  97. // @Summary 新增账号
  98. // @Tags 项目设置-管理员
  99. // @Description 新增账号
  100. // @Accept json
  101. // @Produce json
  102. // @Security ApiKeyAuth
  103. // @Param account body string true "账号"
  104. // @Param password body string true "密码"
  105. // @Param name body string true "姓名"
  106. // @Param company body string true "公司"
  107. // @Param position body string true "职位"
  108. // @Param mobile body string true "手机"
  109. // @Param telephone body string true "座机"
  110. // @Param accountGroup body string true "账号组"
  111. // @Success 200 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  112. // @Router /api/projectSetting/account/add [post]
  113. func (c *ProjectSettingApi) PostAccountAdd() {
  114. ErrMsg := ""
  115. // 验证内容
  116. AccountData, err := c.ServiceProjectAccount.ValidRule(c.Ctx)
  117. if err != nil {
  118. ErrMsg = utils.FormValidError(err)
  119. c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
  120. return
  121. } else {
  122. // 新增账号信息
  123. err := c.ServiceProjectAccount.Add(AccountData)
  124. if err != nil {
  125. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  126. return
  127. }
  128. c.Ctx.JSON(iris.Map{"code": 0, "msg": "新增成功"})
  129. }
  130. }