AuthRoleForm.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\constants\OptionSetting;
  9. use app\models\AdminRole;
  10. use app\models\Md;
  11. use app\models\Option;
  12. use app\models\Store;
  13. use Yii;
  14. use yii\base\Model;
  15. use app\models\AuthRole;
  16. class AuthRoleForm extends Model
  17. {
  18. public $id;
  19. public $name;
  20. public $data;
  21. public $edit_data;
  22. public $describe;
  23. public $status;
  24. public function rules()
  25. {
  26. return [
  27. [['name'], 'trim'],
  28. [['name'], 'string', 'max' => 64],
  29. [['describe'], 'string', 'max' => 255],
  30. [['data', 'edit_data'], 'string'],
  31. [['status', 'id'], 'integer']
  32. ];
  33. }
  34. public function attributeLabels()
  35. {
  36. return [
  37. 'id' => '角色ID',
  38. 'name' => '角色名称',
  39. 'data' => '角色数据',
  40. 'edit_data' => '角色数据',
  41. 'describe' => '简介',
  42. 'status' => '状态',
  43. ];
  44. }
  45. /**
  46. * 创建角色
  47. * @return array
  48. */
  49. public function create()
  50. {
  51. if ($this->validate()) {
  52. if (AuthRole::findOne(['name' => $this->name, 'store_id' => get_store_id()])) {
  53. return [
  54. 'code' => 1,
  55. 'msg' => '角色名称已经存在.',
  56. ];
  57. }
  58. $data = json_decode($this->data, true);
  59. array_unshift($data, 'dashboard');
  60. $role = new AuthRole();
  61. $role->name = $this->name;
  62. $role->data = json_encode($data);
  63. $role->edit_data = $this->edit_data;
  64. $role->status = $this->status;
  65. $role->store_id = get_store_id();
  66. if ($this->describe) {
  67. $role->describe = $this->describe;
  68. }
  69. if ($role->save()) {
  70. return [
  71. 'code' => 0,
  72. 'msg' => '创建成功'
  73. ];
  74. }
  75. return [
  76. 'code' => 1,
  77. 'msg' => '创建失败',
  78. ];
  79. }
  80. return [
  81. 'code' => 1,
  82. 'msg' => $this->getErrorSummary(false)[0],
  83. ];
  84. }
  85. /**
  86. * 编辑角色
  87. * @return array
  88. */
  89. public function edit()
  90. {
  91. if ($this->validate()) {
  92. $role = AuthRole::findOne($this->id);
  93. if (! $role) {
  94. return [
  95. 'code' => 1,
  96. 'msg' => '角色不存在',
  97. ];
  98. }
  99. $data = json_decode($this->data, true);
  100. array_unshift($data, 'dashboard');
  101. $role->name = $this->name;
  102. $role->data = json_encode($data);
  103. $role->edit_data = $this->edit_data;
  104. $role->describe = $this->describe;
  105. $role->status = $this->status;
  106. if ($role->save()) {
  107. return [
  108. 'code' => 0,
  109. 'msg' => '保存成功',
  110. ];
  111. }
  112. return [
  113. 'code' => 1,
  114. 'msg' => '保存失败',
  115. ];
  116. }
  117. return [
  118. 'code' => 1,
  119. 'msg' => $this->getErrorSummary(false)[0],
  120. ];
  121. }
  122. /**
  123. * @return array
  124. * @throws \Throwable
  125. * @throws \yii\base\InvalidConfigException
  126. * @throws \yii\db\StaleObjectException
  127. */
  128. public function deteleRole()
  129. {
  130. if (! $this->id) {
  131. return [
  132. 'code' => 1,
  133. 'msg' => '请提供角色ID',
  134. ];
  135. }
  136. $role = AuthRole::findOne($this->id);
  137. if (! $role) {
  138. return [
  139. 'code' => 1,
  140. 'msg' => '角色不存在',
  141. ];
  142. }
  143. $adminCount = $role->getAdmins()->count();
  144. if ($adminCount > 0) {
  145. return [
  146. 'code' => 1,
  147. 'msg' => '该角色还有员工使用,不能删除',
  148. ];
  149. }
  150. if ($role->delete()) {
  151. return [
  152. 'code' => 0,
  153. 'msg' => '删除成功',
  154. ];
  155. }
  156. return [
  157. 'code' => 1,
  158. 'msg' => '删除失败',
  159. ];
  160. }
  161. /**
  162. * 获取角色列表
  163. * @return array
  164. */
  165. public function getRoles()
  166. {
  167. $query = AuthRole::find()->where(['store_id' => get_store_id()]);
  168. $pagination = pagination_make($query);
  169. $roles = $pagination['list'];
  170. foreach ($roles as $key => $role) {
  171. $roles[$key]['data'] = json_decode($role['data']);
  172. AuthRoleForm::formatPermission($roles[$key]['data']);
  173. $roles[$key]['edit_data'] = [];
  174. if (!empty($role['edit_data'])) {
  175. $roles[$key]['edit_data'] = json_decode($role['edit_data'], true);
  176. AuthRoleForm::formatPermission($roles[$key]['edit_data']);
  177. }
  178. }
  179. return [
  180. 'code' => 0,
  181. 'msg' => 'success',
  182. 'data' => [
  183. 'data' => $roles,
  184. 'pageNo' => $pagination['pageNo'],
  185. 'totalCount' => $pagination['totalCount'],
  186. ],
  187. ];
  188. }
  189. /**
  190. * 格式化权限
  191. * @param array $permission
  192. * @param $key
  193. */
  194. public static function formatPermission(array &$permission, $key = null)
  195. {
  196. foreach ($permission as &$value) {
  197. if ($key) {
  198. if (isset($value['type']) && $value['type'] == 'action') {
  199. $action = explode('_', $key);
  200. $action = array_pop($action);
  201. $value['key'] = $key . '_' . $action . '@' . $value['key'];
  202. } else {
  203. $value['key'] = $key . '_' . $value['key'];
  204. }
  205. }
  206. if (isset($value['children']) && count($value['children']) > 0) {
  207. static::formatPermission($value['children'], $value['key']);
  208. }
  209. }
  210. }
  211. /**
  212. * 获取账号拥有的权限
  213. * @param null $params
  214. * @return array
  215. */
  216. public static function getAdminPermission($params = null)
  217. {
  218. $adminPermission = $params;
  219. if (! $params) {
  220. $admin = Yii::$app->jwt->getAdmin();
  221. $adminPermission = AdminRole::find()->alias('ar')->where(['ar.admin_id' => $admin->id])
  222. ->leftJoin(['ad' => AuthRole::tableName()], 'ad.id=ar.role_id')->select('ad.data')->column();
  223. if (count($adminPermission) > 0) {
  224. foreach ($adminPermission as &$v) {
  225. $v = json_decode($v);
  226. }
  227. $adminPermission = array_unique(array_merge(...$adminPermission));
  228. }
  229. }
  230. $result = [];
  231. foreach ($adminPermission as $value) {
  232. $permission = explode('_', is_array($value) ? '' : $value);
  233. foreach ($permission as $v) {
  234. if (! isset($result[$v])) {
  235. if (strpos($v, '@') !== false) {
  236. $ex = explode('@', $v);
  237. $result[$ex[0]]['actionEntitySet'][] = [
  238. 'action' => $ex[1],
  239. ];
  240. } else {
  241. $result[$v] = [
  242. 'permissionId' => $v,
  243. ];
  244. }
  245. }
  246. }
  247. }
  248. //获取门店是否开启不开启独立运营
  249. $id = get_md_id();
  250. $md_detail = Md::findOne($id);
  251. if (!empty($id) && $md_detail->is_single === 0) {
  252. unset($result['outletGoods']);
  253. unset($result['wastoreManageGoods']);
  254. }
  255. return array_values($result);
  256. }
  257. /**
  258. * 获取所有权限key
  259. * @param $params
  260. * @param $result
  261. */
  262. public static function getAllPermission($params, &$result)
  263. {
  264. foreach ($params as $value) {
  265. $result[] = $value['key'];
  266. if (isset($value['children'])) {
  267. static::getAllPermission($value['children'], $result);
  268. }
  269. }
  270. }
  271. public static function delEmptyAuth($params) {
  272. $arr = [
  273. ];
  274. foreach ($params as $value) {
  275. if (!isset($value['children'])) {
  276. $arr[] = $value;
  277. } else {
  278. if (!empty($value['children'])) {
  279. static::delEmptyAuth($value['children']);
  280. $arr[] = $value;
  281. }
  282. }
  283. }
  284. return $arr;
  285. }
  286. // 过滤菜单
  287. public static function filterAuth($params, $whitelist = [])
  288. {
  289. $result = []; // 用于存储过滤后的结果
  290. foreach ($params as $item) {
  291. // 如果当前项的 key 在白名单中
  292. if (in_array($item['key'], $whitelist)) {
  293. // 复制当前项到结果中
  294. $filteredItem = $item;
  295. // 如果当前项有 children,递归过滤 children
  296. if (isset($item['children'])) {
  297. $filteredItem['children'] = static::filterAuth($item['children'], $whitelist);
  298. }
  299. $result[] = $filteredItem;
  300. } else {
  301. // 如果当前项不在白名单中,但存在 children,递归过滤 children
  302. if (isset($item['children'])) {
  303. $filteredChildren = static::filterAuth($item['children'], $whitelist);
  304. // 如果过滤后的 children 不为空,将当前项加入结果
  305. if (!empty($filteredChildren)) {
  306. $item['children'] = $filteredChildren;
  307. $result[] = $item;
  308. }
  309. }
  310. }
  311. }
  312. return $result;
  313. }
  314. //获取公用的方法
  315. public static function getCommonPermission() {
  316. $admin = get_admin();
  317. $md_id = get_md_id();
  318. $mch_id = get_mch_id();
  319. if ($md_id && $md_id > 0) {
  320. $params = require Yii::$app->basePath . '/config/saas_md_permission.php';
  321. $md = \app\models\Md::findOne($md_id);
  322. if ($md && $md->is_single == 0) { // 非独立运营门店去除同城配送
  323. // \Yii::$app->removeMenu($params, ['WechatNewDelivery']);//非独立运营门店也需要自己独立的发货地址
  324. }
  325. $params = \Yii::$app->filterMenu($params);
  326. AuthRoleForm::formatPermission($params);
  327. $result = [];
  328. AuthRoleForm::getAllPermission($params, $result);
  329. }elseif ($mch_id && $mch_id > 0) {
  330. $params = require Yii::$app->basePath . '/config/saas_mch_permission.php';
  331. $params = \Yii::$app->filterMenu($params);
  332. AuthRoleForm::formatPermission($params);
  333. $result = [];
  334. AuthRoleForm::getAllPermission($params, $result);
  335. } else {
  336. $storeInfo = Store::find()->where(['id'=> $admin->store_id,"is_delete"=>0])->asArray()->one();
  337. if(!$storeInfo || !$storeInfo['business_model']){
  338. return [
  339. 'code' => 1,
  340. 'msg' => '店铺信息有误,请联系管理员',
  341. ];
  342. }
  343. if (!empty($storeInfo['auth']) && $storeInfo['auth'] != '[]') {
  344. $result = json_decode($storeInfo['auth'], true);
  345. $mho = Option::get(OptionSetting::MCH_HIDE_OSS, 0, 'saas', '0')['value'];
  346. if ($mho && \in_array('config_baseConfig_uploadConfig', $result)) {
  347. // 删除数组中config_baseConfig_uploadConfig
  348. $result = array_diff($result, ['config_baseConfig_uploadConfig']);
  349. }
  350. } else {
  351. if ($storeInfo['business_model'] == 1) { //独立运行
  352. // $cloudStore = StoreCloud::find()->where(['store_id'=> $admin->store_id,"is_delete"=>0])->one();
  353. $params = require Yii::$app->basePath . '/config/saas_store_permission.php';
  354. // if (!$cloudStore) {
  355. // \Yii::$app->removeMenu($params, ['cloudMerchant']);
  356. // $params = \Yii::$app->array_values_recursive($params);
  357. // }
  358. $self_mini = \app\models\Option::get('self_mini', get_store_id(), 'store', 0)['value'];
  359. if (\Yii::$app->prod_is_dandianpu()) {
  360. if ($self_mini) {
  361. \Yii::$app->removeMenu($params, ['saasActivitySubmit', 'storeAccount', 'storeCash']);//独立运营店铺以及供应链使用独立小程序店铺不显示商盟菜单
  362. } else {
  363. \Yii::$app->removeMenu($params,['appletManagement', 'wxLive',
  364. 'cityDelivery', 'wechatConfig', 'miniConfig', 'h5Management', 'appletManagementAlipay', 'storeIndexQrcode']);
  365. //TODO 去除GoodsReviewed (小程序提审商品),供应链非独立商城首页商品未显示,故删除
  366. }
  367. }
  368. //商盟独立
  369. if (\Yii::$app->prod_is_shangmengduli()) {
  370. \Yii::$app->removeMenu($params, ['h5Management', 'wechatAccountManagement']);
  371. }
  372. //商盟
  373. if (\Yii::$app->prod_is_shangmeng()) {
  374. \Yii::$app->removeMenu($params, ['h5Management', 'wechatAccountManagement']);
  375. }
  376. $mho = Option::get(OptionSetting::MCH_HIDE_OSS, 0, 'saas', '0')['value'];
  377. if ($mho) {
  378. \Yii::$app->removeMenu($params, ['uploadConfig', 'runOverConfig']);
  379. // $params = \Yii::$app->array_values_recursive($params);
  380. }
  381. if (\Yii::$app->isSaas()) {
  382. //非独立部署时 去除小程序发布
  383. \Yii::$app->removeMenu($params, ['uploadWechat','uploadAlipay','storeUpgrade', 'saasActivitySubmit']);
  384. } else {
  385. //独立部署时 去除小程序管理和支付进件
  386. \Yii::$app->removeMenu($params, ['appletManagement','payIncoming', 'saasActivitySubmit', 'storeAccount', 'storeCash']);
  387. }
  388. if (\Yii::$app->prod_is_dandianpu()) {
  389. //单店铺时去除手机端管理员
  390. \Yii::$app->removeMenu($params, ['alipayPromotion', 'h5Management', 'saasAllianceCoupon', 'wechatAccountManagement']);
  391. } else {
  392. //非单店铺时去除商城提现
  393. \Yii::$app->removeMenu($params, [ 'cloudOrderList', 'storeAccount', 'storeCash']);
  394. }
  395. if (is_open_platform()) {
  396. //设置小程序配置
  397. $WechatConfig = \app\models\WechatConfig::findOne(['store_id' => get_store_id()]);
  398. $store_mini = \app\models\StoreMini::find()->where(['appid' => $WechatConfig->app_id, 'store_id' => get_store_id()])->select('id, appid, authorizer_refresh_token')->one();
  399. if (empty($store_mini->appid) || empty($store_mini->authorizer_refresh_token)) {
  400. // return [
  401. // 'code'=>1,
  402. // 'msg'=>"参数配置错误"
  403. // ];
  404. \Yii::$app->removeMenu($params, ['dataStatistic']);
  405. }
  406. }
  407. } elseif ($storeInfo['business_model'] == 2) { //平台运营
  408. $params = require Yii::$app->basePath . '/config/business_model/platform_permission.php';
  409. } elseif ($storeInfo['business_model'] == 3) { //当面付
  410. $params = require Yii::$app->basePath . '/config/business_model/scan_permission.php';
  411. \Yii::$app->removeMenu($params,['appletManagement']);
  412. } elseif ($storeInfo['business_model'] == 4) { //点餐
  413. $params = require Yii::$app->basePath . '/config/business_model/food_permission.php';
  414. \Yii::$app->removeMenu($params,['appletManagement']);
  415. }
  416. $params = \Yii::$app->filterMenu($params, $storeInfo['business_model']);
  417. AuthRoleForm::formatPermission($params);
  418. $result = [];
  419. AuthRoleForm::getAllPermission($params, $result);
  420. }
  421. }
  422. \Yii::$app->cache->set('is_platform', false);
  423. $adminPermission = AuthRoleForm::getAdminPermission($result);
  424. return self::handlePermission($adminPermission);
  425. }
  426. //处理权限
  427. public static function handlePermission($adminPermission) {
  428. $permissionId = array_column($adminPermission, 'permissionId');
  429. if (in_array('marketingManage', $permissionId)) { //存在营销 没有任何类
  430. if (!in_array('marketingCategory', $permissionId) &&
  431. !in_array('resourceCategory', $permissionId) &&
  432. !in_array('toolCategory', $permissionId) &&
  433. !in_array('industryCategory', $permissionId) &&
  434. !in_array('channelCategory', $permissionId) &&
  435. !in_array('thirdCategory', $permissionId) &&
  436. !in_array('branchStoreCategory', $permissionId)
  437. ) {
  438. $adminPermission = array_merge($adminPermission, [
  439. [
  440. 'permissionId' => 'marketingCategory'
  441. ],
  442. [
  443. 'permissionId' => 'resourceCategory'
  444. ],
  445. [
  446. 'permissionId' => 'toolCategory'
  447. ],
  448. [
  449. 'permissionId' => 'industryCategory'
  450. ],
  451. [
  452. 'permissionId' => 'channelCategory'
  453. ],
  454. [
  455. 'permissionId' => 'thirdCategory'
  456. ],
  457. [
  458. 'permissionId' => 'branchStoreCategory'
  459. ],
  460. ]);
  461. }
  462. }
  463. return $adminPermission;
  464. }
  465. }