project_setting.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. ServiceProject services.ProjectService
  22. }
  23. // @Summary 获得项目账号列表
  24. // @Tags 项目设置-管理员
  25. // @Description id获得单条信息<br/>
  26. // @Accept json
  27. // @Produce json
  28. // @Security ApiKeyAuth
  29. // @Param id body string false "账号ID"
  30. // @Success 200 {object} viewmodels.ProjectAccount "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  31. // @Router /api/projectSetting/account [get]
  32. func (c *ProjectSettingApi) GetAccount() {
  33. // 获得项目ID
  34. projectIdInt, err := utils.GetProjectId(c.Ctx)
  35. if err != nil {
  36. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  37. return
  38. }
  39. // 获得账号ID
  40. AccountData := viewmodels.ProjectAccount{}
  41. err = c.Ctx.ReadJSON(&AccountData)
  42. if err != nil {
  43. // 获得所有项目下的账号
  44. dataList := c.ServiceProjectAccount.GetAll(projectIdInt)
  45. c.Ctx.JSON(iris.Map{
  46. "code": 0,
  47. "msg": "",
  48. "data": dataList,
  49. })
  50. } else {
  51. // 获得单个账号ID
  52. id, err := utils.GetDecryptId(AccountData.Id)
  53. if err != nil {
  54. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  55. return
  56. }
  57. accountData := c.ServiceProjectAccount.Get(id, projectIdInt)
  58. c.Ctx.JSON(iris.Map{
  59. "code": 0,
  60. "msg": "",
  61. "data": accountData,
  62. })
  63. }
  64. }
  65. // @Summary 检索账号信息
  66. // @Tags 项目设置-管理员
  67. // @Description 检索字段:账号 姓名 单位 手机 前匹配
  68. // @Accept json
  69. // @Produce json
  70. // @Security ApiKeyAuth
  71. // @Param name body string true "检索内容"
  72. // @Success 200 {object} viewmodels.ProjectAccount "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  73. // @Router /api/projectSetting/account/search [get]
  74. func (c *ProjectSettingApi) GetAccountSearch() {
  75. // 获得项目ID
  76. projectIdInt, err := utils.GetProjectId(c.Ctx)
  77. if err != nil {
  78. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  79. return
  80. }
  81. c.Ctx.Header("X-CSRF-Token", csrf.Token(c.Ctx))
  82. fmt.Println(c.Ctx.GetHeader("X-CSRF-Token"))
  83. fmt.Println(csrf.Token(c.Ctx))
  84. // 获得检索关键字
  85. AccountData := viewmodels.ProjectAccount{}
  86. err = c.Ctx.ReadJSON(&AccountData)
  87. if err != nil {
  88. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("JSON转换异常, error=%s", err)})
  89. return
  90. }
  91. // 检索
  92. dataList := c.ServiceProjectAccount.Search(AccountData.Name, projectIdInt)
  93. c.Ctx.JSON(iris.Map{
  94. "code": 0, "msg": "",
  95. "data": dataList,
  96. })
  97. }
  98. // @Summary 新增账号
  99. // @Tags 项目设置-管理员
  100. // @Description 新增账号
  101. // @Accept json
  102. // @Produce json
  103. // @Security ApiKeyAuth
  104. // @Param account body string true "账号"
  105. // @Param password body string true "密码"
  106. // @Param name body string true "姓名"
  107. // @Param company body string true "公司"
  108. // @Param position body string true "职位"
  109. // @Param mobile body string true "手机"
  110. // @Param telephone body string true "座机"
  111. // @Param accountGroup body int true "账号组"
  112. // @Param X-CSRF-Token header string true "csrf"
  113. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  114. // @Router /api/projectSetting/account/add [post]
  115. func (c *ProjectSettingApi) PostAccountAdd() {
  116. ErrMsg := ""
  117. // 验证内容
  118. AccountData, err := c.ServiceProjectAccount.ValidRule(c.Ctx)
  119. if err != nil {
  120. ErrMsg = utils.FormValidError(err)
  121. c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
  122. return
  123. } else {
  124. projectId, err := utils.GetProjectId(c.Ctx)
  125. if err != nil {
  126. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  127. return
  128. }
  129. // 新增账号信息
  130. err = c.ServiceProjectAccount.Add(AccountData, projectId)
  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{"code": 0, "msg": "新增成功"})
  136. }
  137. }
  138. // @Summary 编辑账号
  139. // @Tags 项目设置-管理员
  140. // @Description 编辑账号
  141. // @Accept json
  142. // @Produce json
  143. // @Security ApiKeyAuth
  144. // @Param id body string true "账号ID" default(PcqqGsn1O0jBSmLqkuOTwQ)
  145. // @Param name body string true "姓名"
  146. // @Param company body string true "公司"
  147. // @Param position body string true "职位"
  148. // @Param telephone body string true "座机"
  149. // @Param accountGroup body int true "账号组"
  150. // @Param X-CSRF-Token header string true "csrf"
  151. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  152. // @Router /api/projectSetting/account/save [post]
  153. func (c *ProjectSettingApi) PostAccountSave() {
  154. // 验证内容
  155. AccountData, err := c.ServiceProjectAccount.ValidRule(c.Ctx)
  156. if err != nil {
  157. ErrMsg := utils.FormValidError(err)
  158. c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
  159. return
  160. } else {
  161. // 获得更新账号ID
  162. id, err := utils.GetDecryptId(AccountData.Id)
  163. if err != nil {
  164. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  165. return
  166. }
  167. projectId, err := utils.GetProjectId(c.Ctx)
  168. if err != nil {
  169. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  170. return
  171. }
  172. err = c.ServiceProjectAccount.Save(AccountData, id, projectId)
  173. if err != nil {
  174. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  175. return
  176. }
  177. c.Ctx.JSON(iris.Map{"code": 0, "msg": "编辑成功"})
  178. }
  179. }
  180. // @Summary 账号启用/禁用
  181. // @Tags 项目设置-管理员
  182. // @Description 账号启用/禁用
  183. // @Accept json
  184. // @Produce json
  185. // @Security ApiKeyAuth
  186. // @Param id body string true "账号ID" default(PcqqGsn1O0jBSmLqkuOTwQ)
  187. // @Param enable body int true "启用/禁用"
  188. // @Param X-CSRF-Token header string true "csrf"
  189. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  190. // @Router /api/projectSetting/account/enable [post]
  191. func (c *ProjectSettingApi) PostAccountEnable() {
  192. accountVaild := viewmodels.ProjectAccount{}
  193. err := c.Ctx.ReadJSON(&accountVaild)
  194. if err != nil {
  195. c.Ctx.JSON(iris.Map{"code": -1, "msg": "参数错误"})
  196. return
  197. }
  198. // 账号ID校验
  199. id, err := utils.GetDecryptId(accountVaild.Id)
  200. if err != nil {
  201. c.Ctx.JSON(iris.Map{"code": -1, "msg": "账号异常"})
  202. return
  203. }
  204. // 项目ID
  205. projectId, err := utils.GetProjectId(c.Ctx)
  206. if err != nil {
  207. c.Ctx.JSON(iris.Map{"code": -1, "msg": "账号异常"})
  208. return
  209. }
  210. err = c.ServiceProjectAccount.Enable(id, projectId, accountVaild.Enable)
  211. if err != nil {
  212. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  213. return
  214. }
  215. c.Ctx.JSON(iris.Map{"code": 0, "msg": "操作成功"})
  216. }
  217. // @Summary 删除账号
  218. // @Tags 项目设置-管理员
  219. // @Description 删除账号
  220. // @Accept json
  221. // @Produce json
  222. // @Security ApiKeyAuth
  223. // @Param id body string true "账号ID" default(PcqqGsn1O0jBSmLqkuOTwQ)
  224. // @Param X-CSRF-Token header string true "csrf"
  225. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  226. // @Router /api/projectSetting/account/delete [post]
  227. func (c *ProjectSettingApi) PostAccountDelete() {
  228. accountVaild := viewmodels.ProjectAccount{}
  229. err := c.Ctx.ReadJSON(&accountVaild)
  230. if err != nil {
  231. c.Ctx.JSON(iris.Map{"code": -1, "msg": "参数错误"})
  232. return
  233. }
  234. // 账号ID校验
  235. id, err := utils.GetDecryptId(accountVaild.Id)
  236. if err != nil {
  237. c.Ctx.JSON(iris.Map{"code": -1, "msg": "账号异常"})
  238. return
  239. }
  240. // 项目ID
  241. projectId, err := utils.GetProjectId(c.Ctx)
  242. if err != nil {
  243. c.Ctx.JSON(iris.Map{"code": -1, "msg": "账号异常"})
  244. return
  245. }
  246. err = c.ServiceProjectAccount.Delete(id, projectId)
  247. if err != nil {
  248. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  249. return
  250. }
  251. c.Ctx.JSON(iris.Map{"code": 0, "msg": "操作成功"})
  252. }
  253. // @Summary 设置账号密码
  254. // @Tags 项目设置-管理员
  255. // @Description 设置账号密码
  256. // @Accept json
  257. // @Produce json
  258. // @Security ApiKeyAuth
  259. // @Param id body string true "账号ID" default(PcqqGsn1O0jBSmLqkuOTwQ)
  260. // @Param account body string true "账号" default(textoopd)
  261. // @Param password body string true "密码" default(ww123456)
  262. // @Param X-CSRF-Token header string true "csrf"
  263. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  264. // @Router /api/projectSetting/account/change [post]
  265. func (c *ProjectSettingApi) PostAccountChange() {
  266. // 验证内容
  267. AccountData, err := c.ServiceProjectAccount.ValidRule(c.Ctx)
  268. if err != nil {
  269. ErrMsg := utils.FormValidError(err)
  270. c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
  271. return
  272. } else {
  273. // 获得更新账号ID
  274. id, err := utils.GetDecryptId(AccountData.Id)
  275. if err != nil {
  276. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  277. return
  278. }
  279. projectId, err := utils.GetProjectId(c.Ctx)
  280. if err != nil {
  281. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  282. return
  283. }
  284. err = c.ServiceProjectAccount.ChangeAccount(id, projectId, AccountData)
  285. if err != nil {
  286. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  287. return
  288. }
  289. c.Ctx.JSON(iris.Map{"code": 0, "msg": "编辑成功"})
  290. }
  291. }
  292. // @Summary 保存项目信息
  293. // @Tags 项目设置-管理员
  294. // @Description 保存项目信息
  295. // @Accept json
  296. // @Produce json
  297. // @Security ApiKeyAuth
  298. // @Param name body string true "账号ID" default(红旗大桥)
  299. // @Param X-CSRF-Token header string true "csrf"
  300. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  301. // @Router /api/projectSetting/project/save [post]
  302. func (c *ProjectSettingApi) PostProjectSave() {
  303. ProjectData, err := c.ServiceProject.ValidRule(c.Ctx)
  304. if err != nil {
  305. ErrMsg := utils.FormValidError(err)
  306. c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
  307. return
  308. } else {
  309. // 项目ID
  310. projectId, err := utils.GetProjectId(c.Ctx)
  311. if err != nil {
  312. c.Ctx.JSON(iris.Map{"code": -1, "msg": "账号异常"})
  313. return
  314. }
  315. err = c.ServiceProject.Save(projectId, ProjectData)
  316. if err != nil {
  317. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  318. return
  319. }
  320. c.Ctx.JSON(iris.Map{"code": 0, "msg": "操作成功"})
  321. }
  322. }