WechatMaterialService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. namespace App\Modules\WechatMaterial\Services;
  3. use App\Modules\WechatMaterial\Models\BatchWechatMaterial;
  4. use App\Modules\WechatMaterial\Models\WechatMaterial;
  5. use App\Modules\WechatMaterial\Models\WechatMaterialSendMsg;
  6. class WechatMaterialService
  7. {
  8. public static function get_zs_wechat_material_list($request,$extra_param=[]){
  9. $channel_user_id = $extra_param['channel_user_id'];
  10. \Log::info('get_zs_wechat_material_list channel_user_id:'.$channel_user_id);
  11. return BatchWechatMaterial::get_zs_wechat_material_list($channel_user_id);
  12. }
  13. public static function del_zs_wechat_material($request,$extra_param=[]){
  14. $batch_id = $request->has('batch_id')?$request->get('batch_id'):'';
  15. $channel_user_id = $extra_param['channel_user_id'];
  16. \Log::info('del_zs_wechat_material:'.$batch_id.' channel_user_id:'.$channel_user_id);
  17. // 权限检测报错出来
  18. if(!self::check_batch_wechat_user_priv($channel_user_id,$batch_id)){
  19. return response()->error('BATCH_WECHAT_PRIV_ERROR');
  20. }
  21. $result = BatchWechatMaterial::del_zs_wechat_material($batch_id,$channel_user_id);
  22. // TODO循环删除appid的素材
  23. return $result;
  24. }
  25. public static function create_zs_wechat_material($request,$extra_param=[]){
  26. $content = $request->has('content')?$request->get('content'):'';
  27. $extend_html_content = $request->has('extend_html_content')?$request->get('extend_html_content'):'';
  28. $name = $request->has('name')?$request->get('name'):'';
  29. if(!is_array($content)){
  30. $content_items = json_decode($content);
  31. }else{
  32. $content_items = $content;
  33. }
  34. $channel_user_id = $extra_param['channel_user_id'];
  35. $distribution_channel_id = $extra_param['channel_id'];
  36. // 批量
  37. $batchWechatMaterial = [
  38. 'channel_user_id'=>$channel_user_id,
  39. 'distribution_channel_id'=>$distribution_channel_id,
  40. 'name'=>$name,
  41. 'content'=>json_encode($content_items),
  42. 'status'=>'init',
  43. 'del_flag'=>0,
  44. 'created_at'=>date('Y-m-d H:i:s'),
  45. 'updated_at'=>date('Y-m-d H:i:s'),
  46. ];
  47. \Log::info('$create_zs_wechat_material:'.$name);
  48. \Log::info($batchWechatMaterial);
  49. $new_batchWechatMaterial = BatchWechatMaterial::firstOrCreate($batchWechatMaterial);
  50. $batch_id = isset($new_batchWechatMaterial->id)?$new_batchWechatMaterial->id:'';
  51. \Log::info('$new_batchWechatMaterial_batch_id:'.$batch_id);
  52. \Log::info($content_items);
  53. // 单个映射素材表
  54. if(!empty($content_items)){
  55. foreach($content_items as $key=>$content_item){
  56. $sort_no = $key+1;
  57. $wechatMaterial = [
  58. 'batch_id'=>$batch_id,
  59. 'bid'=>isset($content_item[4]->bid)?$content_item[4]->bid:'',
  60. 'book_name'=>isset($content_item[5]->book_name)?$content_item[5]->book_name:'',
  61. 'title'=>isset($content_item[0]->title)?$content_item[0]->title:'',
  62. 'cover_url'=>isset($content_item[3]->image)?$content_item[3]->image:'',
  63. 'url'=>isset($content_item[2]->url)?$content_item[2]->url:'',
  64. 'desc'=>isset($content_item[1]->description)?$content_item[1]->description:'',
  65. 'content_mode'=>isset($content_item[6]->content_mode)?$content_item[6]->content_mode:'',
  66. 'content'=>isset($content_item[7]->content)?$content_item[7]->content:'',
  67. 'sort_no'=>$sort_no,
  68. 'created_at'=>date('Y-m-d H:i:s'),
  69. 'updated_at'=>date('Y-m-d H:i:s'),
  70. ];
  71. \Log::info('$wechatMaterial:');\Log::info($wechatMaterial);
  72. WechatMaterial::firstOrCreate($wechatMaterial);
  73. }
  74. }
  75. return $new_batchWechatMaterial;
  76. }
  77. public static function get_zs_wechat_batch_distribution_infos($request,$extra_param=[]){
  78. $batch_id = $request->has('batch_id')?$request->get('batch_id'):'';
  79. return WechatMaterialSendMsg::get_zs_wechat_batch_distribution_infos($batch_id);
  80. }
  81. public static function update_zs_wechat_material($request,$extra_param=[]){
  82. $batch_id = $id = $request->has('batch_id')?$request->get('batch_id'):'';
  83. $channel_user_id = $extra_param['channel_user_id'];
  84. \Log::info('update_zs_wechat_material:'.$id.' channel_user_id:'.$channel_user_id);
  85. if(!self::check_batch_wechat_user_priv($channel_user_id,$batch_id)){
  86. return response()->error('BATCH_WECHAT_PRIV_ERROR');
  87. }
  88. $content = $request->has('content')?$request->get('content'):'';
  89. if(!is_array($content)){
  90. $content_items = json_decode($content);
  91. }else{
  92. $content_items = $content;
  93. }
  94. $name = $request->has('name')?$request->get('name'):'';
  95. $updated_at = date('Y-m-d H:i:s');
  96. $content = json_encode($content_items);
  97. $base_data = compact('id','channel_user_id');
  98. $extra_data = compact('content','updated_at');
  99. \Log::info('update_zs_wechat_material:'.$id);
  100. BatchWechatMaterial::where($base_data)->update($extra_data);
  101. // 单个映射素材表
  102. if(!empty($content_items)){
  103. foreach($content_items as $key=>$content_item){
  104. $sort_no = $key+1;
  105. // 删除老的素材
  106. WechatMaterial::delByBatchId($id);
  107. $wechatMaterial = [
  108. 'batch_id'=>$batch_id,
  109. 'bid'=>isset($content_item[4]->bid)?$content_item[4]->bid:'',
  110. 'book_name'=>isset($content_item[5]->book_name)?$content_item[5]->book_name:'',
  111. 'title'=>isset($content_item[0]->title)?$content_item[0]->title:'',
  112. 'cover_url'=>isset($content_item[3]->image)?$content_item[3]->image:'',
  113. 'url'=>isset($content_item[2]->url)?$content_item[2]->url:'',
  114. 'desc'=>isset($content_item[1]->description)?$content_item[1]->description:'',
  115. 'content_mode'=>isset($content_item[6]->content_mode)?$content_item[6]->content_mode:'',
  116. 'content'=>isset($content_item[7]->content)?$content_item[7]->content:'',
  117. 'sort_no'=>$sort_no,
  118. 'created_at'=>date('Y-m-d H:i:s'),
  119. 'updated_at'=>date('Y-m-d H:i:s'),
  120. ];
  121. \Log::info('$wechatMaterial:');\Log::info($wechatMaterial);
  122. WechatMaterial::firstOrCreate($wechatMaterial);
  123. }
  124. }
  125. return true;
  126. }
  127. public static function set_wechat_material_batch_send_info($request,$extra_param=[]){
  128. $id = $request->has('batch_id')?$request->get('batch_id'):'';
  129. $send_time = $request->has('send_time')?$request->get('send_time'):'';
  130. $subscribe_time = $request->has('subscribe_time')?$request->get('subscribe_time'):'';
  131. $balance = $request->has('balance')?$request->get('balance'):'';
  132. $sex = $request->has('sex')?$request->get('sex'):'';
  133. $pay_type = $request->has('pay_type')?$request->get('pay_type'):'';
  134. $interaction_time = $request->has('interaction_time')?$request->get('interaction_time'):'';
  135. // $status = $request->has('status')?$request->get('status'):'';
  136. $is_all_user = $request->has('is_all_user')?$request->get('is_all_user'):'';
  137. // $del_flag = $request->has('del_flag')?$request->get('del_flag'):'';
  138. $channel_user_id = $extra_param['channel_user_id'];
  139. \Log::info('set_wechat_material_batch_send_info:'.$id.' channel_user_id:'.$channel_user_id);
  140. // 权限检测报错出来
  141. if(!self::check_batch_wechat_user_priv($channel_user_id,$batch_id)){
  142. return response()->error('BATCH_WECHAT_PRIV_ERROR');
  143. }
  144. $updated_at = date('Y-m-d H:i:s');
  145. \Log::info('set_wechat_material_batch_send_info:'.$id);
  146. $base_data = compact('id');
  147. $extra_data = compact('send_time', 'subscribe_time', 'balance', 'sex', 'pay_type', 'interaction_time', 'is_all_user','updated_at');
  148. \Log::info('set_wechat_material_batch_send_info:'.$id);
  149. return BatchWechatMaterial::where($base_data)->update($extra_data);
  150. }
  151. /**
  152. * 同步素材到公众号
  153. * @param unknown $request
  154. * @param array $extra_param
  155. */
  156. public static function sync_zs_wechat_material($request,$extra_param=[]){
  157. $batch_id = $id = $request->has('batch_id')?$request->get('batch_id'):'';
  158. $distribution_appids = $request->has('distribution_appids')?$request->get('distribution_appids'):'';
  159. $channel_user_id = $extra_param['channel_user_id'];
  160. \Log::info('sync_zs_wechat_material:'.$id.' channel_user_id:'.$channel_user_id);
  161. if(!self::check_batch_wechat_user_priv($channel_user_id,$batch_id)){
  162. return response()->error('BATCH_WECHAT_PRIV_ERROR');
  163. }
  164. // 获取素材
  165. $batch_wechat_material = BatchWechatMaterial::find($batch_id);
  166. $batch_wechat_material_contents = WechatMaterialService::getWeChatMaterials($batch_id);
  167. \Log::info('batch_wechat_material');\Log::info($batch_wechat_material);
  168. \Log::info('batch_wechat_material_contents');\Log::info($batch_wechat_material_contents);
  169. $distribution_appids = json_decode($distribution_appids);
  170. foreach($distribution_appids as $distribution_channel_id=>$appids){
  171. foreach($appids as $appid){
  172. try{
  173. // 把任务扔队列处理
  174. \Log::info('sync_zs_wechat_material:'.$distribution_channel_id.' appid:'.$appid);
  175. $wechatMaterialSendMsg = [
  176. 'batch_id'=>$batch_id,
  177. 'appid'=>$appid,
  178. 'distribution_channel_id'=>$distribution_channel_id,
  179. 'created_at'=>date('Y-m-d H:i:s'),
  180. 'updated_at'=>date('Y-m-d H:i:s')
  181. ];
  182. WechatMaterialSendMsg::firstOrCreate($wechatMaterialSendMsg);
  183. $queue_param = [
  184. 'action_type'=>'sync_zs_wechat_material',
  185. 'batch_wechat_material'=>$batch_wechat_material,
  186. 'batch_wechat_material_contents'=>$batch_wechat_material_contents,
  187. ];
  188. ActionService::PushActionToQueue($queue_param);
  189. }catch(\exception $e){
  190. \Log::info('sync_zs_wechat_material_ept:'.$e->getMessage());
  191. }
  192. }
  193. }
  194. }
  195. /**
  196. * 检查权限
  197. * @param unknown $request
  198. * @param array $extra_param
  199. */
  200. public static function check_batch_wechat_user_priv($channel_user_id,$batch_id){
  201. $checkRes = BatchWechatMaterial::get_zs_wechat_material($batch_id,$channel_user_id);
  202. if(empty($checkRes)){
  203. return false;
  204. }else{
  205. return true;
  206. }
  207. }
  208. /**
  209. * 获取素材内容
  210. * @param unknown $request
  211. * @param array $extra_param
  212. */
  213. public static function getWeChatMaterials($batch_id){
  214. return WechatMaterial::getWeChatMaterials($batch_id);
  215. }
  216. public static function delWechatMaterialById($id){
  217. try{
  218. $queue_param = [
  219. 'action_type'=>'del_wechat_material',
  220. 'wechat_material_id'=>$id,
  221. ];
  222. ActionService::PushActionToQueue($queue_param);
  223. }catch(\exception $e){
  224. \Log::info('delWechatMaterial:'.$e->getMessage());
  225. }
  226. return 1;
  227. // return WechatMaterial::where('id',$id)->update(
  228. // [
  229. // 'del_flag'=>1,
  230. // 'updated_at'=>date('Y-m-d H:i:s')
  231. // ]
  232. // );
  233. }
  234. public static function deleteBatchSendWechatMaterial($id){
  235. try{
  236. $queue_param = [
  237. 'action_type'=>'del_batch_send_wechat_material',
  238. 'batch_id'=>$id,
  239. ];
  240. ActionService::PushActionToQueue($queue_param);
  241. }catch(\exception $e){
  242. \Log::info('delWechatMaterial:'.$e->getMessage());
  243. }
  244. return 1;
  245. }
  246. }