123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- namespace app\system\controller\v1;
- use app\BaseController;
- use library\services\UtilService as Util;
- use app\model\system\Attachment as AttachmentModel;
- use library\services\upload\Upload as Upload;
- /**
- * 图片管理类
- * Class Attachment
- * @package app\system\controller\v1
- */
- class Attachment extends BaseController
- {
- /**
- * 显示资源列表
- *
- * @return \think\Response
- */
- public function index()
- {
- $where = Util::getMore([
- ['page', 1],
- ['limit', 18],
- ['pid', 0]
- ]);
- return app('json')->success(AttachmentModel::getImageList($where));
- }
- /**
- * 删除指定资源
- *
- * @param string $ids
- * @return \think\Response
- */
- public function delete()
- {
- $request = app('request');
- $ids = $request->post('ids');
- $ids = explode(',', $ids);
- if (empty($ids))
- return app('json')->fail('请选择要删除的图片');
- foreach ($ids as $v) {
- self::deleteImg($v);
- }
- return app('json')->success('删除成功');
- }
- /**
- * 图片管理上传图片
- * @return \think\response\Json
- */
- public function upload()
- {
- [$pid, $file] = Util::getMore([
- ['pid', 0],
- ['file', 'file']
- ], $this->request, true);
- try {
- $path = make_path('attach', 2, true);
- $upload = new Upload(2,config('qiniu')['upload']);
- $res = $upload->to($path)->validate()->move($file);
- if ($res === false) {
- return app('json')->fail($upload->getError());
- } else {
- $fileInfo = $upload->getUploadInfo();
- if ($fileInfo) {
- AttachmentModel::attachmentAdd($fileInfo['name'], $fileInfo['size'], $fileInfo['type'], $fileInfo['dir'], $fileInfo['thumb_path'], $pid, 2, $fileInfo['time'], 1, 0);
- }
- return app('json')->success('上传成功', ['src' => $res->filePath]);
- }
- } catch (\Exception $e) {
- return app('json')->fail($e->getMessage());
- }
- }
- /**删除图片和数据记录
- * @param $att_id
- */
- public function deleteImg($att_id)
- {
- $attinfo = AttachmentModel::get($att_id);
- if ($attinfo) {
- try {
- $upload = new Upload(2,config('qiniu')['upload']);
- $upload->delete($attinfo['name']);
- } catch (\Throwable $e) {
- }
- AttachmentModel::where('att_id', $att_id)->delete();
- }
- }
- /**
- * 移动图片分类操作
- */
- public function moveImageCate()
- {
- $data = Util::postMore([
- 'pid',
- 'images'
- ]);
- if ($data['images'] == '') return app('json')->fail('请选择图片');
- if (!$data['pid']) return app('json')->fail('请选择分类');
- $res = AttachmentModel::where('att_id', 'in', $data['images'])->update(['pid' => $data['pid']]);
- if ($res)
- return app('json')->success('移动成功');
- else
- return app('json')->fail('移动失败!');
- }
- }
|