Index.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace addons\nkeditor\controller;
  3. use app\common\model\Attachment;
  4. use GuzzleHttp\Client;
  5. use Symfony\Component\HttpFoundation\StreamedResponse;
  6. use think\addons\Controller;
  7. class Index extends Controller
  8. {
  9. public function index()
  10. {
  11. $this->error('该插件暂无前台页面');
  12. }
  13. /**
  14. * 文件列表
  15. */
  16. public function attachment()
  17. {
  18. $model = new Attachment;
  19. $page = $this->request->request('page');
  20. $fileType = $this->request->request('fileType');
  21. $module = $this->request->param('module');
  22. $pagesize = 15;
  23. $config = get_addon_config('nkeditor');
  24. $type = [];
  25. $imageSuffix = ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'webp', 'svg'];
  26. if ($fileType == 'image') {
  27. $type = $imageSuffix;
  28. } elseif ($fileType == 'flash') {
  29. $type = ['swf', 'flv'];
  30. } elseif ($fileType == 'media') {
  31. $type = ['swf', 'flv', 'mp4', 'mpeg', 'mp3', 'wav', 'ogg', 'acc', 'webm'];
  32. } elseif ($fileType == 'file') {
  33. }
  34. if ($module == 'admin') {
  35. $auth = \app\admin\library\Auth::instance();
  36. if (!$auth->id) {
  37. $this->error('请登录后再操作!');
  38. } else {
  39. $mode = $config['attachmentmode_admin'];
  40. }
  41. if ($mode == 'all') {
  42. } else {
  43. if (!$auth->isSuperAdmin()) {
  44. $adminIds = $mode == 'auth' ? $auth->getChildrenAdminIds(true) : [$auth->id];
  45. $model->where('admin_id', 'in', $adminIds);
  46. }
  47. }
  48. } else {
  49. if (!$this->auth->id) {
  50. $this->error('请登录后再操作!');
  51. } else {
  52. $mode = $config['attachmentmode_index'];
  53. }
  54. if ($mode == 'all') {
  55. } else {
  56. $model->where('user_id', 'in', [$this->auth->id]);
  57. }
  58. }
  59. if ($type) {
  60. $model->where('imagetype', 'in', $type);
  61. }
  62. $list = $model
  63. ->order('id', 'desc')
  64. ->paginate($pagesize);
  65. $fullmode = !!($config['fullmode'] ?? '1');
  66. $items = $list->items();
  67. $data = [];
  68. foreach ($items as $k => &$v) {
  69. $v['imagetype'] = strtolower($v['imagetype']);
  70. $v['fullurl'] = cdnurl($v['url'], true);
  71. $thumbUrl = addon_url("nkeditor/index/preview") . "?suffix=" . $v['imagetype'];
  72. $data[] = [
  73. 'width' => $v['imagewidth'],
  74. 'height' => $v['imageheight'],
  75. 'filename' => $v['filename'],
  76. 'filesize' => $v['filesize'],
  77. 'oriURL' => $fullmode ? $v['fullurl'] : cdnurl($v['url']),
  78. 'thumbURL' => $v['fullurl'],
  79. ];
  80. }
  81. $result = [
  82. 'code' => '000',
  83. 'count' => $list->total(),
  84. 'page' => $page,
  85. 'pagesize' => $pagesize,
  86. 'extra' => '',
  87. 'data' => $data
  88. ];
  89. return json($result);
  90. }
  91. /**
  92. * 下载图片
  93. */
  94. public function download()
  95. {
  96. $url = $this->request->request("url");
  97. $contentType = '';
  98. try {
  99. $client = new Client();
  100. $response = $client->request('GET', $url, ['stream' => true, 'verify' => false, 'allow_redirects' => ['strict' => true]]);
  101. $body = $response->getBody();
  102. $contentType = $response->getHeader('Content-Type');
  103. $contentType = $contentType[0] ?? 'image/png';
  104. } catch (\Exception $e) {
  105. $this->error("图片下载失败");
  106. }
  107. $contentTypeArr = explode('/', $contentType);
  108. if ($contentTypeArr[0] !== 'image') {
  109. $this->error("只支持图片文件");
  110. }
  111. $response = new StreamedResponse(function () use ($body) {
  112. while (!$body->eof()) {
  113. echo $body->read(1024);
  114. }
  115. });
  116. $response->headers->set('Content-Type', $contentType);
  117. $response->send();
  118. return;
  119. }
  120. public function get_map_config()
  121. {
  122. $config = get_addon_config('nkeditor');
  123. $params = [
  124. 'baidumapkey' => $config['baidumapkey'] ?? ''
  125. ];
  126. return json($params);
  127. }
  128. }