new_role_api_controller.go 21KB

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