MModule.Class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /**
  3. * 菜单管理模块
  4. * Created by PhpStorm.
  5. * User: wxj
  6. * Date: 2019/10/30
  7. * Time: 14:02
  8. */
  9. namespace JinDouYun\Model\System;
  10. use Exception;
  11. use Jindouyun\Cache\RoleAclCache;
  12. use JinDouYun\Cache\SystemCache;
  13. use Mall\Framework\Core\ErrorCode;
  14. use Mall\Framework\Core\StatusCode;
  15. use Mall\Framework\Core\ResultWrapper;
  16. use Mall\Framework\Factory;
  17. use JinDouYun\Dao\System\DModule;
  18. class MModule
  19. {
  20. private $objDModule;
  21. private $objRoleAclCache;
  22. public function __construct()
  23. {
  24. $this->objDModule = new DModule('default');
  25. $this->objRoleAclCache = new RoleAclCache();
  26. }
  27. /**
  28. * 添加菜单管理
  29. * @param $params
  30. * @return ResultWrapper
  31. * @throws Exception
  32. */
  33. public function addModule($params)
  34. {
  35. if (empty($params['extend'])) {
  36. if (self::checkAliasExists($params['alias'])) {
  37. return ResultWrapper::fail('别名已存在', ErrorCode::$dberror);
  38. }
  39. }
  40. $params['associate'] = json_encode($params['associate']);
  41. $params['associatePath'] = json_encode($params['associatePath']);
  42. $ModuleId = $this->objDModule->insert($params);
  43. if ($ModuleId === false) {
  44. return ResultWrapper::fail($this->objDModule->error(), ErrorCode::$dberror);
  45. } else {
  46. if(!empty($params['extend'])){
  47. $this->objRoleAclCache->addAuthority($params['extend']);
  48. }
  49. $objSystemCache = new SystemCache();
  50. $objSystemCache->delUriRelName(strtolower($params['extend']));
  51. return ResultWrapper::success($ModuleId);
  52. }
  53. }
  54. /**
  55. * 获取指定菜单管理信息
  56. * @param $ModuleIds
  57. * @return ResultWrapper
  58. * @throws Exception
  59. */
  60. public function getModuleInfo($ModuleIds)
  61. {
  62. $dbResult = $this->objDModule->get_by('id', $ModuleIds);
  63. if ($dbResult === false) {
  64. return ResultWrapper::fail($this->objDModule->error(), ErrorCode::$dberror);
  65. } else {
  66. $dbResult['associate'] = json_decode($dbResult['associate'], true);
  67. $dbResult['associatePath'] = json_decode($dbResult['associatePath'], true);
  68. return ResultWrapper::success($dbResult);
  69. }
  70. }
  71. /**
  72. * 编辑菜单管理
  73. * 事务修改联系人信息
  74. * @param int|array $params 修改菜单管理的数据
  75. *
  76. * @return ResultWrapper
  77. * @throws Exception
  78. */
  79. public function editModule($params)
  80. {
  81. if (empty($params['id'])) {
  82. return ResultWrapper::fail('没有指定要修改的菜单管理id', ErrorCode::$paramError);
  83. }
  84. $module = $this->objDModule->get(['id' => $params['id']]);
  85. if($module === false){
  86. return ResultWrapper::fail($this->objDModule->error(), ErrorCode::$dberror);
  87. }
  88. if(empty($module)){
  89. return ResultWrapper::fail('id不存在', ErrorCode::$dberror);
  90. }
  91. if (empty($params['extend'])) {
  92. if (self::checkAliasExists($params['alias'], $params['id'])) {
  93. return ResultWrapper::fail('别名已存在', ErrorCode::$dberror);
  94. }
  95. }
  96. $updateModuleId = $params['id'];
  97. unset($params['id']);
  98. $params['associate'] = json_encode($params['associate']);
  99. $params['associatePath'] = json_encode($params['associatePath']);
  100. $dbResult = $this->objDModule->update($params, $updateModuleId);
  101. if ($dbResult === false) {
  102. return ResultWrapper::fail($this->objDModule->error(), ErrorCode::$dberror);
  103. } else {
  104. $this->objRoleAclCache->delAuthority($module['extend']);
  105. $result = $this->objRoleAclCache->addAuthority($params['extend']);
  106. if(!$result){
  107. return ResultWrapper::fail('缓存添加失败', ErrorCode::$paramError);
  108. }
  109. return ResultWrapper::success($dbResult);
  110. }
  111. }
  112. /**
  113. * 删除菜单管理
  114. *
  115. * @param array $delModuleIds 要删除的菜单管理id
  116. * @return ResultWrapper
  117. * @throws Exception
  118. */
  119. public function delModule($delModuleIds)
  120. {
  121. if (empty($delModuleIds)) {
  122. return ResultWrapper::fail('参数错误', ErrorCode::$paramError);
  123. }
  124. $module = $this->objDModule->get(['id' => $delModuleIds]);
  125. if($module === false){
  126. return ResultWrapper::fail($this->objDModule->error(), ErrorCode::$dberror);
  127. }
  128. if(empty($module)){
  129. return ResultWrapper::fail('id不存在', ErrorCode::$dberror);
  130. }
  131. $dbResult = $this->objDModule->get(['pid' => $delModuleIds, 'deleteStatus' => StatusCode::$standard]);
  132. if($dbResult === false){
  133. return ResultWrapper::fail($this->objDModule->error(), ErrorCode::$dberror);
  134. }
  135. if(!empty($dbResult)){
  136. return ResultWrapper::fail('请先删除所有下级', ErrorCode::$dberror);
  137. }
  138. $dbResult = $this->objDModule->update(['deleteStatus' => StatusCode::$delete], $delModuleIds);
  139. if ($dbResult === false) {
  140. return ResultWrapper::fail($this->objDModule->error(), ErrorCode::$dberror);
  141. }
  142. $this->objRoleAclCache->delAuthority($module['extend']);
  143. return ResultWrapper::success($dbResult);
  144. }
  145. /**
  146. * 菜单管理启用和禁用
  147. * @param array $params
  148. * @return ResultWrapper
  149. * @throws Exception
  150. */
  151. public function updateModuleStatus($params)
  152. {
  153. $module = $this->objDModule->get(['id' => $params['id']]);
  154. if($module === false){
  155. return ResultWrapper::fail($this->objDModule->error(), ErrorCode::$dberror);
  156. }
  157. if(empty($module)){
  158. return ResultWrapper::fail('id不存在', ErrorCode::$dberror);
  159. }
  160. if($params['enableStatus'] == StatusCode::$delete){
  161. $dbResult = $this->objDModule->select(['pid' => $params['id'], 'deleteStatus' => StatusCode::$standard, 'enableStatus' => StatusCode::$standard]);
  162. if($dbResult === false){
  163. return ResultWrapper::fail($this->objDModule->error(), ErrorCode::$dberror);
  164. }
  165. if(!empty($dbResult)){
  166. return ResultWrapper::fail('请先禁用所有下级', ErrorCode::$dberror);
  167. }
  168. }
  169. $dbResult = $this->objDModule->update(['enableStatus' => $params['enableStatus']], $params['id']);
  170. if ($dbResult === false) {
  171. return ResultWrapper::fail($this->objDModule->error(), ErrorCode::$dberror);
  172. } else {
  173. if($params['enableStatus'] == StatusCode::$standard){
  174. $this->objRoleAclCache->addAuthority($module['extend']);
  175. }else{
  176. $this->objRoleAclCache->delAuthority($module['extend']);
  177. }
  178. $this->objRoleAclCache->delAuthority($params['id']);
  179. return ResultWrapper::success($dbResult);
  180. }
  181. }
  182. /**
  183. * 判断别名是否存在
  184. * @param $alias
  185. * @param null $id
  186. * @return bool
  187. */
  188. public function checkAliasExists($alias, $id = null)
  189. {
  190. $where = 'alias = "' . $alias . '"';
  191. if ($id) {
  192. $where .= ' AND id !=' . $id;
  193. }
  194. $where .= ' AND deleteStatus =' . StatusCode::$standard;
  195. $result = $this->objDModule->select($where);
  196. return !empty($result) ? true : false;
  197. }
  198. /**
  199. * 获取企业下的所有菜单管理数据
  200. *
  201. * @param $params
  202. * @return ResultWrapper
  203. */
  204. public function getAllModule($params)
  205. {
  206. $selectParams['deleteStatus'] = StatusCode::$standard;
  207. !empty($params['enableStatus']) && $selectParams['enableStatus'] = $params['enableStatus'];
  208. $dbResult = $this->objDModule->select($selectParams, '*', 'createTime asc');
  209. if ($dbResult === false) {
  210. return ResultWrapper::fail($this->objDModule->error(), ErrorCode::$dberror);
  211. }
  212. $dbResult = self::arr2tree($dbResult);
  213. return ResultWrapper::success($dbResult);
  214. }
  215. public function getDataModule($where = [])
  216. {
  217. $dbResult = $this->objDModule->select($where);
  218. if ($dbResult === false) {
  219. return ResultWrapper::fail($this->objDModule->error(), ErrorCode::$dberror);
  220. }
  221. return ResultWrapper::success($dbResult);
  222. }
  223. /**
  224. * 多维数组转树状结构
  225. *
  226. * @param $tree
  227. * @param int $rootId
  228. * @return array
  229. */
  230. public function arr2tree($tree, $rootId = 0)
  231. {
  232. $return = [];
  233. foreach ($tree as $leaf) {
  234. if ($leaf['pid'] == $rootId) {
  235. foreach ($tree as $subLeaf) {
  236. if ($subLeaf['pid'] == $leaf['id']) {
  237. $leaf['children'] = self::arr2tree($tree, $leaf['id']);
  238. break;
  239. }
  240. }
  241. $return[] = $leaf;
  242. }
  243. }
  244. return $return;
  245. }
  246. /**
  247. * Doc: (des="根据路径获取菜单路径名称")
  248. * User: XMing
  249. * Date: 2020/11/14
  250. * Time: 6:12 下午
  251. * @param string $uri
  252. * @return string
  253. */
  254. public function getModuleNameByUri(string $uri): string
  255. {
  256. $objSystemCache = new SystemCache();
  257. $moduleNames = $objSystemCache->getUriRelName($uri);
  258. if ($moduleNames != ''){
  259. return $moduleNames;
  260. }
  261. $module = $this->objDModule->get(['extend' => $uri], 'name,pid');
  262. if ($module === false){
  263. return '';
  264. }
  265. if ($module['pid'] == 0){
  266. $objSystemCache->setUriRelName($uri,$module['name']);
  267. return $module['name'];
  268. }
  269. $pid = $module['pid'];
  270. $moduleNames = '['.$module['name'].']';
  271. unset($module);
  272. while ($pid){
  273. $module = $this->objDModule->get(['id' => $pid],'pid,name');
  274. if ($module === false){
  275. return '';
  276. }
  277. $moduleNames = '['.$module['name'].']->'.$moduleNames;
  278. $pid = $module['pid'];
  279. }
  280. $objSystemCache->setUriRelName($uri,$moduleNames);
  281. return $moduleNames;
  282. }
  283. /**
  284. * 初始化缓存
  285. */
  286. public function initCache()
  287. {
  288. $data = $this->objDModule->select(['deleteStatus' => StatusCode::$standard, 'enableStatus' => StatusCode::$standard]);
  289. if($data === false){
  290. return ResultWrapper::fail($this->objDModule->error(), ErrorCode::$paramError);
  291. }
  292. foreach($data as $value){
  293. if(!empty($value['extend'])){
  294. $this->objRoleAclCache->addAuthority(strtolower($value['extend']));
  295. }
  296. }
  297. return ResultWrapper::success('初始化成功');
  298. }
  299. }