AttachmentController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: liuyifan
  5. * Date: 2019/2/28
  6. * Time: 10:06
  7. */
  8. namespace app\platform\modules\system\controllers;
  9. use app\common\helpers\Cache;
  10. use app\common\services\qcloud\Conf;
  11. use app\platform\controllers\BaseController;
  12. use app\platform\modules\system\models\Attachment;
  13. use app\platform\modules\system\models\SystemSetting;
  14. class AttachmentController extends BaseController
  15. {
  16. public $remote;
  17. public function __construct()
  18. {
  19. $this->remote = SystemSetting::settingLoad('remote', 'system_remote');
  20. }
  21. /**
  22. * 保存及显示全局设置
  23. *
  24. * @return \Illuminate\Http\JsonResponse
  25. * @throws \app\common\exceptions\AppException
  26. */
  27. public function globals()
  28. {
  29. $post_max_size = ini_get('post_max_size');
  30. $post_max_size = $post_max_size > 0 ? bytecount($post_max_size) / 1024 : 0;
  31. $upload_max_filesize = ini_get('upload_max_filesize');
  32. $global = SystemSetting::settingLoad('global', 'system_global');
  33. $set_data = request()->upload;
  34. if ($set_data) {
  35. $validate = $this->validate($this->rules(''), $set_data, $this->message());
  36. if ($validate) {
  37. return $validate;
  38. }
  39. $attach = Attachment::saveGlobal($set_data, $post_max_size);
  40. if ($attach['result']) {
  41. return $this->successJson('成功');
  42. } else {
  43. return $this->errorJson($attach['msg']);
  44. }
  45. }
  46. $global['thumb_width'] = intval($global['thumb_width']);
  47. if ($global['image_extentions']['0']) {
  48. $global['image_extentions'] = implode("\n", $global['image_extentions']);
  49. }
  50. if ($global['audio_extentions']['0']) {
  51. $global['audio_extentions'] = implode("\n", $global['audio_extentions']);
  52. }
  53. return $this->successJson('成功', [
  54. 'global' => $global,
  55. 'post_max_size' => $post_max_size,
  56. 'upload_max_filesize' => $upload_max_filesize
  57. ]);
  58. }
  59. /**
  60. * 保存及显示远程设置
  61. *
  62. * @return \Illuminate\Http\JsonResponse
  63. * @throws \app\common\exceptions\AppException
  64. */
  65. public function remote()
  66. {
  67. $alioss = request()->alioss;
  68. $cos = request()->cos;
  69. $obs = request()->obs;
  70. if ($alioss || $cos || $obs) {
  71. $validate = false;
  72. if ($alioss['key']) {
  73. $validate = $this->validate($this->rules(1), $alioss, $this->message());
  74. } elseif($cos['key']) {
  75. $validate = $this->validate($this->rules(2), $cos, $this->message());
  76. } elseif ($obs['key']) {
  77. $validate = $this->validate($this->rules(3), $obs, $this->message());
  78. }
  79. if ($validate) {
  80. return $validate;
  81. }
  82. $attach = Attachment::saveRemote($alioss, $cos, $obs, $this->remote);
  83. if ($attach['result']) {
  84. Cache::flush();
  85. return $this->successJson('成功');
  86. } else {
  87. return $this->errorJson($attach['msg']);
  88. }
  89. }
  90. $this->remote['alioss']['internal'] ? $this->remote['alioss']['internal'] = intval($this->remote['alioss']['internal']) : null;
  91. switch($this->remote['cos']['local']) {
  92. case 'ap-nanjing':
  93. $this->remote['cos']['local'] = '南京';
  94. break;
  95. case 'ap-guangzhou':
  96. $this->remote['cos']['local'] = '广州';
  97. break;
  98. case 'ap-chengdu':
  99. $this->remote['cos']['local'] = '成都';
  100. break;
  101. case 'ap-beijing':
  102. $this->remote['cos']['local'] = '北京';
  103. break;
  104. case 'ap-chongqing':
  105. $this->remote['cos']['local'] = '重庆';
  106. break;
  107. case 'ap-shanghai':
  108. $this->remote['cos']['local'] = '上海';
  109. break;
  110. case 'ap-hongkong':
  111. $this->remote['cos']['local'] = '香港';
  112. break;
  113. case 'ap-beijing-fsi':
  114. $this->remote['cos']['local'] = '北京金融';
  115. break;
  116. case 'ap-shanghai-fsi':
  117. $this->remote['cos']['local'] = '上海金融';
  118. break;
  119. case 'ap-shenzhen-fsi':
  120. $this->remote['cos']['local'] = '深圳金融';
  121. break;
  122. }
  123. return $this->successJson('成功', $this->remote);
  124. }
  125. /**
  126. * 验证数据
  127. *
  128. * @param array $rules
  129. * @param \Request|null $request
  130. * @param array $messages
  131. * @param array $customAttributes
  132. * @return \Illuminate\Http\JsonResponse
  133. */
  134. public function validate(array $rules, $request = null, array $messages = [], array $customAttributes = [])
  135. {
  136. if (!isset($request)) {
  137. $request = request();
  138. }
  139. $validator = $this->getValidationFactory()->make($request, $rules, $messages, $customAttributes);
  140. if ($validator->fails()) {
  141. return $this->errorJson('失败', $validator->errors()->all());
  142. }
  143. }
  144. /**
  145. * 配置验证规则
  146. *
  147. * @param $param
  148. * @return array
  149. */
  150. public function rules($param)
  151. {
  152. $rules = [];
  153. if (request()->path() == "admin/system/globals") {
  154. $rules = [
  155. 'image_extentions' => 'required',
  156. 'image_limit' => 'required',
  157. 'audio_extentions' => 'required',
  158. 'audio_limit' => 'required',
  159. ];
  160. }
  161. if ($param == 1) {
  162. $rules = [
  163. 'key' => 'required',
  164. 'secret' => 'required',
  165. ];
  166. } elseif ($param == 2) {
  167. $rules = [
  168. 'appid' => 'required',
  169. 'secretid' => 'required',
  170. 'secretkey' => 'required',
  171. 'bucket' => 'required',
  172. ];
  173. } elseif ($param == 3) {
  174. $rules = [
  175. 'key' => 'required',
  176. 'secret' => 'required',
  177. 'endpoint' => 'required',
  178. 'bucket' => 'required',
  179. ];
  180. }
  181. return $rules;
  182. }
  183. /**
  184. * 自定义错误信息
  185. *
  186. * @return array
  187. */
  188. public function message()
  189. {
  190. return [
  191. 'image_extentions.required' => '图片后缀不能为空.',
  192. 'image_limit.required' => '图片上传大小不能为空.',
  193. 'audio_extentions.required' => '音频视频后缀不能为空.',
  194. 'audio_limit.required' => '音频视频大小不能为空.',
  195. 'key' => '阿里云OSS-Access Key ID不能为空',
  196. 'secret' => '阿里云OSS-Access Key Secret不能为空',
  197. 'appid' => '请填写APPID',
  198. 'secretid' => '请填写SECRETID',
  199. 'secretkey' => '请填写SECRETKEY',
  200. 'bucket' => '请填写BUCKET',
  201. 'endpoint' => '请填写Endpoint',
  202. ];
  203. }
  204. /**
  205. * 阿里云搜索 bucket
  206. *
  207. * @return \Illuminate\Http\JsonResponse
  208. */
  209. public function bucket()
  210. {
  211. $key = request()->key;
  212. $secret = request()->secret;
  213. $buckets = attachment_alioss_buctkets($key, $secret);
  214. if (is_error($buckets)) {
  215. return $this->errorJson($buckets['message']);
  216. }
  217. $bucket_datacenter = attachment_alioss_datacenters();
  218. $bucket = array();
  219. foreach ($buckets as $key => $value) {
  220. $value['loca_name'] = $key. '@@'. $bucket_datacenter[$value['location']];
  221. $value['value'] = $key. '@@'. $value['location'];
  222. $bucket[] = $value;
  223. }
  224. return $this->successJson('成功', $bucket);
  225. }
  226. /**
  227. * 测试阿里云配置是否成功
  228. *
  229. * @return \Illuminate\Http\JsonResponse
  230. */
  231. public function oss()
  232. {
  233. $alioss = request()->alioss;
  234. $secret = strexists($alioss['secret'], '*') ? $this->remote['alioss']['secret'] : $alioss['secret'];
  235. $buckets = attachment_alioss_buctkets($alioss['key'], $secret);
  236. list($bucket, $url) = explode('@@', $alioss['bucket']);
  237. $result = attachment_newalioss_auth($alioss['key'], $secret, $bucket, $alioss['internal']);
  238. if (is_error($result)) {
  239. return $this->errorJson('OSS-Access Key ID 或 OSS-Access Key Secret错误,请重新填写');
  240. }
  241. $ossurl = $buckets[$bucket]['location'].'.aliyuncs.com';
  242. if ($alioss['url']) {
  243. if (!strexists($alioss['url'], 'http://') && !strexists($alioss['url'],'https://')) {
  244. $url = 'http://'. trim($alioss['url']);
  245. } else {
  246. $url = trim($alioss['url']);
  247. }
  248. $url = trim($url, '/').'/';
  249. } else {
  250. $url = 'http://'.$bucket.'.'.$buckets[$bucket]['location'].'.aliyuncs.com/';
  251. }
  252. $filename = 'logo.png';
  253. $response = \Curl::to($url. '/'.$filename)->get();
  254. if (!$response) {
  255. return $this->errorJson('配置失败,阿里云访问url错误');
  256. }
  257. $image = getimagesizefromstring($response);
  258. if ($image && strexists($image['mime'], 'image')) {
  259. return $this->successJson('配置成功', request()->alioss);
  260. } else {
  261. return $this->errorJson('配置失败,阿里云访问url错误');
  262. }
  263. }
  264. /**
  265. * 测试腾讯云配置是否成功
  266. *
  267. * @return \Illuminate\Http\JsonResponse
  268. */
  269. public function cos()
  270. {
  271. $cos = request()->cos;
  272. switch($cos['local']) {
  273. case '南京':
  274. $cos['local'] = 'ap-nanjing';
  275. break;
  276. case '成都':
  277. $cos['local'] = 'ap-chengdu';
  278. break;
  279. case '北京':
  280. $cos['local'] = 'ap-beijing';
  281. break;
  282. case '广州':
  283. $cos['local'] = 'ap-guangzhou';
  284. break;
  285. case '上海':
  286. $cos['local'] = 'ap-shanghai';
  287. break;
  288. case '重庆':
  289. $cos['local'] = 'ap-chongqing';
  290. break;
  291. case '北京金融':
  292. $cos['local'] = 'ap-beijing-fsi';
  293. break;
  294. case '上海金融':
  295. $cos['local'] = 'ap-shanghai-fsi';
  296. break;
  297. case '深圳金融':
  298. $cos['local'] = 'ap-shenzhen-fsi';
  299. break;
  300. case '香港':
  301. $cos['local'] = 'ap-hongkong';
  302. break;
  303. }
  304. $secretkey = strexists($cos['secretkey'], '*') ? $this->remote['cos']['secretkey'] : trim($cos['secretkey']);
  305. $bucket = str_replace("-{$cos['appid']}", '', trim($cos['bucket']));
  306. if (!$cos['url']) {
  307. $cos['url'] = sprintf('https://%s-%s.cos.%s.myqcloud.com', $bucket, $cos['appid'], $cos['local']);
  308. }
  309. $cos['url'] = rtrim($cos['url'], '/');
  310. Conf::$appid = $cos['appid'];
  311. Conf::$secretid = $cos['secretid'];
  312. Conf::$tsecretkey = $cos['secretkey'];
  313. $auth = attachment_cos_auth($bucket, $cos['appid'], $cos['secretid'], $secretkey, $cos['local']);
  314. if (is_error($auth)) {
  315. return $this->errorJson('配置失败,请检查配置' . $auth['message']);
  316. }
  317. $filename = 'logo.png';
  318. $response = \Curl::to($cos['url']. '/'. $filename)->get();
  319. if (!$response) {
  320. return $this->errorJson('配置失败,腾讯cos访问url错误');
  321. }
  322. $image = getimagesizefromstring($response);
  323. if ($image && strexists($image['mime'], 'image')) {
  324. return $this->successJson('配置成功', request()->cos);
  325. } else {
  326. return $this->errorJson('配置失败,腾讯cos访问url错误');
  327. }
  328. }
  329. /**
  330. * 测试腾讯云配置是否成功
  331. *
  332. * @return \Illuminate\Http\JsonResponse
  333. */
  334. public function obs()
  335. {
  336. $obs = request()->obs;
  337. $key = trim($obs['key']);
  338. $secret = strexists($obs['secret'], '*') ? $this->remote['cos']['secret'] : trim($obs['secret']);
  339. $bucket = trim($obs['bucket']);
  340. $endpoint = trim($obs['endpoint']);
  341. if (!$obs['url']) {
  342. $obs['url'] = sprintf('https://%s.%s', $obs['bucket'], $obs['endpoint']);
  343. }
  344. $obs['url'] = rtrim($obs['url'], '/');
  345. $auth = attachment_obs_auth($key, $secret, $endpoint, $bucket);
  346. if (is_error($auth)) {
  347. return $this->errorJson('配置失败,请检查配置' . $auth['message']);
  348. }
  349. $filename = '/static/logo.png';
  350. $response = \Curl::to($obs['url']. '/'. $filename)->get();
  351. if (!$response) {
  352. return $this->errorJson('配置失败,华为云obs访问url错误');
  353. }
  354. $image = getimagesizefromstring($response);
  355. if ($image && strexists($image['mime'], 'image')) {
  356. return $this->successJson('配置成功', request()->obs);
  357. } else {
  358. return $this->errorJson('配置失败,华为云obs访问url错误');
  359. }
  360. }
  361. public function sms()
  362. {
  363. $type = request()->type;
  364. if (request()->input()) {
  365. $data = request()->input();
  366. if ($data) {
  367. $res = SystemSetting::settingSave($data, 'sms', 'system_sms');
  368. if ($res) {
  369. Cache::flush();//清除缓存
  370. return $this->successJson('短信设置成功');
  371. } else {
  372. return $this->errorJson('短信设置失败');
  373. }
  374. }
  375. }
  376. }
  377. }