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); 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' => 5242880, 'fileExt' => 'jpg,jpeg,png,bmp,gif', 'fileMime' => 'image/jpeg,image/png,image/gif', ]])->check([$field => $file]); $upload = UploadService::create(); $time = date('Y-m-d'); $path = "chuxubao/images/" . $time . '/'; $data = $upload->to($info['enname'])->move($field,$path); if ($data === false) { return app('json')->fail($upload->getError()); } $res = $upload->getUploadInfo(); $res['dir'] = path_to_url($res['dir']); if (strpos($res['dir'], 'http') === false) $res['dir'] = systemConfig('site_url') . $res['dir']; //if (strpos($res['dir'], 'http') === false) $res['dir'] = $this->request->domain() . $res['dir']; $data = [ 'attachment_category_id' => $info['id'], 'attachment_name' => $file->getOriginalName(), 'attachment_src' => $res['dir'] ]; $this->repository->create(1, $this->merId, $this->request->adminId(), $data); return app('json')->success(['src' => $data['attachment_src']]); } public function video($id,$field, AttachmentCategoryRepository $repository) { $file = $this->request->file($field); 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' => 1024*1024*50, 'fileExt' => 'mp4,mov', // 'fileMime' => 'video/mp4,video/quicktime,image/gif', ]])->check([$field => $file]); $upload = UploadService::create(); $time = date('Y-m-d'); $path = "chuxubao/video/" . $time . '/'; $data = $upload->to($info['enname'])->move($field,$path); if ($data === false) { return app('json')->fail($upload->getError()); } $res = $upload->getUploadInfo(); $res['dir'] = path_to_url($res['dir']); if (strpos($res['dir'], 'http') === false) $res['dir'] = systemConfig('site_url') . $res['dir']; //if (strpos($res['dir'], 'http') === false) $res['dir'] = $this->request->domain() . $res['dir']; $data = [ 'attachment_category_id' => $info['id'], 'attachment_name' => $file->getOriginalName(), 'attachment_src' => $res['dir'] ]; // $this->repository->create(1, $this->merId, $this->request->adminId(), $data); return app('json')->success(['src' => $data['attachment_src']]); } /** * 获取列表 * @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]]); $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('删除成功'); } }