| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\controller\admin\system\attachment;
- use app\common\repositories\system\attachment\AttachmentCategoryRepository;
- use app\common\repositories\system\attachment\AttachmentRepository;
- use crmeb\basic\BaseController;
- use crmeb\services\DownloadImageService;
- use crmeb\services\ImageHostService;
- use crmeb\services\UploadService;
- use Exception;
- use think\App;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\facade\Cache;
- use think\response\Json;
- /**
- * 附件
- */
- class Attachment extends BaseController
- {
- /**
- * @var AttachmentRepository
- */
- protected $repository;
- /**
- * @var int
- */
- protected $merId;
- /**
- * Attachment constructor.
- * @param App $app
- * @param AttachmentRepository $repository
- */
- public function __construct(App $app, AttachmentRepository $repository)
- {
- parent::__construct($app);
- $this->repository = $repository;
- $this->merId = $this->request->merId();
- }
- /**
- * 上传图片
- * @param int $id
- * @param string $field
- * @param AttachmentCategoryRepository $repository
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @author xaboy
- * @day 2020-04-15
- */
- public function image($id, $field, AttachmentCategoryRepository $repository)
- {
- $file = $this->request->file($field);
- $ueditor = $this->request->param('ueditor');
- if (!$file)
- return app('json')->fail('请上传图片');
- $file = is_array($file) ? $file[0] : $file;
- if ($id) {
- if (!$category = $repository->get($id, $this->merId))
- return app('json')->fail('目录不存在');
- $info = [
- 'enname' => $category->attachment_category_enname,
- 'id' => $category->attachment_category_id
- ];
- } else {
- $info = [
- 'enname' => 'def',
- 'id' => 0
- ];
- }
- validate(["$field|图片" => [
- 'fileSize' => config('upload.filesize'),
- 'fileExt' => config('upload.iamge_fileExt'),
- 'fileMime' => config('upload.image_fileMime'),
- ]])->check([$field => $file]);
- $type = (int)systemConfig('upload_type') ?: 1;
- $upload = UploadService::create($type);
- $data = $upload->to($info['enname'])->move($field);
- if ($data === false) {
- return app('json')->fail($upload->getError());
- }
- $res = $upload->getUploadInfo();
- $res['dir'] = tidy_url($res['dir']);
- $_name = '.' . $file->getOriginalExtension();
- $data = [
- 'attachment_category_id' => $info['id'],
- 'attachment_name' => str_replace($_name, '', $file->getOriginalName()),
- 'attachment_src' => $res['dir']
- ];
- $image = $this->repository->create($type, $this->merId, $this->request->adminId(), $data);
- if ($ueditor)
- return response([
- 'state' => 'SUCCESS',
- 'url' => $data['attachment_src'],
- 'title' => $data['attachment_src'],
- 'original' => $data['attachment_src'],
- ], 200, [], 'json');
- return app('json')->success(['src' => $data['attachment_src'], 'attachment_id' => $image->attachment_id]);
- }
- /**
- * 获取列表
- * @return Json
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @author 张先生
- * @date 2020-03-27
- */
- public function getList()
- {
- [$page, $limit] = $this->getPage();
- $where = $this->request->params([['attachment_category_id', 0], 'order', 'attachment_name']);
- $where['user_type'] = $this->merId;
- return app('json')->success($this->repository->getList($where, $page, $limit));
- }
- /**
- * 批量修改分类
- * @param AttachmentCategoryRepository $attachmentCategoryRepository
- * @return mixed
- * @throws DbException
- * @author xaboy
- * @day 2020-04-16
- */
- public function batchChangeCategory(AttachmentCategoryRepository $attachmentCategoryRepository)
- {
- [$ids, $attachment_category_id] = $this->request->params([['ids', []], 'attachment_category_id'], true);
- if ($attachment_category_id && !$attachmentCategoryRepository->merExists($this->merId, $attachment_category_id))
- return app('json')->fail('分类不存在');
- if (!is_array($ids) || !count($ids))
- return app('json')->fail('请选择要修改分类的附件');
- $this->repository->batchChangeCategory(array_map('intval', $ids), intval($attachment_category_id), $this->merId);
- return app('json')->success('图片移动成功');
- }
- /**
- * 批量删除
- *
- * @return Json
- * @throws Exception
- * @author 张先生
- * @date 2020-03-30
- */
- public function delete()
- {
- $ids = (array)$this->request->param('ids', []);
- if (!count($ids))
- return app('json')->fail('数据不存在');
- $this->repository->batchDelete($ids, $this->merId);
- return app('json')->success('删除成功');
- }
- public function updateForm($id)
- {
- if (!$this->repository->getWhereCount(['attachment_id' => $id, 'user_type' => $this->request->merId()]))
- return app('json')->fail('数据不存在');
- return app('json')->success(formToData($this->repository->form($id, $this->request->merId())));
- }
- /**
- * 编辑名称
- * @param $id
- * @return Json
- * @author Qinii
- */
- public function update($id)
- {
- $data = $this->request->params(['attachment_name']);
- if (!$this->repository->getWhereCount(['attachment_id' => $id, 'user_type' => $this->request->merId()]))
- return app('json')->fail('数据不存在');
- $this->repository->update($id, $data);
- return app('json')->success('修改成功');
- }
- /**
- * 扫码上传图片,生成二维码
- * @param $pid
- * @return Json
- * @author Qinii
- * @day 2023/7/18
- */
- public function scanUploadQrcode($pid)
- {
- $merId = $this->request->merId();
- $token = $merId . md5(time());
- $url = rtrim(systemConfig('site_url'), '/') . '/pages/admin/scan/index?pid=' . $pid . '&mer_id=' . $merId . '&token=' . $token;
- Cache::set('scan_mer_' . $token, $merId);
- return app('json')->success(compact('token', 'pid', 'url'));
- }
- /**
- * 获取通过二维码上传的图片
- * @param $token
- * @return Json
- * @author Qinii
- * @day 2023/7/18
- */
- public function scanUploadImage($token)
- {
- $res = Cache::smembers('scan_' . $token);
- $data = [];
- if ($res) {
- $data = $this->repository->getSearch(['ids' => $res])->field('attachment_id,attachment_src')->select();
- }
- return app('json')->success($data);
- }
- /**
- * 提交通过扫码上传的图片信息,可修改分类
- * @param $token
- * @param AttachmentCategoryRepository $attachmentCategoryRepository
- * @return Json
- * @author Qinii
- * @day 2023/7/18
- */
- public function scanUploadSave($token, AttachmentCategoryRepository $attachmentCategoryRepository)
- {
- $id = $this->request->param('pid');
- $ids = $this->request->param('ids');
- if ($id && !$attachmentCategoryRepository->get($id, $this->merId))
- return app('json')->fail('目录不存在');
- $this->repository->updates($ids, ['attachment_category_id' => $id]);
- Cache::del('scan_' . $token);
- Cache::del('scan_mer_' . $token);
- return app('json')->success('操作成功');
- }
- /**
- * 线上图片上传
- * @param AttachmentCategoryRepository $attachmentCategoryRepository
- * @return Json
- * @author Qinii
- * @day 2023/7/19
- */
- public function onlineUpload(AttachmentCategoryRepository $attachmentCategoryRepository)
- {
- $id = $this->request->param('id', 0);
- $images = $this->request->param('images', []);
- if (!$images) return app('json')->fail('请上传图片地址');
- if ($id) {
- if (!$category = $attachmentCategoryRepository->get($id, $this->merId))
- return app('json')->fail('目录不存在');
- $info = [
- 'enname' => $category->attachment_category_enname,
- 'id' => $category->attachment_category_id
- ];
- } else {
- $info = [
- 'enname' => 'def',
- 'id' => 0
- ];
- }
- $type = (int)systemConfig('upload_type') ?: 1;
- $upload = app()->make(DownloadImageService::class)->downloadImages($images, $info['enname']);
- $data = [];
- foreach ($upload as $item) {
- $data[] = [
- 'attachment_category_id' => $info['id'],
- 'attachment_name' => $item['name'],
- 'attachment_src' => $item['path'],
- 'upload_type' => $type,
- 'user_type' => $this->merId,
- 'user_id' => $this->request->adminId()
- ];
- }
- if ($data) $this->repository->insertAll($data);
- return app('json')->success('操作成功');
- }
- /**
- * 替换图片域名
- * @return Json
- * @author Qinii
- */
- public function replaceHost()
- {
- $origin = $this->request->param('origin');
- $replace = $this->request->param('replace');
- $server = ImageHostService::getInstance();
- $res = $server->execute($origin,$replace);
- return app('json')->success($res ? '操作成功' : '执行过程中存在错误,请在runtime/log中查看具体错误信息');
- }
- /**
- * 视频分片上传
- * @return mixed
- */
- public function videoUpload()
- {
- $data = $this->request->params([
- ['chunkNumber', 0],//第几分片
- ['currentChunkSize', 0],//分片大小
- ['chunkSize', 0],//总大小
- ['totalChunks', 0],//分片总数
- ['file', 'file'],//文件
- ['md5', ''],//MD5
- ['filename', ''],//文件名称
- ]);
- $res = $this->repository->videoUpload($data, $_FILES['file']);
- return app('json')->success($res);
- }
- }
|