project_setting_api.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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/kataras/iris/v12"
  11. "go.mod/services"
  12. "go.mod/web/utils"
  13. "go.mod/web/viewmodels"
  14. )
  15. type ProjectSettingApi struct {
  16. //框架-web应用上下文环境
  17. Ctx iris.Context
  18. // 需要用的service
  19. ServiceProjectAccount services.ProjectAccountService
  20. ServiceProject services.ProjectService
  21. ServiceBidAccount services.BidAccountService
  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. // 获得检索关键字
  83. AccountData := viewmodels.ProjectAccount{}
  84. err = c.Ctx.ReadForm(&AccountData)
  85. if err != nil {
  86. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("JSON转换异常, error=%s", err)})
  87. return
  88. }
  89. // 检索
  90. dataList := c.ServiceProjectAccount.Search(AccountData.Name, projectIdInt)
  91. c.Ctx.JSON(iris.Map{
  92. "code": 0, "msg": "",
  93. "data": dataList,
  94. })
  95. }
  96. // @Summary 创建账号
  97. // @Tags 项目设置-管理员
  98. // @Description 新增账号
  99. // @Accept json
  100. // @Produce json
  101. // @Security ApiKeyAuth
  102. // @Param account body string true "账号"
  103. // @Param password body string true "密码"
  104. // @Param role body int true "角色ID"
  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. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  112. // @Router /api/projectSetting/account/create [post]
  113. func (c *ProjectSettingApi) PostAccountCreate() {
  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": fmt.Sprintf("%s", ErrMsg)})
  120. return
  121. } else {
  122. projectId, err := utils.GetProjectId(c.Ctx)
  123. if err != nil {
  124. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  125. return
  126. }
  127. // 新增账号信息
  128. err = c.ServiceProjectAccount.Add(AccountData, projectId)
  129. if err != nil {
  130. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  131. return
  132. }
  133. c.Ctx.JSON(iris.Map{"code": 0, "msg": "新增成功"})
  134. }
  135. }
  136. // @Summary 编辑账号
  137. // @Tags 项目设置-管理员
  138. // @Description 编辑账号
  139. // @Accept json
  140. // @Produce json
  141. // @Security ApiKeyAuth
  142. // @Param id body string true "账号ID" default(PcqqGsn1O0jBSmLqkuOTwQ)
  143. // @Param role body int true "角色ID"
  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. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  188. // @Router /api/projectSetting/account/enable [post]
  189. func (c *ProjectSettingApi) PostAccountEnable() {
  190. // 修改验证方式——TODO
  191. accountVaild := viewmodels.ProjectAccount{}
  192. err := c.Ctx.ReadJSON(&accountVaild)
  193. if err != nil {
  194. c.Ctx.JSON(iris.Map{"code": -1, "msg": "参数错误"})
  195. return
  196. }
  197. // 账号ID校验
  198. id, err := utils.GetDecryptId(accountVaild.Id)
  199. if err != nil {
  200. c.Ctx.JSON(iris.Map{"code": -1, "msg": "账号异常"})
  201. return
  202. }
  203. // 项目ID
  204. projectId, err := utils.GetProjectId(c.Ctx)
  205. if err != nil {
  206. c.Ctx.JSON(iris.Map{"code": -1, "msg": "账号异常"})
  207. return
  208. }
  209. err = c.ServiceProjectAccount.Enable(id, projectId, accountVaild.Enable)
  210. if err != nil {
  211. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  212. return
  213. }
  214. c.Ctx.JSON(iris.Map{"code": 0, "msg": "操作成功"})
  215. }
  216. // @Summary 删除账号
  217. // @Tags 项目设置-管理员
  218. // @Description 删除账号
  219. // @Accept json
  220. // @Produce json
  221. // @Security ApiKeyAuth
  222. // @Param id body string true "账号ID" default(PcqqGsn1O0jBSmLqkuOTwQ)
  223. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  224. // @Router /api/projectSetting/account/delete [post]
  225. func (c *ProjectSettingApi) PostAccountDelete() {
  226. accountVaild := viewmodels.ProjectAccount{}
  227. err := c.Ctx.ReadJSON(&accountVaild)
  228. if err != nil {
  229. c.Ctx.JSON(iris.Map{"code": -1, "msg": "参数错误"})
  230. return
  231. }
  232. // 账号ID校验
  233. id, err := utils.GetDecryptId(accountVaild.Id)
  234. if err != nil {
  235. c.Ctx.JSON(iris.Map{"code": -1, "msg": "账号异常"})
  236. return
  237. }
  238. // 项目ID
  239. projectId, err := utils.GetProjectId(c.Ctx)
  240. if err != nil {
  241. c.Ctx.JSON(iris.Map{"code": -1, "msg": "账号异常"})
  242. return
  243. }
  244. err = c.ServiceProjectAccount.Delete(id, projectId)
  245. if err != nil {
  246. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  247. return
  248. }
  249. c.Ctx.JSON(iris.Map{"code": 0, "msg": "操作成功"})
  250. }
  251. // @Summary 设置账号密码
  252. // @Tags 项目设置-管理员
  253. // @Description 设置账号密码
  254. // @Accept json
  255. // @Produce json
  256. // @Security ApiKeyAuth
  257. // @Param id body string true "账号ID" default(PcqqGsn1O0jBSmLqkuOTwQ)
  258. // @Param account body string true "账号" default(textoopd)
  259. // @Param password body string true "密码" default(ww123456)
  260. // @Param X-CSRF-Token header string true "csrf"
  261. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  262. // @Router /api/projectSetting/account/change [post]
  263. func (c *ProjectSettingApi) PostAccountChange() {
  264. // 验证内容
  265. AccountData, err := c.ServiceProjectAccount.ValidRule(c.Ctx)
  266. if err != nil {
  267. ErrMsg := utils.FormValidError(err)
  268. c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
  269. return
  270. } else {
  271. // 获得更新账号ID
  272. id, err := utils.GetDecryptId(AccountData.Id)
  273. if err != nil {
  274. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  275. return
  276. }
  277. projectId, err := utils.GetProjectId(c.Ctx)
  278. if err != nil {
  279. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  280. return
  281. }
  282. err = c.ServiceProjectAccount.ChangeAccount(id, projectId, AccountData)
  283. if err != nil {
  284. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  285. return
  286. }
  287. c.Ctx.JSON(iris.Map{"code": 0, "msg": "编辑成功"})
  288. }
  289. }
  290. // @Summary 获取项目信息
  291. // @Tags 项目设置-管理员
  292. // @Description 获取项目信息
  293. // @Accept json
  294. // @Produce json
  295. // @Security ApiKeyAuth
  296. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  297. // @Router /api/projectSetting/project [get]
  298. func (c *ProjectSettingApi) GetProject() {
  299. projectId, err := utils.GetProjectId(c.Ctx)
  300. if err != nil {
  301. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  302. return
  303. }
  304. projectdata, err := c.ServiceProject.Get(projectId)
  305. userId, _ := utils.GetDecryptId(projectdata.UserId)
  306. accountData := c.ServiceProjectAccount.Get(userId, projectId)
  307. data := make(map[string]interface{})
  308. data["project"] = projectdata
  309. data["account"] = accountData
  310. if err != nil {
  311. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  312. return
  313. } else {
  314. c.Ctx.JSON(iris.Map{"code": 0, "msg": "操作成功", "data": data})
  315. }
  316. }
  317. // @Summary 保存项目信息
  318. // @Tags 项目设置-管理员
  319. // @Description 保存项目信息
  320. // @Accept json
  321. // @Produce json
  322. // @Security ApiKeyAuth
  323. // @Param name body string true "账号ID" default(红旗大桥)
  324. // @Param X-CSRF-Token header string true "csrf"
  325. // @Success 200 {string} string "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  326. // @Router /api/projectSetting/project/save [post]
  327. func (c *ProjectSettingApi) PostProjectSave() {
  328. ProjectData, err := c.ServiceProject.ValidRule(c.Ctx)
  329. if err != nil {
  330. ErrMsg := utils.FormValidError(err)
  331. c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
  332. return
  333. } else {
  334. // 项目ID
  335. projectId, err := utils.GetProjectId(c.Ctx)
  336. if err != nil {
  337. c.Ctx.JSON(iris.Map{"code": -1, "msg": "账号异常"})
  338. return
  339. }
  340. err = c.ServiceProject.Save(projectId, ProjectData)
  341. if err != nil {
  342. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  343. return
  344. }
  345. c.Ctx.JSON(iris.Map{"code": 0, "msg": "操作成功"})
  346. }
  347. }