system_api_controller.go 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. package admin_api_controllers
  2. import (
  3. "XT_Admin_Api/enums"
  4. "XT_Admin_Api/models"
  5. "XT_Admin_Api/models/admin_models"
  6. "XT_Admin_Api/service"
  7. "XT_Admin_Api/utils"
  8. "encoding/json"
  9. "github.com/jinzhu/gorm"
  10. "reflect"
  11. "time"
  12. )
  13. type SystemApiController struct {
  14. AdminBaseAPIAuthController
  15. }
  16. func (this *SystemApiController) GetUserList() {
  17. list, _ := service.GetAllAdmin()
  18. this.ServeSuccessJSON(map[string]interface{}{
  19. "list": list,
  20. })
  21. }
  22. func (this *SystemApiController) CreateAdminUser() {
  23. name := this.GetString("name")
  24. mobile := this.GetString("mobile")
  25. password := this.GetString("password")
  26. admin := &admin_models.AdminAccount{
  27. Account: mobile,
  28. Pwd: password,
  29. Name: name,
  30. Status: 1,
  31. IsSuperAdmin: 2,
  32. Ctime: time.Now().Unix(),
  33. Mtime: time.Now().Unix(),
  34. }
  35. admins, _ := service.FindUserInfoByAccount(mobile)
  36. if admins.ID > 0 {
  37. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNameException)
  38. return
  39. }
  40. err := service.CreateAdmin(admin)
  41. if err == nil {
  42. this.ServeSuccessJSON(map[string]interface{}{
  43. "admin": admin,
  44. })
  45. }
  46. }
  47. func (this *SystemApiController) ModifyAdminUser() {
  48. id, _ := this.GetInt64("id")
  49. name := this.GetString("name")
  50. mobile := this.GetString("mobile")
  51. password := this.GetString("password")
  52. status, _ := this.GetInt64("status")
  53. admins, _ := service.FindAdminById(id)
  54. admin := &admin_models.AdminAccount{
  55. ID: admins.ID,
  56. Account: mobile,
  57. Pwd: password,
  58. Name: name,
  59. Status: status,
  60. IsSuperAdmin: 2,
  61. Ctime: admins.Ctime,
  62. Mtime: time.Now().Unix(),
  63. }
  64. info, _ := service.FindUserInfoByAccount(mobile)
  65. if info.ID > 0 {
  66. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNameException)
  67. return
  68. }
  69. err := service.UpdateAdmin(admin)
  70. if err == nil {
  71. this.ServeSuccessJSON(map[string]interface{}{
  72. "admin": admin,
  73. })
  74. }
  75. }
  76. func (this *SystemApiController) DeleteAdminUser() {
  77. id, _ := this.GetInt64("id")
  78. err := service.DeleteAdmin(id)
  79. if err == nil {
  80. this.ServeSuccessJSON(map[string]interface{}{
  81. "msg": "删除成功",
  82. })
  83. }
  84. }
  85. func (this *SystemApiController) GetUserInitData() {
  86. org, err := service.FindAllOrg()
  87. if err == nil {
  88. this.ServeSuccessJSON(map[string]interface{}{
  89. "org": org,
  90. })
  91. }
  92. }
  93. func (this *SystemApiController) GetFollowOrg() {
  94. id, _ := this.GetInt64("admin_user_id")
  95. FollowOrgs, err := service.FindFollowOrg(id)
  96. unFollow, _ := service.FindUnFollowOrgByIds(id)
  97. if err == nil {
  98. this.ServeSuccessJSON(map[string]interface{}{
  99. "follows": FollowOrgs,
  100. "unFollow": unFollow,
  101. })
  102. }
  103. }
  104. func (this *SystemApiController) GetAdminUserById() {
  105. id, _ := this.GetInt64("id")
  106. account, err := service.FindAdminById(id)
  107. if err == nil {
  108. this.ServeSuccessJSON(map[string]interface{}{
  109. "account": account,
  110. })
  111. }
  112. }
  113. func (this *SystemApiController) PostFollowInfo() {
  114. time := time.Now().Unix()
  115. var orgFollow []*models.OrgFollow
  116. dataBody := make(map[string]interface{}, 0)
  117. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  118. if err != nil {
  119. utils.ErrorLog(err.Error())
  120. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  121. return
  122. }
  123. if dataBody["Follow"] != nil && reflect.TypeOf(dataBody["Follow"]).String() == "[]interface {}" {
  124. thisFollow, _ := dataBody["Follow"].([]interface{})
  125. if len(thisFollow) > 0 {
  126. for _, item := range thisFollow {
  127. items := item.(map[string]interface{})
  128. if items["org_id"] == nil || reflect.TypeOf(items["org_id"]).String() != "float64" {
  129. utils.ErrorLog("org_id")
  130. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  131. return
  132. }
  133. org_id := int64(items["org_id"].(float64))
  134. if items["admin_user_id"] == nil || reflect.TypeOf(items["admin_user_id"]).String() != "float64" {
  135. utils.ErrorLog("admin_user_id")
  136. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  137. return
  138. }
  139. admin_user_id := int64(items["admin_user_id"].(float64))
  140. follow := &models.OrgFollow{
  141. Mtime: time,
  142. Status: 1,
  143. Ctime: time,
  144. OrgId: org_id,
  145. AdminUserId: admin_user_id,
  146. }
  147. orgFollow = append(orgFollow, follow)
  148. }
  149. }
  150. }
  151. //防止数据冗余,先查出之前被删的数据是否存在,存在则直接改变删除状态,不插入新数据
  152. for index, follow := range orgFollow {
  153. info, err := service.FindFollowRecordByID(follow)
  154. if err == nil { //存在,则修改删除状态
  155. orgFollow = append(orgFollow[:index], orgFollow[index+1:]...)
  156. if info.Status == 0 {
  157. follow := &models.OrgFollow{
  158. ID: info.ID,
  159. Mtime: info.Mtime,
  160. Status: 1,
  161. Ctime: info.Ctime,
  162. OrgId: info.OrgId,
  163. AdminUserId: info.AdminUserId,
  164. }
  165. service.UpdateFollow(follow)
  166. }
  167. }
  168. }
  169. if len(orgFollow) > 0 {
  170. errs := service.CreateFollowInfo(orgFollow)
  171. if errs == nil {
  172. this.ServeSuccessJSON(map[string]interface{}{
  173. "msg": "提交成功",
  174. })
  175. }
  176. } else {
  177. this.ServeSuccessJSON(map[string]interface{}{
  178. "msg": "提交成功",
  179. })
  180. }
  181. }
  182. func (this *SystemApiController) CancelFollowInfo() {
  183. time := time.Now().Unix()
  184. var orgFollow []*models.OrgFollow
  185. dataBody := make(map[string]interface{}, 0)
  186. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  187. if err != nil {
  188. utils.ErrorLog(err.Error())
  189. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  190. return
  191. }
  192. if dataBody["Follow"] != nil && reflect.TypeOf(dataBody["Follow"]).String() == "[]interface {}" {
  193. thisFollow, _ := dataBody["Follow"].([]interface{})
  194. if len(thisFollow) > 0 {
  195. for _, item := range thisFollow {
  196. items := item.(map[string]interface{})
  197. if items["org_id"] == nil || reflect.TypeOf(items["org_id"]).String() != "float64" {
  198. utils.ErrorLog("org_id")
  199. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  200. return
  201. }
  202. org_id := int64(items["org_id"].(float64))
  203. if items["follow_id"] == nil || reflect.TypeOf(items["follow_id"]).String() != "float64" {
  204. utils.ErrorLog("follow_id")
  205. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  206. return
  207. }
  208. follow_id := int64(items["follow_id"].(float64))
  209. if items["admin_user_id"] == nil || reflect.TypeOf(items["admin_user_id"]).String() != "float64" {
  210. utils.ErrorLog("admin_user_id")
  211. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  212. return
  213. }
  214. admin_user_id := int64(items["admin_user_id"].(float64))
  215. follow := &models.OrgFollow{
  216. ID: follow_id,
  217. Mtime: time,
  218. Status: 0,
  219. Ctime: time,
  220. OrgId: org_id,
  221. AdminUserId: admin_user_id,
  222. }
  223. orgFollow = append(orgFollow, follow)
  224. }
  225. }
  226. }
  227. for _, follow := range orgFollow {
  228. service.UpdateFollow(follow)
  229. }
  230. this.ServeSuccessJSON(map[string]interface{}{
  231. "follow": orgFollow,
  232. })
  233. }
  234. func (this *SystemApiController) PostUnFollow() {
  235. org_id, _ := this.GetInt64("org_id")
  236. admin_user_id, _ := this.GetInt64("admin_user_id")
  237. info, _ := service.FindFollowInfoById(org_id, admin_user_id)
  238. follow := &models.OrgFollow{
  239. ID: info.ID,
  240. Mtime: time.Now().Unix(),
  241. Status: 0,
  242. Ctime: info.Ctime,
  243. OrgId: org_id,
  244. AdminUserId: info.AdminUserId,
  245. }
  246. service.UpdateFollow(follow)
  247. }
  248. func (this *SystemApiController) ModifyFollowInfo() {
  249. org_id, _ := this.GetInt64("org_id")
  250. time := time.Now().Unix()
  251. var orgFollow []*models.OrgFollow
  252. dataBody := make(map[string]interface{}, 0)
  253. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  254. if err != nil {
  255. utils.ErrorLog(err.Error())
  256. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  257. return
  258. }
  259. if dataBody["Follow"] != nil && reflect.TypeOf(dataBody["Follow"]).String() == "[]interface {}" {
  260. thisFollow, _ := dataBody["Follow"].([]interface{})
  261. if len(thisFollow) > 0 {
  262. for _, item := range thisFollow {
  263. items := item.(map[string]interface{})
  264. if items["admin_user_id"] == nil || reflect.TypeOf(items["admin_user_id"]).String() != "float64" {
  265. utils.ErrorLog("admin_user_id")
  266. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  267. return
  268. }
  269. admin_user_id := int64(items["admin_user_id"].(float64))
  270. follow := &models.OrgFollow{
  271. Mtime: time,
  272. Status: 1,
  273. Ctime: time,
  274. OrgId: org_id,
  275. AdminUserId: admin_user_id,
  276. }
  277. orgFollow = append(orgFollow, follow)
  278. }
  279. }
  280. }
  281. //防止数据冗余,先查出之前被删的数据是否存在,存在则直接改变删除状态,不插入新数据
  282. for index, follow := range orgFollow {
  283. info, err := service.FindFollowRecordByID(follow)
  284. if err == gorm.ErrRecordNotFound { //不存在,过滤掉该数据
  285. orgFollow = append(orgFollow[:index], orgFollow[index+1:]...)
  286. } else if err == nil { //存在,则修改删除状态
  287. follow := &models.OrgFollow{
  288. ID: info.ID,
  289. Mtime: info.Mtime,
  290. Status: 1,
  291. Ctime: info.Ctime,
  292. OrgId: info.OrgId,
  293. AdminUserId: info.AdminUserId,
  294. }
  295. service.UpdateFollow(follow)
  296. }
  297. }
  298. if len(orgFollow) > 0 {
  299. errs := service.CreateFollowInfo(orgFollow)
  300. if errs == nil {
  301. this.ServeSuccessJSON(map[string]interface{}{
  302. "msg": "提交成功",
  303. })
  304. }
  305. } else {
  306. this.ServeSuccessJSON(map[string]interface{}{
  307. "msg": "提交成功",
  308. })
  309. }
  310. }