project_setting_api.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * @description: 项目设置相关--管理员可访问
  3. * @Author: CP
  4. * @Date: 2020-10-09 10:35:38
  5. * @FilePath: \construction_management\web\api\project_setting_api.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. ServiceBidAccount services.BidAccountService
  23. }
  24. // @Summary 获得项目账号列表
  25. // @Tags 项目设置-管理员
  26. // @Description id获得单条信息<br/>
  27. // @Accept json
  28. // @Produce json
  29. // @Security ApiKeyAuth
  30. // @Param id body string false "账号ID"
  31. // @Success 200 {object} viewmodels.ProjectAccount "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  32. // @Router /api/projectSetting/account [get]
  33. func (c *ProjectSettingApi) GetAccount() {
  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. // 获得账号ID
  41. AccountData := viewmodels.ProjectAccount{}
  42. err = c.Ctx.ReadJSON(&AccountData)
  43. if err != nil {
  44. // 获得所有项目下的账号
  45. dataList := c.ServiceProjectAccount.GetAll(projectIdInt)
  46. c.Ctx.JSON(iris.Map{
  47. "code": 0,
  48. "msg": "",
  49. "data": dataList,
  50. })
  51. } else {
  52. // 获得单个账号ID
  53. id, err := utils.GetDecryptId(AccountData.Id)
  54. if err != nil {
  55. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  56. return
  57. }
  58. accountData := c.ServiceProjectAccount.Get(id, projectIdInt)
  59. c.Ctx.JSON(iris.Map{
  60. "code": 0,
  61. "msg": "",
  62. "data": accountData,
  63. })
  64. }
  65. }
  66. // @Summary 检索账号信息
  67. // @Tags 项目设置-管理员
  68. // @Description 检索字段:账号 姓名 单位 手机 前匹配
  69. // @Accept json
  70. // @Produce json
  71. // @Security ApiKeyAuth
  72. // @Param name body string true "检索内容"
  73. // @Success 200 {object} viewmodels.ProjectAccount "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  74. // @Router /api/projectSetting/account/search [get]
  75. func (c *ProjectSettingApi) GetAccountSearch() {
  76. // 获得项目ID
  77. projectIdInt, err := utils.GetProjectId(c.Ctx)
  78. if err != nil {
  79. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  80. return
  81. }
  82. c.Ctx.Header("X-CSRF-Token", csrf.Token(c.Ctx))
  83. // 获得检索关键字
  84. AccountData := viewmodels.ProjectAccount{}
  85. err = c.Ctx.ReadForm(&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 role body int true "角色ID"
  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. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  113. // @Router /api/projectSetting/account/create [post]
  114. func (c *ProjectSettingApi) PostAccountCreate() {
  115. ErrMsg := ""
  116. // 验证内容
  117. AccountData, err := c.ServiceProjectAccount.ValidRule(c.Ctx)
  118. if err != nil {
  119. ErrMsg = utils.FormValidError(err)
  120. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", ErrMsg)})
  121. return
  122. } else {
  123. projectId, err := utils.GetProjectId(c.Ctx)
  124. if err != nil {
  125. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  126. return
  127. }
  128. // 新增账号信息
  129. err = c.ServiceProjectAccount.Add(AccountData, projectId)
  130. if err != nil {
  131. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  132. return
  133. }
  134. c.Ctx.JSON(iris.Map{"code": 0, "msg": "新增成功"})
  135. }
  136. }
  137. // @Summary 编辑账号
  138. // @Tags 项目设置-管理员
  139. // @Description 编辑账号
  140. // @Accept json
  141. // @Produce json
  142. // @Security ApiKeyAuth
  143. // @Param id body string true "账号ID" default(PcqqGsn1O0jBSmLqkuOTwQ)
  144. // @Param role body int true "角色ID"
  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. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  189. // @Router /api/projectSetting/account/enable [post]
  190. func (c *ProjectSettingApi) PostAccountEnable() {
  191. // 修改验证方式——TODO
  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. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  225. // @Router /api/projectSetting/account/delete [post]
  226. func (c *ProjectSettingApi) PostAccountDelete() {
  227. accountVaild := viewmodels.ProjectAccount{}
  228. err := c.Ctx.ReadJSON(&accountVaild)
  229. if err != nil {
  230. c.Ctx.JSON(iris.Map{"code": -1, "msg": "参数错误"})
  231. return
  232. }
  233. // 账号ID校验
  234. id, err := utils.GetDecryptId(accountVaild.Id)
  235. if err != nil {
  236. c.Ctx.JSON(iris.Map{"code": -1, "msg": "账号异常"})
  237. return
  238. }
  239. // 项目ID
  240. projectId, err := utils.GetProjectId(c.Ctx)
  241. if err != nil {
  242. c.Ctx.JSON(iris.Map{"code": -1, "msg": "账号异常"})
  243. return
  244. }
  245. err = c.ServiceProjectAccount.Delete(id, projectId)
  246. if err != nil {
  247. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  248. return
  249. }
  250. c.Ctx.JSON(iris.Map{"code": 0, "msg": "操作成功"})
  251. }
  252. // @Summary 设置账号密码
  253. // @Tags 项目设置-管理员
  254. // @Description 设置账号密码
  255. // @Accept json
  256. // @Produce json
  257. // @Security ApiKeyAuth
  258. // @Param id body string true "账号ID" default(PcqqGsn1O0jBSmLqkuOTwQ)
  259. // @Param account body string true "账号" default(textoopd)
  260. // @Param password body string true "密码" default(ww123456)
  261. // @Param X-CSRF-Token header string true "csrf"
  262. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  263. // @Router /api/projectSetting/account/change [post]
  264. func (c *ProjectSettingApi) PostAccountChange() {
  265. // 验证内容
  266. AccountData, err := c.ServiceProjectAccount.ValidRule(c.Ctx)
  267. if err != nil {
  268. ErrMsg := utils.FormValidError(err)
  269. c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
  270. return
  271. } else {
  272. // 获得更新账号ID
  273. id, err := utils.GetDecryptId(AccountData.Id)
  274. if err != nil {
  275. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  276. return
  277. }
  278. projectId, err := utils.GetProjectId(c.Ctx)
  279. if err != nil {
  280. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  281. return
  282. }
  283. err = c.ServiceProjectAccount.ChangeAccount(id, projectId, AccountData)
  284. if err != nil {
  285. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  286. return
  287. }
  288. c.Ctx.JSON(iris.Map{"code": 0, "msg": "编辑成功"})
  289. }
  290. }
  291. // @Summary 获取项目信息
  292. // @Tags 项目设置-管理员
  293. // @Description 获取项目信息
  294. // @Accept json
  295. // @Produce json
  296. // @Security ApiKeyAuth
  297. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  298. // @Router /api/projectSetting/project [get]
  299. func (c *ProjectSettingApi) GetProject() {
  300. projectId, err := utils.GetProjectId(c.Ctx)
  301. if err != nil {
  302. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  303. return
  304. }
  305. data, err := c.ServiceProjectAccount.GetProjectInfo(projectId)
  306. if err != nil {
  307. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  308. return
  309. } else {
  310. c.Ctx.JSON(iris.Map{"code": 0, "msg": "操作成功", "data": data})
  311. }
  312. }
  313. // @Summary 保存项目信息
  314. // @Tags 项目设置-管理员
  315. // @Description 保存项目信息
  316. // @Accept json
  317. // @Produce json
  318. // @Security ApiKeyAuth
  319. // @Param name body string true "账号ID" default(红旗大桥)
  320. // @Param X-CSRF-Token header string true "csrf"
  321. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  322. // @Router /api/projectSetting/project/save [post]
  323. func (c *ProjectSettingApi) PostProjectSave() {
  324. ProjectData, err := c.ServiceProject.ValidRule(c.Ctx)
  325. if err != nil {
  326. ErrMsg := utils.FormValidError(err)
  327. c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
  328. return
  329. } else {
  330. // 项目ID
  331. projectId, err := utils.GetProjectId(c.Ctx)
  332. if err != nil {
  333. c.Ctx.JSON(iris.Map{"code": -1, "msg": "账号异常"})
  334. return
  335. }
  336. err = c.ServiceProject.Save(projectId, ProjectData)
  337. if err != nil {
  338. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  339. return
  340. }
  341. c.Ctx.JSON(iris.Map{"code": 0, "msg": "操作成功"})
  342. }
  343. }