Ajax.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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:config*");
  55. RedisCache::keyDel("caches:sysconfig*");
  56. RedisCache::keyDel("caches:articles*");
  57. RedisCache::keyDel("caches:banners*");
  58. RedisCache::keyDel("caches:goods*");
  59. // 其他临时缓存
  60. RedisCache::keyDel("caches:tem*");
  61. Cache::clear();
  62. $this->success('清理缓存成功');
  63. }
  64. /**
  65. * 上传文件
  66. */
  67. public function upload()
  68. {
  69. $this->checkPostRequest();
  70. $data = [
  71. 'upload_type' => $this->request->post('upload_type'),
  72. 'file' => $this->request->file('file'),
  73. ];
  74. $uploadConfig = sysconfig('upload');
  75. empty($data['upload_type']) && $data['upload_type'] = $uploadConfig['upload_type'];
  76. $rule = [
  77. 'upload_type|指定上传类型有误' => "in:{$uploadConfig['upload_allow_type']}",
  78. 'file|文件' => "require|file|fileExt:{$uploadConfig['upload_allow_ext']}|fileSize:{$uploadConfig['upload_allow_size']}",
  79. ];
  80. $this->validate($data, $rule);
  81. try {
  82. $upload = Uploadfile::instance()
  83. ->setUploadType($data['upload_type'])
  84. ->setUploadConfig($uploadConfig)
  85. ->setFile($data['file'])
  86. ->save();
  87. } catch (\Exception $e) {
  88. $this->error($e->getMessage() . $e->getLine(). $e->getFile());
  89. }
  90. if ($upload['save'] == true) {
  91. $this->success($upload['msg'], ['url' => $upload['url'],'path'=> getUploadPath($upload['url'])]);
  92. } else {
  93. $this->error($upload['msg']);
  94. }
  95. }
  96. /**
  97. * 上传图片至编辑器
  98. * @return \think\response\Json
  99. */
  100. public function uploadEditor()
  101. {
  102. $this->checkPostRequest();
  103. $data = [
  104. 'upload_type' => $this->request->post('upload_type'),
  105. 'file' => $this->request->file('upload'),
  106. ];
  107. $uploadConfig = sysconfig('upload');
  108. empty($data['upload_type']) && $data['upload_type'] = $uploadConfig['upload_type'];
  109. $rule = [
  110. 'upload_type|指定上传类型有误' => "in:{$uploadConfig['upload_allow_type']}",
  111. 'file|文件' => "require|file|fileExt:{$uploadConfig['upload_allow_ext']}|fileSize:{$uploadConfig['upload_allow_size']}",
  112. ];
  113. $this->validate($data, $rule);
  114. try {
  115. $upload = Uploadfile::instance()
  116. ->setUploadType($data['upload_type'])
  117. ->setUploadConfig($uploadConfig)
  118. ->setFile($data['file'])
  119. ->save();
  120. } catch (\Exception $e) {
  121. $this->error($e->getMessage());
  122. }
  123. if ($upload['save'] == true) {
  124. return json([
  125. 'error' => [
  126. 'message' => '上传成功',
  127. 'number' => 201,
  128. ],
  129. 'fileName' => '',
  130. 'uploaded' => 1,
  131. 'url' => $upload['url'],
  132. ]);
  133. } else {
  134. $this->error($upload['msg']);
  135. }
  136. }
  137. /**
  138. * 获取上传文件列表
  139. * @return \think\response\Json
  140. * @throws \think\db\exception\DataNotFoundException
  141. * @throws \think\db\exception\DbException
  142. * @throws \think\db\exception\ModelNotFoundException
  143. */
  144. public function getUploadFiles()
  145. {
  146. $get = $this->request->get();
  147. $page = isset($get['page']) && !empty($get['page']) ? $get['page'] : 1;
  148. $limit = isset($get['limit']) && !empty($get['limit']) ? $get['limit'] : 10;
  149. $title = isset($get['title']) && !empty($get['title']) ? $get['title'] : null;
  150. $this->model = new SystemUploadfile();
  151. $count = $this->model
  152. ->where(function (Query $query) use ($title) {
  153. !empty($title) && $query->where('original_name', 'like', "%{$title}%");
  154. })
  155. ->count();
  156. $list = $this->model
  157. ->where(function (Query $query) use ($title) {
  158. !empty($title) && $query->where('original_name', 'like', "%{$title}%");
  159. })
  160. ->page($page, $limit)
  161. ->order($this->sort)
  162. ->select();
  163. $data = [
  164. 'code' => 0,
  165. 'msg' => '',
  166. 'count' => $count,
  167. 'data' => $list,
  168. ];
  169. return json($data);
  170. }
  171. }