Role.Class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * 角色管理Controller
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2019/11/08
  7. * Time: 16:00
  8. */
  9. namespace JinDouYun\Controller\Department;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\StatusCode;
  12. use JinDouYun\Controller\BaseController;
  13. use JinDouYun\Model\Department\MRole;
  14. use JinDouYun\Cache\RoleAclCache;
  15. class Role extends BaseController
  16. {
  17. private $objMRole;
  18. private $objRoleAclCache;
  19. public function __construct($isCheckAcl = true, $isMustLogin = true)
  20. {
  21. parent::__construct($isCheckAcl, $isMustLogin);
  22. $this->objMRole = new MRole($this->onlineEnterpriseId);
  23. $this->objRoleAclCache = new RoleAclCache();
  24. }
  25. /**
  26. * 获取参数
  27. *
  28. * @return array
  29. */
  30. public function commonFieldFilter()
  31. {
  32. $params = $this->request->getRawJson();
  33. if (empty($params)) {
  34. $this->sendOutput('参数为空', ErrorCode::$paramError);
  35. }
  36. $returnData = [
  37. "roleName" => isset($params['roleName']) ? $params['roleName'] : '', //角色名称
  38. ];
  39. //必填项
  40. foreach ($returnData as $key => $value) {
  41. if (empty($value) && $value !== 0) {
  42. $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
  43. }
  44. }
  45. //选填项
  46. $returnData['isAdministrator'] = isset($params['isAdministrator']) ? $params['isAdministrator'] : StatusCode::$delete;
  47. $returnData['pid'] = isset($params['pid']) ? $params['pid'] : 0;
  48. $returnData['updateTime'] = time();
  49. //权限
  50. $returnData['acl'] = !empty($params['acl']) ? json_encode($params['acl']) : NULL;
  51. return $returnData;
  52. }
  53. /**
  54. * 角色添加
  55. */
  56. public function addRole()
  57. {
  58. $addRoleData = $this->commonFieldFilter();
  59. $addRoleData['shopId'] = $this->shopId;
  60. $result = $this->objMRole->addRole($addRoleData);
  61. if ($result->isSuccess()) {
  62. parent::sendOutput($result->getData());
  63. } else {
  64. parent::sendOutput($result->getData(), $result->getErrorCode());
  65. }
  66. }
  67. /**
  68. * 删
  69. */
  70. /**
  71. * 角色删除
  72. */
  73. public function deleteRole()
  74. {
  75. $id = $this->request->param('request_id');
  76. if(empty($id)){
  77. parent::sendOutput('参数为空', ErrorCode::$paramError);
  78. }
  79. $result = $this->objMRole->deleteRole($id);
  80. if ($result->isSuccess()) {
  81. $this->objRoleAclCache->delRoleIdAndAcl($this->onlineEnterpriseId, $id);
  82. parent::sendOutput($result->getData());
  83. } else {
  84. parent::sendOutput($result->getData(), $result->getErrorCode());
  85. }
  86. }
  87. /**
  88. * 改
  89. */
  90. /**
  91. * 角色修改
  92. */
  93. public function updateRole()
  94. {
  95. $id = $this->request->param('request_id');
  96. if (empty($id)) {
  97. $this->sendOutput('参数为空', ErrorCode::$paramError);
  98. }
  99. $params = $this->commonFieldFilter();
  100. $result = $this->objMRole->updateRole($params, $id);
  101. if ($result->isSuccess()) {
  102. //缓存角色和权限的关系
  103. $aclData = [
  104. 'isAdministrator' => $params['isAdministrator'],
  105. 'acl' => json_decode($params['acl']),
  106. ];
  107. $this->objRoleAclCache->cacheRoleIdAndAcl($this->onlineEnterpriseId, $id, $aclData);
  108. parent::sendOutput($result->getData());
  109. } else {
  110. parent::sendOutput($result->getData(), $result->getErrorCode());
  111. }
  112. }
  113. /**
  114. * 查
  115. */
  116. /**
  117. * 角色列表
  118. */
  119. public function getAllRole()
  120. {
  121. $params = $this->request->getRawJson();
  122. /*
  123. $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
  124. $selectParams['limit'] = $pageParams['limit'];
  125. $selectParams['offset'] = $pageParams['offset']; */
  126. if(isset($params['keyword']) && !empty($params['keyword'])) {
  127. $selectParams['roleName'] = $params['keyword'];
  128. }
  129. $selectParams['shopId'] = $this->shopId;
  130. //角色id
  131. $result = $this->objMRole->getAllRole($selectParams);
  132. if ($result->isSuccess()) {
  133. $returnData = $result->getData();
  134. /*
  135. $pageData = [
  136. 'pageIndex' => $params['page'],
  137. 'pageSize' => $params['pageSize'],
  138. 'pageTotal' => $returnData['total'],
  139. ];
  140. parent::sendOutput($returnData['data'], 0, $pageData);*/
  141. parent::sendOutput($returnData['data']);
  142. } else {
  143. parent::sendOutput($result->getData(), ErrorCode::$dberror);
  144. }
  145. }
  146. /**
  147. * 角色详情
  148. */
  149. public function getRoleInfo()
  150. {
  151. $params['id'] = $this->request->param('request_id');
  152. if (empty($params['id'])) {
  153. $this->sendOutput('参数为空', ErrorCode::$paramError);
  154. }
  155. //自增id
  156. $result = $this->objMRole->getRoleInfo($params);
  157. if ($result->isSuccess()) {
  158. parent::sendOutput($result->getData());
  159. } else {
  160. parent::sendOutput($result->getData(), $result->getErrorCode());
  161. }
  162. }
  163. }