企微短剧业务系统

MaterialController.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Log;
  4. use App\Service\ImportService;
  5. use App\Service\MaterialService;
  6. use Illuminate\Http\Request;
  7. use App\Http\Controllers\Controller;
  8. use Illuminate\Validation\Rule;
  9. class MaterialController extends Controller
  10. {
  11. /**
  12. * 上传素材
  13. * */
  14. public function upload(Request $request)
  15. {
  16. $validator = \Validator::make($request->all(),[
  17. 'corpid' => 'required|string',
  18. 'type' => ['required', Rule::in([1, 2, 3, 4])],
  19. 'need_media_id' => ['nullable', Rule::in([0, 1])],
  20. ]);
  21. if ($validator->fails()) {
  22. return self::returnValue($validator->getMessageBag(), 1102);
  23. }
  24. $file = $request->file('material_file');
  25. $corpid = $request->input('corpid');
  26. $type = $request->input('type');
  27. $needMediaId = $request->input('need_media_id');
  28. $uploadToOss = $request->input('upload_to_oss', 1);
  29. $data = [];
  30. $errno = MaterialService::uploadMaterial($corpid, $type, $file, $needMediaId, $uploadToOss, $data);
  31. return self::returnValue($data, $errno);
  32. }
  33. /**
  34. * 导入excel
  35. * @param Request $request
  36. * @return mixed
  37. */
  38. public function importExcel(Request $request)
  39. {
  40. $validator = \Validator::make($request->all(),[
  41. 'corpid' => 'required|string',
  42. 'file' => 'required|file',
  43. ]);
  44. if ($validator->fails()) {
  45. return self::returnValue($validator->getMessageBag(), 1102);
  46. }
  47. $file = $request->file('file');
  48. $corpid = $request->input('corpid');
  49. $importType = $request->input('import_type', 1);
  50. list($res, $code) = ImportService::importExcel($file, $corpid, $importType);
  51. return self::returnValue($res, $code);
  52. }
  53. public function uploadFileToOss(Request $request)
  54. {
  55. $validator = \Validator::make($request->all(),[
  56. 'type' => ['required', Rule::in([1, 2, 3, 4])],
  57. ]);
  58. if ($validator->fails()) {
  59. return self::returnValue($validator->getMessageBag(), 1102);
  60. }
  61. $file = $request->file('file');
  62. $corpid = $request->input('corpid');
  63. $type = $request->input('type');
  64. $isMaterial = $request->input('is_material', 1);
  65. $data = [];
  66. $errno = MaterialService::uploadFileToOss($corpid, $type, $file, $isMaterial, $data);
  67. return self::returnValue($data, $errno);
  68. }
  69. public function uploadFileByUrl(Request $request)
  70. {
  71. $validator = \Validator::make($request->all(),[
  72. 'corpid' => 'required|string',
  73. 'img_url' => 'required|string',
  74. ]);
  75. if ($validator->fails()) {
  76. return self::returnValue($validator->getMessageBag(), 1102);
  77. }
  78. $corpid = $request->input('corpid');
  79. $imgUrl = $request->input('img_url');
  80. $ossUrl = '';
  81. $errno = MaterialService::uploadFileByUrl($corpid, $imgUrl, $ossUrl);
  82. return self::returnValue(['oss_url' => $ossUrl], $errno);
  83. }
  84. }