Ajax.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | EasyAdmin
  4. // +----------------------------------------------------------------------
  5. // | PHP交流群: 763822524
  6. // +----------------------------------------------------------------------
  7. // | 开源协议 https://mit-license.org
  8. // +----------------------------------------------------------------------
  9. // | github开源项目:https://github.com/zhongshaofa/EasyAdmin
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller;
  12. use app\common\model\SystemUploadfile;
  13. use app\common\controller\AdminController;
  14. use app\common\service\MenuService;
  15. use EasyAdmin\upload\Uploadfile;
  16. use services\CacheServices;
  17. use think\db\Query;
  18. use think\facade\Cache;
  19. use utils\RedisCache;
  20. class Ajax extends AdminController
  21. {
  22. /**
  23. * 初始化后台接口地址
  24. * @return \think\response\Json
  25. * @throws \think\db\exception\DataNotFoundException
  26. * @throws \think\db\exception\DbException
  27. * @throws \think\db\exception\ModelNotFoundException
  28. */
  29. public function initAdmin()
  30. {
  31. $cacheData = Cache::get('initAdmin_' . session('admin.id'));
  32. if (!empty($cacheData)) {
  33. return json($cacheData);
  34. }
  35. $menuService = new MenuService(session('admin.id'));
  36. $data = [
  37. 'logoInfo' => [
  38. 'title' => sysconfig('site', 'logo_title'),
  39. 'image' => sysconfig('site', 'logo_image'),
  40. 'href' => __url('index/index'),
  41. ],
  42. 'homeInfo' => $menuService->getHomeInfo(),
  43. 'menuInfo' => $menuService->getMenuTree(),
  44. ];
  45. Cache::tag('initAdmin')->set('initAdmin_' . session('admin.id'), $data);
  46. return json($data);
  47. }
  48. /**
  49. * 清理缓存接口
  50. */
  51. public function clearCache()
  52. {
  53. CacheServices::clear();
  54. RedisCache::keyDel("caches*");
  55. Cache::clear();
  56. $this->success('清理缓存成功');
  57. }
  58. /**
  59. * 上传文件
  60. */
  61. public function upload()
  62. {
  63. $this->checkPostRequest();
  64. $data = [
  65. 'upload_type' => $this->request->post('upload_type'),
  66. 'file' => $this->request->file('file'),
  67. ];
  68. $uploadConfig = sysconfig('upload');
  69. empty($data['upload_type']) && $data['upload_type'] = $uploadConfig['upload_type'];
  70. $rule = [
  71. 'upload_type|指定上传类型有误' => "in:{$uploadConfig['upload_allow_type']}",
  72. 'file|文件' => "require|file|fileExt:{$uploadConfig['upload_allow_ext']}|fileSize:{$uploadConfig['upload_allow_size']}",
  73. ];
  74. $this->validate($data, $rule);
  75. try {
  76. $upload = Uploadfile::instance()
  77. ->setUploadType($data['upload_type'])
  78. ->setUploadConfig($uploadConfig)
  79. ->setFile($data['file'])
  80. ->save();
  81. } catch (\Exception $e) {
  82. $this->error($e->getMessage() . $e->getLine(). $e->getFile());
  83. }
  84. if ($upload['save'] == true) {
  85. $this->success($upload['msg'], ['url' => $upload['url']]);
  86. } else {
  87. $this->error($upload['msg']);
  88. }
  89. }
  90. /**
  91. * 上传图片至编辑器
  92. * @return \think\response\Json
  93. */
  94. public function uploadEditor()
  95. {
  96. $this->checkPostRequest();
  97. $data = [
  98. 'upload_type' => $this->request->post('upload_type'),
  99. 'file' => $this->request->file('upload'),
  100. ];
  101. $uploadConfig = sysconfig('upload');
  102. empty($data['upload_type']) && $data['upload_type'] = $uploadConfig['upload_type'];
  103. $rule = [
  104. 'upload_type|指定上传类型有误' => "in:{$uploadConfig['upload_allow_type']}",
  105. 'file|文件' => "require|file|fileExt:{$uploadConfig['upload_allow_ext']}|fileSize:{$uploadConfig['upload_allow_size']}",
  106. ];
  107. $this->validate($data, $rule);
  108. try {
  109. $upload = Uploadfile::instance()
  110. ->setUploadType($data['upload_type'])
  111. ->setUploadConfig($uploadConfig)
  112. ->setFile($data['file'])
  113. ->save();
  114. } catch (\Exception $e) {
  115. $this->error($e->getMessage());
  116. }
  117. if ($upload['save'] == true) {
  118. return json([
  119. 'error' => [
  120. 'message' => '上传成功',
  121. 'number' => 201,
  122. ],
  123. 'fileName' => '',
  124. 'uploaded' => 1,
  125. 'url' => $upload['url'],
  126. ]);
  127. } else {
  128. $this->error($upload['msg']);
  129. }
  130. }
  131. /**
  132. * 获取上传文件列表
  133. * @return \think\response\Json
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\DbException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. */
  138. public function getUploadFiles()
  139. {
  140. $get = $this->request->get();
  141. $page = isset($get['page']) && !empty($get['page']) ? $get['page'] : 1;
  142. $limit = isset($get['limit']) && !empty($get['limit']) ? $get['limit'] : 10;
  143. $title = isset($get['title']) && !empty($get['title']) ? $get['title'] : null;
  144. $this->model = new SystemUploadfile();
  145. $count = $this->model
  146. ->where(function (Query $query) use ($title) {
  147. !empty($title) && $query->where('original_name', 'like', "%{$title}%");
  148. })
  149. ->count();
  150. $list = $this->model
  151. ->where(function (Query $query) use ($title) {
  152. !empty($title) && $query->where('original_name', 'like', "%{$title}%");
  153. })
  154. ->page($page, $limit)
  155. ->order($this->sort)
  156. ->select();
  157. $data = [
  158. 'code' => 0,
  159. 'msg' => '',
  160. 'count' => $count,
  161. 'data' => $list,
  162. ];
  163. return json($data);
  164. }
  165. }