企微短剧业务系统

MediaService.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace App\Service;
  3. use App\Error;
  4. use App\Log;
  5. use App\Models\Material;
  6. use App\Models\Media;
  7. use App\Models\MediaGroup;
  8. use Illuminate\Support\Facades\DB;
  9. class MediaService
  10. {
  11. /**
  12. * 创建/编辑 素材组
  13. * */
  14. public static function editGroup($groupId, $groupName, $sysGroupId, $adminId)
  15. {
  16. # 创建/编辑 素材组
  17. $errno = MediaGroup::editGroup($groupId, $groupName, $sysGroupId, $adminId);
  18. return [$groupId, $errno];
  19. }
  20. /**
  21. * 删除素材组
  22. * */
  23. public static function delGroup($groupId, $sysGroupId, $adminId)
  24. {
  25. try {
  26. DB::beginTransaction();
  27. # 判断素材组是否存在
  28. $groupModel = MediaGroup::where('id', $groupId)->where('sys_group_id', $sysGroupId)
  29. ->where('enable', 1)->first();
  30. if(empty($groupModel)) {
  31. DB::rollBack();
  32. return 2410;
  33. }
  34. # 删除
  35. $groupModel->enable = 0;
  36. $groupModel->admin_id = $adminId;
  37. $delResult = $groupModel->save();
  38. if(!$delResult) { // 删除失败
  39. DB::rollBack();
  40. return 2412;
  41. }
  42. $mediaCount = Media::where('group_id', $groupId)->where('sys_group_id', $sysGroupId)->count();
  43. if($mediaCount){ # 解除该组的素材分组信息
  44. $updateRes = Media::where('group_id', $groupId)->where('sys_group_id', $sysGroupId)->update(['group_id' => null]);
  45. if(!$updateRes) {
  46. DB::rollBack();
  47. return 2412;
  48. }
  49. }
  50. DB::commit();
  51. } catch (\Exception $e) {
  52. DB::rollBack();
  53. Log::logError('删除素材库流程发生异常', [
  54. 'line' => $e->getLine(),
  55. 'msg' => $e->getMessage(),
  56. 'params' => [
  57. 'group_id' => $groupId,
  58. 'sys_group_id' => $sysGroupId,
  59. 'admin_id' => $adminId
  60. ]
  61. ], 'DelGroup');
  62. }
  63. return 0;
  64. }
  65. /**
  66. * 获取素材组列表
  67. * */
  68. public static function getGroupList($sysGroupId, $keyword, $page, $pageSize)
  69. {
  70. # 获取列表数据
  71. list($list, $count) = MediaGroup::groupList($sysGroupId, $keyword, $page, $pageSize);
  72. return [$list, $count];
  73. }
  74. /**
  75. * 批量保存素材
  76. * */
  77. public static function editMedia($mediaData, $sysGroupId, $adminId, &$failList)
  78. {
  79. if(empty($mediaData)) return 2413;
  80. try {
  81. DB::beginTransaction();
  82. foreach ($mediaData as $datum) {
  83. $mediaId = $datum['id'] ?? null;
  84. $groupId = $datum['group_id'];
  85. $platformId = $datum['platform_id'];
  86. $title = $datum['title'];
  87. $type = $datum['type'];
  88. $localPath = $datum['local_path'];
  89. $ossUrl = $datum['url'];
  90. if(!$localPath || !$ossUrl) continue;
  91. # 保存素材
  92. $errorNo = Media::editMedia($title, $localPath, $ossUrl, $type, $mediaId, $groupId, $platformId, $sysGroupId, $adminId);
  93. if($errorNo) {
  94. $failList[] = array(
  95. 'id' => $mediaId,
  96. 'url' => $ossUrl,
  97. 'local_path' => $localPath,
  98. 'err_msg' => Error::getError($errorNo)
  99. );
  100. }
  101. }
  102. DB::commit();
  103. } catch (\Exception $e) {
  104. DB::rollBack();
  105. Log::logError('批量保存素材流程发生异常', [
  106. 'line' => $e->getLine(),
  107. 'msg' => $e->getMessage(),
  108. 'params' => [
  109. 'media_data' => $mediaData,
  110. 'sys_group_id' => $sysGroupId,
  111. 'admin_id' => $adminId
  112. ]
  113. ], 'EditMedia');
  114. return 2415;
  115. }
  116. return 0;
  117. }
  118. /**
  119. * 获取素材列表
  120. * */
  121. public static function getMediaList($sysGroupId, $keyword, $platformId, $groupId, $page, $pageSize)
  122. {
  123. list($list, $count) = Media::mediaList($sysGroupId, $keyword, $platformId, $groupId, $page, $pageSize);
  124. # 获取列表中的组信息
  125. $groupIdList = array_unique($list->pluck('group_id')->toArray());
  126. $groupData = MediaGroup::whereIn('id', $groupIdList)->get();
  127. # 获取平台列表
  128. $platformList = IntelligentMassSendingService::platformIndex();
  129. $platformList = collect($platformList);
  130. foreach ($list as $item) {
  131. # 处理平台信息
  132. $platformInfo = $platformList->where('platform_id', $item->platform_id)->first();
  133. $item->platform_name = $platformInfo['platform_name'] ?? null;
  134. # 处理分组信息
  135. $groupInfo = $groupData->where('id', $item->group_id)->first();
  136. $item->group_name = $groupInfo['name'] ?? null;
  137. }
  138. return [$list, $count];
  139. }
  140. /**
  141. * 批量移动分组
  142. * */
  143. public static function changeGroup($mediaIds, $groupId, $sysGroupId, $adminId)
  144. {
  145. $mediaIds = explode(',', $mediaIds);
  146. if(empty($mediaIds)) return 2416;
  147. if($groupId){ # 判断分组id是否合法
  148. $isExist = MediaGroup::where('id', $groupId)->where('sys_group_id', $sysGroupId)->where('enable', 1)->exists();
  149. if(!$isExist) return 2417;
  150. }
  151. $result = Media::whereIn('id', $mediaIds)
  152. ->where('sys_group_id', $sysGroupId)
  153. ->where('enable', 1)
  154. ->update(['group_id' => $groupId, 'admin_id' => $adminId]);
  155. // return $result ? 0 : 2418;
  156. return 0;
  157. }
  158. /**
  159. * 批量删除素材
  160. * */
  161. public static function delMedia($mediaIds, $sysGroupId, $adminId)
  162. {
  163. $mediaIds = explode(',', $mediaIds);
  164. if(empty($mediaIds)) return 2419;
  165. $mediaList = Media::where('enable', 1)->whereIn('id', $mediaIds)->where('sys_group_id', $sysGroupId)->get();
  166. foreach ($mediaList as $media) {
  167. $localPath = $media->local_path;
  168. $ossUrl = $media->oss_url;
  169. # 删除本地图片
  170. @unlink($localPath);
  171. # 删除oss图片
  172. $oss = new OssService('playlet');
  173. $oss->delete($ossUrl);
  174. # 删除数据表中的数据
  175. Media::where('id', $media->id)->update(['enable' => 0, 'admin_id' => $adminId]);
  176. }
  177. return 0;
  178. }
  179. /**
  180. * 保存素材
  181. * */
  182. public static function saveMaterial($mediaId, $corpid, &$errno)
  183. {
  184. # 查询素材信息
  185. $mediaInfo = Media::where('enable', 1)->where('id', $mediaId)->first();
  186. if(empty($mediaInfo)) {
  187. $errno = 2420;
  188. return [];
  189. }
  190. if(!$mediaInfo->oss_url || !$mediaInfo->local_path) {
  191. $errno = 2421;
  192. return [];
  193. }
  194. # 存储素材信息到material表
  195. $materialId = Material::saveMaterial($corpid, $mediaInfo->type, $mediaInfo->local_path, $mediaInfo->oss_url);
  196. if($materialId === false) {
  197. $errno = 2422;
  198. return [];
  199. }
  200. return ['material_id' => $materialId, 'url' => $mediaInfo->oss_url];
  201. }
  202. }