Files.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\controller\admin;
  3. use laytp\controller\Backend;
  4. use app\service\admin\UserServiceFacade;
  5. use laytp\library\CommonFun;
  6. use laytp\library\UploadDomain;
  7. use think\facade\Config;
  8. /**
  9. * 附件管理
  10. */
  11. class Files extends Backend
  12. {
  13. /**
  14. * files模型对象
  15. * @var \app\model\Files
  16. */
  17. protected $model;
  18. public $hasSoftDel = 1;//是否拥有软删除功能
  19. public function initialize()
  20. {
  21. parent::initialize();
  22. $this->model = new \app\model\Files();
  23. }
  24. /**
  25. * 查看
  26. * @throws \think\db\exception\DbException
  27. */
  28. public function index()
  29. {
  30. global $_W;
  31. $where = $this->buildSearchParams();
  32. $where[] = ['uniacid','=',$_W['uniacid']];
  33. $order = $this->buildOrder();
  34. $data = $this->model->where($where)->order($order)->with(['category', 'createAdminUser', 'updateAdminUser']);
  35. $paging = $this->request->param('paging', false);
  36. if ($paging) {
  37. $limit = $this->request->param('limit', Config::get('paginate.limit'));
  38. $data = $data->paginate($limit)->toArray();
  39. $data['data'] = $this->getSelectedData($data['data']);
  40. } else {
  41. $data = $data->select()->toArray();
  42. }
  43. return $this->success('数据获取成功', $data);
  44. }
  45. //添加
  46. public function add()
  47. {
  48. return $this->error('当前版本暂时不支持在附件管理中添加附件');
  49. $post = CommonFun::filterPostData($this->request->post());
  50. $post['create_admin_user_id'] = UserServiceFacade::getUser()->id;
  51. $post['update_admin_user_id'] = UserServiceFacade::getUser()->id;
  52. $post['path'] = UploadDomain::singleDelUploadDomain($post['path']);
  53. if ($this->model->create($post)) {
  54. return $this->success('添加成功', $post);
  55. } else {
  56. return $this->error('操作失败');
  57. }
  58. }
  59. //不经过服务器方式上传成功后,回调的ajax请求地址,将上传成功的文件信息存入表中
  60. public function unViaSave()
  61. {
  62. $post = $this->request->post();
  63. $post['create_admin_user_id'] = UserServiceFacade::getUser()->id;
  64. $post['update_admin_user_id'] = UserServiceFacade::getUser()->id;
  65. $post['create_time'] = date('Y-m-d H:i:s');
  66. $post['update_time'] = date('Y-m-d H:i:s');
  67. $post['id'] = $this->model->insertGetId($post);
  68. if ($post['id']) {
  69. return $this->success('添加成功', $post);
  70. } else {
  71. return $this->error('操作失败');
  72. }
  73. }
  74. //编辑
  75. public function edit()
  76. {
  77. return $this->error('当前版本暂时不支持在附件管理中编辑附件');
  78. $id = $this->request->param('id');
  79. $info = $this->model->find($id);
  80. $post = CommonFun::filterPostData($this->request->post());
  81. $post['update_admin_user_id'] = UserServiceFacade::getUser()->id;
  82. $post['path'] = UploadDomain::singleDelUploadDomain($post['path']);
  83. foreach ($post as $k => $v) {
  84. $info->$k = $v;
  85. }
  86. try {
  87. $updateRes = $info->save();
  88. if ($updateRes) {
  89. return $this->success('编辑成功');
  90. } else if ($updateRes === 0) {
  91. return $this->success('未做修改');
  92. } else if ($updateRes === null) {
  93. return $this->error('操作失败');
  94. }
  95. } catch (\Exception $e) {
  96. return $this->exceptionError($e);
  97. }
  98. }
  99. //删除
  100. public function del()
  101. {
  102. $id = $this->request->param('id');
  103. $info = $this->model->find($id);
  104. $post = CommonFun::filterPostData($this->request->post());
  105. $post['update_admin_user_id'] = UserServiceFacade::getUser()->id;
  106. $post['path'] = UploadDomain::singleDelUploadDomain($post['path']);
  107. foreach ($post as $k => $v) {
  108. $info->$k = $v;
  109. }
  110. try {
  111. $updateRes = $info->save();
  112. if (!$updateRes) {
  113. return $this->error('操作失败');
  114. } else {
  115. return $this->success('编辑成功');
  116. }
  117. } catch (\Exception $e) {
  118. return $this->exceptionError($e);
  119. }
  120. }
  121. //回收站
  122. public function recycle()
  123. {
  124. global $_W;
  125. $where = $this->buildSearchParams();
  126. $where[] = ['uniacid','=',$_W['uniacid']];
  127. $order = $this->buildOrder();
  128. $limit = $this->request->param('limit', Config::get('paginate.limit'));
  129. $data = $this->model->onlyTrashed()
  130. ->with(['category', 'createAdminUser', 'updateAdminUser'])
  131. ->order($order)->where($where)->paginate($limit)->toArray();
  132. return $this->success('回收站数据获取成功', $data);
  133. }
  134. }