PermissionController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace frontend\modules\api\controllers;
  3. use common\components\AjaxException;
  4. use common\models\BaseMenu;
  5. use common\models\BaseRole;
  6. use common\models\BaseRoleClientRules;
  7. use common\services\MenuService;
  8. use common\services\RoleService;
  9. use frontend\modules\api\components\BaseAdminController;
  10. use Yii;
  11. use yii\base\InvalidConfigException;
  12. use yii\web\Response;
  13. class PermissionController extends BaseAdminController
  14. {
  15. /**
  16. * 路由列表
  17. * @param int $current
  18. * @param int $page
  19. * @return Response
  20. */
  21. public function actionRoute(int $current = 1, int $page = 10)
  22. {
  23. //公共部分
  24. $query = BaseMenu::find()
  25. ->limit($page)
  26. ->offset(($current - 1) * $page);
  27. //排序
  28. $this->initSequence($query);
  29. //主要筛选条件
  30. $query = $this->addConditionToQuery("id", $query);
  31. $query = $this->addConditionToQuery("url", $query, true);
  32. $query = $this->addConditionToQuery("name", $query, true);
  33. $query = $this->addConditionToQuery("type", $query);
  34. $query = $this->addConditionToQuery("status", $query);
  35. //组合数据
  36. $data['data'] = $query->asArray()->all();
  37. $data['total'] = $query->count();
  38. return $this->asJson($data);
  39. }
  40. /**
  41. * 刷新路由
  42. * @return Response
  43. */
  44. public function actionRouteRefresh()
  45. {
  46. $data = Yii::$app->request->post();
  47. $arr = [];
  48. $defaultArr = [
  49. "/",
  50. "/login",
  51. "/register",
  52. "/redirect",
  53. "/permission",
  54. "/permission/index",
  55. "/permission/role",
  56. "/permission/route",
  57. "/:pathMatch(.*)",
  58. "/redirect/:path(.*)",
  59. ];
  60. //获取客户端权限
  61. if ($data['menu']) {
  62. $i = 0;
  63. foreach ($data['menu'] as $value) {
  64. if (in_array($value['path'], $defaultArr)) {
  65. continue;
  66. }
  67. $arr[$i]['url'] = $value['path'];
  68. $arr[$i]['name'] = $value['name'];
  69. $i++;
  70. }
  71. }
  72. foreach ($arr as $value) {
  73. //如果没有则插入
  74. $mInfo = BaseMenu::findOne(['url' => $value['url']]);
  75. if ($mInfo) {
  76. //如果有,则修改
  77. $mInfo->name = $value['name'];
  78. $mInfo->save();
  79. } else {
  80. //插入
  81. $params = [
  82. 'url' => $value['url'],
  83. 'name' => $value['name'],
  84. 'type' => 2,
  85. 'status' => 1,
  86. ];
  87. $insertInfo = new BaseMenu($params);
  88. $insertInfo->save();
  89. }
  90. }
  91. //找父级
  92. $all = BaseMenu::find()
  93. ->select("url,parentid,status")
  94. ->where(['status' => 1])
  95. ->orderBy("url asc")
  96. ->asArray()
  97. ->all();
  98. foreach ($all as $key => $value) {
  99. $newUrl = $value['url'];
  100. $n = mb_substr_count($newUrl, '/');
  101. if ($n > 0) {
  102. $urlArray = explode('/', $newUrl);
  103. $parentUrl = "";
  104. foreach ($urlArray as $urlArrayKey => $url) {
  105. if ($urlArrayKey >= $n) {
  106. break;
  107. }
  108. if ($urlArrayKey == 0) {
  109. $parentUrl .= $url;
  110. } else {
  111. $parentUrl .= "/" . $url;
  112. }
  113. }
  114. $thatOne = BaseMenu::findOne(['url' => $parentUrl]);
  115. $parent_id = $thatOne->id;
  116. $thisOne = BaseMenu::findOne(['url' => $value['url']]);
  117. $thisOne->parentid = $parent_id;
  118. $thisOne->save();
  119. }
  120. }
  121. //返回
  122. return $this->asJson($arr);
  123. }
  124. /**
  125. * 读取路由
  126. * @return Response
  127. */
  128. public function actionRouteList()
  129. {
  130. //公共部分
  131. $query = BaseMenu::find()
  132. ->select('id,url,name,parentid,status')
  133. ->where(['status' => 1])
  134. ->orderBy("url asc");
  135. $data['route'] = MenuService::getMenuObjData($query->all());
  136. return $this->asJson($data);
  137. }
  138. /**
  139. * 路由修改
  140. * @param $url
  141. * @param $name
  142. * @param $status
  143. * @return Response
  144. */
  145. public function actionRouteUpdate($url, $name, $status)
  146. {
  147. $info = BaseMenu::findOne(['url' => $url]);
  148. if ($info) {
  149. $info->name = $name;
  150. $info->status = $status;
  151. $info->save();
  152. }
  153. $data = [];
  154. return $this->asJson($data);
  155. }
  156. /**
  157. * 角色列表
  158. * @param int $current
  159. * @param int $page
  160. * @return Response
  161. */
  162. public function actionRole(int $current = 1, int $page = 10)
  163. {
  164. //公共部分
  165. $query = BaseRole::find()
  166. ->where("id > 1")
  167. ->limit($page)
  168. ->offset(($current - 1) * $page);
  169. //排序
  170. $this->initSequence($query);
  171. //主要筛选条件
  172. $query = $this->addConditionToQuery("id", $query);
  173. $query = $this->addConditionToQuery("name",$query, true);
  174. $query = $this->addConditionToQuery("status", $query);
  175. //组合数据
  176. $data['data'] = $query->asArray()->all();
  177. $rules = '';
  178. //拼接数据
  179. foreach ($data["data"] as $recordKey => $record) {
  180. /** @var BaseRoleClientRules $roleMenuInfo */
  181. $roleMenuInfo = BaseRoleClientRules::find()
  182. ->select("rules")
  183. ->where(['role_id' => $record['id']])
  184. ->one();
  185. if ($roleMenuInfo) {
  186. $data['data'][$recordKey]['rules'] = implode(",", json_decode($roleMenuInfo->rules, true));
  187. } else {
  188. $data['data'][$recordKey]['rules'] = "";
  189. }
  190. }
  191. $data['total'] = $query->count();
  192. return $this->asJson($data);
  193. }
  194. /**
  195. * 角色合集
  196. * @return Response
  197. */
  198. public function actionRolesList()
  199. {
  200. //公共部分
  201. $query = BaseRole::find()
  202. ->select("id,name")
  203. ->where("id > 1")
  204. ->orderBy("id desc");
  205. //组合数据
  206. $data = $query->asArray()->all();
  207. $arr = [];
  208. foreach ($data as $k => $v) {
  209. $arr[$k] = $v;
  210. }
  211. $data['roles'] = $arr;
  212. return $this->asJson($data);
  213. }
  214. /**
  215. * 角色新增
  216. * @param $name
  217. * @param $code
  218. * @param $status
  219. * @return Response
  220. * @throws AjaxException
  221. */
  222. public function actionRoleAdd($name, $code, $status)
  223. {
  224. RoleService::add($name, $code, $status);
  225. return $this->asJson();
  226. }
  227. /**
  228. * 角色资料和权限修改
  229. * @return Response
  230. * @throws AjaxException
  231. * @throws InvalidConfigException
  232. */
  233. public function actionRoleUpdate()
  234. {
  235. RoleService::update();
  236. $data = [
  237. 'id' => $_GET['id'],
  238. ];
  239. return $this->asJson($data);
  240. }
  241. }