| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\constants\OptionSetting;
- use app\models\AdminRole;
- use app\models\Md;
- use app\models\Option;
- use app\models\Store;
- use Yii;
- use yii\base\Model;
- use app\models\AuthRole;
- class AuthRoleForm extends Model
- {
- public $id;
- public $name;
- public $data;
- public $edit_data;
- public $describe;
- public $status;
- public function rules()
- {
- return [
- [['name'], 'trim'],
- [['name'], 'string', 'max' => 64],
- [['describe'], 'string', 'max' => 255],
- [['data', 'edit_data'], 'string'],
- [['status', 'id'], 'integer']
- ];
- }
- public function attributeLabels()
- {
- return [
- 'id' => '角色ID',
- 'name' => '角色名称',
- 'data' => '角色数据',
- 'edit_data' => '角色数据',
- 'describe' => '简介',
- 'status' => '状态',
- ];
- }
- /**
- * 创建角色
- * @return array
- */
- public function create()
- {
- if ($this->validate()) {
- if (AuthRole::findOne(['name' => $this->name, 'store_id' => get_store_id()])) {
- return [
- 'code' => 1,
- 'msg' => '角色名称已经存在.',
- ];
- }
- $data = json_decode($this->data, true);
- array_unshift($data, 'dashboard');
- $role = new AuthRole();
- $role->name = $this->name;
- $role->data = json_encode($data);
- $role->edit_data = $this->edit_data;
- $role->status = $this->status;
- $role->store_id = get_store_id();
- if ($this->describe) {
- $role->describe = $this->describe;
- }
- if ($role->save()) {
- return [
- 'code' => 0,
- 'msg' => '创建成功'
- ];
- }
- return [
- 'code' => 1,
- 'msg' => '创建失败',
- ];
- }
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- /**
- * 编辑角色
- * @return array
- */
- public function edit()
- {
- if ($this->validate()) {
- $role = AuthRole::findOne($this->id);
- if (! $role) {
- return [
- 'code' => 1,
- 'msg' => '角色不存在',
- ];
- }
- $data = json_decode($this->data, true);
- array_unshift($data, 'dashboard');
- $role->name = $this->name;
- $role->data = json_encode($data);
- $role->edit_data = $this->edit_data;
- $role->describe = $this->describe;
- $role->status = $this->status;
- if ($role->save()) {
- return [
- 'code' => 0,
- 'msg' => '保存成功',
- ];
- }
- return [
- 'code' => 1,
- 'msg' => '保存失败',
- ];
- }
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- /**
- * @return array
- * @throws \Throwable
- * @throws \yii\base\InvalidConfigException
- * @throws \yii\db\StaleObjectException
- */
- public function deteleRole()
- {
- if (! $this->id) {
- return [
- 'code' => 1,
- 'msg' => '请提供角色ID',
- ];
- }
- $role = AuthRole::findOne($this->id);
- if (! $role) {
- return [
- 'code' => 1,
- 'msg' => '角色不存在',
- ];
- }
- $adminCount = $role->getAdmins()->count();
- if ($adminCount > 0) {
- return [
- 'code' => 1,
- 'msg' => '该角色还有员工使用,不能删除',
- ];
- }
- if ($role->delete()) {
- return [
- 'code' => 0,
- 'msg' => '删除成功',
- ];
- }
- return [
- 'code' => 1,
- 'msg' => '删除失败',
- ];
- }
- /**
- * 获取角色列表
- * @return array
- */
- public function getRoles()
- {
- $query = AuthRole::find()->where(['store_id' => get_store_id()]);
- $pagination = pagination_make($query);
- $roles = $pagination['list'];
- foreach ($roles as $key => $role) {
- $roles[$key]['data'] = json_decode($role['data']);
- AuthRoleForm::formatPermission($roles[$key]['data']);
- $roles[$key]['edit_data'] = [];
- if (!empty($role['edit_data'])) {
- $roles[$key]['edit_data'] = json_decode($role['edit_data'], true);
- AuthRoleForm::formatPermission($roles[$key]['edit_data']);
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $roles,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ],
- ];
- }
- /**
- * 格式化权限
- * @param array $permission
- * @param $key
- */
- public static function formatPermission(array &$permission, $key = null)
- {
- foreach ($permission as &$value) {
- if ($key) {
- if (isset($value['type']) && $value['type'] == 'action') {
- $action = explode('_', $key);
- $action = array_pop($action);
- $value['key'] = $key . '_' . $action . '@' . $value['key'];
- } else {
- $value['key'] = $key . '_' . $value['key'];
- }
- }
- if (isset($value['children']) && count($value['children']) > 0) {
- static::formatPermission($value['children'], $value['key']);
- }
- }
- }
- /**
- * 获取账号拥有的权限
- * @param null $params
- * @return array
- */
- public static function getAdminPermission($params = null)
- {
- $adminPermission = $params;
- if (! $params) {
- $admin = Yii::$app->jwt->getAdmin();
- $adminPermission = AdminRole::find()->alias('ar')->where(['ar.admin_id' => $admin->id])
- ->leftJoin(['ad' => AuthRole::tableName()], 'ad.id=ar.role_id')->select('ad.data')->column();
- if (count($adminPermission) > 0) {
- foreach ($adminPermission as &$v) {
- $v = json_decode($v);
- }
- $adminPermission = array_unique(array_merge(...$adminPermission));
- }
- }
- $result = [];
- foreach ($adminPermission as $value) {
- $permission = explode('_', is_array($value) ? '' : $value);
- foreach ($permission as $v) {
- if (! isset($result[$v])) {
- if (strpos($v, '@') !== false) {
- $ex = explode('@', $v);
- $result[$ex[0]]['actionEntitySet'][] = [
- 'action' => $ex[1],
- ];
- } else {
- $result[$v] = [
- 'permissionId' => $v,
- ];
- }
- }
- }
- }
- //获取门店是否开启不开启独立运营
- $id = get_md_id();
- $md_detail = Md::findOne($id);
- if (!empty($id) && $md_detail->is_single === 0) {
- unset($result['outletGoods']);
- unset($result['wastoreManageGoods']);
- }
- return array_values($result);
- }
- /**
- * 获取所有权限key
- * @param $params
- * @param $result
- */
- public static function getAllPermission($params, &$result)
- {
- foreach ($params as $value) {
- $result[] = $value['key'];
- if (isset($value['children'])) {
- static::getAllPermission($value['children'], $result);
- }
- }
- }
- public static function delEmptyAuth($params) {
- $arr = [
- ];
- foreach ($params as $value) {
- if (!isset($value['children'])) {
- $arr[] = $value;
- } else {
- if (!empty($value['children'])) {
- static::delEmptyAuth($value['children']);
- $arr[] = $value;
- }
- }
- }
- return $arr;
- }
- // 过滤菜单
- public static function filterAuth($params, $whitelist = [])
- {
- $result = []; // 用于存储过滤后的结果
- foreach ($params as $item) {
- // 如果当前项的 key 在白名单中
- if (in_array($item['key'], $whitelist)) {
- // 复制当前项到结果中
- $filteredItem = $item;
- // 如果当前项有 children,递归过滤 children
- if (isset($item['children'])) {
- $filteredItem['children'] = static::filterAuth($item['children'], $whitelist);
- }
- $result[] = $filteredItem;
- } else {
- // 如果当前项不在白名单中,但存在 children,递归过滤 children
- if (isset($item['children'])) {
- $filteredChildren = static::filterAuth($item['children'], $whitelist);
- // 如果过滤后的 children 不为空,将当前项加入结果
- if (!empty($filteredChildren)) {
- $item['children'] = $filteredChildren;
- $result[] = $item;
- }
- }
- }
- }
- return $result;
- }
- //获取公用的方法
- public static function getCommonPermission() {
- $admin = get_admin();
- $md_id = get_md_id();
- $mch_id = get_mch_id();
- if ($md_id && $md_id > 0) {
- $params = require Yii::$app->basePath . '/config/saas_md_permission.php';
- $md = \app\models\Md::findOne($md_id);
- if ($md && $md->is_single == 0) { // 非独立运营门店去除同城配送
- // \Yii::$app->removeMenu($params, ['WechatNewDelivery']);//非独立运营门店也需要自己独立的发货地址
- }
- $params = \Yii::$app->filterMenu($params);
- AuthRoleForm::formatPermission($params);
- $result = [];
- AuthRoleForm::getAllPermission($params, $result);
- }elseif ($mch_id && $mch_id > 0) {
- $params = require Yii::$app->basePath . '/config/saas_mch_permission.php';
- $params = \Yii::$app->filterMenu($params);
- AuthRoleForm::formatPermission($params);
- $result = [];
- AuthRoleForm::getAllPermission($params, $result);
- } else {
- $storeInfo = Store::find()->where(['id'=> $admin->store_id,"is_delete"=>0])->asArray()->one();
- if(!$storeInfo || !$storeInfo['business_model']){
- return [
- 'code' => 1,
- 'msg' => '店铺信息有误,请联系管理员',
- ];
- }
- if (!empty($storeInfo['auth']) && $storeInfo['auth'] != '[]') {
- $result = json_decode($storeInfo['auth'], true);
- $mho = Option::get(OptionSetting::MCH_HIDE_OSS, 0, 'saas', '0')['value'];
- if ($mho && \in_array('config_baseConfig_uploadConfig', $result)) {
- // 删除数组中config_baseConfig_uploadConfig
- $result = array_diff($result, ['config_baseConfig_uploadConfig']);
- }
- } else {
- if ($storeInfo['business_model'] == 1) { //独立运行
- // $cloudStore = StoreCloud::find()->where(['store_id'=> $admin->store_id,"is_delete"=>0])->one();
- $params = require Yii::$app->basePath . '/config/saas_store_permission.php';
- // if (!$cloudStore) {
- // \Yii::$app->removeMenu($params, ['cloudMerchant']);
- // $params = \Yii::$app->array_values_recursive($params);
- // }
- $self_mini = \app\models\Option::get('self_mini', get_store_id(), 'store', 0)['value'];
- if (\Yii::$app->prod_is_dandianpu()) {
- if ($self_mini) {
- \Yii::$app->removeMenu($params, ['saasActivitySubmit', 'storeAccount', 'storeCash']);//独立运营店铺以及供应链使用独立小程序店铺不显示商盟菜单
- } else {
- \Yii::$app->removeMenu($params,['appletManagement', 'wxLive',
- 'cityDelivery', 'wechatConfig', 'miniConfig', 'h5Management', 'appletManagementAlipay', 'storeIndexQrcode']);
- //TODO 去除GoodsReviewed (小程序提审商品),供应链非独立商城首页商品未显示,故删除
- }
- }
- //商盟独立
- if (\Yii::$app->prod_is_shangmengduli()) {
- \Yii::$app->removeMenu($params, ['h5Management', 'wechatAccountManagement']);
- }
- //商盟
- if (\Yii::$app->prod_is_shangmeng()) {
- \Yii::$app->removeMenu($params, ['h5Management', 'wechatAccountManagement']);
- }
- $mho = Option::get(OptionSetting::MCH_HIDE_OSS, 0, 'saas', '0')['value'];
- if ($mho) {
- \Yii::$app->removeMenu($params, ['uploadConfig', 'runOverConfig']);
- // $params = \Yii::$app->array_values_recursive($params);
- }
- if (\Yii::$app->isSaas()) {
- //非独立部署时 去除小程序发布
- \Yii::$app->removeMenu($params, ['uploadWechat','uploadAlipay','storeUpgrade', 'saasActivitySubmit']);
- } else {
- //独立部署时 去除小程序管理和支付进件
- \Yii::$app->removeMenu($params, ['appletManagement','payIncoming', 'saasActivitySubmit', 'storeAccount', 'storeCash']);
- }
- if (\Yii::$app->prod_is_dandianpu()) {
- //单店铺时去除手机端管理员
- \Yii::$app->removeMenu($params, ['alipayPromotion', 'h5Management', 'saasAllianceCoupon', 'wechatAccountManagement']);
- } else {
- //非单店铺时去除商城提现
- \Yii::$app->removeMenu($params, [ 'cloudOrderList', 'storeAccount', 'storeCash']);
- }
- if (is_open_platform()) {
- //设置小程序配置
- $WechatConfig = \app\models\WechatConfig::findOne(['store_id' => get_store_id()]);
- $store_mini = \app\models\StoreMini::find()->where(['appid' => $WechatConfig->app_id, 'store_id' => get_store_id()])->select('id, appid, authorizer_refresh_token')->one();
- if (empty($store_mini->appid) || empty($store_mini->authorizer_refresh_token)) {
- // return [
- // 'code'=>1,
- // 'msg'=>"参数配置错误"
- // ];
- \Yii::$app->removeMenu($params, ['dataStatistic']);
- }
- }
- } elseif ($storeInfo['business_model'] == 2) { //平台运营
- $params = require Yii::$app->basePath . '/config/business_model/platform_permission.php';
- } elseif ($storeInfo['business_model'] == 3) { //当面付
- $params = require Yii::$app->basePath . '/config/business_model/scan_permission.php';
- \Yii::$app->removeMenu($params,['appletManagement']);
- } elseif ($storeInfo['business_model'] == 4) { //点餐
- $params = require Yii::$app->basePath . '/config/business_model/food_permission.php';
- \Yii::$app->removeMenu($params,['appletManagement']);
- }
- $params = \Yii::$app->filterMenu($params, $storeInfo['business_model']);
- AuthRoleForm::formatPermission($params);
- $result = [];
- AuthRoleForm::getAllPermission($params, $result);
- }
- }
- \Yii::$app->cache->set('is_platform', false);
- $adminPermission = AuthRoleForm::getAdminPermission($result);
- return self::handlePermission($adminPermission);
- }
- //处理权限
- public static function handlePermission($adminPermission) {
- $permissionId = array_column($adminPermission, 'permissionId');
- if (in_array('marketingManage', $permissionId)) { //存在营销 没有任何类
- if (!in_array('marketingCategory', $permissionId) &&
- !in_array('resourceCategory', $permissionId) &&
- !in_array('toolCategory', $permissionId) &&
- !in_array('industryCategory', $permissionId) &&
- !in_array('channelCategory', $permissionId) &&
- !in_array('thirdCategory', $permissionId) &&
- !in_array('branchStoreCategory', $permissionId)
- ) {
- $adminPermission = array_merge($adminPermission, [
- [
- 'permissionId' => 'marketingCategory'
- ],
- [
- 'permissionId' => 'resourceCategory'
- ],
- [
- 'permissionId' => 'toolCategory'
- ],
- [
- 'permissionId' => 'industryCategory'
- ],
- [
- 'permissionId' => 'channelCategory'
- ],
- [
- 'permissionId' => 'thirdCategory'
- ],
- [
- 'permissionId' => 'branchStoreCategory'
- ],
- ]);
- }
- }
- return $adminPermission;
- }
- }
|