Ajax.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\SystemUploadfile;
  4. use app\admin\service\UploadService;
  5. use app\common\controller\AdminController;
  6. use app\common\service\MenuService;
  7. use app\Request;
  8. use think\db\exception\DataNotFoundException;
  9. use think\db\exception\DbException;
  10. use think\db\exception\ModelNotFoundException;
  11. use think\db\Query;
  12. use think\facade\Cache;
  13. use think\response\Json;
  14. class Ajax extends AdminController
  15. {
  16. /**
  17. * 初始化后台接口地址
  18. * @return Json
  19. * @throws DataNotFoundException
  20. * @throws DbException
  21. * @throws ModelNotFoundException
  22. */
  23. public function initAdmin(): Json
  24. {
  25. $cacheData = Cache::get('initAdmin_' . $this->adminUid);
  26. if (!empty($cacheData)) {
  27. return json($cacheData);
  28. }
  29. $menuService = new MenuService($this->adminUid);
  30. $data = [
  31. 'logoInfo' => [
  32. 'title' => sysConfig('site', 'logo_title'),
  33. 'image' => sysConfig('site', 'logo_image'),
  34. 'href' => __url('index/index'),
  35. ],
  36. 'homeInfo' => $menuService->getHomeInfo(),
  37. 'menuInfo' => $menuService->getMenuTree(),
  38. ];
  39. Cache::tag('initAdmin')->set('initAdmin_' . $this->adminUid, $data);
  40. return json($data);
  41. }
  42. /**
  43. * 清理缓存接口
  44. */
  45. public function clearCache(): void
  46. {
  47. Cache::clear();
  48. $this->success('清理缓存成功');
  49. }
  50. /**
  51. * 上传文件
  52. * @param Request $request
  53. * @return Json|null
  54. */
  55. public function upload(Request $request): Json|null
  56. {
  57. $this->isDemo && $this->error('演示环境下不允许修改');
  58. $this->checkPostRequest();
  59. $type = $request->param('type', '');
  60. $data = [
  61. 'upload_type' => $request->post('upload_type'),
  62. 'file' => $request->file($type == 'editor' ? 'upload' : 'file'),
  63. ];
  64. $uploadConfig = sysConfig('upload');
  65. empty($data['upload_type']) && $data['upload_type'] = $uploadConfig['upload_type'];
  66. $rule = [
  67. 'upload_type|指定上传类型有误' => "in:{$uploadConfig['upload_allow_type']}",
  68. 'file|文件' => "require|file|fileExt:{$uploadConfig['upload_allow_ext']}|fileSize:{$uploadConfig['upload_allow_size']}",
  69. ];
  70. $this->validate($data, $rule);
  71. $upload_type = $data['upload_type'];
  72. try {
  73. $upload = UploadService::instance()->setConfig($uploadConfig)->$upload_type($data['file'], $type);
  74. }catch (\Exception $e) {
  75. $this->error($e->getMessage());
  76. }
  77. $code = $upload['code'] ?? 0;
  78. if ($code == 0) {
  79. $this->error($upload['data'] ?? '');
  80. }else {
  81. if ($type == 'editor') {
  82. return json(
  83. [
  84. 'error' => ['message' => '上传成功', 'number' => 201,],
  85. 'fileName' => '',
  86. 'uploaded' => 1,
  87. 'url' => $upload['data']['url'] ?? '',
  88. ]
  89. );
  90. }else {
  91. $this->success('上传成功', $upload['data'] ?? '');
  92. }
  93. }
  94. }
  95. /**
  96. * 获取上传文件列表
  97. * @param Request $request
  98. * @return Json
  99. * @throws DataNotFoundException
  100. * @throws DbException
  101. * @throws ModelNotFoundException
  102. */
  103. public function getUploadFiles(Request $request): Json
  104. {
  105. $get = $request->get();
  106. $page = !empty($get['page']) ? $get['page'] : 1;
  107. $limit = !empty($get['limit']) ? $get['limit'] : 10;
  108. $title = !empty($get['title']) ? $get['title'] : null;
  109. $count = SystemUploadfile::where(function(Query $query) use ($title) {
  110. !empty($title) && $query->where('original_name', 'like', "%{$title}%");
  111. })
  112. ->count();
  113. $list = SystemUploadfile::where(function(Query $query) use ($title) {
  114. !empty($title) && $query->where('original_name', 'like', "%{$title}%");
  115. })
  116. ->page($page, $limit)
  117. ->order($this->sort)
  118. ->select()->toArray();
  119. $data = [
  120. 'code' => 0,
  121. 'msg' => '',
  122. 'count' => $count,
  123. 'data' => $list,
  124. ];
  125. return json($data);
  126. }
  127. /**
  128. * 百度编辑器上传
  129. * @param Request $request
  130. * @return Json
  131. * @throws DataNotFoundException
  132. * @throws DbException
  133. * @throws ModelNotFoundException
  134. */
  135. public function uploadUEditor(Request $request): Json
  136. {
  137. $uploadConfig = sysConfig('upload');
  138. $upload_allow_size = $uploadConfig['upload_allow_size'];
  139. $_upload_allow_ext = explode(',', $uploadConfig['upload_allow_ext']);
  140. $upload_allow_ext = [];
  141. array_map(function($value) use (&$upload_allow_ext) {
  142. $upload_allow_ext[] = '.' . $value;
  143. }, $_upload_allow_ext);
  144. $config = [
  145. // 上传图片配置项
  146. "imageActionName" => "image",
  147. "imageFieldName" => "file",
  148. "imageMaxSize" => $upload_allow_size,
  149. "imageAllowFiles" => $upload_allow_ext,
  150. "imageCompressEnable" => true,
  151. "imageCompressBorder" => 5000,
  152. "imageInsertAlign" => "none",
  153. "imageUrlPrefix" => "",
  154. // 列出图片
  155. "imageManagerActionName" => "listImage",
  156. "imageManagerListSize" => 20,
  157. "imageManagerUrlPrefix" => "",
  158. "imageManagerInsertAlign" => "none",
  159. "imageManagerAllowFiles" => $upload_allow_ext,
  160. // 上传 video
  161. "videoActionName" => "video",
  162. "videoFieldName" => "file",
  163. "videoUrlPrefix" => "",
  164. "videoMaxSize" => $upload_allow_size,
  165. "videoAllowFiles" => $upload_allow_ext,
  166. // 上传 附件
  167. "fileActionName" => "attachment",
  168. "fileFieldName" => "file",
  169. "fileMaxSize" => $upload_allow_size,
  170. "fileAllowFiles" => $upload_allow_ext,
  171. ];
  172. $action = $request->param('action/s', '');
  173. $file = $request->file('file');
  174. $upload_type = $uploadConfig['upload_type'];
  175. switch ($action) {
  176. case 'image':
  177. case 'attachment':
  178. case 'video':
  179. if ($this->isDemo) return json(['state' => '演示环境下不允许修改']);
  180. try {
  181. $upload = UploadService::instance()->setConfig($uploadConfig)->$upload_type($file);
  182. $code = $upload['code'] ?? 0;
  183. if ($code == 0) {
  184. return json(['state' => $upload['data'] ?? '上传错误信息']);
  185. }else {
  186. return json(['state' => 'SUCCESS', 'url' => $upload['data']['url'] ?? '']);
  187. }
  188. }catch (\Exception $e) {
  189. $this->error($e->getMessage());
  190. }
  191. break;
  192. case 'listImage':
  193. $list = (new SystemUploadfile())->order($this->sort)->limit(100)->field('url')->select()->toArray();
  194. $result = [
  195. "state" => "SUCCESS",
  196. "list" => $list,
  197. "total" => 0,
  198. "start" => 0,
  199. ];
  200. return json($result);
  201. default:
  202. return json($config);
  203. }
  204. }
  205. public function composerInfo(): Json
  206. {
  207. $lockFilePath = root_path() . '/composer.lock';
  208. $list = [];
  209. if (file_exists($lockFilePath)) {
  210. $lockFileContent = file_get_contents($lockFilePath);
  211. if ($lockFileContent !== false) {
  212. $lockData = json_decode($lockFileContent, true);
  213. if (!empty($lockData['packages'])) {
  214. foreach ($lockData['packages'] as $package) {
  215. $list[] = ['name' => $package['name'], 'version' => $package['version']];
  216. }
  217. }
  218. }
  219. }
  220. $this->success('success', $list);
  221. }
  222. }