123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?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\controller\admin\v1\system\config;
- use app\controller\admin\AuthController;
- use app\services\system\config\SystemConfigServices;
- use app\services\system\config\SystemStorageServices;
- use crmeb\services\SystemConfigService;
- use crmeb\services\UploadService;
- use think\facade\App;
- /**
- * Class SystemStorage
- * @package app\adminapi\controller\v1\setting
- */
- class SystemStorage extends AuthController
- {
- /**
- * SystemStorage constructor.
- * @param App $app
- * @param SystemStorageServices $services
- */
- public function __construct(App $app, SystemStorageServices $services)
- {
- parent::__construct($app);
- $this->services = $services;
- }
- /**
- * @return mixed
- */
- public function index()
- {
- return app('json')->success($this->services->getList(['type' => $this->request->get('type')]));
- }
- /**
- * 获取创建数据表单
- * @param $type
- * @return mixed
- * @throws \FormBuilder\Exception\FormBuilderException
- */
- public function create($type)
- {
- if (!$type) {
- return app('json')->fail('缺少参数');
- }
- return app('json')->success($this->services->getFormStorage((int)$type));
- }
- /**
- * 获取配置表单
- * @param $type
- * @return mixed
- * @throws \FormBuilder\Exception\FormBuilderException
- */
- public function getConfigForm($type)
- {
- return app('json')->success($this->services->getFormStorageConfig((int)$type));
- }
- /**
- * 获取配置类型
- * @return mixed
- */
- public function getConfig()
- {
- return app('json')->success(['type' => (int)sys_config('upload_type', 1)]);
- }
- /**
- * @param SystemConfigServices $services
- * @return mixed
- */
- public function saveConfig(SystemConfigServices $services)
- {
- $type = (int)$this->request->post('type', 0);
- $services->update('upload_type', ['value' => json_encode($type)], 'menu_name');
- if (1 === $type) {
- $this->services->transaction(function () {
- $this->services->update(['status' => 1, 'is_delete' => 0], ['status' => 0]);
- });
- }
- \crmeb\services\CacheService::redisHandler(SystemConfigService::getTag())->clear();
- $data = $this->request->postMore([
- ['accessKey', ''],
- ['secretKey', ''],
- ['appid', ''],
- ['storageRegion', '']
- ]);
- $this->services->saveConfig((int)$type, $data);
- return app('json')->success('保存成功');
- }
- /**
- * @param $type
- * @return mixed
- */
- public function synch($type)
- {
- try {
- $this->services->synchronization((int)$type);
- return app('json')->success('同步成功');
- } catch (\Throwable $e) {
- return app('json')->fail('同步失败,失败原因:' . $e->getMessage());
- }
- }
- /**
- * 保存类型
- * @param $type
- * @return mixed
- */
- public function save($type)
- {
- $data = $this->request->postMore([
- ['accessKey', ''],
- ['secretKey', ''],
- ['appid', ''],
- ['name', ''],
- ['region', ''],
- ['acl', ''],
- ]);
- $type = (int)$type;
- if ($type === 4) {
- if (!$data['appid'] && !sys_config('tengxun_appid')) {
- return app('json')->fail('缺少APPID');
- }
- }
- if (!$data['accessKey']) {
- unset($data['accessKey'], $data['secretKey'], $data['appid']);
- }
- $this->services->saveStorage((int)$type, $data);
- return app('json')->success('添加成功');
- }
- /**
- * 修改状态
- * @param $id
- * @return mixed
- */
- public function status($id)
- {
- if (!$id) {
- return app('json')->fail('缺少参数');
- }
- $info = $this->services->get($id);
- $info->status = 1;
- if (!$info->domain) {
- return app('json')->fail('请先设置空间域名');
- }
- //设置跨域规则
- try {
- $upload = UploadService::init($info->type);
- $upload->setBucketCors($info->name, $info->region);
- } catch (\Throwable $e) {
- }
- //修改状态
- $this->services->transaction(function () use ($id, $info) {
- $this->services->update(['type' => $info->type, 'status' => 1, 'is_delete' => 0], ['status' => 0]);
- $info->save();
- });
- return app('json')->success('修改成功');
- }
- /**
- * @param $id
- * @return mixed
- * @throws \FormBuilder\Exception\FormBuilderException
- */
- public function getUpdateDomainForm($id)
- {
- return app('json')->success($this->services->getUpdateDomainForm((int)$id));
- }
- /**
- * @param $id
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function updateDomain($id)
- {
- $domain = $this->request->post('domain', '');
- $data = $this->request->postMore([
- ['pri', ''],
- ['ca', '']
- ]);
- if (!$domain) {
- return app('json')->fail('缺少参数');
- }
- if (strstr($domain, 'https://') === false && strstr($domain, 'http://') === false) {
- return app('json')->fail('格式错误,请输入格式为:http://域名');
- }
- // if (strstr($domain, 'https://') !== false && !$data['pri']) {
- // return app('json')->fail('域名为HTTPS访问时,必须填写证书');
- // }
- $this->services->updateDomain($id, $domain);
- return app('json')->success('修改成功');
- }
- /**
- * 删除
- * @param $id
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function delete($id)
- {
- if (!$id) {
- return app('json')->fail('缺少参数');
- }
- if ($this->services->deleteStorage($id)) {
- return app('json')->success('删除成功');
- } else {
- return app('json')->fail('删除失败');
- }
- }
- /**
- * 切换存储类型
- * @param SystemConfigServices $services
- * @param $type
- * @return mixed
- */
- public function uploadType(SystemConfigServices $services, $type)
- {
- $status = $this->services->count(['type' => $type, 'status' => 1]);
- if (!$status && $type != 1) {
- return app('json')->fail('未有正在使用的存储空间');
- }
- $services->update('upload_type', ['value' => json_encode($type)], 'menu_name');
- \crmeb\services\SystemConfigService::clear();
- return app('json')->success('切换成功');
- }
- }
|