123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 附件
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\admin\controller;
  8. use app\work\traits\WorkAuthTrait;
  9. use think\Hook;
  10. use think\Request;
  11. class File extends ApiCommon
  12. {
  13. use WorkAuthTrait;
  14. /**
  15. * 用于判断权限
  16. * @permission 无限制
  17. * @allow 登录用户可访问
  18. * @other 其他根据系统设置
  19. **/
  20. public function _initialize()
  21. {
  22. $action = [
  23. 'permission'=>[''],
  24. 'allow'=>['index', 'save', 'delete', 'update', 'read', 'download', 'deleteall', 'downloadimage']
  25. ];
  26. Hook::listen('check_auth',$action);
  27. $request = Request::instance();
  28. $a = strtolower($request->action());
  29. if (!in_array($a, $action['permission'])) {
  30. parent::_initialize();
  31. }
  32. }
  33. /**
  34. * 附件列表
  35. * @author Michael_xu
  36. * @param
  37. * @return
  38. */
  39. public function index()
  40. {
  41. $fileModel = model('File');
  42. $param = $this->param;
  43. $data = $fileModel->getDataList($param, $param['by']);
  44. return resultArray(['data' => $data]);
  45. }
  46. /**
  47. * 附件上传
  48. * @author Michael_xu
  49. * @return
  50. */
  51. public function save()
  52. {
  53. header('Access-Control-Allow-Origin: *');
  54. header('Access-Control-Allow-Methods: POST');
  55. header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
  56. $type = $this->param['type'];
  57. $files = request()->file('file');
  58. $imgs = request()->file('img');
  59. $i = 0;
  60. $newFiles = [];
  61. # 项目上传附件权限
  62. if (!empty($this->param['module']) && $this->param['module'] == 'work_task' && !empty($this->param['work_id'])) {
  63. if (!$this->checkWorkOperationAuth('uploadTaskFile', $this->param['work_id'], $this->userInfo['id'])) {
  64. header('Content-Type:application/json; charset=utf-8');
  65. exit(json_encode(['code' => 102, 'error' => '无权操作!']));
  66. }
  67. }
  68. if (!empty($type) && in_array($type, ['img', 'file'])) {
  69. # todo 兼容11.0前端
  70. if ($type == 'img') {
  71. $newFiles[0]['obj'] = $files;
  72. $newFiles[0]['types'] = 'img';
  73. }
  74. if ($type == 'file') {
  75. $newFiles[0]['obj'] = $files;
  76. $newFiles[0]['types'] = 'file';
  77. }
  78. } else {
  79. # todo 兼容9.0前端
  80. if (!empty($files)) {
  81. foreach ($files as $v) {
  82. $newFiles[$i]['obj'] = $v;
  83. $newFiles[$i]['types'] = 'file';
  84. $i++;
  85. }
  86. }
  87. if (!empty($imgs)) {
  88. foreach ($imgs as $v) {
  89. $newFiles[$i]['obj'] = $v;
  90. $newFiles[$i]['types'] = 'img';
  91. $i++;
  92. }
  93. }
  94. }
  95. $fileModel = model('File');
  96. $param = $this->param;
  97. $param['create_user_id'] = $this->userInfo['id'];
  98. $res = $fileModel->createData($newFiles, $param);
  99. if($res){
  100. return resultArray(['data' => $res]);
  101. } else {
  102. return resultArray(['error' => $fileModel->getError()]);
  103. }
  104. }
  105. /**
  106. * 附件删除
  107. * @author Michael_xu
  108. * @param 通过 save_name 作为条件 来删除附件
  109. * @return
  110. */
  111. public function delete()
  112. {
  113. $fileModel = model('File');
  114. $param = $this->param;
  115. # 项目删除附件权限
  116. if (!empty($this->param['module']) && $this->param['module'] == 'work_task' && !empty($this->param['work_id'])) {
  117. if (!$this->checkWorkOperationAuth('deleteTaskFile', $this->param['work_id'], $this->userInfo['id'])) {
  118. header('Content-Type:application/json; charset=utf-8');
  119. exit(json_encode(['code' => 102, 'error' => '无权操作!']));
  120. }
  121. }
  122. $res = $fileModel->delFileBySaveName($param['save_name'], $param);
  123. if (!$res) {
  124. return resultArray(['error' => $fileModel->getError()]);
  125. }
  126. return resultArray(['data' => '删除成功']);
  127. }
  128. /**
  129. * 全部删除(活动、产品)
  130. *
  131. * @return \think\response\Json
  132. * @throws \think\Exception
  133. * @throws \think\exception\PDOException
  134. */
  135. public function deleteAll()
  136. {
  137. if ((empty($this->param['module']) && empty($this->param['module_id'])) || empty($this->param['file_id'])) {
  138. return resultArray(['error' => '参数错误!']);
  139. }
  140. $fileModel = new \app\admin\model\File();
  141. if (!$fileModel->deleteAll($this->param)) return resultArray(['error' => '操作失败!']);
  142. return resultArray(['data' => '操作成功!']);
  143. }
  144. /**
  145. * 附件编辑
  146. */
  147. public function update()
  148. {
  149. $fileModel = model('File');
  150. $param = $this->param;
  151. if ( $param['save_name'] && $param['name'] ) {
  152. $ret = $fileModel->updateNameBySaveName($param['save_name'],$param['name']);
  153. if ($ret) {
  154. return resultArray(['data'=>'操作成功']);
  155. } else {
  156. return resultArray(['error'=>'操作失败']);
  157. }
  158. } else {
  159. return resultArray(['error'=>'参数错误']);
  160. }
  161. }
  162. /**
  163. * 附件查看(下载)
  164. * @author Michael_xu
  165. * @return
  166. */
  167. public function read()
  168. {
  169. $fileModel = model('File');
  170. $param = $this->param;
  171. $data = $fileModel->getDataBySaveName($param['save_name']);
  172. if (!$data) {
  173. return resultArray(['error' => $this->getError()]);
  174. }
  175. return resultArray(['data' => $data]);
  176. }
  177. /**
  178. * 静态资源文件下载
  179. */
  180. public function download()
  181. {
  182. if(isset($this->param['path'])){
  183. $path = $this->param['path'];
  184. $name = $this->param['name'] ?: '';
  185. if (empty($path)) return resultArray(['error' => '参数错误!']);
  186. return download(realpath('./public/' . $path), $name);
  187. }else{
  188. $path = $this->param['save_name'];
  189. $name = $this->param['name'] ?: '';
  190. if (empty($path)) return resultArray(['error' => '参数错误!']);
  191. if (!strstr($path, 'uploads')) $path = 'uploads/' . $path;
  192. return download(realpath('./public/' . $path), $name);
  193. }
  194. }
  195. /**
  196. * 下载图片(头像),前端要求重写一个。
  197. *
  198. * @return \think\response\Json|void
  199. */
  200. public function downloadImage()
  201. {
  202. $path = $this->param['path'];
  203. $file = explode('public/', $path);
  204. if (empty($path) || empty($file[1])) return resultArray(['error' => '参数错误!']);
  205. return download(realpath('./public/'.$file[1]));
  206. }
  207. }