123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\services\system\config;
- use app\model\system\config\SystemStorage;
- use qiniu\basic\BaseServices;
- use qiniu\services\CacheService;
- use qiniu\services\SystemConfigService;
- use qiniu\services\UploadService;
- use think\db\exception\DbException;
- use think\exception\ValidateException;
- /**
- * Class SystemStorageServices
- * @package app\services\system\config
- * @mixin SystemStorage
- */
- class SystemStorageServices extends BaseServices
- {
- /**
- * SystemStorageServices constructor.
- * @param SystemStorage $model
- */
- public function __construct(SystemStorage $model)
- {
- $this->model = $model;
- }
- /**
- * @param array $where
- * @return array
- * @throws DbException
- */
- public function getStorageList(array $where)
- {
- [$page, $limit] = $this->getPageValue();
- $config = $this->getStorageConfig((int)$where['type']);
- $where['access_key'] = $config['accessKey'];
- $list = $this->getList($where, '*', $page, $limit);
- foreach ($list as &$item) {
- $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
- $item['_update_time'] = date('Y-m-d H:i:s', $item['update_time']);
- $service = UploadService::init($item['type']);
- $region = $service->getRegion();
- foreach ($region as $value) {
- if (strstr($item['region'], $value['value'])) {
- $item['_region'] = $value['label'];
- }
- }
- if (!$item['cname']) {
- $item['cname'] = str_replace(['http://', 'https://'], '', $item['domain']);
- }
- }
- $count = $this->getCount($where);
- return compact('list', 'count');
- }
- /**
- * @param int $type
- * @return array
- */
- public function getStorageConfig(int $type)
- {
- $config = [
- 'accessKey' => '',
- 'secretKey' => ''
- ];
- switch ($type) {
- case 2://七牛
- $config = [
- 'accessKey' => sys_config('qiniu_accessKey', ''),
- 'secretKey' => sys_config('qiniu_secretKey', ''),
- ];
- break;
- case 3:// oss 阿里云
- $config = [
- 'accessKey' => sys_config('accessKey', ''),
- 'secretKey' => sys_config('secretKey', ''),
- ];
- break;
- case 4:// cos 腾讯云
- $config = [
- 'accessKey' => sys_config('tengxun_accessKey', ''),
- 'secretKey' => sys_config('tengxun_secretKey', ''),
- 'appid' => sys_config('tengxun_appid', ''),
- ];
- break;
- }
- return $config;
- }
- /**
- * 删除空间
- * @param int $id
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function deleteStorage(int $id)
- {
- $storageInfo = $this->get(['is_delete' => 0, 'id' => $id]);
- if (!$storageInfo) {
- throw new ValidateException('删除的云存储不存在');
- }
- if ($storageInfo->status) {
- throw new ValidateException('云存储正在使用中,需要启动其他空间才能删除');
- }
- try {
- $upload = UploadService::init($storageInfo->type);
- $res = $upload->deleteBucket($storageInfo->name, $storageInfo->region);
- if (false === $res) {
- throw new ValidateException($upload->getError());
- }
- } catch (\Throwable $e) {
- throw new ValidateException($e->getMessage());
- }
- $storageInfo->is_delete = 1;
- $storageInfo->save();
- return true;
- }
- public function saveConfig(int $type, array $data)
- {
- //保存配置信息
- if (1 !== $type) {
- $accessKey = $secretKey = $appid = $storageRegion = '';
- if (isset($data['accessKey']) && isset($data['secretKey']) && $data['accessKey'] && $data['secretKey']) {
- $accessKey = $data['accessKey'];
- $secretKey = $data['secretKey'];
- unset($data['accessKey'], $data['secretKey']);
- }
- if (isset($data['appid']) && $data['appid']) {
- $appid = $data['appid'];
- unset($data['appid']);
- }
- if (isset($data['storageRegion']) && $data['storageRegion']) {
- $storageRegion = $data['storageRegion'];
- unset($data['storageRegion']);
- }
- if (!$accessKey || !$secretKey) {
- return true;
- }
- /** @var SystemConfigServices $make */
- $make = app()->make(SystemConfigServices::class);
- switch ($type) {
- case 2://七牛
- $make->update('qiniu_accessKey', ['value' => json_encode($accessKey)], 'menu_name');
- $make->update('qiniu_secretKey', ['value' => json_encode($secretKey)], 'menu_name');
- break;
- case 3:// oss 阿里云
- $make->update('accessKey', ['value' => json_encode($accessKey)], 'menu_name');
- $make->update('secretKey', ['value' => json_encode($secretKey)], 'menu_name');
- break;
- case 4:// cos 腾讯云
- $make->update('tengxun_accessKey', ['value' => json_encode($accessKey)], 'menu_name');
- $make->update('tengxun_secretKey', ['value' => json_encode($secretKey)], 'menu_name');
- $make->update('tengxun_appid', ['value' => json_encode($appid)], 'menu_name');
- break;
- }
- CacheService::redisHandler(SystemConfigService::getTag())->clear();
- }
- }
- /**
- * 保存云存储
- * @param int $type
- * @param array $data
- * @return mixed
- */
- public function saveStorage(int $type, array $data)
- {
- //保存配置信息
- $this->saveConfig($type, $data);
- $name = $data['name'];
- switch ($type) {
- case 3://阿里云oss
- $data['region'] = $this->getReagionHost($type, $data['region']);
- break;
- case 4://腾讯云cos
- $name = $data['name'] . '-' . sys_config('tengxun_appid');
- break;
- }
- if ($this->getCount(['type' => $type, 'name' => $name])) {
- throw new ValidateException('云空间名称不能重复');
- }
- //保存云存储
- $data['type'] = $type;
- $upload = UploadService::init($type);
- $res = $upload->createBucket($data['name'], $data['region'], $data['acl']);
- if (false === $res) {
- throw new ValidateException($upload->getError());
- }
- $data['domain'] = $this->getDomain($type, $data['name'], $data['region'], sys_config('tengxun_appid'));
- if (2 === $type) {
- $domianList = $upload->getDomian($data['name']);
- $data['domain'] = $domianList[count($domianList) - 1];
- } else {
- $data['cname'] = $data['domain'];
- }
- $data['name'] = $name;
- $data['add_time'] = time();
- $data['update_time'] = time();
- $config = $this->getStorageConfig($type);
- $data['access_key'] = $config['accessKey'];
- return $this->save($data);
- }
- /**
- * 同步云储存桶
- * @param int $type
- * @return bool
- * @throws \OSS\Core\OssException
- */
- public function synchronization(int $type)
- {
- $data = [];
- switch ($type) {
- case 2://七牛
- $config = $this->getStorageConfig($type);
- $upload = UploadService::init($type);
- $list = $upload->listbuckets();
- if (false === $list) {
- throw new ValidateException('同步失败,失败原因:' . $upload->getError());
- }
- foreach ($list as $item) {
- if (!$this->getCount(['name' => $item['id'], 'is_delete' => 0, 'access_key' => $config['accessKey']])) {
- $data[] = [
- 'type' => $type,
- 'access_key' => $config['accessKey'],
- 'name' => $item['id'],
- 'region' => $item['region'],
- 'acl' => $item['private'] == 0 ? 'public-read' : 'private',
- 'status' => 0,
- 'is_delete' => 0,
- 'add_time' => time(),
- 'update_time' => time()
- ];
- }
- }
- break;
- case 3:// oss 阿里云
- $upload = UploadService::init($type);
- $list = $upload->listbuckets();
- $config = $this->getStorageConfig($type);
- foreach ($list as $item) {
- if (!$this->getCount(['name' => $item['name'], 'is_delete' => 0, 'access_key' => $config['accessKey']])) {
- $region = $this->getReagionHost($type, $item['location']);
- $data[] = [
- 'type' => $type,
- 'access_key' => $config['accessKey'],
- 'name' => $item['name'],
- 'region' => $region,
- 'acl' => 'public-read',
- 'domain' => $this->getDomain($type, $item['name'], $region),
- 'status' => 0,
- 'is_delete' => 0,
- 'add_time' => strtotime($item['createTime']),
- 'update_time' => time()
- ];
- }
- }
- break;
- case 4:// cos 腾讯云
- $upload = UploadService::init($type);
- $list = $upload->listbuckets();
- $config = $this->getStorageConfig($type);
- foreach ($list as $item) {
- if (($id = $this->getOneValue(['name' => $item['Name'], 'is_delete' => 0, 'access_key' => $config['accessKey']], 'id'))) {
- $this->update($id, [
- 'update_time' => time(),
- 'region' => $item['Location'],
- 'name' => $item['Name'],
- 'domain' => sys_config('tengxun_appid') ? $this->getDomain($type, $item['Name'], $item['Location']) : '',
- ]);
- } else {
- $data[] = [
- 'type' => $type,
- 'access_key' => $config['accessKey'],
- 'name' => $item['Name'],
- 'region' => $item['Location'],
- 'acl' => 'public-read',
- 'status' => 0,
- 'domain' => sys_config('tengxun_appid') ? $this->getDomain($type, $item['Name'], $item['Location']) : '',
- 'is_delete' => 0,
- 'add_time' => strtotime($item['CreationDate']),
- 'update_time' => time()
- ];
- }
- }
- break;
- }
- if ($data) {
- $this->saveAll($data);
- }
- return true;
- }
- /**
- * @param int $type
- * @param string $reagion
- * @return mixed|string
- */
- public function getReagionHost(int $type, string $reagion)
- {
- $upload = UploadService::init($type);
- $reagionList = $upload->getRegion();
- foreach ($reagionList as $item) {
- if (strstr($item['value'], $reagion) !== false) {
- return $item['value'];
- }
- }
- return '';
- }
- /**
- * 获取域名
- * @param int $type
- * @param string $name
- * @param string $reagion
- * @param string $appid
- * @return string
- */
- public function getDomain(int $type, string $name, string $reagion, string $appid = '')
- {
- $domainName = '';
- switch ($type) {
- case 3:// oss 阿里云
- $domainName = 'https://' . $name . '.' . $reagion;
- break;
- case 4:// cos 腾讯云
- $domainName = 'https://' . $name . ($appid ? '-' . $appid : '') . '.cos.' . $reagion . '.myqcloud.com';
- break;
- }
- return $domainName;
- }
- /**
- * 获取云存储配置
- * @param int $type
- * @return array|string[]
- */
- public function getConfig(int $type)
- {
- $res = ['name' => '', 'region' => '', 'domain' => ''];
- try {
- $config = $this->get(['type' => $type, 'status' => 1, 'is_delete' => 0]);
- if ($config) {
- return ['name' => $config->name, 'region' => $config->region, 'domain' => $config->domain];
- }
- } catch (\Throwable $e) {
- }
- return $res;
- }
- /**
- * 修改域名并绑定
- * @param int $id
- * @param string $domain
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function updateDomain(int $id, string $domain, array $data = [])
- {
- $info = $this->get($id);
- if (!$info) {
- throw new ValidateException('没有查询到数据');
- }
- if ($info->domain != $domain) {
- $info->domain = $domain;
- $upload = UploadService::init($info->type);
- //是否添加过域名不存在需要绑定域名
- $domainList = $upload->getDomian($info->name, $info->region);
- $domainParse = parse_url($domain);
- if (!in_array($domainParse['host'], $domainList)) {
- //绑定域名到云储存桶
- $res = $upload->bindDomian($info->name, $domain, $info->region);
- if (false === $res) {
- throw new ValidateException($upload->getError());
- }
- }
- //七牛云需要通过接口获取cname
- if (2 === ((int)$info->type)) {
- $resDomain = $upload->getDomianInfo($domain);
- $info->cname = $resDomain['cname'] ?? '';
- }
- return $info->save();
- }
- if ($info->cdn != $data['cdn']) {
- $info->cdn = $data['cdn'];
- $info->save();
- }
- return true;
- }
- }
|