SystemFile.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\system;
  12. use think\facade\App;
  13. use app\adminapi\controller\AuthController;
  14. use app\services\system\log\SystemFileServices;
  15. /**
  16. * 文件校验控制器
  17. * Class SystemFile
  18. * @package app\admin\controller\system
  19. *
  20. */
  21. class SystemFile extends AuthController
  22. {
  23. /**
  24. * 构造方法
  25. * SystemFile constructor.
  26. * @param App $app
  27. * @param SystemFileServices $services
  28. */
  29. public function __construct(App $app, SystemFileServices $services)
  30. {
  31. parent::__construct($app);
  32. $this->services = $services;
  33. }
  34. /**
  35. * 文件校验记录
  36. * @return mixed
  37. */
  38. public function index()
  39. {
  40. return app('json')->success(['list' => $this->services->getFileList()]);
  41. }
  42. /**
  43. * @return mixed
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\DbException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. *
  48. * @date 2022/09/07
  49. * @author yyw
  50. */
  51. public function login()
  52. {
  53. [$password] = $this->request->postMore([
  54. 'password',
  55. ], true);
  56. $adminInfo = $this->request->adminInfo();
  57. if (!$adminInfo) return app('json')->fail(100101);
  58. if ($adminInfo['level'] != 0) return app('json')->fail(100101);
  59. if ($password === '') return app('json')->fail(400256);
  60. return app('json')->success($this->services->login($password, 'file_edit'));
  61. }
  62. //打开目录
  63. public function opendir()
  64. {
  65. return app('json')->success($this->services->opendir());
  66. }
  67. //读取文件
  68. public function openfile()
  69. {
  70. $file = $this->request->param('filepath');
  71. if (empty($file)) return app('json')->fail(410087);
  72. return app('json')->success($this->services->openfile($file));
  73. }
  74. //保存文件
  75. public function savefile()
  76. {
  77. $comment = $this->request->param('comment');
  78. $filepath = $this->request->param('filepath');
  79. if (empty($filepath)) {
  80. return app('json')->fail(410087);
  81. }
  82. $res = $this->services->savefile($filepath, $comment);
  83. if ($res) {
  84. return app('json')->success(100000);
  85. } else {
  86. return app('json')->fail(100006);
  87. }
  88. }
  89. /**
  90. * 创建文件夹
  91. * @return mixed
  92. *
  93. * @date 2022/09/17
  94. * @author yyw
  95. */
  96. public function createFolder()
  97. {
  98. [$path, $name] = $this->request->postMore([
  99. ['path', ''],
  100. ['name', '']
  101. ], true);
  102. if (empty($path) || empty($name)) {
  103. return app('json')->fail(410087);
  104. }
  105. $data = [];
  106. try {
  107. $res = $this->services->createFolder($path, $name);
  108. if ($res) {
  109. $data = [
  110. 'children' => [],
  111. 'contextmenu' => true,
  112. 'isDir' => true,
  113. 'loading' => false,
  114. 'path' => $path,
  115. 'pathname' => $path . DS . $name,
  116. 'title' => $name,
  117. ];
  118. } else {
  119. return app('json')->fail(100005);
  120. }
  121. } catch (\Exception $e) {
  122. return app('json')->fail($e->getMessage());
  123. }
  124. return app('json')->success($data);
  125. }
  126. /**
  127. * 创建文件
  128. * @return mixed
  129. *
  130. * @date 2022/09/17
  131. * @author yyw
  132. */
  133. public function createFile()
  134. {
  135. [$path, $name] = $this->request->postMore([
  136. ['path', ''],
  137. ['name', '']
  138. ], true);
  139. if (empty($path) || empty($name)) {
  140. return app('json')->fail(410087);
  141. }
  142. $data = [];
  143. try {
  144. $res = $this->services->createFile($path, $name);
  145. if ($res) {
  146. $data = [
  147. 'children' => [],
  148. 'contextmenu' => true,
  149. 'isDir' => false,
  150. 'loading' => false,
  151. 'path' => $path,
  152. 'pathname' => $path . DS . $name,
  153. 'title' => $name,
  154. ];
  155. } else {
  156. return app('json')->fail(100005);
  157. }
  158. } catch (\Exception $e) {
  159. return app('json')->fail($e->getMessage());
  160. }
  161. return app('json')->success($data);
  162. }
  163. /**
  164. * 删除文件或文件夹
  165. * @return mixed
  166. *
  167. * @date 2022/09/17
  168. * @author yyw
  169. */
  170. public function delFolder()
  171. {
  172. [$path] = $this->request->postMore([
  173. ['path', '']
  174. ], true);
  175. if (empty($path)) {
  176. return app('json')->fail(410087);
  177. }
  178. try {
  179. $this->services->delFolder($path);
  180. } catch (\Exception $e) {
  181. return app('json')->fail($e->getMessage());
  182. }
  183. return app('json')->success(100010);
  184. }
  185. /**
  186. * 文件重命名
  187. * @return mixed
  188. *
  189. * @date 2022/09/28
  190. * @author yyw
  191. */
  192. public function rename()
  193. {
  194. [$newname, $oldname] = $this->request->postMore([
  195. ['newname', ''],
  196. ['oldname', '']
  197. ], true);
  198. if (empty($newname) || empty($oldname)) {
  199. return app('json')->fail(410087);
  200. }
  201. try {
  202. $this->services->rename($newname, $oldname);
  203. } catch (\Exception $e) {
  204. return app('json')->fail($e->getMessage());
  205. }
  206. return app('json')->success(100010);
  207. }
  208. public function copyFolder()
  209. {
  210. [$surDir, $toDir] = $this->request->postMore([
  211. ['surDir', ''],
  212. ['toDir', '']
  213. ], true);
  214. if (empty($surDir) || empty($toDir)) {
  215. return app('json')->fail(410087);
  216. }
  217. try {
  218. return app('json')->success($this->services->copyFolder($surDir, $toDir));
  219. } catch (\Exception $e) {
  220. return app('json')->fail($e->getMessage());
  221. }
  222. }
  223. }