| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
- namespace common\models\diy;
- use common\enums\AddonsEnum;
- use common\enums\DiyEnum;
- use common\enums\StatusEnum;
- use common\events\mall\IsShowDiyComponentsEvent;
- use Yii;
- /**
- * This is the model class for table "{{%diy_components}}".
- *
- * @property int $id 主键
- * @property string $code 标识
- * @property string $title 标题
- * @property string $group 组件分组
- * @property string $icon 图标
- * @property string $source 来源
- * @property int|null $sort 排序,越小排越前
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int|null $created_at 添加时间
- * @property int|null $updated_at 修改时间
- */
- class DiyComponents extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%diy_components}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['sort', 'status', 'created_at', 'updated_at'], 'integer'],
- [['code', 'group', 'source'], 'string', 'max' => 30],
- [['title'], 'string', 'max' => 50],
- [['icon'], 'string', 'max' => 300],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '主键',
- 'code' => '标识',
- 'title' => '标题',
- 'group' => '组件分组',
- 'icon' => '图标',
- 'sort' => '排序,越小排越前',
- 'source' => '来源',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '添加时间',
- 'updated_at' => '修改时间',
- ];
- }
- /**
- * 根据分组获取组件
- * @Author: lun
- * @DateTime: 2021/9/27 0027 16:55
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $group 需要查询的分组
- * @param array $showComponents 显示需要查询的分组中选中的组件,格式 ['goods' => ['pic','price'], 'img_text' => ['ad','banner']]
- * @param array $validAddons 商城已安装的有效插件
- * @return array|\yii\db\ActiveRecord[]
- */
- public static function getComponent(array $groups, $showComponents = [], $validAddons = [], int $mallId = 0)
- {
- $where = [['in', 'group', $groups]];
- $select = "id,code type,title,group,icon img,count,is_top,permission,source";
- $components = self::getDiyComponents($where, [], true, $select);
- if (!$components) return [];
- $new_components = $temp_component = [];
- $group_arr = DiyEnum::groupName();// 获取分组名称
- if(!in_array('Store', $validAddons)) unset($group_arr['store']);
- $checkIsInstallAddons = [AddonsEnum::STAR_CAR_CARE, AddonsEnum::ADDONS_NAME_XUN_AI];
- foreach ($components as $key => $component) {
- // 商城未安装的插件不显示对应组件
- if ($component['group'] == 'marketing' || in_array($component['source'], $checkIsInstallAddons)) {
- if (empty($validAddons)) continue;// 没有安装插件直接跳过
- if (!in_array($component['source'], $validAddons)) continue;// 对应组件不在已安装的插件中跳过循环
- }
- if ($mallId) {
- $event = new IsShowDiyComponentsEvent($mallId);
- $data = ['component' => $component, 'mall_id' => $mallId];
- \Yii::$app->services->events->dispatch($event, $data, $event->listeners);
- if (!$event->getIsShow()) {
- continue;
- }
- }
- // 需要显示的组件进行重新组合
- if (!empty($showComponents[$component['group']])) {
- if (in_array($component['code'], $showComponents[$component['group']])) {
- $temp_component[] = $component;
- }
- } else {
- $temp_component[] = $component;
- }
- }
- // 重新组合分组
- $group_index = 0;
- foreach ($group_arr as $group => $title) {
- $new_components[$group_index]['title'] = $title;
- foreach ($temp_component as $item) {
- if ($item['group'] == $group) {
- $new_components[$group_index]['list'][] = $item;
- }
- }
- if (empty($new_components[$group_index]['list'])) unset($new_components[$group_index]);
- $group_index++;
- }
- return array_values($new_components);
- }
- /**
- * 重新组合数组,返回组件内容
- * @Author: lun
- * @DateTime: 2021/9/27 0027 17:22
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $components
- * @param array $showComponents
- * @return array
- */
- private static function makeUpGroup(array $components, array $makeUpArr)
- {
- }
- /**
- *
- * @Author: lun
- * @DateTime: 2021/9/27 0027 16:55
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $cond
- * @param array $with
- * @param bool $is_array
- * @param string $select
- * @return array|\yii\db\ActiveRecord[]
- */
- public static function getDiyComponents(array $cond=[],array $with=[],bool $is_array=true,string $select='*'){
- $default_cond = [['<>','status',StatusEnum::DELETE]];
- $cond = array_merge($default_cond,$cond);
- $query = self::find()->select($select);
- self::paramPack(['where'=>$cond,'with'=>$with],$query);
- $data = $query->asArray($is_array)->all();
- return $data;
- }
- /**
- * 递归获取文件夹里面的文件名
- * @Author: lun
- * @DateTime: 2021/9/27 0027 18:23
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $path
- * @param array $fileArr
- * @return array
- */
- public static function getDir($path, $initialPath, &$fileArr = [])
- {
- if(is_dir($path)){
- $dir = scandir($path);
- foreach ($dir as $value){
- $sub_path = $path . '/' . $value;
- if($value == '.' || $value == '..'){
- continue;
- }else if(is_dir($sub_path)){
- self::getDir($sub_path, $initialPath, $fileArr);
- }else{
- $file = str_replace('.php' ,'' ,$sub_path);
- $fileArr[] = str_replace($initialPath . '/', '', $file);
- }
- }
- }
- return $fileArr;
- }
- }
|