TemplateCustomSendService.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. <?php
  2. /**
  3. * Created by sublime.
  4. * User: wosinC
  5. * Date: 2017/12/2
  6. * Time: 上午11:39
  7. */
  8. namespace App\Modules\OfficialAccount\Services;
  9. use App\Modules\OfficialAccount\Models\WechatTemplateMsgs;
  10. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  11. use App\Modules\OfficialAccount\Models\ForceSubscribeUsers;
  12. use App\Modules\OfficialAccount\Models\OfficialAccount;
  13. use App\Modules\OfficialAccount\Models\OfficialCustomMsg;
  14. use App\Modules\OfficialAccount\Models\CustomMsgSwitchs;
  15. use App\Modules\OfficialAccount\Models\CustomMsgSwitchsMsgs;
  16. use App\Modules\OfficialAccount\Models\CustomSendMsgs;
  17. use App\Modules\OfficialAccount\Models\DistributionChannelSetting;
  18. use App\Modules\OfficialAccount\Models\OfficialImgtextUrls;
  19. use App\Modules\User\Services\ReadRecordService;
  20. use App\Modules\User\Services\UserService;
  21. use App\Modules\Book\Services\BookConfigService;
  22. use App\Modules\OfficialAccount\Services\OfficialAccountService;
  23. use App\Modules\Promotion\Services\PromotionService;
  24. use App\Modules\Channel\Services\ChannelService;
  25. use App\Modules\OfficialAccount\Models\MediaCustomer;
  26. use App\Modules\OfficialAccount\Models\CustomPushActivitys;
  27. use App\Modules\OfficialAccount\Models\CustomSendDayStats;
  28. use GuzzleHttp\Client;
  29. use App\Libs\OSS;
  30. use Redis;
  31. use App\Jobs\SendNews;
  32. use App\Jobs\SendTemplate;
  33. use App\Jobs\SendTexts;
  34. use App\Jobs\CustomMsgSendJob;
  35. class TemplateCustomSendService
  36. {
  37. /**
  38. * @return int|void toCheckAndSendCustomMsg
  39. * 查找并发送模板消息
  40. */
  41. static function toCheckAndSendTemplateMsg()
  42. {
  43. try {
  44. \Log::info('toCheckAndSendTemplateMsg_start');
  45. //查找模板消息并发送
  46. $statusPram = '1';
  47. $allStandByWechatTemplateMsgs = WechatTemplateMsgs::wechatTemplateMsgsByStatusStandBy($statusPram);
  48. $allStandByWechatTemplateMsgsArray = [];
  49. if ($allStandByWechatTemplateMsgs){
  50. //先单独更新数据库 把要发送的状态改为8 以免发送期间超时,导致重复发送
  51. for ($x=0; $x < count($allStandByWechatTemplateMsgs); $x++) {
  52. $wechatTemplateMsgs = $allStandByWechatTemplateMsgs[$x];
  53. $send_time = $wechatTemplateMsgs['send_time'];
  54. if (strtotime($send_time) - time() > 600) { //发送时间大于当前时间10分钟
  55. continue;
  56. } else {
  57. \Log::info('========现将模板消息设置成发送状态 "8" 用户不能操作(删除,编辑,停止) = ' . $wechatTemplateMsgs['id']);
  58. $wechatTemplateMsgs['status'] = '8';
  59. $wechatTemplateMsgs->save();
  60. $officialAccountArray = $wechatTemplateMsgs->toArray();
  61. Redis::hset('send_wechat_msg:task_id:' . $wechatTemplateMsgs['id'], 'wechat_msg', json_encode($officialAccountArray));
  62. $allStandByWechatTemplateMsgsArray[] = $wechatTemplateMsgs;
  63. }
  64. }
  65. for ($x=0; $x < count($allStandByWechatTemplateMsgsArray); $x++){
  66. $wechatTemplateMsgs = $allStandByWechatTemplateMsgsArray[$x];
  67. \Log::info($wechatTemplateMsgs);
  68. // $send_time = $wechatTemplateMsgs['send_time'];
  69. // if (strtotime($send_time) - time() > 600){ //发送时间大于当前时间10分钟
  70. // continue;
  71. // }
  72. $wechatTemplateMsgs['redirect_url'] = self::redirect_url_add_param($wechatTemplateMsgs['redirect_url'],$wechatTemplateMsgs['id'],'template',false);
  73. \Log::info('template_redirect_url_convert:id:'.$wechatTemplateMsgs['id'].' redirect_url:'.$wechatTemplateMsgs['redirect_url']);
  74. $appid = $wechatTemplateMsgs['appid'];
  75. $template_id = $wechatTemplateMsgs['template_id'];
  76. $template_content = $wechatTemplateMsgs['template_content'];
  77. $origin_template_content = $template_content;
  78. $redirect_url = $wechatTemplateMsgs['redirect_url'];
  79. // 新版要自动添加域名
  80. $redirect_url = self::checkAddDomain($wechatTemplateMsgs['distribution_channel_id'],$redirect_url);
  81. \Log::info('checkAddDomain_after:distribution_channel_id:'.$wechatTemplateMsgs['distribution_channel_id'].' redirect_url:'.$redirect_url);
  82. $task_id = $wechatTemplateMsgs['id'];
  83. $send_time = $wechatTemplateMsgs['send_time'];
  84. $is_full_send = isset($wechatTemplateMsgs['is_full_send'])?$wechatTemplateMsgs['is_full_send']:0;
  85. \Log::info('-----------------模板消息参数--------------appid = '.$appid.', channelid = '.$wechatTemplateMsgs['distribution_channel_id']);
  86. \Log::info('========发送的模板消息内容========== template_content');
  87. \Log::info($template_content);
  88. //1,通过筛选得到条件符合的强关用户
  89. $skip = 0;
  90. $next_openid = '';
  91. $loop = 0;
  92. while ( $skip <= 2500000) {
  93. $loop++;
  94. \Log::info('-------------模板消息-----分页取强关用户,目前页数----------'.$skip.' is_full_send:'.$is_full_send.' task_id:'.$task_id.' loop:'.$loop);
  95. $users = array();
  96. // 全服务号粉丝发,要记录next_openid
  97. if($is_full_send){
  98. if($loop > 1 && empty($next_openid)){
  99. \Log::info('send_template_appid:'.$appid.' break,skip:'.$skip);
  100. break;
  101. }
  102. $user_res = ForceSubscribeService::getFullOfficialAccountUsers($appid,$next_openid);
  103. $users = $user_res['openids'];// TODO格式化成一样的格式
  104. $next_openid = $user_res['next_openid'];
  105. }else{
  106. $users = ForceSubscribeService::forceUserCountByTemplatePrams($appid,$wechatTemplateMsgs['distribution_channel_id'],$wechatTemplateMsgs['subscribe_time'],$wechatTemplateMsgs['sex'],$wechatTemplateMsgs['balance'],$wechatTemplateMsgs['category_id'],$wechatTemplateMsgs['order_type'],$skip);
  107. }
  108. \Log::info('-----------------实际发送模板消息用户数量-------------'.count($users).' $next_openid:'.$next_openid);
  109. // \Log::info($users);
  110. $skip += count($users);
  111. // continue;
  112. // die('end2');
  113. if(count($users)>0){
  114. for ($i=0; $i < count($users); $i++) {
  115. // 占位符替换
  116. $place_holder_params = array();
  117. $place_holder_params['nickname'] = isset($users[$i]['nickname'])?$users[$i]['nickname']:'';
  118. \Log::info('openid:'.$users[$i]['openid'].' nickname:'.$place_holder_params['nickname']);
  119. $template_content = ForceSubscribeService::change_placeholder($origin_template_content,$place_holder_params);
  120. //2,循环用户发送模板消息
  121. //发送到最后一条时,将
  122. if ($i == count($users)-1) {
  123. $data = array();
  124. $data['statusstr'] = (time());
  125. $data['openid'] = $users[$i]['openid'];
  126. $data['appid'] = $appid;
  127. $data['template_id'] = $template_id;
  128. //4,通过RebitMQ发送模板消息
  129. // $data['template_content'] = $templateSendContents;
  130. $data['template_content'] =str_replace(".DATA","",$template_content);
  131. $data['type'] = 'last_task';// last_task,one_task
  132. $data['url'] = $redirect_url;
  133. $data['task_id'] = $task_id;
  134. $data['send_time'] = $send_time;
  135. $send_data=array(
  136. 'send_time'=>$send_time,
  137. 'data' => $data
  138. );
  139. $now_time = strtotime($send_time)-time();
  140. $delay = $now_time < 0 ? 0:$now_time;
  141. \Log::info('------------------发送最后一条模板消息----------task_id = '.$task_id.', 时间差 = '.$now_time.' openid:'.$data['openid']);
  142. $job = (new SendTemplate($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_template_list');
  143. dispatch($job);
  144. }else{
  145. $data = array();
  146. $data['statusstr'] = (time());
  147. $data['openid'] = $users[$i]['openid'];
  148. $data['appid'] = $appid;
  149. $data['template_id'] = $template_id;
  150. \Log::info('========================[[[[send template message]]]]====================');
  151. \Log::info(str_replace(".DATA","",$template_content));
  152. $data['template_content'] =str_replace(".DATA","",$template_content);
  153. $data['type'] = 'one_task';// last_task,one_task
  154. $data['url'] = $redirect_url;
  155. $data['task_id'] = $task_id;
  156. $data['send_time'] = $send_time;
  157. $send_data=array(
  158. 'send_time'=>$send_time,
  159. 'data' => $data
  160. );
  161. $now_time = strtotime($send_time)-time();
  162. $delay = $now_time < 0 ? 0:$now_time;
  163. \Log::info('========================发送模板消息 不是最后一条模板=================task_id = '.$task_id.'. openid = '.$data['openid'].', 时间差 = '.$delay);
  164. $job = (new SendTemplate($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_template_list');
  165. dispatch($job);
  166. }
  167. }
  168. }else{
  169. \Log::info('========================发送模板消息_没找到用户===================='.$task_id);
  170. break;
  171. }
  172. }
  173. //更新模板消息的用户数量并更新redis
  174. \Log::info('========================更新模板消息并存redis====================task_id = '.$task_id);
  175. $wechatTemplateMsgs['user_num'] = $skip;
  176. // 若没有用户,则直接更新为已发送
  177. if($skip == 0){
  178. \Log::info('has_no_template_users:'.$wechatTemplateMsgs['id']);
  179. $wechatTemplateMsgs['status'] = '3';
  180. }
  181. $wechatTemplateMsgs->save();
  182. $officialAccountArray = $wechatTemplateMsgs->toArray();
  183. Redis::hset('send_wechat_msg:task_id:'.$wechatTemplateMsgs['id'], 'wechat_msg', json_encode($officialAccountArray));
  184. }
  185. }else{
  186. \Log::info('========================!!!数据库中没有要发送的模板消息!!!====================');
  187. }
  188. } catch (\Exception $e) {
  189. \Log::info('========================循环用户发送模板消息时报错====================');
  190. \Log::info($e->getMessage());
  191. }
  192. }
  193. /**
  194. * @return int|void toCheckAndSendCustomMsg
  195. * 查找并发送客服消息
  196. */
  197. static function toCheckAndSendCustomMsg()
  198. {
  199. //
  200. try {
  201. //查找模板消息并发送
  202. $statusPram = '1';
  203. $allStandByWechatCustomMsgs = CustomSendMsgs::customSendMsgsByStatusStandBy($statusPram);
  204. // \Log::info($allStandByWechatCustomMsgs);
  205. $allStandByWechatCustomMsgsArray = [];
  206. if ($allStandByWechatCustomMsgs) {
  207. //先单独更新数据库 把要发送的状态改为8 以免发送期间超时,导致重复发送
  208. for ($x = 0; $x < count($allStandByWechatCustomMsgs); $x++) {
  209. $customSendMsgsPrams = $allStandByWechatCustomMsgs[$x];
  210. $send_time = $customSendMsgsPrams['send_time'];
  211. if (strtotime($send_time) - time() > 60*15) { //发送时间大于当前时间25分钟
  212. //\Log::info('=====客服消息超时被过滤=====ID为'.$customSendMsgsPrams['id']);
  213. continue;
  214. } else {
  215. \Log::info('========现将客服消息设置成发送状态 "8" 用户不能操作(删除,编辑,停止) = ' . $customSendMsgsPrams['id']);
  216. $customSendMsgsPrams['status'] = '8';
  217. $customSendMsgsPrams->save();
  218. $officialAccountArray = $customSendMsgsPrams->toArray();
  219. Redis::hset('send_wechat_msg:task_id:' . $customSendMsgsPrams['task_id'], 'wechat_msg', json_encode($officialAccountArray));
  220. $allStandByWechatCustomMsgsArray[] = $customSendMsgsPrams;
  221. }
  222. }
  223. for ($x = 0; $x < count($allStandByWechatCustomMsgsArray); $x++) {
  224. $customSendMsgsPrams = $allStandByWechatCustomMsgsArray[$x];
  225. $is_has_user_tag = 0;
  226. try {
  227. if (strstr($customSendMsgsPrams['content'], '{user}')) $is_has_user_tag = 1;
  228. }catch (\Exception $e) {
  229. \Log::error($e);
  230. }
  231. if($customSendMsgsPrams['custom_type'] == 'image'){
  232. // 转化图文的链接url
  233. $customSendMsgsPrams['content'] = self::convert_custom_content($customSendMsgsPrams['content'],$customSendMsgsPrams['id'],$customSendMsgsPrams['distribution_channel_id'],$customSendMsgsPrams['trusteeship']);
  234. }else{
  235. // 转化文字的链接url
  236. $customSendMsgsPrams['content'] = self::convert_custom_text_content($customSendMsgsPrams['content'],$customSendMsgsPrams['id'],$customSendMsgsPrams['distribution_channel_id'],$customSendMsgsPrams['trusteeship']);
  237. }
  238. $appids = $customSendMsgsPrams['appid'];
  239. $is_full_send = 0;
  240. \Log::info('------------------客服消息强关用户,appids:'.$appids.' is_full_send:'.$is_full_send);
  241. $appidArray = explode(',', $appids);
  242. $usercountnum = 0;
  243. for ($j=0; $j < count($appidArray); $j++) {
  244. $skip = 0;
  245. $next_openid = '';
  246. $loop = 0;
  247. while ( $skip <= 2500000) {
  248. $loop++;
  249. \Log::info('------------------分页取强关用户,目前页数----------'.$skip.' loop:'.$loop.' next_openid:'.$next_openid);
  250. $users = array();
  251. // 全服务号粉丝发,要记录next_openid
  252. if($is_full_send){
  253. // 防止异常
  254. if($loop > 1 && empty($next_openid)){
  255. \Log::info('send_template_appid:'.$appidArray[$j].' break,skip:'.$skip);
  256. break;
  257. }
  258. try{
  259. $user_res = ForceSubscribeService::getFullOfficialAccountUsers($appidArray[$j],$next_openid);
  260. $users = $user_res['openids'];// TODO格式化成一样的格式
  261. $next_openid = $user_res['next_openid'];
  262. } catch (\Exception $e) {
  263. \Log::info($e);
  264. }
  265. }else{
  266. //5,获取所选条件下筛选出的用户
  267. $sign_time = 'z';
  268. $users = ForceSubscribeService::forceUserCountByCustomPrams($appidArray[$j],$customSendMsgsPrams['distribution_channel_id'],$customSendMsgsPrams['subscribe_time'],$customSendMsgsPrams['sex'],$customSendMsgsPrams['balance'],$customSendMsgsPrams['category_id'],$customSendMsgsPrams['order_type'],$sign_time,$skip,$customSendMsgsPrams['subscribe_time_attach']);
  269. }
  270. \Log::info('--------------------users_num:'.count($users));
  271. // continue;
  272. if (count($users)>0) {
  273. $usercountnum += count($users);
  274. try {
  275. if ($is_has_user_tag) {
  276. $current_openids = [];
  277. foreach ($users as $_u) {
  278. $current_openids[] = $_u['openid'];
  279. }
  280. $user_properties = ForceSubscribeService::getForceUsersNickname($current_openids);
  281. }
  282. }catch (\Exception $e){ \Log::error($e);}
  283. for ($i=0; $i < count($users); $i++) {
  284. // 7,循环用户发送客服消息
  285. $data = array();
  286. if ($i == count($users)-1) {
  287. $data['type'] = 'last_task';
  288. }else{
  289. $data['type'] = 'one_task';
  290. }
  291. $content = $customSendMsgsPrams['content'];
  292. //判断是否有标志位
  293. if($is_has_user_tag)
  294. {
  295. try{
  296. $current_nickname = isset($user_properties[$users[$i]['openid']]) ? $user_properties[$users[$i]['openid']] :'书友';
  297. $content = str_replace('{user}',$current_nickname,$customSendMsgsPrams['content']);
  298. }catch (\Exception $e){ \Log::error($e);}
  299. }
  300. if($customSendMsgsPrams['custom_type'] == 'image'){
  301. $data['news_content'] = $content;
  302. }else{
  303. $data['content'] = json_decode($content);
  304. }
  305. $data['openid'] = $users[$i]['openid'];
  306. $data['appid'] = $appidArray[$j];
  307. $data['type'] = 'last_task';
  308. $data['task_id'] = $customSendMsgsPrams['task_id'];
  309. $data['send_time'] = $customSendMsgsPrams['send_time'];
  310. $send_data=array(
  311. 'send_time'=>$customSendMsgsPrams['send_time'],
  312. 'data' => $data
  313. );
  314. $now_time = strtotime($customSendMsgsPrams['send_time'])-time();
  315. $delay = $now_time < 0 ? 0:$now_time;
  316. if($customSendMsgsPrams['custom_type'] == 'image')
  317. {
  318. $job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_news_list');
  319. }else{
  320. $job = (new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list');
  321. }
  322. dispatch($job);
  323. }
  324. }else{
  325. \Log::info('------------------发送客服消息没有查找到强关用户----------'.$customSendMsgsPrams['task_id']);
  326. break;
  327. }
  328. $skip += count($users);
  329. }
  330. }
  331. $customSendMsgsPrams['user_num'] = $usercountnum;
  332. // 若没有用户,则直接更新为已发送
  333. if($usercountnum == 0){
  334. \Log::info('has_no_custom_users:'.$customSendMsgsPrams['task_id']);
  335. $customSendMsgsPrams['status'] = '3';
  336. }
  337. \Log::info('------------------更新发送用户----------'.$customSendMsgsPrams['task_id'].' usercountnum:'.$usercountnum);
  338. $customSendMsgsPrams->save();
  339. }
  340. }else{
  341. \Log::info('========================!!!数据库中没有要发送的客服消息!!!====================');
  342. }
  343. } catch (\Exception $e) {
  344. \Log::info('===================================发送客服消息报错');
  345. \Log::info($e->getMessage());
  346. }
  347. }
  348. /**
  349. * 转换客服消息内容
  350. * 1条--新版
  351. * @param unknown_type $content
  352. * @return string
  353. */
  354. public static function convert_custom_content($content,$mark_str,$distribution_channel_id,$trusteeship=0){
  355. \Log::info('convert_custom_content_start:');
  356. $_contents = json_decode($content);
  357. // \Log::info('convert_custom_content_before:');
  358. // \Log::info($_contents);
  359. $new_contents = null;
  360. // 智能托管,加上各自渠道域名,统计精确到图文每条
  361. if($trusteeship){
  362. $origin_mark_str = $mark_str;
  363. foreach($_contents as $key=>$_content){
  364. $url = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com'.$_content[2]->url;
  365. $mark_str = $origin_mark_str.'_'.($key+1).'&source=wechatmsg';// source能让用户继续阅读
  366. \Log::info('convert_custom_content_url:'.$url.' mark_str:'.$mark_str);
  367. $_contents[$key][2]->url = self::redirect_url_add_param($url,$mark_str,'custom',true);
  368. $new_contents[] = $_contents[$key];
  369. break;
  370. }
  371. }
  372. // 非托管
  373. else{
  374. $origin_mark_str = $mark_str;
  375. $send_type = 'custom';
  376. // 智能推送不加前缀
  377. if(strpos($origin_mark_str,'smart_push_') > -1){
  378. $send_type = 'smart_push';
  379. $origin_mark_str = str_replace('smart_push_','',$origin_mark_str);
  380. }
  381. \Log::info('origin_mark_str:'.$origin_mark_str);
  382. foreach($_contents as $key=>$_content){
  383. // 新版要自动添加域名
  384. $_content[2]->url = self::checkAddDomain($distribution_channel_id,$_content[2]->url);
  385. $mark_str = $origin_mark_str.'&source=wechatmsg';// source能让用户继续阅读
  386. $_contents[$key][2]->url = self::redirect_url_add_param($_content[2]->url,$mark_str,$send_type,false);
  387. $new_contents[] = $_contents[$key];
  388. break;
  389. }
  390. }
  391. // \Log::info('convert_custom_content_after:');
  392. // \Log::info($new_contents);
  393. $_convert_contents = json_encode($new_contents);
  394. return $_convert_contents;
  395. }
  396. /**
  397. * 转换客服消息内容--文字版
  398. * @param unknown_type $content
  399. * @return string
  400. */
  401. public static function convert_custom_text_content($content,$mark_str,$distribution_channel_id,$trusteeship=0){
  402. \Log::info('convert_custom_content_start:');
  403. $new_content = $content = json_decode($content);
  404. $origin_mark_str = $mark_str;
  405. $send_type = 'custom';
  406. \Log::info('origin_mark_str:'.$origin_mark_str);
  407. \Log::info('convert_custom_text_content_before:');
  408. \Log::info($content);
  409. myLog('htx_test')->notice($content);
  410. preg_match_all("/<a(s*[^>]+s*)href(\s*)=(\s*)([\"|']?)([^\"'>\s]+)([\"|']?)/ies",$content,$matchs);
  411. $matchs_all = isset($matchs[5])?$matchs[5]:[];
  412. \Log::info('$matchs_all');\Log::info($matchs_all);
  413. myLog('htx_test')->notice($matchs_all);
  414. if(!empty($matchs_all)){
  415. foreach($matchs_all as $match){
  416. $new_url = $url = $match;
  417. myLog('htx_test')->notice($url);
  418. $mark_str = $origin_mark_str.'&source=wechatmsg';// source能让用户继续阅读
  419. $new_url = self::redirect_url_add_param($url,$mark_str,$send_type,false);
  420. // 小链接可能多次被替换,所以一定要引号结尾
  421. $new_content = str_replace($url.'"',$new_url.'"',$new_content);
  422. $new_content = str_replace($url."'",$new_url."'",$new_content);
  423. }
  424. }
  425. \Log::info('convert_custom_text_content_after:');
  426. \Log::info($new_content);
  427. $_convert_contents = json_encode($new_content);
  428. return $_convert_contents;
  429. }
  430. /**
  431. * 转换客服消息内容
  432. * 多条--老版
  433. * @param unknown_type $content
  434. * @return string
  435. */
  436. public static function convert_custom_content_multy($content,$mark_str,$distribution_channel_id,$trusteeship=0){
  437. \Log::info('convert_custom_content_start:');
  438. $_contents = json_decode($content);
  439. // 智能托管,加上各自渠道域名,统计精确到图文每条
  440. if($trusteeship){
  441. $origin_mark_str = $mark_str;
  442. foreach($_contents as $key=>$_content){
  443. $url = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com'.$_content[2]->url;
  444. $mark_str = $origin_mark_str.'_'.($key+1).'&source=wechatmsg';// source能让用户继续阅读
  445. \Log::info('convert_custom_content_url:'.$url.' mark_str:'.$mark_str);
  446. $_contents[$key][2]->url = self::redirect_url_add_param($url,$mark_str,'custom',true);
  447. }
  448. }
  449. // 非托管
  450. else{
  451. $origin_mark_str = $mark_str;
  452. $send_type = 'custom';
  453. // 智能推送不加前缀
  454. if(strpos($origin_mark_str,'smart_push_') > -1){
  455. $send_type = 'smart_push';
  456. $origin_mark_str = str_replace('smart_push_','',$origin_mark_str);
  457. }
  458. \Log::info('origin_mark_str:'.$origin_mark_str);
  459. foreach($_contents as $key=>$_content){
  460. $mark_str = $origin_mark_str.'&source=wechatmsg';// source能让用户继续阅读
  461. $_contents[$key][2]->url = self::redirect_url_add_param($_content[2]->url,$mark_str,$send_type,false);
  462. }
  463. }
  464. $_convert_contents = json_encode($_contents);
  465. //\Log::info('convert_custom_content_after:');
  466. //\Log::info($_convert_contents);
  467. return $_convert_contents;
  468. }
  469. /**
  470. * 跳转地址加参
  471. * @param unknown_type $redirect_url
  472. * @param unknown_type $type
  473. * @return string
  474. */
  475. public static function redirect_url_add_param($redirect_url,$mark_str,$type='template',$trusteeship=false){
  476. $pre_str = '';
  477. if($type == 'template'){
  478. $pre_str = 'template_';
  479. }elseif($type == 'custom'){
  480. $pre_str = 'custom_';
  481. }
  482. // 模板客服消息,单独加参
  483. $inner_order_id = '';
  484. if(!$trusteeship){
  485. $inner_order_id = '&inner_order_id='.$pre_str.$mark_str;
  486. }
  487. $send_time_str = '';
  488. if(strpos($redirect_url,'newUser') > -1){
  489. \Log::info('newUserActivity_send_time:'.$inner_order_id);
  490. $send_time_str = '&send_time='.time();
  491. }
  492. // 跳转链接加参
  493. if(strpos($redirect_url,'?') > -1){
  494. $redirect_url .= '&fromtype='.$pre_str.$mark_str.$inner_order_id.$send_time_str;
  495. }else{
  496. $redirect_url .= '?fromtype='.$pre_str.$mark_str.$inner_order_id.$send_time_str;
  497. }
  498. return $redirect_url;
  499. }
  500. // 新版要自动添加域名
  501. public static function checkAddDomain($distribution_channel_id,$redirect_url){
  502. // 非托管,如果url没有域名,则加上
  503. if(strpos($redirect_url, 'http') !== false){
  504. \Log::info('not_need_add_domain:'.$distribution_channel_id.' url:'.$redirect_url);
  505. }else{
  506. \Log::info('need_add_domain_before:'.$distribution_channel_id.' url:'.$redirect_url);
  507. $redirect_url = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com'.$redirect_url;
  508. \Log::info('need_add_domain_after:'.$distribution_channel_id.' url:'.$redirect_url);
  509. }
  510. return $redirect_url;
  511. }
  512. }