Attachment.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2019/12/5
  6. * Time: 17:44
  7. */
  8. namespace app\admin\controller;
  9. use app\admin\controller\base\Permissions;
  10. use app\common\behavior\AdminLogBehavior;
  11. use app\common\model\Attachment as model;
  12. use file\FileHelper;
  13. use file\PathHelper;
  14. use think\Db;
  15. use think\Log;
  16. use think\Session;
  17. use time\Timestamp;
  18. class Attachment extends Permissions
  19. {
  20. public function index()
  21. {
  22. $model = new model();
  23. if ($this->request->isAjax()) {
  24. $post = $this->request->param();
  25. $where = [];
  26. if (isset($post['filename']) and !empty($post['filename'])) {
  27. $where['filename'] = ['like', '%' . $post['filename'] . '%'];
  28. }
  29. if (isset($post['filepath']) and !empty($post['filepath'])) {
  30. $where['filepath'] = ['like', '%' . $post['filepath'] . '%'];
  31. }
  32. if (isset($post['status']) and ($post['status'] == 1 or $post['status'] === '0' or $post['status'] == -1)) {
  33. $where['status'] = $post['status'];
  34. }
  35. if (isset($post['type'])) {
  36. if ($post['type'] == 1) {
  37. $where['filepath'] = ['like', '%http%'];
  38. }
  39. if ($post['type'] == -1) {
  40. $where['filepath'] = ['not like', '%http%'];
  41. }
  42. }
  43. if (isset($post['create_time']) and !empty($post['create_time'])) {
  44. $min_time = strtotime($post['create_time']);
  45. $max_time = $min_time + 24 * 60 * 60;
  46. $where['create_time'] = [['>=', $min_time], ['<=', $max_time]];
  47. }
  48. $count = $model->where($where)->count();
  49. $data = $model->where($where)->page($post['page']??0, $post['limit']??15)->order('create_time desc')->select();
  50. foreach ($data as $k => $v) {
  51. $v['thumb_url'] = $v['filepath'];
  52. $v['status_text'] = $v->status_text;
  53. $data[$k] = $v;
  54. }
  55. return array('code' => 0, 'count' => $count, 'data' => $data);
  56. } else {
  57. return $this->fetch();
  58. }
  59. }
  60. /**
  61. * 选择图片
  62. * @return mixed
  63. */
  64. public function selectImage()
  65. {
  66. $model = new model();
  67. $where = [
  68. 'fileext' => ['in', ['jpg', 'png', 'gif', 'jpeg', 'bmp', 'webp']],
  69. 'status' => model::STATUS_OPEN
  70. ];
  71. $attachment = $model->where($where)->order('create_time desc')->paginate(24);
  72. $this->assign('attachment', $attachment);
  73. $this->assign('showUpload', in_array(43, $this->getPermission()));
  74. return $this->fetch();
  75. }
  76. /**
  77. * 审核
  78. */
  79. public function status()
  80. {
  81. if ($this->request->isPost()) {
  82. $post = $this->request->post();
  83. $admin_id = \think\Session::get(self::ADMIN_ID);
  84. if (false == Db::name('attachment')->where('id', $post['id'])->update(['status' => $post['status'], 'admin_id' => $admin_id, 'audit_time' => time()])) {
  85. $this->error('设置失败');
  86. } else {
  87. $this->success('设置成功', 'index');
  88. }
  89. }
  90. }
  91. /**
  92. * 删除本地文件
  93. */
  94. public function delete()
  95. {
  96. if ($this->request->isAjax()) {
  97. $id = $this->request->param('id', 0, 'intval');
  98. $attachment = Db::name('attachment')->where('id', $id)->value('filepath');
  99. if (!isUrl($attachment) && file_exists(ROOT_PATH . 'public' . $attachment)) {
  100. //删除本地文件
  101. if (unlink(ROOT_PATH . 'public' . $attachment)) {
  102. if (false == Db::name('attachment')->where('id', $id)->delete()) {
  103. $this->error('删除失败');
  104. } else {
  105. $this->success('删除成功', 'admin/attachment/index');
  106. }
  107. } else {
  108. $this->error('删除失败');
  109. }
  110. } else {
  111. if (false == Db::name('attachment')->where('id', $id)->delete()) {
  112. $this->error('删除失败');
  113. } else {
  114. $this->success('删除成功', 'admin/attachment/index');
  115. }
  116. }
  117. }
  118. }
  119. /**
  120. * 批量删除本地文件
  121. */
  122. public function deletes()
  123. {
  124. if ($this->request->isAjax()) {
  125. $post = $this->request->param();
  126. $ids = $post['ids'];
  127. $attachments = (new model())->where('id', 'in', $ids)->select();
  128. foreach ($attachments as $attachment) {
  129. if (!isUrl($attachment->filepath) && file_exists(ROOT_PATH . 'public' . $attachment->filepath)) {
  130. //删除本地文件
  131. if (unlink(ROOT_PATH . 'public' . $attachment->filepath)) {
  132. if (false == Db::name('attachment')->where('id', $attachment->id)->delete()) {
  133. $this->error('删除失败');
  134. }
  135. } else {
  136. $this->error('删除失败');
  137. }
  138. } else {
  139. if (false == Db::name('attachment')->where('id', $attachment->id)->delete()) {
  140. $this->error('删除失败');
  141. }
  142. }
  143. }
  144. Log::log("批量删除附件:" . implode(',', $ids));
  145. $this->success('删除成功');
  146. }
  147. }
  148. /**
  149. * 下载
  150. * @return \think\response\Json
  151. */
  152. public function download()
  153. {
  154. if ($this->request->isAjax()) {
  155. $id = $this->request->param('id', 0, 'intval');
  156. if ($id > 0) {
  157. //获取下载链接
  158. $data = Db::name('attachment')->where('id', $id)->find();
  159. $res['data'] = $data['filepath'];
  160. $res['name'] = $data['filename'];
  161. //增加下载量
  162. Db::name('attachment')->where('id', $id)->setInc('download', 1);
  163. $res['code'] = 1;
  164. return json($res);
  165. } else {
  166. $this->error('错误请求');
  167. }
  168. }
  169. }
  170. /**
  171. * 网络文件本地化
  172. */
  173. public function imgPersistence()
  174. {
  175. $attachments = (new model())->where(['filepath' => ['like', '%http%']])->order('id desc')->select();
  176. if (count($attachments) > 50) {
  177. $this->error('网络文件过多,请使用命令行操作');
  178. }
  179. $savePath = DS . 'uploads' . DS . 'admin' . DS . 'attachment' . DS . Timestamp::dayStart(time()) . DS;
  180. foreach ($attachments as $attachment) {
  181. $saveName = PathHelper::getFilename($attachment->filepath);
  182. FileHelper::fetchDownFile($attachment->filepath, ROOT_PATH . 'public' . $savePath, $saveName);
  183. $attachment->save(['filepath' => $savePath . $saveName]);
  184. }
  185. $msg = '执行完成,本地化' . count($attachments) . '个文件';
  186. if ($this->request->isCli()) {
  187. echo $msg . PHP_EOL;
  188. } else {
  189. $this->success($msg);
  190. }
  191. }
  192. /**
  193. * 添加网络图片
  194. */
  195. public function create()
  196. {
  197. if ($this->request->isAjax()) {
  198. $post = $this->request->post();
  199. $validate = new \think\Validate([
  200. ['filepath', 'require|max:200'],
  201. ]);
  202. if (!$validate->check($post)) {
  203. $this->error('提交失败:' . $validate->getError());
  204. }
  205. $module = $this->request->param('module', 'admin');
  206. $use = $this->request->param('use', 'attachment');
  207. if (ifContain($use, '/')) {
  208. $use = deleteStartDS(replaceDS($use));
  209. }
  210. if (ifContain($use, '.')) {
  211. $use = explode('.', $use)[0];
  212. }
  213. $filepath = $post['filepath'];
  214. $pathinfo = pathinfo($filepath);
  215. $data = [
  216. 'module' => $module,//模块
  217. 'use' => $use,//来源
  218. 'filename' => $pathinfo['basename'],//文件名
  219. 'filepath' => $filepath,//文件路径
  220. 'fileext' => $pathinfo['extension']??'webp',//文件后缀
  221. 'filesize' => 0,//文件大小
  222. 'create_time' => time(),//时间
  223. 'uploadip' => $this->request->ip(),//IP
  224. 'user_id' => Session::has(self::ADMIN_ID) ? Session::get(self::ADMIN_ID) : 0,
  225. 'status' => model::STATUS_OPEN,
  226. 'admin_id' => 0,
  227. 'audit_time' => 0,
  228. ];
  229. $res['id'] = Db::name('attachment')->insertGetId($data);
  230. $res['src'] = $filepath;
  231. $this->success('完成', '', $res);
  232. }
  233. }
  234. /**
  235. * 上传附件
  236. * @return \think\response\Json
  237. */
  238. public function upload()
  239. {
  240. $module = $this->request->param('module', 'admin');
  241. $use = $this->request->param('use', 'attachment');
  242. if (ifContain($use, '/')) {
  243. $use = deleteStartDS(replaceDS($use));
  244. }
  245. if (ifContain($use, '.')) {
  246. $use = explode('.', $use)[0];
  247. }
  248. if ($this->request->file('file')) {
  249. $file = $this->request->file('file');
  250. } else {
  251. $res['code'] = 1;
  252. $res['msg'] = '没有上传文件';
  253. return json($res);
  254. }
  255. $web_config = Db::name('webconfig')->where('id', 1)->find();
  256. $info = $file->validate(['size' => $web_config['file_size'] * 1024, 'ext' => $web_config['file_type']])->rule('date')->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . $module . DS . $use);
  257. if ($info) {
  258. //写入到附件表
  259. $data = [];
  260. $data['module'] = $module;//模块
  261. $data['use'] = $use;//来源
  262. $data['filename'] = $info->getFilename();
  263. $data['filepath'] = DS . 'uploads' . DS . $module . DS . $use . DS . $info->getSaveName();
  264. $data['fileext'] = $info->getExtension();
  265. $data['filesize'] = $info->getSize();
  266. $data['create_time'] = time();
  267. $data['uploadip'] = $this->request->ip();
  268. $data['user_id'] = Session::has(self::ADMIN_ID) ? Session::get(self::ADMIN_ID) : 0;
  269. if ($data['module'] = 'admin') {
  270. //通过后台上传的文件直接审核通过
  271. $data['status'] = model::STATUS_OPEN;
  272. $data['admin_id'] = $data['user_id'];
  273. $data['audit_time'] = time();
  274. }
  275. $res['id'] = Db::name('attachment')->insertGetId($data);
  276. $res['src'] = replaceUrlDS($data['filepath']);
  277. (new AdminLogBehavior())->updateLastLog('上传附件:' . $data['filepath']);
  278. $this->success('上传完成', 'admin/attachment/index', $res);
  279. } else {
  280. $this->error('上传失败:' . $file->getError());
  281. }
  282. }
  283. }