project_account_api.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * @description: 项目账号相关
  3. * @Author: CP
  4. * @Date: 2020-09-25 14:42:31
  5. * @FilePath: \construction_management\web\api\project_account_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 ProjectAccountApi struct {
  16. //框架-web应用上下文环境
  17. Ctx iris.Context
  18. // 需要用的service
  19. ServiceProjectAccount services.ProjectAccountService
  20. }
  21. // @Summary 获得登陆账号信息相关
  22. // @Tags 项目账号相关
  23. // @Description 获得登陆账号相关信息
  24. // @Security ApiKeyAuth
  25. // @Success 200 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  26. // @Router /api/projectAccount [get]
  27. func (c *ProjectAccountApi) Get() {
  28. // 获得项目账号ID
  29. projectAccountIdInt, err := utils.GetProjectAccountId(c.Ctx)
  30. if err != nil {
  31. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  32. return
  33. }
  34. // 获得项目ID
  35. projectIdInt, err := utils.GetProjectId(c.Ctx)
  36. if err != nil {
  37. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  38. return
  39. }
  40. // 获得登陆用户
  41. AccountData := c.ServiceProjectAccount.Get(projectAccountIdInt, projectIdInt)
  42. c.Ctx.JSON(iris.Map{
  43. "code": 0,
  44. "msg": "",
  45. "data": AccountData,
  46. })
  47. }
  48. // @Summary 获得项目账号列表
  49. // @Tags 项目账号相关
  50. // @Description 获得项目账号列表
  51. // @Security ApiKeyAuth
  52. // @Success 200 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  53. // @Router /api/projectAccount/list [get]
  54. func (c *ProjectAccountApi) GetList() {
  55. // 获得项目ID
  56. projectIdInt, err := utils.GetProjectId(c.Ctx)
  57. if err != nil {
  58. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  59. return
  60. }
  61. AccountData := c.ServiceProjectAccount.GetAll(projectIdInt)
  62. c.Ctx.JSON(iris.Map{
  63. "code": 0,
  64. "msg": "",
  65. "data": AccountData,
  66. })
  67. }
  68. // @Summary 获得项目账号组
  69. // @Tags 项目账号相关
  70. // @Description 获得项目账号组
  71. // @Security ApiKeyAuth
  72. // @Success 200 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  73. // @Router /api/projectAccount/group [get]
  74. func (c *ProjectAccountApi) GetGroup() {
  75. // 获得检索关键字
  76. // AccountData := viewmodels.ProjectAccount{}
  77. // err := c.Ctx.ReadForm(&AccountData)
  78. // if err != nil {
  79. // c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("JSON转换异常, error=%s", err)})
  80. // return
  81. // }
  82. // 获得项目ID
  83. // projectId, err := utils.GetProjectId(c.Ctx)
  84. // if err != nil {
  85. // c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  86. // return
  87. // }
  88. // accountData := c.ServiceProjectAccount.GetAll(projectId)
  89. // 检索
  90. // accountData := c.ServiceProjectAccount.Search(AccountData.Name, projectId)
  91. groupDate := utils.GetAccountGroup()
  92. c.Ctx.JSON(iris.Map{
  93. "code": 0,
  94. "msg": "",
  95. "data": groupDate,
  96. // "accountData": accountData,
  97. })
  98. }
  99. // @Summary 检索账号信息
  100. // @Tags 项目账号相关
  101. // @Description 检索字段:账号 姓名 单位 手机 前匹配
  102. // @Accept json
  103. // @Produce json
  104. // @Security ApiKeyAuth
  105. // @Param name body string true "检索内容"
  106. // @Success 200 {object} viewmodels.ProjectAccount "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  107. // @Router /api/projectAccount/search [get]
  108. func (c *ProjectAccountApi) GetSearch() {
  109. // 获得项目ID
  110. projectIdInt, err := utils.GetProjectId(c.Ctx)
  111. if err != nil {
  112. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  113. return
  114. }
  115. // 获得检索关键字
  116. AccountData := viewmodels.ProjectAccount{}
  117. err = c.Ctx.ReadForm(&AccountData)
  118. if err != nil {
  119. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("JSON转换异常, error=%s", err)})
  120. return
  121. }
  122. // 检索
  123. dataList := c.ServiceProjectAccount.Search(AccountData.Name, projectIdInt)
  124. c.Ctx.JSON(iris.Map{
  125. "code": 0, "msg": "",
  126. "data": dataList,
  127. })
  128. }
  129. // @Summary 编辑账号
  130. // @Tags 项目账号相关
  131. // @Description 编辑账号
  132. // @Accept json
  133. // @Produce json
  134. // @Security ApiKeyAuth
  135. // @Param id body string true "账号ID" default(PcqqGsn1O0jBSmLqkuOTwQ)
  136. // @Param name body string true "姓名"
  137. // @Param company body string true "公司"
  138. // @Param position body string true "职位"
  139. // @Param telephone body string true "座机"
  140. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  141. // @Router /api/projectAccount/account/save [post]
  142. func (c *ProjectAccountApi) PostAccountSave() {
  143. // 验证内容
  144. AccountData, err := c.ServiceProjectAccount.ValidRuleAccount(c.Ctx)
  145. if err != nil {
  146. ErrMsg := utils.FormValidError(err)
  147. c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
  148. return
  149. } else {
  150. // 获得更新账号ID
  151. id, err := utils.GetDecryptId(AccountData.Id)
  152. if err != nil {
  153. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  154. return
  155. }
  156. projectId, err := utils.GetProjectId(c.Ctx)
  157. if err != nil {
  158. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  159. return
  160. }
  161. err = c.ServiceProjectAccount.SaveAccount(AccountData, id, projectId)
  162. if err != nil {
  163. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  164. return
  165. }
  166. c.Ctx.JSON(iris.Map{"code": 0, "msg": "编辑成功"})
  167. }
  168. }
  169. // @Summary 修改密码
  170. // @Tags 项目账号相关
  171. // @Description 编辑账号
  172. // @Accept json
  173. // @Produce json
  174. // @Security ApiKeyAuth
  175. // @Param password body string true "密码"
  176. // @Param newPassword body string true "新密码"
  177. // @Param confirmPassword body string true "确认新密码"
  178. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  179. // @Router /api/projectAccount/change/password [post]
  180. func (c *ProjectAccountApi) PostChangePassword() {
  181. // 验证内容
  182. AccountData, err := c.ServiceProjectAccount.ValidRuleChangePassword(c.Ctx)
  183. if err != nil {
  184. ErrMsg := utils.FormValidError(err)
  185. c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
  186. return
  187. } else {
  188. if AccountData.ConfirmPassword != AccountData.NewPassword {
  189. c.Ctx.JSON(iris.Map{"code": -1, "msg": "新密码和确认密码不一致"})
  190. return
  191. }
  192. // 获得项目ID
  193. projectId, err := utils.GetProjectId(c.Ctx)
  194. if err != nil {
  195. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  196. return
  197. }
  198. // 获得项目账号ID
  199. projectAccountId, err := utils.GetProjectAccountId(c.Ctx)
  200. if err != nil {
  201. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  202. return
  203. }
  204. err = c.ServiceProjectAccount.ChangePassword(AccountData, projectId, projectAccountId)
  205. if err != nil {
  206. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  207. return
  208. }
  209. c.Ctx.JSON(iris.Map{"code": 0, "msg": "更新成功"})
  210. }
  211. }
  212. // 获得登陆账号信息
  213. func (c *ProjectAccountApi) GetAccount() {
  214. var (
  215. accountId int
  216. projectId int
  217. err error
  218. accountData *viewmodels.ProjectAccount
  219. )
  220. if accountId, err = utils.GetProjectAccountId(c.Ctx); err != nil {
  221. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  222. return
  223. }
  224. if projectId, err = utils.GetProjectId(c.Ctx); err != nil {
  225. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  226. return
  227. }
  228. fmt.Println(accountId)
  229. accountData = c.ServiceProjectAccount.Get(accountId, projectId)
  230. if accountData.Id == "0" {
  231. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  232. return
  233. }
  234. c.Ctx.JSON(iris.Map{
  235. "code": 0,
  236. "msg": "",
  237. "data": accountData,
  238. })
  239. }