UEditorController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. namespace common\widgets\ueditor;
  3. use Exception;
  4. use Yii;
  5. use yii\helpers\Json;
  6. use yii\web\Controller;
  7. use yii\web\Response;
  8. use yii\filters\AccessControl;
  9. use yii\base\InvalidConfigException;
  10. use common\helpers\ArrayHelper;
  11. use common\helpers\UploadHelper;
  12. use common\enums\AttachmentUploadTypeEnum;
  13. use League\Flysystem\Adapter\Local;
  14. use League\Flysystem\Filesystem;
  15. /**
  16. * 百度编辑器
  17. *
  18. * Class UEditorController
  19. * @package common\widgets\ueditor
  20. * @author jianyan74 <751393839@qq.com>
  21. */
  22. class UEditorController extends Controller
  23. {
  24. /**
  25. * @var bool
  26. */
  27. public $enableCsrfValidation = false;
  28. /**
  29. * @var array
  30. */
  31. public $config = [];
  32. /**
  33. * 显示驱动
  34. *
  35. * 有Attachment、WechatAttachment、Local
  36. * @var string
  37. */
  38. public $showDrive = 'Attachment';
  39. /**
  40. * @var array
  41. */
  42. public $actions = [
  43. 'uploadimage' => 'image',
  44. 'uploadscrawl' => 'scrawl',
  45. 'uploadvideo' => 'video',
  46. 'uploadfile' => 'file',
  47. 'listimage' => 'list-image',
  48. 'listfile' => 'list-file',
  49. 'catchimage' => 'catch-image',
  50. 'config' => 'config',
  51. 'listinfo' => 'list-info',
  52. ];
  53. /**
  54. * @var int
  55. */
  56. protected $fileStart;
  57. /**
  58. * @var int
  59. */
  60. protected $fileEnd;
  61. /**
  62. * @var int
  63. */
  64. protected $fileNum = 0;
  65. /**
  66. * @var Local
  67. */
  68. protected $filesystem;
  69. /**
  70. * 行为控制
  71. */
  72. public function behaviors()
  73. {
  74. return [
  75. 'access' => [
  76. 'class' => AccessControl::class,
  77. 'rules' => [
  78. [
  79. 'allow' => true,
  80. 'roles' => ['@'],// 登录
  81. ],
  82. ],
  83. ],
  84. ];
  85. }
  86. /**
  87. * @throws InvalidConfigException
  88. */
  89. public function init()
  90. {
  91. parent::init();
  92. $this->config = [
  93. // server config @see http://fex-team.github.io/ueditor/#server-config
  94. 'scrawlMaxSize' => Yii::$app->params['uploadConfig']['images']['maxSize'],
  95. 'videoMaxSize' => Yii::$app->params['uploadConfig']['videos']['maxSize'],
  96. 'imageMaxSize' => Yii::$app->params['uploadConfig']['images']['maxSize'],
  97. 'fileMaxSize' => Yii::$app->params['uploadConfig']['files']['maxSize'],
  98. 'imageManagerListPath' => Yii::$app->params['uploadConfig']['images']['path'],
  99. 'fileManagerListPath' => Yii::$app->params['uploadConfig']['files']['path'],
  100. 'scrawlFieldName' => 'image',
  101. 'videoFieldName' => 'file',
  102. 'fileFieldName' => 'file',
  103. 'imageFieldName' => 'file',
  104. ];
  105. $configPath = Yii::getAlias('@common')."/widgets/ueditor/";
  106. // 保留UE默认的配置引入方式
  107. if (file_exists($configPath.'config.json')) {
  108. $config = Json::decode(preg_replace("/\/\*[\s\S]+?\*\//", '',
  109. file_get_contents($configPath.'config.json')));
  110. $this->config = ArrayHelper::merge($config, $this->config);
  111. }
  112. // 设置显示驱动
  113. $showDrive = Yii::$app->request->get('showDrive');
  114. if (!empty($showDrive) && in_array($showDrive, ['Attachment', 'WechatAttachment', 'Local'])) {
  115. $this->showDrive = $showDrive;
  116. }
  117. }
  118. /**
  119. * 后台统一入口
  120. *
  121. * @return array|mixed
  122. */
  123. public function actionIndex()
  124. {
  125. Yii::$app->response->format = Response::FORMAT_JSON;
  126. $action = strtolower(Yii::$app->request->get('action', 'config'));
  127. $actions = $this->actions;
  128. if (isset($actions[$action])) {
  129. return $this->run($actions[$action]);
  130. }
  131. return $this->result('找不到方法');
  132. }
  133. /**
  134. * 显示配置信息
  135. */
  136. public function actionConfig()
  137. {
  138. return $this->config;
  139. }
  140. /**
  141. * 上传图片
  142. *
  143. * @return array
  144. */
  145. public function actionImage()
  146. {
  147. try {
  148. $data = Yii::$app->request->get();
  149. $upload = Yii::$app->services->extendUpload->saveFile($data, AttachmentUploadTypeEnum::IMAGES);
  150. $baseInfo = $upload->getInfo();
  151. return $this->result('SUCCESS', $baseInfo['url']);
  152. } catch (Exception $e) {
  153. return $this->result($e->getMessage());
  154. }
  155. }
  156. /**
  157. * 上传涂鸦
  158. *
  159. * @return array
  160. */
  161. public function actionScrawl()
  162. {
  163. try {
  164. // 保存扩展名称
  165. $data = [
  166. 'fileData' => base64_decode(Yii::$app->request->post('image', '')),
  167. 'extend' => Yii::$app->request->post('extend', 'jpg'),
  168. ];
  169. $upload = Yii::$app->services->extendUpload->saveFile($data, AttachmentUploadTypeEnum::IMAGES, 'base64');
  170. $baseInfo = $upload->getInfo();
  171. return $this->result('SUCCESS', $baseInfo['url']);
  172. } catch (Exception $e) {
  173. return $this->result($e->getMessage());
  174. }
  175. }
  176. /**
  177. * 上传视频
  178. *
  179. * @return array
  180. */
  181. public function actionVideo()
  182. {
  183. try {
  184. $data = Yii::$app->request->get();
  185. $upload = Yii::$app->services->extendUpload->saveFile($data, AttachmentUploadTypeEnum::VIDEOS);
  186. $baseInfo = $upload->getInfo();
  187. $url = $baseInfo['url'];
  188. if ($upload->poster == true) {
  189. $newUpload = UploadHelper::videoPoster($upload);
  190. $baseInfo = !empty($newUpload) ? $newUpload->getInfo() : [];
  191. $posterUrl = $baseInfo['url'] ?? '';
  192. } else {
  193. $posterUrl = '';
  194. }
  195. return [
  196. 'state' => 'SUCCESS',
  197. 'url' => $url,
  198. 'posterUrl' => $posterUrl,
  199. ];
  200. } catch (Exception $e) {
  201. return $this->result($e->getMessage());
  202. }
  203. }
  204. /**
  205. * 上传文件
  206. */
  207. public function actionFile()
  208. {
  209. try {
  210. $data = Yii::$app->request->get();
  211. $upload = Yii::$app->services->extendUpload->saveFile($data, AttachmentUploadTypeEnum::FILES);
  212. $baseInfo = $upload->getInfo();
  213. return $this->result('SUCCESS', $baseInfo['url']);
  214. } catch (Exception $e) {
  215. return $this->result($e->getMessage());
  216. }
  217. }
  218. /**
  219. * 获取远程图片
  220. *
  221. * @return array
  222. * @throws Exception
  223. */
  224. public function actionCatchImage()
  225. {
  226. /* 上传配置 */
  227. $source = Yii::$app->request->post('source', []);
  228. $data = Yii::$app->request->get();
  229. foreach ($source as $imgUrl) {
  230. try {
  231. $data['fileData'] = $imgUrl;
  232. $upload = Yii::$app->services->extendUpload->saveFile($data, AttachmentUploadTypeEnum::IMAGES, 'url');
  233. if ($file = Yii::$app->services->attachment->findByMd5($upload->md5)) {
  234. $url = $file['url'];
  235. } else {
  236. $baseInfo = $upload->getInfo();
  237. $url = $baseInfo['url'];
  238. }
  239. $list[] = [
  240. 'state' => 'SUCCESS',
  241. 'url' => $url,
  242. 'source' => $imgUrl,
  243. ];
  244. } catch (Exception $e) {
  245. $list[] = [
  246. 'state' => $e->getMessage(),
  247. 'url' => '',
  248. 'source' => $imgUrl,
  249. ];
  250. }
  251. }
  252. /* 返回抓取数据 */
  253. return [
  254. 'state' => count($list) ? 'SUCCESS' : 'ERROR',
  255. 'list' => $list,
  256. ];
  257. }
  258. /**
  259. * 文件列表
  260. *
  261. * @return array
  262. */
  263. public function actionListFile()
  264. {
  265. $prefix = Yii::$app->params['uploadConfig']['files']['fullPath'] == true ? Yii::$app->request->hostInfo : '';
  266. $action = 'get'.$this->showDrive;
  267. return $this->$action(
  268. $this->config['fileManagerListSize'],
  269. $this->config['fileManagerListPath'],
  270. $prefix
  271. );
  272. }
  273. /**
  274. * 图片列表
  275. *
  276. * @return array
  277. */
  278. public function actionListImage()
  279. {
  280. $prefix = Yii::$app->params['uploadConfig']['images']['fullPath'] == true ? Yii::$app->request->hostInfo : '';
  281. $action = 'get'.$this->showDrive;
  282. return $this->$action(
  283. $this->config['imageManagerListSize'],
  284. $this->config['imageManagerListPath'],
  285. $prefix
  286. );
  287. }
  288. /**
  289. * 获取数据库资源文件列表
  290. *
  291. * @param $size
  292. * @param $path
  293. * @return array
  294. */
  295. public function getAttachment($size, $path)
  296. {
  297. $start = Yii::$app->request->get('start');
  298. $uploadType = $path == $this->config['imageManagerListPath'] ? AttachmentUploadTypeEnum::IMAGES : AttachmentUploadTypeEnum::FILES;
  299. list($files, $total) = Yii::$app->services->attachment->baiduListPage($uploadType, $start, $size);
  300. return [
  301. 'state' => 'SUCCESS',
  302. 'list' => $files,
  303. 'start' => $start,
  304. 'total' => $total,
  305. ];
  306. }
  307. /**
  308. * 文件和图片管理action使用
  309. *
  310. * @param $allowFiles
  311. * @param $listSize
  312. * @param $path
  313. * @return array
  314. */
  315. protected function getLocal($listSize, $path, $prefix)
  316. {
  317. /* 获取参数 */
  318. $size = Yii::$app->request->get('size', $listSize);
  319. $this->fileStart = Yii::$app->request->get('start', 0);
  320. $this->fileEnd = $this->fileStart + $size;
  321. $files = $this->getLocalFiles($path, $prefix);
  322. return [
  323. 'state' => 'SUCCESS',
  324. 'list' => $files,
  325. 'start' => $this->fileStart,
  326. 'total' => $this->fileNum,
  327. ];
  328. }
  329. /**
  330. * @param string $path 文件路径
  331. * @param string $allowFiles 文件后缀
  332. * @param array $files 文件列表
  333. * @param string $prefix 前缀
  334. * @return array
  335. */
  336. public function getLocalFiles($path, $prefix, &$files = [])
  337. {
  338. if (!$this->filesystem) {
  339. $adapter = new Local(Yii::getAlias('@attachment'));
  340. $this->filesystem = new Filesystem($adapter);
  341. }
  342. $listFiles = $this->filesystem->listContents($path);
  343. foreach ($listFiles as $key => $listFile) {
  344. if ($listFile['type'] == 'dir') {
  345. $this->getLocalFiles($listFile['path'], $prefix, $files);
  346. } else {
  347. // 获取选中列表
  348. if ($this->fileNum >= $this->fileStart && $this->fileNum < $this->fileEnd) {
  349. $url = $prefix.Yii::getAlias('@attachurl').'/'.$listFile['path'];
  350. $files[] = [
  351. 'url' => $url,
  352. 'mtime' => $listFile['timestamp'],
  353. ];
  354. }
  355. $this->fileNum++;
  356. }
  357. unset($listFiles[$key]);
  358. }
  359. return $files;
  360. }
  361. /**
  362. * 获取当前上传成功文件的各项信息
  363. * @return array
  364. */
  365. protected function result($state = 'ERROR', $url = '')
  366. {
  367. return [
  368. "state" => $state,
  369. "url" => $url,
  370. ];
  371. }
  372. }
粤ICP备19079148号