999990, 'name' => '系统图标', 'parent_id' => 0, ], [ 'id' => 99991, 'name' => '商业模式', 'parent_id' => 999990, ], [ 'id' => 99992, 'name' => '拓客营销', 'parent_id' => 999990, ], [ 'id' => 99993, 'name' => '我的订单', 'parent_id' => 999990, ], [ 'id' => 99994, 'name' => '行业运营', 'parent_id' => 999990, ], // 增加商品分类默认图标,禅道ID1152,类型:优化 // 注意事项:三级分类的id位数为二级分类的位数,三级分类的第三位开始二一级分类的最后一位,依次递增。 [ 'id' => 99995, 'name' => '商品分类', 'parent_id' => 999990, ], ['id' => 95001, 'name' => '电脑办公', 'parent_id' => 99995,], ['id' => 95002, 'name' => '个护清洁', 'parent_id' => 99995,], ['id' => 95003, 'name' => '家用电器', 'parent_id' => 99995,], ['id' => 95004, 'name' => '美妆护肤', 'parent_id' => 99995,], ['id' => 95005, 'name' => '生鲜', 'parent_id' => 99995,], ['id' => 95006, 'name' => '食品酒饮', 'parent_id' => 99995,], ['id' => 95007, 'name' => '医疗保健', 'parent_id' => 99995,], // ]; /** * 修改分组 * * @param array $post * @return boolean * @throws Exception */ public function saveGroup(array $post) { if ($this->isSystemGroup($post['parent_id'])) { throw new Exception('系统图标无法更改'); } return AttachmentGroup::saveGroup($post); } /** * 删除分组 * * @param array $ids * @param integer $mall_id * @param integer $mch_id * @return boolean * @throws Exception */ public function deleteGroup(array $ids, int $mall_id, int $mch_id = 0) { if ($this->isSystemGroup($ids)) { throw new Exception('系统图标无法删除'); } return AttachmentGroup::deleteGroup($ids, $mall_id, $mch_id); } /** * 获取分组 * * @param integer $mall_id * @param integer $type 1 or 0 * @param integer $is_recycle * @return array */ public function getGroupByTypeAndMallId(int $mall_id, int $type = 0, int $is_recycle = 0) { $query = AttachmentGroup::find()->where([ 'mall_id' => $mall_id, 'is_delete' => 0, // 'mch_id' => 0 ]); $query->andWhere(['type' => $type]); $is_recycle && $query->andWhere(['is_recycle' => $is_recycle]); $list = $query->asArray()->all(); return ArrayHelper::merge($list, $this->systemIconGroup); } /** * 删除文件 * * @param array $ids * @param integer $mall_id * @return boolean * @throws Exception */ public function delFile(array $ids, int $mall_id) { // 删除前查询删除内容是否包含系统图标 if ( Attachment::isSystemFile($ids, $this->getSystemIconGroupIds()) ) { throw new Exception('删除内容包含系统图标无法操作'); } // 更改 Attachment::updateAll(['is_delete' => Attachment::IS_DELETE_TEMP], [ 'id' => $ids, 'mall_id' => $mall_id, ]); // 删除文件 Yii::$app->services->rabbitMq->push( RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $ids, get_class(Yii::$app->services->attachment), 'delete' ); return true; } /** * 挪动文件到某个分组 * * @param array|int $ids * @param integer $mall_id * @param integer $group_id * @return boolean */ public function moveFileToGroupId($ids, int $mall_id, int $group_id) { // 移动前查询删除内容是否包含系统图标 if ( Attachment::isSystemFile($ids, $this->getSystemIconGroupIds()) ) { throw new Exception('移动内容包含系统图标无法操作'); } if ($this->isSystemGroup($group_id)) { throw new Exception('系统图标文件夹不允许操作'); } $attachmentGroup = AttachmentGroup::getGroupById($mall_id, $group_id); // 分组不存在 if (empty($attachmentGroup)) throw new Exception('分组不存在'); Attachment::updateAll(['group_id' => $group_id], [ 'id' => $ids, 'mall_id' => $mall_id, ]); return true; } /** * @param $data * @return int * @throws NotFoundHttpException */ public function create($data) { $model = new Attachment(); $model->attributes = $data; if (!$model->save()) { $firstErrors = $model->getFirstErrors() ?: ['未捕获到错误信息']; throw new NotFoundHttpException(array_values($firstErrors)[0]); } return $model->id; } /** * 获取系统图标组的IDS * * @return array */ public function getSystemIconGroupIds() { return ArrayHelper::getColumn($this->systemIconGroup, 'id'); } /** * 是否是系统图标组 * * @param integer|array $group_id * @return boolean */ public function isSystemGroup($group_id): bool { if (is_array($group_id)) { foreach ($group_id as $item) { if (in_array($item, $this->getSystemIconGroupIds())) { return true; } } return false; } else { return in_array($group_id, $this->getSystemIconGroupIds()); } } /** * @param int $mall_id * @param string $uploadType * @param int $group_id * @param string $drive * @param string $keyword * @return array */ public function getListPage($mall_id, $uploadType = '', $group_id = 0,$drive = '', $keyword = '', $store_id = 0, $mch_id = 0, $addons_name = '') { // 这些插件上传的图片不显示出来 $excludeAddons = [ AddonsEnum::ADDONS_NAME_CLOUD_PRESCRIPTION ]; $where = [ ['=', 'is_delete', 0], ['=', 'mall_id', $mall_id], ['NOT IN', 'addons_name', $excludeAddons], ]; // 查询系统图标时 mall_id 修改为0 if ($this->isSystemGroup($group_id)) { $where = [ ['=', 'is_delete', 0], ['=', 'mall_id', 0], ]; } if ($uploadType) { $where[] = ['=', 'upload_type', $uploadType]; } if ($group_id) { $where[] = ['=', 'group_id', $group_id]; } if ($drive) { $where[] = ['=', 'drive', $drive]; } if ($keyword) { $where[] = ['like', 'old_name', $keyword]; } // 如果是门店则只能看到自己门店和平台的 if ($store_id) { $where[] = ['in', 'store_id', [0, $store_id]]; } if (!empty($addons_name)) { $where[] = ['=', 'addons_name', $addons_name]; } // 如果是多商户则只能看到自己多商户和平台的 if ($mch_id) { $where[] = ['in', 'mch_id', [0, $mch_id]]; } $select = ['id', 'url', 'thumb_url', 'upload_type', 'drive', 'name', 'old_name', 'size', 'created_at']; $result = Attachment::getPageList(20, function ($query) use ($where, $select) { array_unshift($where, 'and'); $query->andWhere($where)->select($select); }); $static_url = Yii::$app->services->setting->get('system.static_url') ?? ''; if ($result && $static_url) { foreach ($result['list'] as &$item) { if ($item['drive'] == 'local' && strpos($item['url'], 'http') === false) { $item['url'] = $static_url . $item['url']; } } } return $result; } /** * 删除文件 * @param array $ids * @throws * @return bool */ public function delete($ids) { try{ foreach($ids as $id){ $attachment = Attachment::findOne(['id' => $id]); if ($this->isSystemGroup($attachment->group_id)) { continue; } if(!$attachment){ continue; } if($attachment->drive == Attachment::DRIVE_LOCAL){ $config = ['drive' => Attachment::DRIVE_LOCAL]; }else{ //获取对应配置 $config = $this->getStorageConfig($attachment->mall_id,$attachment->drive); } $upload = new UploadHelper($config, $attachment->upload_type); $res = $upload->delete($attachment->path); if($res){ $attachment->is_delete = Attachment::IS_DELETE_COMPLETE; $attachment->save(); } } return true; }catch(\Exception $e){ Yii::error('delete file error:'.$e->getMessage()); return false; } } /** * 获取商城的设置 * @param $mall_id * @param string $driver * @return array|mixed */ public function getStorageConfigMall($mall_id,$driver = '') { $setting = []; $mall_driver = $driver ?: Yii::$app->services->setting->mall->get($mall_id, 'storage.driver'); if($mall_driver !== Attachment::DRIVE_LOCAL) { $mall_setting = !empty($mall_driver) ? Yii::$app->services->setting->mall->get($mall_id, 'storage_driver.' . $mall_driver) : []; if($mall_setting){ $has_storage = true;// 判断配置是否为空 $setting = json_decode($mall_setting, true); // 因为后台添加ram 配置, 但是之前客户是没有填写ram或者不需要填写的。所以这里过滤下 foreach ($setting as $key => $value) { if (empty($value) && $key != 'arn') { $has_storage = false; break; } } if ($has_storage === false) { $setting = [];// 不填配置则直接放回空配置 } else { $setting['drive'] = $mall_driver; } } } return $setting; } /** * 获取文件上传配置 * @param $mall_id * @param string $driver * @return array */ public function getStorageConfig($mall_id,$driver = '') { if($mall_id > 0 ){ $setting = $this->getStorageConfigMall($mall_id,$driver); if($setting) return $setting; } //获取商城配置 $setting = ['drive' => Attachment::DRIVE_LOCAL]; //默认配置:本地存储 $mall_driver = $driver ?: Yii::$app->services->setting->get('storage.driver'); if($mall_driver !== Attachment::DRIVE_LOCAL) { $setting = !empty($mall_driver) ? Yii::$app->services->setting->get('storage_driver.' . $mall_driver) : []; if ($setting) { $setting = json_decode($setting, true); $setting['drive'] = $mall_driver; } else { $setting = $this->getPlatformStorageConfig($driver); } } return $setting; } /** * 获取平台的文件上传配置 * @param $driver * @return array|mixed|void */ public function getPlatformStorageConfig($driver = '') { $driver = $driver ?: PlatformSetting::conf('storage.driver'); $setting = !empty($driver) ? PlatformSetting::conf('storage_driver.'.$driver) : []; $setting = !empty($setting) ? json_decode($setting,true) : []; $setting['drive'] = $driver; return $setting; } public function setLongVideoData($mall_id, $post) { try { //获取商城存储配置 $config = \Yii::$app->services->attachment->getStorageConfig($mall_id); // $post['url'] = 'https://qimallos-1300393541.cos.ap-guangzhou.myqcloud.com/videos/5/2025/08/21/video_1755763652_otl4M422.mp4'; $path_info = pathinfo($post['url']); // $stats = stat($url); $headers_data = get_headers($post['url'], true); $size = $headers_data['Content-Length']; $data = [ 'mall_id' => $mall_id, 'drive' => $config['drive'], 'upload_type' => $post['upload_type'] ?? 'long_video', 'url' => $post['url'], 'path' => $post['url'], 'name' => $path_info['filename'], 'old_name' => $post['old_name'] ?? '', 'size' => $size, 'extension' => $path_info['extension'], 'mch_id' => $post['mch_id'] ?? 0, 'store_id' => $post['store_id'] ?? 0, 'group_id' => $post['attachment_group_id'] ?? 0, ]; $res = $this->create($data); return true; // print_r();exit; } catch (\Exception $e) { throw new Exception($e->getMessage()); } } }