MRole.Class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. /**
  3. * 角色管理Model
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2019/11/08
  7. * Time: 16:00
  8. */
  9. namespace JinDouYun\Model\Department;
  10. use JinDouYun\Dao\System\DAdminSetting;
  11. use JinDouYun\Model\System\MModule;
  12. use Mall\Framework\Core\ErrorCode;
  13. use Mall\Framework\Core\StatusCode;
  14. use Mall\Framework\Core\ResultWrapper;
  15. use JinDouYun\Cache\RoleAclCache;
  16. use JinDouYun\Dao\Department\DRole;
  17. use JinDouYun\Model\Department\MRoleAcl;
  18. class MRole
  19. {
  20. private $objDRole;
  21. private $objMRoleAcl;
  22. private $enterpriseId;
  23. private $objDAdminSetting;
  24. public function __construct($enterpriseId)
  25. {
  26. $this->objDRole = new DRole('default');
  27. $this->objMRoleAcl = new MRoleAcl($enterpriseId);
  28. $this->enterpriseId = $enterpriseId;
  29. $this->objDRole->setTable($this->objDRole->get_Table().'_'.$enterpriseId);
  30. $this->objDAdminSetting = new DAdminSetting();
  31. self::initRole();
  32. }
  33. /**
  34. * Doc: (des="初始化角色")
  35. * User: XMing
  36. * Date: 2020/9/4
  37. * Time: 11:43 上午
  38. */
  39. public function initRole()
  40. {
  41. $adminLists = $this->objDAdminSetting->select(['type'=>StatusCode::$adminSettingType['role']]);
  42. if ($adminLists === false){
  43. return ResultWrapper::fail($this->objDAdminSetting->error(),ErrorCode::$dberror);
  44. }
  45. if (empty($adminLists)){
  46. return ResultWrapper::success();
  47. }
  48. $allSignIds = [];
  49. foreach ($adminLists as $value){
  50. $allSignIds[] = $value['signId'];
  51. }
  52. $roleList = $this->objDRole->select(['signId' => $allSignIds]);
  53. if ($roleList === false){
  54. return ResultWrapper::fail($this->objDRole->error(),ErrorCode::$dberror);
  55. }
  56. $insert = [];
  57. if (empty($roleList)){
  58. foreach ($adminLists as $item){
  59. $insert[] = [
  60. 'roleName' => $item['title'],
  61. 'pid' => 0,
  62. 'createTime' => time(),
  63. 'signId' => $item['signId']
  64. ];
  65. }
  66. }else{
  67. $allRoleSignIds = [];
  68. foreach ($roleList as $role) {
  69. $allRoleSignIds[] = $role['signId'];
  70. }
  71. foreach ($adminLists as $item){
  72. if (!in_array($item['signId'],$allRoleSignIds)){
  73. $insert[] = [
  74. 'roleName' => $item['title'],
  75. 'pid' => 0,
  76. 'createTime' => time(),
  77. 'signId' => $item['signId']
  78. ];
  79. }
  80. }
  81. }
  82. if (!empty($insert)){
  83. $insertResult = $this->objDRole->insert($insert,true);
  84. if ($insertResult === false){
  85. return ResultWrapper::fail($this->objDRole->error(),ErrorCode::$dberror);
  86. }
  87. }
  88. return ResultWrapper::success(true);
  89. }
  90. /**
  91. * 角色添加
  92. * @param $params
  93. * @return ResultWrapper
  94. */
  95. public function addRole($params)
  96. {
  97. $params['createTime'] = $params['updateTime'];
  98. $params['deleteStatus'] = StatusCode::$standard;
  99. $isAdministrator = $params['isAdministrator'];
  100. unset($params['isAdministrator']);
  101. $acl = $params['acl'];
  102. unset($params['acl']);
  103. if(empty( $params['shopId'])){
  104. $params['shopId'] = '';
  105. }
  106. //校验名称
  107. $dbResult = $this->objDRole->select(['roleName' => $params['roleName'], 'shopId' => $params['shopId'], 'deleteStatus' => StatusCode::$standard]);
  108. if($dbResult === false){
  109. return ResultWrapper::fail($this->objDRole->error, ErrorCode::$dberror);
  110. }
  111. if($dbResult){
  112. return ResultWrapper::fail('名称已存在', ErrorCode::$paramError);
  113. }
  114. unset($dbResult);
  115. $dbResult = $this->objDRole->insert($params);
  116. if($dbResult === false){
  117. return ResultWrapper::fail($this->objDRole->error(), ErrorCode::$dberror);
  118. }else{
  119. $roleAclData = [
  120. 'isAdministrator' => $isAdministrator,
  121. 'acl' => $acl,
  122. 'roleId' => $dbResult,
  123. 'updateTime' => time(),
  124. 'createTime' => time()
  125. ];
  126. $result = $this->objMRoleAcl->addRoleAcl($roleAclData);
  127. if($result->isSuccess() == false) {
  128. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  129. }
  130. //缓存角色和权限的关系
  131. $aclData = [
  132. 'isAdministrator' => $isAdministrator,
  133. 'acl' => json_decode($acl),
  134. ];
  135. $objRoleAclCache = new RoleAclCache();
  136. $objRoleAclCache->cacheRoleIdAndAcl($this->enterpriseId, $dbResult, $aclData);
  137. $objMModule = new MModule();
  138. if($isAdministrator == StatusCode::$delete){
  139. //缓存角色绑定权限
  140. $acl = json_decode($acl, true);
  141. if(!empty($acl)){
  142. foreach($acl as $value){
  143. if(!empty($value['extend'])){
  144. $objRoleAclCache->addAuthorityBindRole($this->enterpriseId, $dbResult, $value['extend']);
  145. }
  146. $value['associate'] = json_decode($value['associate'], true);
  147. if(!empty($value['associate'])){
  148. $modelResult = $objMModule->getDataModule(['id' => $value['associate']]);
  149. if(!$modelResult->isSuccess()){
  150. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  151. }
  152. $array = $modelResult->getData();
  153. foreach($array as $vv){
  154. if(!empty($vv['extend'])){
  155. $objRoleAclCache->addAuthorityBindRole($this->enterpriseId, $dbResult, $vv['extend']);
  156. }
  157. }
  158. }
  159. }
  160. }
  161. }else{
  162. //缓存超级管理员
  163. $objRoleAclCache->addAdministrator($this->enterpriseId, $dbResult);
  164. }
  165. return ResultWrapper::success($dbResult);
  166. }
  167. }
  168. /**
  169. * 角色删除
  170. * @param $id
  171. * @return ResultWrapper
  172. */
  173. public function deleteRole($id)
  174. {
  175. $params = [
  176. 'deleteStatus' => StatusCode::$delete,
  177. 'updateTime' => time(),
  178. ];
  179. $roleInfo = $this->objDRole->get(['id'=>$id]);
  180. if ($roleInfo === false){
  181. return ResultWrapper::fail($this->objDRole->error(),ErrorCode::$dberror);
  182. }
  183. if (empty($roleInfo)){
  184. return ResultWrapper::fail('未获取到指定角色',ErrorCode::$paramError);
  185. }
  186. if (in_array($roleInfo['signId'],[StatusCode::$roleId['cashier'],StatusCode::$roleId['guide']])){
  187. return ResultWrapper::fail('内置角色不能删除',ErrorCode::$dberror);
  188. }
  189. $dbResult = $this->objDRole->update($params, $id);
  190. if($dbResult === false){
  191. return ResultWrapper::fail($this->objDRole->error(), ErrorCode::$dberror);
  192. }else{
  193. //删除权限
  194. $result = $this->objMRoleAcl->deleteRoleAcl(['roleId'=>$id]);
  195. if($result->isSuccess() == false) {
  196. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  197. }
  198. $objRoleAclCache = new RoleAclCache();
  199. $objRoleAclCache->delAdministrator($this->enterpriseId, $id);
  200. $objRoleAclCache->delAuthorityBindRole($this->enterpriseId, $id);
  201. return ResultWrapper::success($dbResult);
  202. }
  203. }
  204. /**
  205. * 角色修改
  206. * @param $params
  207. * @param $id
  208. * @return ResultWrapper
  209. */
  210. public function updateRole($params, $id)
  211. {
  212. $isAdministrator = $params['isAdministrator'];
  213. unset($params['isAdministrator']);
  214. $acl = $params['acl'];
  215. unset($params['acl']);
  216. $roleInfo = $this->objDRole->get(['id'=>$id]);
  217. if ($roleInfo === false){
  218. return ResultWrapper::fail($this->objDRole->error(),ErrorCode::$dberror);
  219. }
  220. if (empty($roleInfo)){
  221. return ResultWrapper::fail('未获取到指定角色',ErrorCode::$paramError);
  222. }
  223. if (in_array($roleInfo['signId'],[StatusCode::$roleId['cashier'],StatusCode::$roleId['guide']])){
  224. //收银员,或导购不能修改名称
  225. unset($params['roleName']);
  226. }
  227. $dbResult = $this->objDRole->update($params,$id);
  228. if($dbResult === false){
  229. return ResultWrapper::fail($this->objDRole->error(), ErrorCode::$dberror);
  230. }else{
  231. //修改权限
  232. $roleAclData = [
  233. 'isAdministrator' => $isAdministrator,
  234. 'acl' => $acl,
  235. 'roleId' => $id,
  236. 'updateTime' => time()
  237. ];
  238. $result = $this->objMRoleAcl->addRoleAcl($roleAclData);
  239. if($result->isSuccess() == false) {
  240. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  241. }
  242. //修改缓存
  243. $objRoleAclCache = new RoleAclCache();
  244. if($isAdministrator == StatusCode::$delete){
  245. $objRoleAclCache->delAdministrator($this->enterpriseId, $id);
  246. $objRoleAclCache->delAuthorityBindRole($this->enterpriseId, $id);
  247. //缓存角色绑定权限
  248. $objMModule = new MModule();
  249. $acl = json_decode($acl, true);
  250. foreach($acl as $value){
  251. if(!empty($value['extend'])){
  252. $objRoleAclCache->addAuthorityBindRole($this->enterpriseId, $id, $value['extend']);
  253. }
  254. $value['associate'] = json_decode($value['associate'], true);
  255. if(!empty($value['associate'])){
  256. $modelResult = $objMModule->getDataModule(['id' => $value['associate']]);
  257. if(!$modelResult->isSuccess()){
  258. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  259. }
  260. $array = $modelResult->getData();
  261. foreach($array as $vv){
  262. if(!empty($vv['extend'])){
  263. $objRoleAclCache->addAuthorityBindRole($this->enterpriseId, $id, $vv['extend']);
  264. }
  265. }
  266. }
  267. }
  268. }else{
  269. $objRoleAclCache->addAdministrator($this->enterpriseId, $id);
  270. $objRoleAclCache->delAuthorityBindRole($this->enterpriseId, $id);
  271. }
  272. return ResultWrapper::success($dbResult);
  273. }
  274. }
  275. /**
  276. * 角色列表
  277. * @param $selectParams
  278. * @return ResultWrapper
  279. */
  280. public function getAllRole($selectParams)
  281. {
  282. /*$limit = $selectParams['limit'];
  283. unset($selectParams['limit']);
  284. $offset = $selectParams['offset'];
  285. unset($selectParams['offset']);*/
  286. $where = 'deleteStatus=' . StatusCode::$standard;
  287. if(isset($selectParams['roleName'])) {
  288. $where .= ' AND roleName LIKE "%' . $selectParams['roleName'] . '%"';
  289. }
  290. $dbResult = $this->objDRole->select($where, '*', 'createTime desc');
  291. if ($dbResult === false) {
  292. return ResultWrapper::fail($this->objDRole->error(), ErrorCode::$dberror);
  293. }
  294. $total = $this->objDRole->count($where);
  295. $return = [
  296. 'data' => self::formatList($dbResult),
  297. 'total' => ($total) ? intval($total) : 0,
  298. ];
  299. if($return === false){
  300. return ResultWrapper::fail($this->objDRole->error(), ErrorCode::$dberror);
  301. }else{
  302. return ResultWrapper::success($return);
  303. }
  304. }
  305. /**
  306. * 角色详情
  307. * @param $params
  308. * @return ResultWrapper
  309. */
  310. public function getRoleInfo($params)
  311. {
  312. $dbResult = $this->objDRole->get($params);
  313. if($dbResult === false){
  314. return ResultWrapper::fail($this->objDRole->error(), ErrorCode::$dberror);
  315. }else{
  316. //获取角色的权限
  317. $roleAclResult = $this->objMRoleAcl->getRoleAclInfo(['roleId'=>$params['id']]);
  318. $roleAclInfo = $roleAclResult->getData();
  319. $dbResult['isAdministrator'] = isset($roleAclInfo['isAdministrator']) ? $roleAclInfo['isAdministrator'] : StatusCode::$delete;
  320. $dbResult['acl'] = isset($roleAclInfo['acl']) ? $roleAclInfo['acl'] : [];
  321. return ResultWrapper::success(self::formatInfo($dbResult));
  322. }
  323. }
  324. /**
  325. * 格式化角色详情
  326. * @param $data
  327. * @return mixed
  328. */
  329. public function formatInfo($data)
  330. {
  331. //取出上级id
  332. $roleId = $data['pid'];
  333. //查询上级 id 名称
  334. $roleData = $this->objDRole->get(['id' => $roleId], 'roleName');
  335. $data['pidName'] = isset($roleData['roleName']) ? $roleData['roleName'] : '';
  336. return $data;
  337. }
  338. /**
  339. * 格式化角色列表
  340. */
  341. public function formatList($data)
  342. {
  343. //格式化层级关系
  344. return arr2tree($data);
  345. }
  346. /**
  347. * 根据角色id获取角色id和角色名称
  348. * @param $id
  349. * @return array|ResultWrapper
  350. */
  351. public function roleDataByRoleId($id)
  352. {
  353. //查询 id 部门名称
  354. $where = [
  355. 'deleteStatus' => StatusCode::$standard,
  356. 'id' => $id,
  357. ];
  358. $fields = 'id, roleName';
  359. $departmentData = $this->objDRole->get($where, $fields);
  360. if($departmentData === false){
  361. return ResultWrapper::fail($this->objDRole->error(), ErrorCode::$dberror);
  362. }
  363. return $departmentData;
  364. }
  365. /**
  366. * 根据角色ids获取角色id和角色名称
  367. * @param array $ids
  368. * @return bool|ResultWrapper
  369. */
  370. public function roleDataByRoleIds(array $ids)
  371. {
  372. //查询所有 id 角色名称
  373. $where = [
  374. 'id' => $ids,
  375. 'deleteStatus' => StatusCode::$standard,
  376. ];
  377. $fields = 'id,roleName';
  378. $RoleData = $this->objDRole->select($where, $fields);
  379. if($RoleData === false){
  380. return ResultWrapper::fail($this->objDRole->error(), ErrorCode::$dberror);
  381. }
  382. return $RoleData;
  383. }
  384. }