| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- <?php
- namespace services\common;
- use common\enums\AddonsEnum;
- use Yii;
- use yii\data\Pagination;
- use yii\web\NotFoundHttpException;
- use common\models\common\Attachment;
- use common\components\Service;
- use common\enums\RabbitMqEnum;
- use common\helpers\ArrayHelper;
- use common\helpers\UploadHelper;
- use common\models\common\AttachmentGroup;
- use common\models\common\PlatformSetting;
- use common\models\common\MallSetting;
- use Exception;
- /**
- *
- * Class AttachmentService
- * @package services\common
- */
- class AttachmentService extends Service
- {
- /**
- * 系统图标 全部商城都要显示
- *
- * @var array
- */
- public $systemIconGroup = [
- [
- 'id' => 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,
- ],
- // <hst-start>增加商品分类默认图标,禅道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,],
- // <hst-end>
- ];
- /**
- * 修改分组
- *
- * @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());
- }
- }
- }
|