project_setting_api.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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.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 int true "账号组"
  111. // @Param X-CSRF-Token header string true "csrf"
  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 name body string true "姓名"
  145. // @Param company body string true "公司"
  146. // @Param position body string true "职位"
  147. // @Param telephone body string true "座机"
  148. // @Param accountGroup body int true "账号组"
  149. // @Param X-CSRF-Token header string true "csrf"
  150. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  151. // @Router /api/projectSetting/account/save [post]
  152. func (c *ProjectSettingApi) PostAccountSave() {
  153. // 验证内容
  154. AccountData, err := c.ServiceProjectAccount.ValidRule(c.Ctx)
  155. if err != nil {
  156. ErrMsg := utils.FormValidError(err)
  157. c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
  158. return
  159. } else {
  160. // 获得更新账号ID
  161. id, err := utils.GetDecryptId(AccountData.Id)
  162. if err != nil {
  163. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  164. return
  165. }
  166. projectId, err := utils.GetProjectId(c.Ctx)
  167. if err != nil {
  168. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  169. return
  170. }
  171. err = c.ServiceProjectAccount.Save(AccountData, id, projectId)
  172. if err != nil {
  173. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  174. return
  175. }
  176. c.Ctx.JSON(iris.Map{"code": 0, "msg": "编辑成功"})
  177. }
  178. }
  179. // @Summary 账号启用/禁用
  180. // @Tags 项目设置-管理员
  181. // @Description 账号启用/禁用
  182. // @Accept json
  183. // @Produce json
  184. // @Security ApiKeyAuth
  185. // @Param id body string true "账号ID" default(PcqqGsn1O0jBSmLqkuOTwQ)
  186. // @Param enable body int true "启用/禁用"
  187. // @Param X-CSRF-Token header string true "csrf"
  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. // @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. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  299. // @Router /api/projectSetting/project [get]
  300. func (c *ProjectSettingApi) GetProject() {
  301. projectId, err := utils.GetProjectId(c.Ctx)
  302. if err != nil {
  303. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  304. return
  305. }
  306. data, err := c.ServiceProjectAccount.GetProjectInfo(projectId)
  307. if err != nil {
  308. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  309. return
  310. } else {
  311. c.Ctx.JSON(iris.Map{"code": 0, "msg": "操作成功", "data": data})
  312. }
  313. }
  314. // @Summary 保存项目信息
  315. // @Tags 项目设置-管理员
  316. // @Description 保存项目信息
  317. // @Accept json
  318. // @Produce json
  319. // @Security ApiKeyAuth
  320. // @Param name body string true "账号ID" default(红旗大桥)
  321. // @Param X-CSRF-Token header string true "csrf"
  322. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  323. // @Router /api/projectSetting/project/save [post]
  324. func (c *ProjectSettingApi) PostProjectSave() {
  325. ProjectData, err := c.ServiceProject.ValidRule(c.Ctx)
  326. if err != nil {
  327. ErrMsg := utils.FormValidError(err)
  328. c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
  329. return
  330. } else {
  331. // 项目ID
  332. projectId, err := utils.GetProjectId(c.Ctx)
  333. if err != nil {
  334. c.Ctx.JSON(iris.Map{"code": -1, "msg": "账号异常"})
  335. return
  336. }
  337. err = c.ServiceProject.Save(projectId, ProjectData)
  338. if err != nil {
  339. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  340. return
  341. }
  342. c.Ctx.JSON(iris.Map{"code": 0, "msg": "操作成功"})
  343. }
  344. }