Power.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. package controllers
  2. import (
  3. "ERP_user/Nats/NatsServer"
  4. "ERP_user/conf"
  5. "ERP_user/models/Account"
  6. "ERP_user/models/System"
  7. "fmt"
  8. "github.com/astaxie/beego/logs"
  9. "github.com/beego/beego/v2/adapter/orm"
  10. beego "github.com/beego/beego/v2/server/web"
  11. menulibs "gogs.baozhida.cn/zoie/ERP_libs/Menu"
  12. powerlibs "gogs.baozhida.cn/zoie/ERP_libs/Power"
  13. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  14. "math"
  15. "strings"
  16. )
  17. type PowerController struct {
  18. beego.Controller
  19. User Account.User
  20. }
  21. func (c *PowerController) Prepare() {
  22. c.User = *Account.User_r
  23. }
  24. // 列表 -
  25. func (c *PowerController) List() {
  26. // 分页参数 初始化
  27. page, _ := c.GetInt("page")
  28. if page < 1 {
  29. page = 1
  30. }
  31. page_z, _ := c.GetInt("page_z")
  32. if page_z < 1 {
  33. page_z = conf.Page_size
  34. }
  35. // 查询
  36. T_name := c.GetString("T_name")
  37. R_List, R_cnt := Account.Read_Power_List(T_name, page, page_z)
  38. var r_jsons lib.R_JSONS
  39. r_jsons.Num = R_cnt
  40. r_jsons.Data = R_List
  41. r_jsons.Page = page
  42. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  43. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  44. c.ServeJSON()
  45. return
  46. }
  47. func (c *PowerController) Get() {
  48. type Data struct {
  49. Power powerlibs.Power_R
  50. Menu []menulibs.Menu
  51. Menu_checked []int
  52. }
  53. var data Data
  54. T_id := c.GetString("T_id")
  55. T_code := c.GetString("T_code")
  56. if len(T_code) == 0 {
  57. T_code = conf.ERP_ACCOUNT_Sys
  58. }
  59. if len(T_id) < 1 {
  60. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  61. c.ServeJSON()
  62. return
  63. }
  64. sysName := Account.Get_Sys_Name(T_code)
  65. var menu []menulibs.Menu
  66. var power powerlibs.Power
  67. var err error
  68. power, err = Account.Read_Power_ByT_id(T_id)
  69. if err != nil {
  70. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  71. c.ServeJSON()
  72. return
  73. }
  74. switch T_code {
  75. case conf.ERP_ACCOUNT_Sys:
  76. menu, err = Account.Read_Menu_List()
  77. if err != nil {
  78. c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取用户系统菜单失败!"}
  79. c.ServeJSON()
  80. return
  81. }
  82. default:
  83. power, err = NatsServer.ERP_Read_Power_ByT_id(T_code, T_id)
  84. if err != nil {
  85. if err.Error() == orm.ErrNoRows.Error() {
  86. var_ := powerlibs.Power{
  87. T_id: power.T_id,
  88. T_name: power.T_name,
  89. }
  90. _, err = NatsServer.ERP_Add_Power(T_code, var_)
  91. if err != nil {
  92. logs.Error(lib.FuncName(), err)
  93. }
  94. } else {
  95. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("获取%s权限失败!", sysName)}
  96. c.ServeJSON()
  97. return
  98. }
  99. }
  100. menu, err = NatsServer.ERP_Read_Menu_List(T_code)
  101. if err != nil {
  102. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("获取%s菜单失败!", sysName)}
  103. c.ServeJSON()
  104. return
  105. }
  106. }
  107. data.Power = powerlibs.PowerToPower_R(power)
  108. data.Menu = menu
  109. if power.T_menu != "*" {
  110. data.Menu_checked = lib.SplitStringToIntIds(power.T_menu, "M")
  111. }
  112. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: data}
  113. c.ServeJSON()
  114. return
  115. }
  116. func (c *PowerController) Add() {
  117. T_name := c.GetString("T_name")
  118. rand_x := 0
  119. T_id := ""
  120. for true {
  121. T_id = lib.GetRandstring(8, "", int64(rand_x))
  122. _, err := Account.Read_Power_ByT_id(T_id)
  123. if err != nil {
  124. break
  125. }
  126. rand_x += 1
  127. }
  128. var_ := powerlibs.Power{
  129. T_id: T_id,
  130. T_name: T_name,
  131. }
  132. id, err := Account.Add_Power(var_)
  133. if err != nil {
  134. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  135. c.ServeJSON()
  136. return
  137. }
  138. for _, prefix := range Account.ERP_Sys_prefix {
  139. i := 3
  140. for {
  141. // Nats 发送失败 重试3次
  142. _, err = NatsServer.ERP_Add_Power(prefix, var_)
  143. if err == nil || i == 0 {
  144. break
  145. }
  146. i--
  147. }
  148. if err != nil {
  149. sysName := Account.Get_Sys_Name(prefix)
  150. logs.Error(lib.FuncName(), sysName+"添加失败: ", err)
  151. c.Data["json"] = lib.JSONS{Code: 202, Msg: sysName + "添加失败!"}
  152. c.ServeJSON()
  153. return
  154. }
  155. }
  156. System.Add_UserLogs_T(c.User.T_uuid, "权限", "新增", var_)
  157. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  158. c.ServeJSON()
  159. return
  160. }
  161. func (c *PowerController) Edit() {
  162. T_id := c.GetString("T_id")
  163. T_name := c.GetString("T_name")
  164. if len(T_id) == 0 {
  165. c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_id Err!"}
  166. c.ServeJSON()
  167. return
  168. }
  169. userPower, err := Account.Read_Power_ByT_id(T_id)
  170. if err != nil {
  171. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  172. c.ServeJSON()
  173. return
  174. }
  175. userPower.T_name = T_name
  176. id, err := Account.Update_Power(userPower)
  177. if err != nil {
  178. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  179. c.ServeJSON()
  180. return
  181. }
  182. for _, prefix := range Account.ERP_Sys_prefix {
  183. go func(sys string) {
  184. // Nats 发送失败 重试10次
  185. var power powerlibs.Power
  186. i := 10
  187. for {
  188. power, err = NatsServer.ERP_Read_Power_ByT_id(sys, T_id)
  189. if err != nil {
  190. if err.Error() == orm.ErrNoRows.Error() {
  191. var_ := powerlibs.Power{
  192. T_id: userPower.T_id,
  193. T_name: T_name,
  194. }
  195. _, err = NatsServer.ERP_Add_Power(sys, var_)
  196. if err == nil || i == 0 {
  197. break
  198. }
  199. } else {
  200. continue
  201. }
  202. }
  203. if power.Id > 0 {
  204. power.T_name = T_name
  205. _, err = NatsServer.ERP_Update_Power(sys, power)
  206. if err == nil || i == 0 {
  207. break
  208. }
  209. }
  210. i--
  211. }
  212. }(prefix)
  213. }
  214. System.Add_UserLogs_T(c.User.T_uuid, "权限", "修改", userPower)
  215. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  216. c.ServeJSON()
  217. return
  218. }
  219. func (c *PowerController) Sys_List() {
  220. type Sys struct {
  221. T_sys string
  222. T_name string
  223. }
  224. var syslist []Sys
  225. list := strings.Split(conf.ERP_Sys, ",")
  226. for _, v := range list {
  227. sys, name := strings.Split(v, "|")[0], strings.Split(v, "|")[1]
  228. syslist = append(syslist, Sys{
  229. T_sys: sys,
  230. T_name: name,
  231. })
  232. }
  233. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: syslist}
  234. c.ServeJSON()
  235. return
  236. }
  237. func (c *PowerController) Edit_Menu() {
  238. T_id := c.GetString("T_id")
  239. T_code := c.GetString("T_code")
  240. T_menu := c.GetString("T_menu")
  241. if len(T_id) == 0 {
  242. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  243. c.ServeJSON()
  244. return
  245. }
  246. sysName := Account.Get_Sys_Name(T_code)
  247. if sysName == T_code {
  248. // 不存在这个code
  249. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_code Err!"}
  250. c.ServeJSON()
  251. return
  252. }
  253. power, err := Account.Read_Power_ByT_id(T_id)
  254. if err != nil {
  255. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  256. c.ServeJSON()
  257. return
  258. }
  259. var id int
  260. switch T_code {
  261. case conf.ERP_ACCOUNT_Sys:
  262. power.T_menu = T_menu
  263. id, err = Account.Update_Power(power)
  264. if err != nil {
  265. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  266. c.ServeJSON()
  267. return
  268. }
  269. default:
  270. power, err = NatsServer.ERP_Read_Power_ByT_id(T_code, T_id)
  271. if err != nil {
  272. if err.Error() == orm.ErrNoRows.Error() {
  273. var_ := powerlibs.Power{
  274. T_id: power.T_id,
  275. T_name: power.T_name,
  276. T_menu: T_menu,
  277. }
  278. _, err = NatsServer.ERP_Add_Power(T_code, var_)
  279. if err != nil {
  280. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s 修改失败!", sysName)}
  281. c.ServeJSON()
  282. return
  283. }
  284. } else {
  285. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  286. c.ServeJSON()
  287. return
  288. }
  289. }
  290. if power.Id > 0 {
  291. power.T_menu = T_menu
  292. id, err = NatsServer.ERP_Update_Power(T_code, power)
  293. if err != nil {
  294. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s 修改失败!", sysName)}
  295. c.ServeJSON()
  296. return
  297. }
  298. }
  299. }
  300. Account.Redis_Menu_DelK(T_id)
  301. System.Add_UserLogs_T(c.User.T_uuid, "权限", sysName+" 修改菜单", power)
  302. c.Data["json"] = lib.JSONS{Code: 200, Msg: sysName + " ok!", Data: id}
  303. c.ServeJSON()
  304. return
  305. }
  306. func (c *PowerController) Del() {
  307. T_id := c.GetString("T_id")
  308. if len(T_id) == 0 {
  309. c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_id Err!"}
  310. c.ServeJSON()
  311. return
  312. }
  313. R, err := Account.Read_Power_ByT_id(T_id)
  314. if err != nil {
  315. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  316. c.ServeJSON()
  317. return
  318. }
  319. if R.Id == 1 {
  320. c.Data["json"] = lib.JSONS{Code: 202, Msg: "禁止删除系统角色!"}
  321. c.ServeJSON()
  322. return
  323. }
  324. id, err := Account.Delete_Power(R)
  325. if err != nil {
  326. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  327. c.ServeJSON()
  328. return
  329. }
  330. //for _, prefix := range Account.ERP_Sys_prefix {
  331. // i := 3
  332. // for {
  333. // R, err = NatsServer.ERP_Read_Power_ByT_id(prefix, T_id)
  334. // if err != nil {
  335. // if err.Error() == orm.ErrNoRows.Error() {
  336. // break
  337. // }
  338. // c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  339. // c.ServeJSON()
  340. // return
  341. // }
  342. // // Nats 发送失败 重试3次
  343. // _, err = NatsServer.ERP_Delete_Power(prefix, R)
  344. // if err == nil || i == 0 {
  345. // break
  346. // }
  347. // i--
  348. // }
  349. //
  350. // if err != nil {
  351. // name := Account.Get_Sys_Name(prefix)
  352. // c.Data["json"] = lib.JSONS{Code: 202, Msg: name + " 修改失败!"}
  353. // c.ServeJSON()
  354. // return
  355. // }
  356. //}
  357. for _, prefix := range Account.ERP_Sys_prefix {
  358. go func(sys string) {
  359. var power powerlibs.Power
  360. i := 10
  361. for {
  362. power, err = NatsServer.ERP_Read_Power_ByT_id(sys, T_id)
  363. if err != nil {
  364. if err.Error() == orm.ErrNoRows.Error() {
  365. break
  366. }
  367. continue
  368. }
  369. // Nats 发送失败 重试3次
  370. _, err = NatsServer.ERP_Delete_Power(sys, power)
  371. if err == nil || i == 0 {
  372. break
  373. }
  374. i--
  375. }
  376. }(prefix)
  377. }
  378. System.Add_UserLogs_T(c.User.T_uuid, "权限", "删除", T_id)
  379. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  380. c.ServeJSON()
  381. return
  382. }