new_role_api_controller.go 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. package new_mobile_api_controllers
  2. import (
  3. "XT_New/enums"
  4. "XT_New/models"
  5. "XT_New/service"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type NewRoleApiController struct {
  12. NewMobileBaseAPIAuthController
  13. }
  14. func (this *NewRoleApiController) GetAllOrgUser() {
  15. adminUserInfo := this.GetMobileAdminUserInfo()
  16. var isSubSuperAdmin bool = false
  17. app_role, _ := service.GetAppRoleById(adminUserInfo.AppRole.Id)
  18. role_ids := strings.Split(app_role.RoleIds, ",")
  19. if len(role_ids) > 0 {
  20. if adminUserInfo.AdminUser.Id != adminUserInfo.Org.Creator {
  21. for _, item := range role_ids {
  22. id, _ := strconv.ParseInt(item, 10, 64)
  23. if id != 0 {
  24. role, _ := service.GetRoleByRoleID(id)
  25. if role != nil {
  26. if role.IsSystem == 1 && role.RoleName == "子管理员" {
  27. isSubSuperAdmin = true
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }
  34. viewModels, _, _ := service.GetAllAdminUsersAndRole(adminUserInfo.Org.Id, adminUserInfo.App.Id, 1, 100)
  35. this.ServeSuccessJSON(map[string]interface{}{
  36. "admins": viewModels,
  37. "isSubSuperAdmin": isSubSuperAdmin,
  38. "org_creator": adminUserInfo.Org.Creator,
  39. })
  40. }
  41. func (this *NewRoleApiController) EditAdmin() {
  42. adminUserInfo := this.GetMobileAdminUserInfo()
  43. adminUserId, _ := this.GetInt64("uid")
  44. name := this.GetString("name")
  45. userTitle := this.GetString("title")
  46. roleIds := this.GetString("role_ids")
  47. user_type, _ := this.GetInt64("user_type", 0)
  48. user_title, _ := this.GetInt64("user_title", 0)
  49. //roleIds := this.GetString("role_ids")
  50. if adminUserId <= 0 || len(name) == 0 || len(roleIds) <= 0 {
  51. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  52. return
  53. }
  54. appRole, getAppRoleErr := service.GetAppRole(adminUserInfo.Org.Id, adminUserInfo.App.Id, adminUserId)
  55. if getAppRoleErr != nil {
  56. //beego.Error("查询管理员信息时失败:", getAppRoleErr)
  57. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  58. return
  59. }
  60. if appRole == nil {
  61. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  62. return
  63. }
  64. appRole.UserName = name
  65. appRole.UserTitleName = userTitle
  66. appRole.RoleIds = roleIds
  67. appRole.ModifyTime = time.Now().Unix()
  68. appRole.UserType = int8(user_type)
  69. appRole.UserTitle = int8(user_title)
  70. saveErr := service.SaveAppRole(appRole)
  71. if saveErr != nil {
  72. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  73. } else {
  74. this.ServeSuccessJSON(nil)
  75. }
  76. }
  77. func (this *NewRoleApiController) GetEditAdminInitData() {
  78. adminUserInfo := this.GetMobileAdminUserInfo()
  79. roles, _ := service.GetNewAllOrgValidRoles(adminUserInfo.Org.Id)
  80. this.ServeSuccessJSON(map[string]interface{}{
  81. "roles": roles,
  82. })
  83. }
  84. func (this *NewRoleApiController) GetAdminUserInfo() {
  85. adminUserInfo := this.GetMobileAdminUserInfo()
  86. adminUserId, _ := this.GetInt64("uid")
  87. var isSubSuperAdmin bool = false
  88. app_role, _ := service.GetAppRoleById(adminUserInfo.AppRole.Id)
  89. role_ids := strings.Split(app_role.RoleIds, ",")
  90. if len(role_ids) > 0 {
  91. if adminUserInfo.AdminUser.Id != adminUserInfo.Org.Creator {
  92. for _, item := range role_ids {
  93. id, _ := strconv.ParseInt(item, 10, 64)
  94. if id != 0 {
  95. role, _ := service.GetRoleByRoleID(id)
  96. if role != nil {
  97. if role.IsSystem == 1 && role.RoleName == "子管理员" {
  98. isSubSuperAdmin = true
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. appRole, getAppRoleErr := service.GetAppRole(adminUserInfo.Org.Id, adminUserInfo.App.Id, adminUserId)
  106. if getAppRoleErr != nil {
  107. //beego.Error("查询管理员信息时失败:", getAppRoleErr)
  108. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  109. return
  110. }
  111. if appRole == nil {
  112. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  113. return
  114. }
  115. this.ServeSuccessJSON(map[string]interface{}{
  116. "user_info": appRole,
  117. "isSubSuperAdmin": isSubSuperAdmin,
  118. })
  119. }
  120. func (this *NewRoleApiController) StopAdminUser() {
  121. adminUserInfo := this.GetMobileAdminUserInfo()
  122. adminUserId, _ := this.GetInt64("uid")
  123. appRole, getAppRoleErr := service.GetAppRole(adminUserInfo.Org.Id, adminUserInfo.App.Id, adminUserId)
  124. if getAppRoleErr != nil {
  125. //beego.Error("查询管理员信息时失败:", getAppRoleErr)
  126. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  127. return
  128. }
  129. if appRole == nil {
  130. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  131. return
  132. }
  133. enable, _ := this.GetBool("enable")
  134. if enable {
  135. appRole.Status = 1
  136. } else {
  137. appRole.Status = 0
  138. }
  139. appRole.ModifyTime = time.Now().Unix()
  140. saveErr := service.SaveAppRole(appRole)
  141. if saveErr != nil {
  142. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  143. } else {
  144. this.ServeSuccessJSON(nil)
  145. }
  146. }
  147. func (this *NewRoleApiController) CreateAdminUser() {
  148. adminUserInfo := this.GetMobileAdminUserInfo()
  149. mobile := this.GetString("mobile")
  150. name := this.GetString("name")
  151. role_ids := this.GetString("role_ids")
  152. userTitle := this.GetString("title")
  153. user_type, _ := this.GetInt("user_type", 0)
  154. user_title, _ := this.GetInt("user_title", 0)
  155. if len(mobile) == 0 || len(name) == 0 || len(role_ids) <= 0 {
  156. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  157. return
  158. }
  159. // 判断是否已存在该手机号
  160. if adminUser, err := service.GetValidAdminUserByMobileReturnErr(mobile); err != nil {
  161. //beego.Error("查询用户是否已被添加为管理员时失败:", err)
  162. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  163. return
  164. } else {
  165. if adminUser == nil { //新增账号和用户
  166. _, password, createErr := service.CreateGeneralAdminUser(adminUserInfo.Org.Id, adminUserInfo.App.Id, mobile, name, userTitle, role_ids, user_type, user_title)
  167. if createErr != nil {
  168. //beego.Error("创建管理员失败:", createErr)
  169. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  170. return
  171. } else {
  172. sendSMSErr := service.SMSSendInviteMobileToJoinOrgAdmin(name, mobile, password)
  173. if sendSMSErr != nil {
  174. }
  175. this.ServeSuccessJSON(nil)
  176. return
  177. }
  178. } else {
  179. total, _ := service.FindAdminUserByID(adminUser.Id, adminUserInfo.Org.Id)
  180. if total <= 0 {
  181. //新增用户
  182. app_role := &models.App_Role{
  183. AdminUserId: adminUser.Id,
  184. OrgId: adminUserInfo.Org.Id,
  185. AppId: adminUserInfo.App.Id,
  186. Avatar: "",
  187. UserName: name,
  188. UserTitleName: userTitle,
  189. UserTitle: int8(user_title),
  190. UserType: int8(user_type),
  191. Status: 1,
  192. CreateTime: time.Now().Unix(),
  193. ModifyTime: time.Now().Unix(),
  194. RoleIds: role_ids,
  195. }
  196. err := service.CreateUserRole(app_role)
  197. if err != nil {
  198. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  199. return
  200. }
  201. this.ServeSuccessJSON(nil)
  202. } else {
  203. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRepeatCreateStaffException)
  204. return
  205. }
  206. return
  207. }
  208. }
  209. }
  210. func (this *NewRoleApiController) GetAllOrgRole() {
  211. adminUserInfo := this.GetMobileAdminUserInfo()
  212. var isSubSuperAdmin bool = false
  213. app_role, _ := service.GetAppRoleById(adminUserInfo.AppRole.Id)
  214. role_ids := strings.Split(app_role.RoleIds, ",")
  215. if len(role_ids) > 0 {
  216. if adminUserInfo.AdminUser.Id != adminUserInfo.Org.Creator {
  217. for _, item := range role_ids {
  218. id, _ := strconv.ParseInt(item, 10, 64)
  219. if id != 0 {
  220. role, _ := service.GetRoleByRoleID(id)
  221. if role != nil {
  222. if role.IsSystem == 1 && role.RoleName == "子管理员" {
  223. isSubSuperAdmin = true
  224. }
  225. }
  226. }
  227. }
  228. }
  229. }
  230. fmt.Println(isSubSuperAdmin)
  231. roles, err := service.GetAllOrgValidRoles(adminUserInfo.Org.Id, isSubSuperAdmin)
  232. if err != nil {
  233. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  234. return
  235. } else {
  236. this.ServeSuccessJSON(map[string]interface{}{
  237. "roles": roles,
  238. })
  239. }
  240. }
  241. func (this *NewRoleApiController) EditRole() {
  242. role_id, _ := this.GetInt64("id", 0)
  243. desc := this.GetString("desc")
  244. role, _ := service.GetRoleByRoleID(role_id)
  245. role.RoleIntro = desc
  246. role.ModifyTime = time.Now().Unix()
  247. err := service.SaveRole(role)
  248. if err != nil {
  249. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  250. }
  251. this.ServeSuccessJSON(map[string]interface{}{
  252. "role": role,
  253. })
  254. return
  255. }
  256. func (this *NewRoleApiController) GetOrgRoleInfo() {
  257. role_id, _ := this.GetInt64("id", 0)
  258. role, err := service.GetRoleByRoleID(role_id)
  259. if err != nil {
  260. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  261. return
  262. } else {
  263. this.ServeSuccessJSON(map[string]interface{}{
  264. "role": role,
  265. })
  266. }
  267. }
  268. func (this *NewRoleApiController) CreateRole() {
  269. adminUserInfo := this.GetMobileAdminUserInfo()
  270. role_name := this.GetString("name")
  271. role_desc := this.GetString("desc")
  272. total := service.FindRoleRecordByRoleName(role_name, adminUserInfo.Org.Id)
  273. if total > 0 {
  274. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNameIsExist)
  275. return
  276. }
  277. role := &models.Role{
  278. RoleName: role_name,
  279. RoleIntro: role_desc,
  280. Creator: adminUserInfo.AdminUser.Id,
  281. OrgId: adminUserInfo.Org.Id,
  282. AppId: adminUserInfo.App.Id,
  283. Status: 1,
  284. IsSuperAdmin: false,
  285. CreateTime: time.Now().Unix(),
  286. ModifyTime: time.Now().Unix(),
  287. }
  288. err := service.CreateOrgRole(role)
  289. if err != nil {
  290. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  291. return
  292. } else {
  293. this.ServeSuccessJSON(map[string]interface{}{
  294. "role": role,
  295. })
  296. }
  297. }
  298. func (this *NewRoleApiController) GetAllPermission() {
  299. module, _ := this.GetInt64("module")
  300. parent_id, _ := this.GetInt64("id")
  301. purviews, _ := service.GetAllPurview(module, parent_id)
  302. this.ServeSuccessJSON(map[string]interface{}{
  303. "purviews": purviews,
  304. })
  305. }
  306. func (this *NewRoleApiController) DeleteOrgRole() {
  307. roleID, _ := this.GetInt64("role_id")
  308. enable, _ := this.GetBool("enable")
  309. if roleID <= 0 {
  310. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  311. return
  312. }
  313. adminUserInfo := this.GetMobileAdminUserInfo()
  314. role, getRoleErr := service.GetRoleByRoleID(roleID)
  315. if getRoleErr != nil {
  316. //beego.Error("获取角色失败:", getRoleErr)
  317. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  318. return
  319. } else if role == nil {
  320. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
  321. return
  322. }
  323. if enable == false {
  324. if count, _ := service.RoleAdminUserCountTwo(adminUserInfo.Org.Id, adminUserInfo.App.Id, roleID); count != 0 {
  325. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCannotRemoveRole)
  326. return
  327. }
  328. }
  329. if enable {
  330. role.Status = 1
  331. } else {
  332. role.Status = 2
  333. }
  334. role.ModifyTime = time.Now().Unix()
  335. saveErr := service.ModifyRole(role)
  336. if saveErr != nil {
  337. //beego.Error("修改角色失败:", role.Id, saveErr)
  338. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  339. } else {
  340. this.ServeSuccessJSON(nil)
  341. }
  342. }
  343. func (this *NewRoleApiController) GetRolePurviews() {
  344. adminUserInfo := this.GetMobileAdminUserInfo()
  345. role_id, _ := this.GetInt64("id", 0)
  346. rolePurview, _ := service.GetRoleAndPurviewById(role_id, adminUserInfo.Org.Id, adminUserInfo.App.Id)
  347. funRolePurview, _ := service.GetRoleFuncPurview(role_id)
  348. role, _ := service.GetRoleByRoleID(role_id)
  349. this.ServeSuccessJSON(map[string]interface{}{
  350. "role_purview": rolePurview,
  351. "role": role,
  352. "func_purview": funRolePurview,
  353. })
  354. }
  355. func (this *NewRoleApiController) AddRoleStaff() {
  356. //adminUserInfo := this.GetMobileAdminUserInfo()
  357. role_id, _ := this.GetInt64("id", 0)
  358. staff_ids := this.GetString("ids")
  359. ids := strings.Split(staff_ids, ",")
  360. for _, item := range ids {
  361. id, _ := strconv.ParseInt(item, 10, 64)
  362. role, _ := service.FindAdminUserID(id)
  363. role.RoleIds = role.RoleIds + "," + strconv.FormatInt(role_id, 10)
  364. service.SaveAdminUser(&role)
  365. }
  366. this.ServeSuccessJSON(map[string]interface{}{
  367. "msg": "添加成功",
  368. })
  369. }
  370. func (this *NewRoleApiController) GetFuntionPurviews() {
  371. functionPurview, _ := service.GetFunctionPurview()
  372. this.ServeSuccessJSON(map[string]interface{}{
  373. "funtion_purview": functionPurview,
  374. })
  375. }
  376. func RemoveRepeatedIDSElement(arr []string) (newArr []string) {
  377. newArr = make([]string, 0)
  378. for i := 0; i < len(arr); i++ {
  379. repeat := false
  380. for j := i + 1; j < len(arr); j++ {
  381. if arr[i] == arr[j] {
  382. repeat = true
  383. break
  384. }
  385. }
  386. if !repeat {
  387. newArr = append(newArr, arr[i])
  388. }
  389. }
  390. return
  391. }
  392. func (this *NewRoleApiController) EditRolePermission() {
  393. adminUser := this.GetMobileAdminUserInfo()
  394. role_id, _ := this.GetInt64("id", 0)
  395. purview_id := this.GetString("purview_id")
  396. is_open, _ := this.GetInt64("type", 0)
  397. permissions, err := service.GetRolePurviewIds(role_id)
  398. if err != nil {
  399. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  400. return
  401. } else {
  402. if is_open == 1 { //打开
  403. purview, _ := service.GetRolePurview(role_id)
  404. if purview.Id == 0 { // 新建
  405. purview.PurviewIds = purview_id
  406. purview.CreateTime = time.Now().Unix()
  407. purview.ModifyTime = time.Now().Unix()
  408. purview.Status = 1
  409. purview.RoleId = role_id
  410. purview.OrgId = adminUser.Org.Id
  411. purview.AppId = adminUser.App.Id
  412. err := service.CreateRolePurview(&purview)
  413. if err != nil {
  414. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  415. return
  416. }
  417. this.ServeSuccessJSON(map[string]interface{}{
  418. "is_open": 1,
  419. })
  420. } else { //修改
  421. var permission_arr []string
  422. purview, _ := service.GetRolePurview(role_id)
  423. if len(permissions) > 0 {
  424. permission_arr = strings.Split(permissions, ",")
  425. if len(purview_id) > 0 {
  426. permission_arr = append(permission_arr, strings.Split(purview_id, ",")...)
  427. }
  428. } else {
  429. if len(purview_id) > 0 {
  430. permission_arr = strings.Split(purview_id, ",")
  431. }
  432. }
  433. permission_arr = RemoveRepeatedIDSElement(permission_arr)
  434. permissions = strings.Join(permission_arr, ",")
  435. purview.PurviewIds = permissions
  436. err := service.SaveRolePurview(&purview)
  437. if err != nil {
  438. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  439. return
  440. }
  441. this.ServeSuccessJSON(map[string]interface{}{
  442. "is_open": 1,
  443. })
  444. }
  445. } else { //关闭
  446. permission_arr := strings.Split(permissions, ",")
  447. purview_arr := strings.Split(purview_id, ",")
  448. for _, pur_id := range purview_arr {
  449. for index, id := range permission_arr {
  450. if id == pur_id {
  451. permission_arr = append(permission_arr[:index], permission_arr[index+1:]...)
  452. }
  453. }
  454. }
  455. purview, _ := service.GetRolePurview(role_id)
  456. fmt.Println(purview.CreateTime)
  457. purview.PurviewIds = strings.Join(permission_arr, ",")
  458. err := service.SaveRolePurview(&purview)
  459. if err != nil {
  460. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  461. return
  462. }
  463. this.ServeSuccessJSON(map[string]interface{}{
  464. "is_open": 0,
  465. })
  466. }
  467. }
  468. }
  469. func (this *NewRoleApiController) EditFunctionPermission() {
  470. adminUser := this.GetMobileAdminUserInfo()
  471. role_id, _ := this.GetInt64("id", 0)
  472. purview_id := this.GetString("purview_id")
  473. is_open, _ := this.GetInt64("type", 0)
  474. is_all, _ := this.GetInt64("is_all", 0)
  475. pid := this.GetString("pid")
  476. func_permissions, err := service.GetRoleFuncPurviewIds(role_id)
  477. permissions, _ := service.GetRolePurviewIds(role_id)
  478. if err != nil {
  479. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  480. return
  481. } else {
  482. if is_open == 1 { //打开
  483. //处理功能权限涉及到的菜单权限
  484. purview, _ := service.GetRolePurview(role_id)
  485. if purview.Id == 0 { // 新建
  486. purview.PurviewIds = pid
  487. purview.CreateTime = time.Now().Unix()
  488. purview.ModifyTime = time.Now().Unix()
  489. purview.Status = 1
  490. purview.RoleId = role_id
  491. purview.OrgId = adminUser.Org.Id
  492. purview.AppId = adminUser.App.Id
  493. service.CreateRolePurview(&purview)
  494. } else { //修改
  495. var permission_arr []string
  496. purview, _ := service.GetRolePurview(role_id)
  497. if len(permissions) > 0 {
  498. permission_arr = strings.Split(permissions, ",")
  499. if len(pid) > 0 {
  500. permission_arr = append(permission_arr, strings.Split(pid, ",")...)
  501. }
  502. } else {
  503. if len(pid) > 0 {
  504. permission_arr = strings.Split(pid, ",")
  505. }
  506. }
  507. permission_arr = RemoveRepeatedIDSElement(permission_arr)
  508. permissions = strings.Join(permission_arr, ",")
  509. purview.PurviewIds = permissions
  510. service.SaveRolePurview(&purview)
  511. }
  512. func_purview, _ := service.GetFuncRolePurview(role_id)
  513. if func_purview.ID == 0 { // 新建
  514. func_purview.PurviewIds = purview_id
  515. func_purview.Ctime = time.Now().Unix()
  516. func_purview.Mtime = time.Now().Unix()
  517. func_purview.Status = 1
  518. func_purview.RoleId = role_id
  519. func_purview.OrgId = adminUser.Org.Id
  520. func_purview.AppId = adminUser.App.Id
  521. err := service.CreateFuncRolePurview(&func_purview)
  522. if err != nil {
  523. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  524. return
  525. }
  526. this.ServeSuccessJSON(map[string]interface{}{
  527. "is_open": 1,
  528. })
  529. } else { //修改
  530. var permission_arr []string
  531. func_purview, _ := service.GetRoleFuncPurview(role_id)
  532. if len(func_permissions) > 0 {
  533. permission_arr = strings.Split(func_permissions, ",")
  534. if len(purview_id) > 0 {
  535. permission_arr = append(permission_arr, strings.Split(purview_id, ",")...)
  536. }
  537. } else {
  538. if len(purview_id) > 0 {
  539. permission_arr = strings.Split(purview_id, ",")
  540. }
  541. }
  542. permission_arr = RemoveRepeatedIDSElement(permission_arr)
  543. func_permissions = strings.Join(permission_arr, ",")
  544. func_purview.PurviewIds = func_permissions
  545. err := service.SaveRoleFuncPurview(&func_purview)
  546. if err != nil {
  547. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  548. return
  549. }
  550. this.ServeSuccessJSON(map[string]interface{}{
  551. "is_open": 1,
  552. })
  553. }
  554. } else { //关闭
  555. //处理功能权限涉及到的菜单权限,当关闭到只剩下一个的时候,在将菜单权限对应的权限id清除
  556. func_permission_arr := strings.Split(func_permissions, ",")
  557. if len(func_permission_arr) == 1 {
  558. permission_arr := strings.Split(permissions, ",")
  559. pid_arr := strings.Split(pid, ",")
  560. for _, pur_id := range pid_arr {
  561. for index, id := range permission_arr {
  562. if id == pur_id {
  563. permission_arr = append(permission_arr[:index], permission_arr[index+1:]...)
  564. }
  565. }
  566. }
  567. purview, _ := service.GetRolePurview(role_id)
  568. purview.PurviewIds = strings.Join(permission_arr, ",")
  569. service.SaveRolePurview(&purview)
  570. }
  571. if is_all == 1 {
  572. permission_arr := strings.Split(permissions, ",")
  573. pid_arr := strings.Split(pid, ",")
  574. for _, pur_id := range pid_arr {
  575. for index, id := range permission_arr {
  576. if id == pur_id {
  577. permission_arr = append(permission_arr[:index], permission_arr[index+1:]...)
  578. }
  579. }
  580. }
  581. purview, _ := service.GetRolePurview(role_id)
  582. purview.PurviewIds = strings.Join(permission_arr, ",")
  583. service.SaveRolePurview(&purview)
  584. func_purview, _ := service.GetRoleFuncPurview(role_id)
  585. func_purview.PurviewIds = ""
  586. err := service.SaveRoleFuncPurview(&func_purview)
  587. if err != nil {
  588. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  589. return
  590. }
  591. this.ServeSuccessJSON(map[string]interface{}{
  592. "is_open": 0,
  593. })
  594. } else {
  595. permission_arr := strings.Split(func_permissions, ",")
  596. purview_arr := strings.Split(purview_id, ",")
  597. fmt.Println(permission_arr)
  598. fmt.Println(purview_arr)
  599. for index, id := range permission_arr {
  600. for _, pur_id := range purview_arr {
  601. if id == pur_id {
  602. permission_arr = append(permission_arr[:index], permission_arr[index+1:]...)
  603. }
  604. }
  605. }
  606. func_purview, _ := service.GetRoleFuncPurview(role_id)
  607. func_purview.PurviewIds = strings.Join(permission_arr, ",")
  608. err := service.SaveRoleFuncPurview(&func_purview)
  609. if err != nil {
  610. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  611. return
  612. }
  613. this.ServeSuccessJSON(map[string]interface{}{
  614. "is_open": 0,
  615. })
  616. }
  617. }
  618. }
  619. }
  620. func (this *NewRoleApiController) GetFuncRolePurviews() {
  621. //adminUserInfo := this.GetMobileAdminUserInfo()
  622. role_id, _ := this.GetInt64("id", 0)
  623. purview, _ := service.GetRoleFuncPurview(role_id)
  624. this.ServeSuccessJSON(map[string]interface{}{
  625. "role_purview": purview,
  626. })
  627. }