DiyComponents.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace common\models\diy;
  3. use common\enums\AddonsEnum;
  4. use common\enums\DiyEnum;
  5. use common\enums\StatusEnum;
  6. use common\events\mall\IsShowDiyComponentsEvent;
  7. use Yii;
  8. /**
  9. * This is the model class for table "{{%diy_components}}".
  10. *
  11. * @property int $id 主键
  12. * @property string $code 标识
  13. * @property string $title 标题
  14. * @property string $group 组件分组
  15. * @property string $icon 图标
  16. * @property string $source 来源
  17. * @property int|null $sort 排序,越小排越前
  18. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  19. * @property int|null $created_at 添加时间
  20. * @property int|null $updated_at 修改时间
  21. */
  22. class DiyComponents extends \common\models\base\BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%diy_components}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['sort', 'status', 'created_at', 'updated_at'], 'integer'],
  38. [['code', 'group', 'source'], 'string', 'max' => 30],
  39. [['title'], 'string', 'max' => 50],
  40. [['icon'], 'string', 'max' => 300],
  41. ];
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'id' => '主键',
  50. 'code' => '标识',
  51. 'title' => '标题',
  52. 'group' => '组件分组',
  53. 'icon' => '图标',
  54. 'sort' => '排序,越小排越前',
  55. 'source' => '来源',
  56. 'status' => '状态[-1:删除;0:禁用;1启用]',
  57. 'created_at' => '添加时间',
  58. 'updated_at' => '修改时间',
  59. ];
  60. }
  61. /**
  62. * 根据分组获取组件
  63. * @Author: lun
  64. * @DateTime: 2021/9/27 0027 16:55
  65. * @Copyright: copyright (c) 2021 广东七件事集团
  66. * @param array $group 需要查询的分组
  67. * @param array $showComponents 显示需要查询的分组中选中的组件,格式 ['goods' => ['pic','price'], 'img_text' => ['ad','banner']]
  68. * @param array $validAddons 商城已安装的有效插件
  69. * @return array|\yii\db\ActiveRecord[]
  70. */
  71. public static function getComponent(array $groups, $showComponents = [], $validAddons = [], int $mallId = 0)
  72. {
  73. $where = [['in', 'group', $groups]];
  74. $select = "id,code type,title,group,icon img,count,is_top,permission,source";
  75. $components = self::getDiyComponents($where, [], true, $select);
  76. if (!$components) return [];
  77. $new_components = $temp_component = [];
  78. $group_arr = DiyEnum::groupName();// 获取分组名称
  79. if(!in_array('Store', $validAddons)) unset($group_arr['store']);
  80. $checkIsInstallAddons = [AddonsEnum::STAR_CAR_CARE, AddonsEnum::ADDONS_NAME_XUN_AI];
  81. foreach ($components as $key => $component) {
  82. // 商城未安装的插件不显示对应组件
  83. if ($component['group'] == 'marketing' || in_array($component['source'], $checkIsInstallAddons)) {
  84. if (empty($validAddons)) continue;// 没有安装插件直接跳过
  85. if (!in_array($component['source'], $validAddons)) continue;// 对应组件不在已安装的插件中跳过循环
  86. }
  87. if ($mallId) {
  88. $event = new IsShowDiyComponentsEvent($mallId);
  89. $data = ['component' => $component, 'mall_id' => $mallId];
  90. \Yii::$app->services->events->dispatch($event, $data, $event->listeners);
  91. if (!$event->getIsShow()) {
  92. continue;
  93. }
  94. }
  95. // 需要显示的组件进行重新组合
  96. if (!empty($showComponents[$component['group']])) {
  97. if (in_array($component['code'], $showComponents[$component['group']])) {
  98. $temp_component[] = $component;
  99. }
  100. } else {
  101. $temp_component[] = $component;
  102. }
  103. }
  104. // 重新组合分组
  105. $group_index = 0;
  106. foreach ($group_arr as $group => $title) {
  107. $new_components[$group_index]['title'] = $title;
  108. foreach ($temp_component as $item) {
  109. if ($item['group'] == $group) {
  110. $new_components[$group_index]['list'][] = $item;
  111. }
  112. }
  113. if (empty($new_components[$group_index]['list'])) unset($new_components[$group_index]);
  114. $group_index++;
  115. }
  116. return array_values($new_components);
  117. }
  118. /**
  119. * 重新组合数组,返回组件内容
  120. * @Author: lun
  121. * @DateTime: 2021/9/27 0027 17:22
  122. * @Copyright: copyright (c) 2021 广东七件事集团
  123. * @param $components
  124. * @param array $showComponents
  125. * @return array
  126. */
  127. private static function makeUpGroup(array $components, array $makeUpArr)
  128. {
  129. }
  130. /**
  131. *
  132. * @Author: lun
  133. * @DateTime: 2021/9/27 0027 16:55
  134. * @Copyright: copyright (c) 2021 广东七件事集团
  135. * @param array $cond
  136. * @param array $with
  137. * @param bool $is_array
  138. * @param string $select
  139. * @return array|\yii\db\ActiveRecord[]
  140. */
  141. public static function getDiyComponents(array $cond=[],array $with=[],bool $is_array=true,string $select='*'){
  142. $default_cond = [['<>','status',StatusEnum::DELETE]];
  143. $cond = array_merge($default_cond,$cond);
  144. $query = self::find()->select($select);
  145. self::paramPack(['where'=>$cond,'with'=>$with],$query);
  146. $data = $query->asArray($is_array)->all();
  147. return $data;
  148. }
  149. /**
  150. * 递归获取文件夹里面的文件名
  151. * @Author: lun
  152. * @DateTime: 2021/9/27 0027 18:23
  153. * @Copyright: copyright (c) 2021 广东七件事集团
  154. * @param $path
  155. * @param array $fileArr
  156. * @return array
  157. */
  158. public static function getDir($path, $initialPath, &$fileArr = [])
  159. {
  160. if(is_dir($path)){
  161. $dir = scandir($path);
  162. foreach ($dir as $value){
  163. $sub_path = $path . '/' . $value;
  164. if($value == '.' || $value == '..'){
  165. continue;
  166. }else if(is_dir($sub_path)){
  167. self::getDir($sub_path, $initialPath, $fileArr);
  168. }else{
  169. $file = str_replace('.php' ,'' ,$sub_path);
  170. $fileArr[] = str_replace($initialPath . '/', '', $file);
  171. }
  172. }
  173. }
  174. return $fileArr;
  175. }
  176. }