CustomMsgService.php 157 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394
  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\Book\Models\DataAnalysisBookConfig;
  10. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  11. use App\Modules\OfficialAccount\Services\TemplateCustomSendService;
  12. use App\Modules\OfficialAccount\Models\ForceSubscribeUsers;
  13. use App\Modules\OfficialAccount\Models\OfficialAccount;
  14. use App\Modules\OfficialAccount\Models\OfficialCustomMsg;
  15. use App\Modules\OfficialAccount\Models\CustomMsgSwitchs;
  16. use App\Modules\OfficialAccount\Models\CustomMsgSwitchsMsgs;
  17. use App\Modules\OfficialAccount\Models\CustomSendMsgs;
  18. use App\Modules\OfficialAccount\Models\DistributionChannelSetting;
  19. use App\Modules\OfficialAccount\Models\OfficialImgtextUrls;
  20. use App\Modules\User\Services\ReadRecordService;
  21. use App\Modules\User\Services\UserService;
  22. use App\Modules\Book\Services\BookConfigService;
  23. use App\Modules\OfficialAccount\Services\OfficialAccountService;
  24. use App\Modules\OfficialAccount\Services\SmartPushMsgService;
  25. use App\Modules\Statistic\Services\DataAnalysisChapterService;
  26. use App\Modules\Promotion\Services\PromotionService;
  27. use App\Modules\Channel\Services\ChannelService;
  28. use App\Modules\OfficialAccount\Models\MediaCustomer;
  29. use App\Modules\OfficialAccount\Models\CustomPushActivitys;
  30. use App\Modules\OfficialAccount\Models\CustomPushConfigs;
  31. use App\Modules\OfficialAccount\Models\CustomPushConfigContents;
  32. use App\Modules\OfficialAccount\Models\CustomSendDayStats;
  33. use GuzzleHttp\Client;
  34. use GuzzleHttp\Promise;
  35. use App\Libs\OSS;
  36. use Redis;
  37. use App\Jobs\SendNews;
  38. use App\Jobs\SendTexts;
  39. use App\Jobs\SendTemplate;
  40. use App\Jobs\CustomMsgSendJob;
  41. use DB;
  42. use Hashids;
  43. class CustomMsgService
  44. {
  45. /**
  46. * 通过channelName和text获取神书
  47. */
  48. static function customerMsgByChannelAndText($channelname,$text)
  49. {
  50. return OfficialCustomMsg::customerMsgByChannelAndText($channelname,$text);
  51. }
  52. /**
  53. * 通过分销渠道ID获取客服图片
  54. */
  55. static function customerImgUrlByChannelId($distribution_channel_id)
  56. {
  57. return DistributionChannelSetting::customerImgUrlByChannelId($distribution_channel_id);
  58. }
  59. /**
  60. * 添加客服图片
  61. */
  62. static function updateCustomerImgUrl($customerImgPram)
  63. {
  64. //通过分销渠道号寻找客服图片
  65. $customerImgs = DistributionChannelSetting::customerImgUrlByChannelId($customerImgPram['distribution_channel_id']);
  66. if (empty($customerImgs)) {
  67. //2,删除media_customer表中老的客服图片的mediaid,
  68. //以便灵杰公众号交互下次该渠道请求客服图片时重新去渠道获取新的客服图片
  69. MediaCustomer::where('distribution_channel_id',$customerImgPram['distribution_channel_id'])->delete();
  70. //该渠道配置表中增加客服图片
  71. return DistributionChannelSetting::create($customerImgPram);
  72. }else{
  73. //1,该渠道配置表中更新客服图片
  74. $customerImgs['customer_img_url'] = $customerImgPram['customer_img_url'];
  75. $customerImgs->save();
  76. //2,删除media_customer表中老的客服图片的mediaid,
  77. //以便灵杰公众号交互下次该渠道请求客服图片时重新去渠道获取新的客服图片
  78. MediaCustomer::where('distribution_channel_id',$customerImgPram['distribution_channel_id'])->delete();
  79. return $customerImgs;
  80. }
  81. }
  82. /**
  83. * 未支付订单发送客服消息
  84. */
  85. static function sendUnpaid()
  86. {
  87. //1,遍历渠道表
  88. $distribution_channels = ChannelService::getAllChannels();
  89. $today = date('Y-m-d');
  90. for ($i=0; $i < count($distribution_channels); $i++) {
  91. //2,判断该渠道下有没有开启未支付订单的智能推送
  92. $distribution_channel_id = $distribution_channels[$i]['id'];
  93. // 针对订单级别,一个用户可能扔多次
  94. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:'.$distribution_channel_id;
  95. $customMsgSwitch = CustomMsgSwitchsMsgs::customMsgSwitchsByChannelCate($distribution_channel_id,'not_pay');
  96. if (!empty($customMsgSwitch)) {
  97. if ($customMsgSwitch['status'] == '1') {
  98. \Log::info('<----获取 开启订单未支付提醒的 渠道号---->');
  99. \Log::info($distribution_channel_id);
  100. //3,如果开启了 先获取该渠道下redis待发送的用户
  101. $not_order_users = Redis::hgetall($key);
  102. //\Log::info('<----获取 渠道下 用户---->');
  103. if (!empty($not_order_users)) {
  104. $customSendStatsCount = CustomSendDayStats::customSendDayStatsByChannelAndFromAndDay($distribution_channel_id,'not_pay',date("Y-m-d"));
  105. if (empty($customSendStatsCount)) {
  106. $customSendDayStats['distribution_channel_id'] = $distribution_channel_id;
  107. $customSendDayStats['from'] = 'not_pay';
  108. $customSendDayStats['push_user_num'] = count($not_order_users);
  109. $customSendDayStats['date'] = date("Y-m-d");
  110. CustomSendDayStats::create($customSendDayStats);
  111. }else{
  112. $customSendStatsCount['push_user_num'] = $customSendStatsCount['push_user_num']+count($not_order_users);
  113. $customSendStatsCount->save();
  114. }
  115. foreach ($not_order_users as $uid => $time) {
  116. //\Log::info('<----redis中找到用户 获取uid 时间差---->');
  117. //\Log::info($uid);
  118. //\Log::info(time()-$time);
  119. //4,计算这些用户未支付订单是否大于1小时
  120. $now_time = time()-$time;
  121. if ($now_time > 3600)
  122. {
  123. //5,从Redis中获取并判断这些用户 今日是否已经发送过“未支付订单提醒”
  124. $today_key = "sended_not_pay_uid:distribution_channel_id:".$distribution_channel_id.":day:".$today;
  125. if(!Redis::sismember($today_key, $uid))
  126. {
  127. //6,判断用户当前是否为强关用户
  128. $force_user = ForceSubscribeService::forceSubscribeUsersByUid(compact('uid'));
  129. if (!empty($force_user)) {
  130. //\Log::info('<----满足条件的强关用户 发送客服消息---->');
  131. //\Log::info($uid);
  132. //(1),发送(文字)未支付客服提醒至RebitMQ
  133. self::add_text_task($force_user);
  134. //(2), 今日的redis增加一条记录,每天只发一次
  135. Redis::sadd($today_key, $uid);
  136. Redis::expire($today_key,3600*24*3);
  137. }
  138. //7,将该用户在未支付订单待发送的Redis中移除
  139. Redis::hdel($key,$uid);
  140. }else{
  141. //8,如果发送过,直接将该用户从未支付订单待发送redis中移除
  142. Redis::hdel($key,$uid);
  143. }
  144. }
  145. }
  146. }else{
  147. \Log::info('<----redis中该渠道下没有找到用户---->');
  148. }
  149. }else{
  150. //智能推送未支付订单关闭状态
  151. //直接删除该渠道下未支付订单的redis键
  152. Redis::del($key);
  153. }
  154. }else{
  155. //智能推送还未创建(默认关闭)
  156. //直接删除该渠道下未支付订单的redis键
  157. Redis::del($key);
  158. }
  159. }
  160. }
  161. /**
  162. * RebitMQ发送24小时未支付订单提醒(文字)
  163. */
  164. static function add_text_task($force_user){
  165. $uid = $force_user['uid'];
  166. $distribution_channel_id = $force_user['distribution_channel_id'];
  167. //通过uid获取该用户最近阅读记录的书籍
  168. $userBook = ReadRecordService::getFirstReadRecord($uid);
  169. \Log::info('<----获取用户 最近阅读记录---->');
  170. if (!empty($userBook)) {
  171. $data = array();
  172. $data['openid'] = $force_user['openid'];
  173. $data['appid'] = $force_user['appid'];
  174. //用户最近阅读链接
  175. $continueReadUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com/continue?fromtype=not_pay';
  176. //拼接文案和用户最近阅读书籍的链接以及书籍名称
  177. //$data['content'] = '亲,你上次看的书还在等着你呐~点击继续阅读'."\n".'<a href="'.$continueReadUrl.'"> >《'.$userBook['book_name'].'》</a>'."\n".'首冲50元即可畅读100万字,年费VIP更可全年无限畅读。'."\n".'为方便下次阅读,请'.'<a href="https://help.leyuee.com/top.html">'.'置顶公众号'.'</a>';
  178. $data['content'] = '亲,你上次看的书还在等着你呐~'."\n".'<a href="'.$continueReadUrl.'"> >点击继续阅读</a>'."\n".'首冲50元即可畅读100万字,年费VIP更可全年无限畅读。'."\n".'为方便下次阅读,请'.'<a href="https://help.leyuee.com/top.html">'.'置顶公众号'.'</a>';
  179. $data['type'] = 'one_task';// last_task,one_task
  180. $data['task_id'] = 1;
  181. $data['send_time'] = date("Y-m-d H:i:s");
  182. $send_data=array(
  183. 'send_time'=>date("Y-m-d H:i:s"),
  184. 'data' => $data
  185. );
  186. //\Log::info($data['openid']);
  187. //\Log::info($data['appid']);
  188. //\Log::info($data['content']);
  189. $delay = 0;
  190. $job = (new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list');
  191. dispatch($job);
  192. }else{
  193. \Log::info('<----抱歉 没有最近阅读记录---->');
  194. }
  195. }
  196. /**
  197. * RebitMQ发送24小时未支付订单提醒(图文)
  198. */
  199. static function add_news_task($force_user,$officialCustomMsg){
  200. $data = array();
  201. $data['openid'] = $force_user['openid'];
  202. $data['appid'] = $force_user['appid'];
  203. $data['news_content'] = '[[{"title":"'.$officialCustomMsg['title'].'"},{"description":"'.$officialCustomMsg['description'].'"},{"url":"'.$officialCustomMsg['url'].'"},{"image":"'.$officialCustomMsg['image'].'"}]]';
  204. $data['type'] = 'one_task';// last_task,one_task
  205. $data['task_id'] = 3;
  206. $data['send_time'] = date("Y-m-d H:i:s");
  207. $send_data=array(
  208. 'send_time'=>date("Y-m-d H:i:s"),
  209. 'data' => $data
  210. );
  211. $delay = 0;
  212. $job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_news_list');
  213. dispatch($job);
  214. }
  215. /**
  216. * 三天回本推送(定时任务 12小时推)
  217. */
  218. static function add_news_recovery_push_hot(){
  219. \Log::info('=======================三天回本推送客服消息');
  220. //1,遍历渠道表
  221. $distribution_channels = ChannelService::getAllChannels();
  222. for ($k=0; $k < count($distribution_channels); $k++) {
  223. //2,判断该渠道下有没有开启 未支付订单的智能推送
  224. $distribution_channel_id = $distribution_channels[$k]['id'];
  225. $customMsgSwitch = CustomMsgSwitchsMsgs::customMsgSwitchsByChannelCate($distribution_channel_id,'recovery_push');
  226. if (!empty($customMsgSwitch)) {
  227. if ($customMsgSwitch['status'] == '1') {
  228. \Log::info('<----开启客服的 渠道号---->');
  229. \Log::info($distribution_channel_id);
  230. //3,开通智能推送的渠道获取 渠道下的强关用户
  231. $forceSubscribeUsersPrams['distribution_channel_id'] = $distribution_channel_id;
  232. $forceSubscribeUsersPrams['timeset'] = 43200;
  233. $force_user = ForceSubscribeService::forceSubscribeNewUsersByTimeset($forceSubscribeUsersPrams);
  234. //\Log::info('<----渠道下强关用户数---->');
  235. //\Log::info(count($force_user));
  236. $customSendStatsCount = CustomSendDayStats::customSendDayStatsByChannelAndFromAndDay($distribution_channel_id,'recovery_push',date("Y-m-d"));
  237. if (empty($customSendStatsCount)) {
  238. $customSendDayStats['distribution_channel_id'] = $distribution_channel_id;
  239. $customSendDayStats['from'] = 'recovery_push';
  240. $customSendDayStats['push_user_num'] = count($force_user);
  241. $customSendDayStats['date'] = date("Y-m-d");
  242. CustomSendDayStats::create($customSendDayStats);
  243. }else{
  244. $customSendStatsCount['push_user_num'] = $customSendStatsCount['push_user_num']+count($force_user);
  245. $customSendStatsCount->save();
  246. }
  247. for ($i=0; $i < count($force_user); $i++) {
  248. //(1)通过uid去已发送客服消息Redis中获取是否推送过该类型客服消息
  249. //(2)如果有,获取插入的时间,
  250. // 如果时间在今天之内,那就不发送,
  251. // 如果不是,就发送,并往Redis中插入一条数据
  252. $uid = $force_user[$i]['uid'];
  253. if(!Redis::hget('add_news_recovery_push_hot'.$uid, 'time'))
  254. {
  255. $bid = $force_user[$i]['bid'];
  256. $booksArray = [];
  257. //4,获取男频女频的标签文案
  258. $manPromotion = PromotionService::getRandomHeadline(1,5);
  259. $womanPromotion = PromotionService::getRandomHeadline(2,5);
  260. //7,获取和用户强关进来的书相同类型的5本优质评分的书籍
  261. // $books = BookConfigService::getSimpleChannelBook($bid,5);
  262. $books = BookConfigService::getSimpleChannelBookLoop($bid,5,$uid);
  263. $bookUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com';
  264. $promotionNum = 0;
  265. if ($books) {
  266. foreach ($books as $key => $value) {
  267. //8,将这四本书作为推送的2-5条信息,同样拼接书籍信息和标签文案
  268. $urlBook = $value->url = $bookUrl.$value->url.'&fromtype=recovery_push';
  269. $channelName = $value->channel_name;
  270. $cover = $value->cover;
  271. if($channelName == '男频'){
  272. $book['title'] = $manPromotion[$promotionNum]['title'];
  273. $book['description'] = $manPromotion[$promotionNum]['title'];
  274. }else{
  275. $book['title'] = $womanPromotion[$promotionNum]['title'];
  276. $book['description'] = $womanPromotion[$promotionNum]['title'];
  277. }
  278. $book['url'] = $urlBook;
  279. $book['image'] = $cover;
  280. if ($promotionNum == 0) {
  281. //第一条图片选用图库中的图片地址
  282. $promotionImages = PromotionService::getMessageRandomDocumentCovers(3);
  283. if ($promotionImages) {
  284. $book['image'] = $promotionImages[0]['link'];
  285. }else{
  286. $book['image'] = $cover;
  287. }
  288. }else{
  289. $book['image'] = $cover;
  290. }
  291. $booksArray[] = [['title'=>$book['title']],['description'=>$book['description']],['url'=>$book['url']],['image'=>$book['image']]];
  292. $promotionNum++;
  293. }
  294. //9,通过RebitMQ推送热门书籍到指定用户
  295. $data = array();
  296. $data['openid'] = $force_user[$i]['openid'];
  297. $data['appid'] = $force_user[$i]['appid'];
  298. $data['news_content'] = json_encode($booksArray);
  299. $data['type'] = 'one_task';
  300. $data['task_id'] = 3;
  301. $data['send_time'] = date("Y-m-d H:i:s");
  302. $send_data=array(
  303. 'send_time'=>date("Y-m-d H:i:s"),
  304. 'data' => $data
  305. );
  306. $delay = 0;
  307. //\Log::info('<---- 客服消息发送内容 参数---->');
  308. //\Log::info($data['openid']);
  309. //\Log::info($data['appid']);
  310. //\Log::info($data['news_content']);
  311. //\Log::info('<----Rebit-MQ 发送热门书籍推送客服消息---->');
  312. $job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_news_list');
  313. dispatch($job);
  314. Redis::hset('add_news_recovery_push_hot'.$uid, 'time', time());
  315. }
  316. }
  317. }
  318. }
  319. }
  320. }
  321. }
  322. /**
  323. * 三天回本推送(定时任务 24小时推)
  324. */
  325. static function add_news_recovery_push_activity(){
  326. \Log::info('=======================三天回本推送客服消息');
  327. //1,遍历渠道表
  328. $distribution_channels = ChannelService::getAllChannels();
  329. $customPushActivitys = CustomPushActivitys::customPushActivitys();
  330. $custom_url = $customPushActivitys['url'];
  331. $custom_image = $customPushActivitys['image'];
  332. $custom_title = $customPushActivitys['title'];
  333. for ($k=0; $k < count($distribution_channels); $k++) {
  334. //2,判断该渠道下有没有开启 未支付订单的智能推送
  335. $distribution_channel_id = $distribution_channels[$k]['id'];
  336. $customMsgSwitch = CustomMsgSwitchsMsgs::customMsgSwitchsByChannelCate($distribution_channel_id,'recovery_push');
  337. if (!empty($customMsgSwitch)) {
  338. if ($customMsgSwitch['status'] == '1') {
  339. //\Log::info('<----开启客服的 渠道号---->');
  340. //\Log::info($distribution_channel_id);
  341. //3,开通智能推送的渠道获取 渠道下的强关用户
  342. $forceSubscribeUsersPrams['distribution_channel_id'] = $distribution_channel_id;
  343. $forceSubscribeUsersPrams['timeset'] = 86400;
  344. $force_user = ForceSubscribeService::forceSubscribeNewUsersByTimeset($forceSubscribeUsersPrams);
  345. //\Log::info('<----渠道下强关用户数---->');
  346. //\Log::info(count($force_user));
  347. $customSendStatsCount = CustomSendDayStats::customSendDayStatsByChannelAndFromAndDay($distribution_channel_id,'recovery_push',date("Y-m-d"));
  348. if (empty($customSendStatsCount)) {
  349. $customSendDayStats['distribution_channel_id'] = $distribution_channel_id;
  350. $customSendDayStats['from'] = 'recovery_push';
  351. $customSendDayStats['push_user_num'] = count($force_user);
  352. $customSendDayStats['date'] = date("Y-m-d");
  353. CustomSendDayStats::create($customSendDayStats);
  354. }else{
  355. $customSendStatsCount['push_user_num'] = $customSendStatsCount['push_user_num']+count($force_user);
  356. $customSendStatsCount->save();
  357. }
  358. for ($i=0; $i < count($force_user); $i++) {
  359. //(1)通过uid去已发送客服消息Redis中获取是否推送过该类型客服消息
  360. //(2)如果有,获取插入的时间,
  361. // 如果时间在今天之内,那就不发送,
  362. // 如果不是,就发送,并往Redis中插入一条数据
  363. $uid = $force_user[$i]['uid'];
  364. if(!Redis::hget('add_news_recovery_push_activity'.$uid, 'time'))
  365. {
  366. //9,通过RebitMQ推送热门书籍到指定用户
  367. $data = array();
  368. $data['openid'] = $force_user[$i]['openid'];
  369. $data['appid'] = $force_user[$i]['appid'];
  370. $url = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com'.$custom_url;
  371. $data['news_content'] = '[[{"title":"'.$custom_title.'"},{"description":""},{"url":"'.$url.'"},{"image":"'.$custom_image.'"}]]';
  372. $data['type'] = 'one_task';
  373. $data['task_id'] = 3;
  374. $data['send_time'] = date("Y-m-d H:i:s");
  375. $send_data=array(
  376. 'send_time'=>date("Y-m-d H:i:s"),
  377. 'data' => $data
  378. );
  379. $delay = 0;
  380. //\Log::info('<---- 客服消息发送内容 参数---->');
  381. //\Log::info($data['openid']);
  382. //\Log::info($data['appid']);
  383. //\Log::info($data['news_content']);
  384. //\Log::info('<----Rebit-MQ 发送三天回本活动推送---->');
  385. $job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_news_list');
  386. // $job = (new SendNews($send_data))->onConnection('redis')->delay($delay)->onQueue('send_news_list');
  387. dispatch($job);
  388. Redis::hset('add_news_recovery_push_activity'.$uid, 'time', time());
  389. }
  390. }
  391. }
  392. }
  393. }
  394. }
  395. /**
  396. * 定制书籍推送(定时任务) point_push
  397. */
  398. static function add_news_point_book(){
  399. \Log::info('=======================定制书籍推荐客服消息');
  400. //1,遍历渠道表
  401. $distribution_channels = ChannelService::getAllChannels();
  402. for ($k=0; $k < count($distribution_channels); $k++) {
  403. //2,判断该渠道下有没有开启 未支付订单的智能推送
  404. $distribution_channel_id = $distribution_channels[$k]['id'];
  405. $customMsgSwitch = CustomMsgSwitchsMsgs::customMsgSwitchsByChannelCate($distribution_channel_id,'point_push');
  406. if (!empty($customMsgSwitch)) {
  407. if ($customMsgSwitch['status'] == '1') {
  408. //\Log::info('<----开启客服的 渠道号---->');
  409. //\Log::info($distribution_channel_id);
  410. //3,开通智能推送的渠道获取 渠道下的强关用户
  411. $forceSubscribeUsersPrams['distribution_channel_id'] = $distribution_channel_id;
  412. $forceSubscribeUsersPrams['timeset'] = 151200;// 定制数据是42小时后推送
  413. $force_user = ForceSubscribeService::forceSubscribeUsersByChannelidAndTimeset($forceSubscribeUsersPrams);
  414. //\Log::info('<----渠道下强关用户数---->');
  415. //\Log::info(count($force_user));
  416. $customSendStatsCount = CustomSendDayStats::customSendDayStatsByChannelAndFromAndDay($distribution_channel_id,'point_push',date("Y-m-d"));
  417. if (empty($customSendStatsCount)) {
  418. $customSendDayStats['distribution_channel_id'] = $distribution_channel_id;
  419. $customSendDayStats['from'] = 'point_push';
  420. $customSendDayStats['push_user_num'] = count($force_user);
  421. $customSendDayStats['date'] = date("Y-m-d");
  422. CustomSendDayStats::create($customSendDayStats);
  423. }else{
  424. $customSendStatsCount['push_user_num'] = $customSendStatsCount['push_user_num']+count($force_user);
  425. $customSendStatsCount->save();
  426. }
  427. for ($i=0; $i < count($force_user); $i++) {
  428. //(1)通过uid去已发送客服消息Redis中获取是否推送过该类型客服消息
  429. //(2)如果有,获取插入的时间,
  430. // 如果时间在今天之内,那就不发送,
  431. // 如果不是,就发送,并往Redis中插入一条数据
  432. $uid = $force_user[$i]['uid'];
  433. if(Redis::hget('add_news_top_book_uid'.$uid, 'time'))
  434. {
  435. //\Log::info('<----Redis中存在 获取uid 时间---->');
  436. //\Log::info($force_user[$i]['uid']);
  437. //\Log::info(Redis::hget('add_news_top_book_uid'.$uid, 'time'));
  438. $time = Redis::hget('add_news_top_book_uid'.$uid, 'time');
  439. if (date("Y-m-d",$time) == date("Y-m-d"))
  440. {
  441. // Redis::hdel('add_news_top_book_uid'.$uid,'time');
  442. }else{
  443. //获取定制图书推送是否是在这个时间段推送过,如果推送过这个时间点就不推送了
  444. $point_time = Redis::hget('add_news_point_book_uid'.$uid, 'time');
  445. if ($point_time) {
  446. if (date("Y-m-d",$point_time) == date("Y-m-d")) {
  447. $matchTime = time() - $point_time;
  448. if ($matchTime < 3600) {
  449. continue;
  450. }
  451. }
  452. }
  453. //\Log::info('<----Redis中时间 不是今天---->');
  454. $bid = $force_user[$i]['bid'];
  455. $booksArray = [];
  456. //4,获取男频女频的标签文案
  457. $manPromotion = PromotionService::getRandomHeadline(1,5);
  458. $womanPromotion = PromotionService::getRandomHeadline(2,5);
  459. //5,获取用户最近阅读的书籍信息
  460. $userBook = ReadRecordService::getFirstReadRecord($uid);
  461. if (!empty($userBook)) {
  462. //有最近阅读的书籍!! 头条为最近阅读,次4条为随机选取
  463. //\Log::info('<----获取用户最近阅读的书籍--->');
  464. //6,推送第一条拼接用户最近阅读的书籍信息
  465. //并通过判断该书是男频还是女频分别拼接上不同的标签文案
  466. $continueReadUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com/continue?fromtype=point_push';
  467. $channelName = $userBook['channel_name'];
  468. if($channelName == '男频'){
  469. $book['title'] = $manPromotion[0]['title'];
  470. $book['description'] = $manPromotion[0]['title'];
  471. }else{
  472. $book['title'] = $womanPromotion[0]['title'];
  473. $book['description'] = $womanPromotion[0]['title'];
  474. }
  475. $book['url'] = $continueReadUrl;
  476. //头条的图片从财哥个图库中随机取一张
  477. $promotionImages = PromotionService::getMessageRandomDocumentCovers(3);
  478. if ($promotionImages) {
  479. $book['image'] = $promotionImages[0]['link'];
  480. }else{
  481. $book['image'] = $userBook['cover'];
  482. }
  483. $booksArray[] = [['title'=>$book['title']],['description'=>$book['description']],['url'=>$book['url']],['image'=>$book['image']]];
  484. //\Log::info(json_encode($booksArray));
  485. //7,获取和用户强关进来的书相同类型的4本优质评分的书籍
  486. // $books = BookConfigService::getSimpleChannelBook($bid,4);
  487. $books = BookConfigService::getSimpleChannelBookLoop($bid,4,$uid);
  488. $bookUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com';
  489. $promotionNum = 1;
  490. if ($books) {
  491. foreach ($books as $key => $value) {
  492. //8,将这四本书作为推送的2-5条信息,同样拼接书籍信息和标签文案
  493. $urlBook = $value->url = $bookUrl.$value->url.'&fromtype=point_push';
  494. $channelName = $value->channel_name;
  495. $cover = $value->cover;
  496. if($channelName == '男频'){
  497. $book['title'] = $manPromotion[$promotionNum]['title'];
  498. $book['description'] = $manPromotion[$promotionNum]['title'];
  499. }else{
  500. $book['title'] = $womanPromotion[$promotionNum]['title'];
  501. $book['description'] = $womanPromotion[$promotionNum]['title'];
  502. }
  503. $book['url'] = $urlBook;
  504. $book['image'] = $cover;
  505. $booksArray[] = [['title'=>$book['title']],['description'=>$book['description']],['url'=>$book['url']],['image'=>$book['image']]];
  506. $promotionNum++;
  507. }
  508. //9,通过RebitMQ推送热门书籍到指定用户
  509. $data = array();
  510. $data['openid'] = $force_user[$i]['openid'];
  511. $data['appid'] = $force_user[$i]['appid'];
  512. $data['news_content'] = json_encode($booksArray);
  513. $data['type'] = 'one_task';
  514. $data['task_id'] = 3;
  515. $data['send_time'] = date("Y-m-d H:i:s");
  516. $send_data=array(
  517. 'send_time'=>date("Y-m-d H:i:s"),
  518. 'data' => $data
  519. );
  520. $delay = 0;
  521. //\Log::info('<---- 客服消息发送内容 参数---->');
  522. //\Log::info($data['openid']);
  523. //\Log::info($data['appid']);
  524. //\Log::info($data['news_content']);
  525. //\Log::info('<----Rebit-MQ 发送热门书籍推送客服消息---->');
  526. $job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_news_list');
  527. dispatch($job);
  528. Redis::hset('add_news_top_book_uid'.$uid, 'time', time());
  529. }
  530. }else{
  531. //没有最近阅读的书籍!! 从书库随机选取5本,第一本的图片用财哥图库的图片
  532. //7,获取和用户强关进来的书相同类型的4本优质评分的书籍
  533. // $books = BookConfigService::getSimpleChannelBook($bid,5);
  534. $books = BookConfigService::getSimpleChannelBookLoop($bid,5,$uid);
  535. $bookUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com';
  536. $promotionNum = 0;
  537. if ($books) {
  538. foreach ($books as $key => $value) {
  539. //8,将这四本书作为推送的2-5条信息,同样拼接书籍信息和标签文案
  540. $urlBook = $value->url = $bookUrl.$value->url.'&fromtype=point_push';
  541. $channelName = $value->channel_name;
  542. $cover = $value->cover;
  543. if($channelName == '男频'){
  544. $book['title'] = $manPromotion[$promotionNum]['title'];
  545. $book['description'] = $manPromotion[$promotionNum]['title'];
  546. }else{
  547. $book['title'] = $womanPromotion[$promotionNum]['title'];
  548. $book['description'] = $womanPromotion[$promotionNum]['title'];
  549. }
  550. $book['url'] = $urlBook;
  551. $book['image'] = $cover;
  552. if ($promotionNum == 0) {
  553. //第一条图片选用图库中的图片地址
  554. // $book['image'] = $cover;
  555. $promotionImages = PromotionService::getMessageRandomDocumentCovers(3);
  556. if ($promotionImages) {
  557. $book['image'] = $promotionImages[0]['link'];
  558. }else{
  559. $book['image'] = $cover;
  560. }
  561. }else{
  562. $book['image'] = $cover;
  563. }
  564. $booksArray[] = [['title'=>$book['title']],['description'=>$book['description']],['url'=>$book['url']],['image'=>$book['image']]];
  565. $promotionNum++;
  566. }
  567. //9,通过RebitMQ推送热门书籍到指定用户
  568. $data = array();
  569. $data['openid'] = $force_user[$i]['openid'];
  570. $data['appid'] = $force_user[$i]['appid'];
  571. $data['news_content'] = json_encode($booksArray);
  572. $data['type'] = 'one_task';
  573. $data['task_id'] = 3;
  574. $data['send_time'] = date("Y-m-d H:i:s");
  575. $send_data=array(
  576. 'send_time'=>date("Y-m-d H:i:s"),
  577. 'data' => $data
  578. );
  579. $delay = 0;
  580. //\Log::info('<---- 客服消息发送内容 参数---->');
  581. //\Log::info($data['openid']);
  582. //\Log::info($data['appid']);
  583. //\Log::info($data['news_content']);
  584. //\Log::info('<----Rebit-MQ 发送热门书籍推送客服消息---->');
  585. $job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_news_list');
  586. dispatch($job);
  587. Redis::hset('add_news_point_book_uid'.$uid, 'time', time());
  588. }
  589. }
  590. }
  591. }else{
  592. //获取热门图书推送是否是在这个时间段推送过,如果推送过这个时间点就不推送了
  593. $point_time = Redis::hget('add_news_top_book_uid'.$uid, 'time');
  594. if ($point_time) {
  595. if (date("Y-m-d",$point_time) == date("Y-m-d")) {
  596. $matchTime = time() - $point_time;
  597. if ($matchTime < 3600) {
  598. continue;
  599. }
  600. }
  601. }
  602. //\Log::info('<----Redis中时间 不是今天---->');
  603. $bid = $force_user[$i]['bid'];
  604. $booksArray = [];
  605. //4,获取男频女频的标签文案
  606. $manPromotion = PromotionService::getRandomHeadline(1,5);
  607. $womanPromotion = PromotionService::getRandomHeadline(2,5);
  608. //5,获取用户最近阅读的书籍信息
  609. $userBook = ReadRecordService::getFirstReadRecord($uid);
  610. if (!empty($userBook)) {
  611. //有最近阅读的书籍!! 头条为最近阅读,次4条为随机选取
  612. //\Log::info('<----获取用户最近阅读的书籍--->');
  613. //6,推送第一条拼接用户最近阅读的书籍信息
  614. //并通过判断该书是男频还是女频分别拼接上不同的标签文案
  615. $continueReadUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com/continue?fromtype=point_push';
  616. $channelName = $userBook['channel_name'];
  617. if($channelName == '男频'){
  618. $book['title'] = $manPromotion[0]['title'];
  619. $book['description'] = $manPromotion[0]['title'];
  620. }else{
  621. $book['title'] = $womanPromotion[0]['title'];
  622. $book['description'] = $womanPromotion[0]['title'];
  623. }
  624. $book['url'] = $continueReadUrl;
  625. //头条的图片从财哥个图库中随机取一张
  626. $promotionImages = PromotionService::getMessageRandomDocumentCovers(3);
  627. if ($promotionImages) {
  628. $book['image'] = $promotionImages[0]['link'];
  629. }else{
  630. $book['image'] = $userBook['cover'];
  631. }
  632. $booksArray[] = [['title'=>$book['title']],['description'=>$book['description']],['url'=>$book['url']],['image'=>$book['image']]];
  633. //\Log::info(json_encode($booksArray));
  634. //7,获取和用户强关进来的书相同类型的4本优质评分的书籍
  635. // $books = BookConfigService::getSimpleChannelBook($bid,4);
  636. $books = BookConfigService::getSimpleChannelBookLoop($bid,4,$uid);
  637. $bookUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com';
  638. $promotionNum = 1;
  639. if ($books) {
  640. foreach ($books as $key => $value) {
  641. //8,将这四本书作为推送的2-5条信息,同样拼接书籍信息和标签文案
  642. $urlBook = $value->url = $bookUrl.$value->url.'&fromtype=point_push';
  643. $channelName = $value->channel_name;
  644. $cover = $value->cover;
  645. if($channelName == '男频'){
  646. $book['title'] = $manPromotion[$promotionNum]['title'];
  647. $book['description'] = $manPromotion[$promotionNum]['title'];
  648. }else{
  649. $book['title'] = $womanPromotion[$promotionNum]['title'];
  650. $book['description'] = $womanPromotion[$promotionNum]['title'];
  651. }
  652. $book['url'] = $urlBook;
  653. $promotionImages = PromotionService::getMessageRandomDocumentCovers(3);
  654. if ($promotionImages) {
  655. $book['image'] = $promotionImages[0]['link'];
  656. }else{
  657. $book['image'] = $cover;
  658. }
  659. $booksArray[] = [['title'=>$book['title']],['description'=>$book['description']],['url'=>$book['url']],['image'=>$book['image']]];
  660. $promotionNum++;
  661. }
  662. //9,通过RebitMQ推送热门书籍到指定用户
  663. $data = array();
  664. $data['openid'] = $force_user[$i]['openid'];
  665. $data['appid'] = $force_user[$i]['appid'];
  666. $data['news_content'] = json_encode($booksArray);
  667. $data['type'] = 'one_task';
  668. $data['task_id'] = 3;
  669. $data['send_time'] = date("Y-m-d H:i:s");
  670. $send_data=array(
  671. 'send_time'=>date("Y-m-d H:i:s"),
  672. 'data' => $data
  673. );
  674. $delay = 0;
  675. //\Log::info('<---- 客服消息发送内容 参数---->');
  676. //\Log::info($data['openid']);
  677. //\Log::info($data['appid']);
  678. //\Log::info($data['news_content']);
  679. //\Log::info('<----Rebit-MQ 发送热门书籍推送客服消息---->');
  680. $job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_news_list');
  681. dispatch($job);
  682. Redis::hset('add_news_point_book_uid'.$uid, 'time', time());
  683. }
  684. }else{
  685. //没有最近阅读的书籍!! 从书库随机选取5本,第一本的图片用财哥图库的图片
  686. //7,获取和用户强关进来的书相同类型的4本优质评分的书籍
  687. // $books = BookConfigService::getSimpleChannelBook($bid,5);
  688. $books = BookConfigService::getSimpleChannelBookLoop($bid,5,$uid);
  689. $bookUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com';
  690. $promotionNum = 0;
  691. if ($books) {
  692. foreach ($books as $key => $value) {
  693. //8,将这四本书作为推送的2-5条信息,同样拼接书籍信息和标签文案
  694. $urlBook = $value->url = $bookUrl.$value->url.'&fromtype=point_push';
  695. $channelName = $value->channel_name;
  696. $cover = $value->cover;
  697. if($channelName == '男频'){
  698. $book['title'] = $manPromotion[$promotionNum]['title'];
  699. $book['description'] = $manPromotion[$promotionNum]['title'];
  700. }else{
  701. $book['title'] = $womanPromotion[$promotionNum]['title'];
  702. $book['description'] = $womanPromotion[$promotionNum]['title'];
  703. }
  704. $book['url'] = $urlBook;
  705. $book['image'] = $cover;
  706. if ($promotionNum == 0) {
  707. //第一条图片选用图库中的图片地址
  708. $promotionImages = PromotionService::getMessageRandomDocumentCovers(3);
  709. if ($promotionImages) {
  710. $book['image'] = $promotionImages[0]['link'];
  711. }else{
  712. $book['image'] = $cover;
  713. }
  714. }else{
  715. $book['image'] = $cover;
  716. }
  717. $booksArray[] = [['title'=>$book['title']],['description'=>$book['description']],['url'=>$book['url']],['image'=>$book['image']]];
  718. $promotionNum++;
  719. }
  720. //9,通过RebitMQ推送热门书籍到指定用户
  721. $data = array();
  722. $data['openid'] = $force_user[$i]['openid'];
  723. $data['appid'] = $force_user[$i]['appid'];
  724. $data['news_content'] = json_encode($booksArray);
  725. $data['type'] = 'one_task';
  726. $data['task_id'] = 3;
  727. $data['send_time'] = date("Y-m-d H:i:s");
  728. $send_data=array(
  729. 'send_time'=>date("Y-m-d H:i:s"),
  730. 'data' => $data
  731. );
  732. $delay = 0;
  733. //\Log::info('<---- 客服消息发送内容 参数---->');
  734. //\Log::info($data['openid']);
  735. //\Log::info($data['appid']);
  736. //\Log::info($data['news_content']);
  737. //\Log::info('<----Rebit-MQ 发送热门书籍推送客服消息---->');
  738. $job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_news_list');
  739. dispatch($job);
  740. Redis::hset('add_news_point_book_uid'.$uid, 'time', time());
  741. }
  742. }
  743. }
  744. }
  745. }
  746. }
  747. }
  748. }
  749. /**
  750. * 未充值用户推送客服消息(24-48小时内关注)(定时任务)
  751. */
  752. static function unPaidUserActivity(){
  753. \Log::info('=======================未充值用户推荐客服消息');
  754. //1,遍历渠道表
  755. $distribution_channels = ChannelService::getAllChannels();
  756. for ($k=0; $k < count($distribution_channels); $k++) {
  757. //2,判断该渠道下有没有开启 未支付订单的智能推送
  758. $distribution_channel_id = $distribution_channels[$k]['id'];
  759. // 嘉言小说
  760. // if(!in_array($distribution_channel_id,array('2'))){
  761. // // \Log::info('unPaidUserActivity_continue,$distribution_channel_id:'.$distribution_channel_id);
  762. // continue;
  763. // }
  764. $customMsgSwitch = CustomMsgSwitchsMsgs::customMsgSwitchsByChannelCate($distribution_channel_id,'unpaid_send_activity');
  765. if (!empty($customMsgSwitch)) {
  766. if ($customMsgSwitch['status'] == '1') {
  767. //\Log::info('<----开启未付费推送的 渠道号---->:'.$distribution_channel_id);
  768. //3,开通智能推送的渠道获取 渠道下的强关用户
  769. $forceSubscribeUsersPrams['distribution_channel_id'] = $distribution_channel_id;
  770. $forceSubscribeUsersPrams['start_time'] = date('Y-m-d H:i:s',strtotime('-2 day'));
  771. $forceSubscribeUsersPrams['end_time'] = date('Y-m-d H:i:s',strtotime('-1 day'));
  772. $force_user = ForceSubscribeService::forceSubscribeNewUnpaidUsersByTime($forceSubscribeUsersPrams);
  773. //\Log::info('<----渠道下强关用户数---->');
  774. //\Log::info($force_user);
  775. //\Log::info(count($force_user));
  776. $push_user_num = 0;
  777. for ($i=0; $i < count($force_user); $i++) {
  778. //(1)通过uid去已发送客服消息Redis中获取是否推送过该类型客服消息
  779. //(2)如果有,获取插入的时间,
  780. // 每个新关用户只发送一次
  781. // 如果不是,就发送,并往Redis中插入一条数据
  782. $uid = $force_user[$i]['uid'];
  783. if(!Redis::hget('unpaid_send_activity_uid:'.$uid, 'time'))
  784. {
  785. $bid = $force_user[$i]['bid'];
  786. $data = array();
  787. $data['openid'] = $force_user[$i]['openid'];
  788. $data['appid'] = $force_user[$i]['appid'];
  789. //活动链接
  790. $continueReadUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com/sale/newUserActivity?fromtype=unpaid_send_activity&send_time='.time();
  791. $data['content'] = '您的新用户专享礼包已送达'."\n\n".'充9.9元,得2000书币,只有一次机会哦!'."\n\n".'过期失效,不要错过!'."\n\n".'<a href="'.$continueReadUrl.'">点击立即领取> ></a>'."\n\n".'为方便下次阅读,请'.'<a href="https://help.leyuee.com/top.html">'.'置顶公众号'.'</a>';
  792. $data['type'] = 'one_task';// last_task,one_task
  793. $data['task_id'] = 1;
  794. $data['send_time'] = date("Y-m-d H:i:s");
  795. $send_data=array(
  796. 'send_time'=>date("Y-m-d H:i:s"),
  797. 'data' => $data
  798. );
  799. //\Log::info('unpaid_send_activity_openid:'.$data['openid'].' appid:'.$data['appid'].' content:'.$data['content']);
  800. $delay = 0;
  801. $job = (new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list');
  802. dispatch($job);
  803. Redis::hset('unpaid_send_activity_uid:'.$uid, 'time', time());
  804. Redis::EXPIRE('unpaid_send_activity_uid:'.$uid,86400);
  805. //\Log::info('unpaid_send_activity_settime_uid:'.$uid.' time:'.time());
  806. $push_user_num++;
  807. }else{
  808. \Log::info('unpaid_send_activity_exist_uid:'.$force_user[$i]['uid'].' time:'.Redis::hget('unpaid_send_activity_uid:'.$uid, 'time'));
  809. }
  810. }
  811. \Log::info('unpaid_send_activity,push_user_num:'.$push_user_num);
  812. // 因为有些用户重复筛选,所以人数在后面更新
  813. $customSendStatsCount = CustomSendDayStats::customSendDayStatsByChannelAndFromAndDay($distribution_channel_id,'unpaid_send_activity',date("Y-m-d"));
  814. if (empty($customSendStatsCount)) {
  815. $customSendDayStats['distribution_channel_id'] = $distribution_channel_id;
  816. $customSendDayStats['from'] = 'unpaid_send_activity';
  817. $customSendDayStats['push_user_num'] = $push_user_num;
  818. $customSendDayStats['date'] = date("Y-m-d");
  819. CustomSendDayStats::create($customSendDayStats);
  820. }else{
  821. $customSendStatsCount['push_user_num'] = $customSendStatsCount['push_user_num']+$push_user_num;
  822. $customSendStatsCount->save();
  823. }
  824. }else{
  825. \Log::info('<----未开启 未付费推送的 渠道号---->:'.$distribution_channel_id);
  826. }
  827. }
  828. }
  829. }
  830. /**
  831. * 热门书籍推送(定时任务)hot_push
  832. */
  833. static function add_news_top_book(){
  834. //1,遍历渠道表
  835. $distribution_channels = ChannelService::getAllChannels();
  836. for ($k=0; $k < count($distribution_channels); $k++) {
  837. //2,判断该渠道下有没有开启 未支付订单的智能推送
  838. $distribution_channel_id = $distribution_channels[$k]['id'];
  839. $customMsgSwitch = CustomMsgSwitchsMsgs::customMsgSwitchsByChannelCate($distribution_channel_id,'hot_push');
  840. if (!empty($customMsgSwitch)) {
  841. if ($customMsgSwitch['status'] == '1') {
  842. //\Log::info('<----开启客服的 渠道号---->');
  843. //\Log::info($distribution_channel_id);
  844. //3,开通智能推送的渠道获取 渠道下的强关用户
  845. $forceSubscribeUsersPrams['distribution_channel_id'] = $distribution_channel_id;
  846. $forceSubscribeUsersPrams['timeset'] = 86400;//热门书籍24小时
  847. $force_user = ForceSubscribeService::forceSubscribeUsersByChannelidAndTimeset($forceSubscribeUsersPrams);
  848. // \Log::info('<----符合条件的用户数---->');
  849. // \Log::info(count($force_user));
  850. $customSendStatsCount = CustomSendDayStats::customSendDayStatsByChannelAndFromAndDay($distribution_channel_id,'hot_push',date("Y-m-d"));
  851. if (empty($customSendStatsCount)) {
  852. $customSendDayStats['distribution_channel_id'] = $distribution_channel_id;
  853. $customSendDayStats['from'] = 'point_push';
  854. $customSendDayStats['push_user_num'] = count($force_user);
  855. $customSendDayStats['date'] = date("Y-m-d");
  856. CustomSendDayStats::create($customSendDayStats);
  857. }else{
  858. $customSendStatsCount['push_user_num'] = $customSendStatsCount['push_user_num']+count($force_user);
  859. $customSendStatsCount->save();
  860. }
  861. for ($i=0; $i < count($force_user); $i++) {
  862. $uid = $force_user[$i]['uid'];
  863. if(Redis::hget('add_news_top_book_uid'.$uid, 'time'))
  864. {
  865. $time = Redis::hget('add_news_point_book_uid'.$uid, 'time');
  866. if (date("Y-m-d",$time) == date("Y-m-d"))
  867. {
  868. }else{
  869. //获取定制图书推送是否是在这个时间段推送过,如果推送过这个时间点就不推送了
  870. $top_time = Redis::hget('add_news_point_book_uid'.$uid, 'time');
  871. if ($top_time) {
  872. if (date("Y-m-d",$top_time) == date("Y-m-d")) {
  873. $matchTime = time() - $top_time;
  874. if ($matchTime < 3600) {
  875. continue;
  876. }
  877. }
  878. }
  879. //\Log::info('<---- Redis中存在 且满足条件的 强关用户 uid---->');
  880. //\Log::info($uid);
  881. //4,获取和用户强关进来的书相同类型的5本优质评分的书籍
  882. $bid = $force_user[$i]['bid'];
  883. // $books = BookConfigService::getSimpleChannelBook($bid,5);
  884. $books = BookConfigService::getSimpleChannelBookLoop($bid,5,$uid);
  885. $bookUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com';
  886. $booksArray = [];
  887. //5,获取男频女频的标签文案
  888. $womanPromotion = PromotionService::getRandomHeadline(2,count($books));
  889. $manPromotion = PromotionService::getRandomHeadline(1,count($books));
  890. $promotionNum = 0;
  891. if ($books) {
  892. foreach ($books as $key => $value) {
  893. //6,将这5本书作为推送的1-5条信息,
  894. //通过判断书籍是男频还是女频拼接书籍信息的不同标签文案
  895. $urlBook = $value->url = $bookUrl.$value->url.'&fromtype=hot_push';
  896. $channelName = $value->channel_name;
  897. $cover = $value->cover;
  898. if($channelName == '男频'){
  899. $book['title'] = $manPromotion[$promotionNum]['title'];
  900. $book['description'] = $manPromotion[$promotionNum]['title'];
  901. }else{
  902. $book['title'] = $womanPromotion[$promotionNum]['title'];
  903. $book['description'] = $womanPromotion[$promotionNum]['title'];
  904. }
  905. $book['url'] = $urlBook;
  906. // $book['image'] = $cover;
  907. if ($promotionNum == 0) {
  908. //第一条图片选用图库中的图片地址
  909. $promotionImages = PromotionService::getMessageRandomDocumentCovers(3);
  910. if ($promotionImages) {
  911. $book['image'] = $promotionImages[0]['link'];
  912. }else{
  913. $book['image'] = $cover;
  914. }
  915. }else{
  916. $book['image'] = $cover;
  917. }
  918. $booksArray[$key] = [['title'=>$book['title']],['description'=>$book['description']],['url'=>$book['url']],['image'=>$book['image']]];
  919. $promotionNum++;
  920. }
  921. //7,通过RebitMQ推送热门书籍到指定用户
  922. $data = array();
  923. $data['openid'] = $force_user[$i]['openid'];
  924. $data['appid'] = $force_user[$i]['appid'];
  925. $data['news_content'] = json_encode($booksArray);
  926. $data['type'] = 'one_task';
  927. $data['task_id'] = 3;
  928. $data['send_time'] = date("Y-m-d H:i:s");
  929. $send_data=array(
  930. 'send_time'=>date("Y-m-d H:i:s"),
  931. 'data' => $data
  932. );
  933. $delay = 0;
  934. /*\Log::info('<---- 客服消息发送内容 参数---->');
  935. \Log::info($data['openid']);
  936. \Log::info($data['appid']);*/
  937. //\Log::info($data['news_content']);
  938. $job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_news_list');
  939. dispatch($job);
  940. Redis::hset('add_news_point_book_uid'.$uid, 'time', time());
  941. }
  942. }
  943. }else{
  944. //获取定制图书推送是否是在这个时间段推送过,如果推送过这个时间点就不推送了
  945. $top_time = Redis::hget('add_news_point_book_uid'.$uid, 'time');
  946. if ($top_time) {
  947. if (date("Y-m-d",$top_time) == date("Y-m-d")) {
  948. $matchTime = time() - $top_time;
  949. if ($matchTime < 3600) {
  950. continue;
  951. }
  952. }
  953. }
  954. //\Log::info('<---- Redis不存在的 强关用户 uid---->');
  955. //\Log::info($uid);
  956. //4,获取和用户强关进来的书相同类型的5本优质评分的书籍
  957. $bid = $force_user[$i]['bid'];
  958. // $books = BookConfigService::getSimpleChannelBook($bid,5);
  959. $books = BookConfigService::getSimpleChannelBookLoop($bid,5,$uid);
  960. // if (count($books)>0) {
  961. // # code...
  962. // }
  963. $bookUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com';
  964. $booksArray = [];
  965. //5,获取男频女频的标签文案
  966. $womanPromotion = PromotionService::getRandomHeadline(2,count($books));
  967. $manPromotion = PromotionService::getRandomHeadline(1,count($books));
  968. $promotionNum = 0;
  969. if ($books) {
  970. foreach ($books as $key => $value) {
  971. //6,将这5本书作为推送的1-5条信息,
  972. //通过判断书籍是男频还是女频拼接书籍信息的不同标签文案
  973. $urlBook = $value->url = $bookUrl.$value->url.'&fromtype=hot_push';
  974. $channelName = $value->channel_name;
  975. $cover = $value->cover;
  976. if($channelName == '男频'){
  977. $book['title'] = $manPromotion[$promotionNum]['title'];
  978. $book['description'] = $manPromotion[$promotionNum]['title'];
  979. }else{
  980. $book['title'] = $womanPromotion[$promotionNum]['title'];
  981. $book['description'] = $womanPromotion[$promotionNum]['title'];
  982. }
  983. $book['url'] = $urlBook;
  984. if ($promotionNum == 0) {
  985. //第一条图片选用图库中的图片地址
  986. $promotionImages = PromotionService::getMessageRandomDocumentCovers(3);
  987. if ($promotionImages) {
  988. $book['image'] = $promotionImages[0]['link'];
  989. }else{
  990. $book['image'] = $cover;
  991. }
  992. }else{
  993. $book['image'] = $cover;
  994. }
  995. $booksArray[$key] = [['title'=>$book['title']],['description'=>$book['description']],['url'=>$book['url']],['image'=>$book['image']]];
  996. $promotionNum++;
  997. }
  998. //7,通过RebitMQ推送热门书籍到指定用户
  999. $data = array();
  1000. $data['openid'] = $force_user[$i]['openid'];
  1001. $data['appid'] = $force_user[$i]['appid'];
  1002. $data['news_content'] = json_encode($booksArray);
  1003. $data['type'] = 'one_task';
  1004. $data['task_id'] = 3;
  1005. $data['send_time'] = date("Y-m-d H:i:s");
  1006. $send_data=array(
  1007. 'send_time'=>date("Y-m-d H:i:s"),
  1008. 'data' => $data
  1009. );
  1010. $delay = 0;
  1011. /*\Log::info('<---- 客服消息发送内容 参数---->');
  1012. \Log::info($data['openid']);
  1013. \Log::info($data['appid']);*/
  1014. //\Log::info($data['news_content']);
  1015. $job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_news_list');
  1016. dispatch($job);
  1017. Redis::hset('add_news_top_book_uid'.$uid, 'time', time());
  1018. }
  1019. }
  1020. }
  1021. }
  1022. }
  1023. }
  1024. }
  1025. /**
  1026. * 测试向指定用户发送热门书籍
  1027. */
  1028. static function add_news_top_book_test($force_user){
  1029. $bid = $force_user['bid'];
  1030. $distribution_channel_id = $force_user['distribution_channel_id'];
  1031. $uid = $force_user['uid'];
  1032. $booksArray = [];
  1033. //获取男频女频标签文案
  1034. $womanPromotion = PromotionService::getRandomHeadline(2,5);
  1035. $manPromotion = PromotionService::getRandomHeadline(1,5);
  1036. //获取用户最近阅读记录的书籍
  1037. $userBook = ReadRecordService::getFirstReadRecord($uid);
  1038. if (!empty($userBook)) {
  1039. //推送第一条放最近阅读的书籍,并通过该书分类拼接上标签文案
  1040. $continueReadUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com/continue';
  1041. $channelName = $userBook['channel_name'];
  1042. if($channelName == '男频'){
  1043. $book['title'] = $manPromotion[0]['title'];
  1044. $book['description'] = $manPromotion[0]['title'];
  1045. }else{
  1046. $book['title'] = $womanPromotion[0]['title'];
  1047. $book['description'] = $womanPromotion[0]['title'];
  1048. }
  1049. $book['url'] = $continueReadUrl;
  1050. $book['image'] = $userBook['cover'];
  1051. $booksArray[] = [['title'=>$book['title']],['description'=>$book['description']],['url'=>$book['url']],['image'=>$book['image']]];
  1052. // $books = BookConfigService::getSimpleChannelBook($bid,4);
  1053. $books = BookConfigService::getSimpleChannelBookLoop($bid,4,$uid);
  1054. $bookUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com';
  1055. $promotionNum = 1;
  1056. foreach ($books as $key => $value) {
  1057. //推送2-4条放相同类别的4本高评分的书籍
  1058. $urlBook = $value->url = $bookUrl.$value->url;
  1059. $channelName = $value->channel_name;
  1060. $cover = $value->cover;
  1061. if($channelName == '男频'){
  1062. $book['title'] = $manPromotion[$promotionNum]['title'];
  1063. $book['description'] = $manPromotion[$promotionNum]['title'];
  1064. }else{
  1065. $book['title'] = $womanPromotion[$promotionNum]['title'];
  1066. $book['description'] = $womanPromotion[$promotionNum]['title'];
  1067. }
  1068. $book['url'] = $urlBook;
  1069. $book['image'] = $cover;
  1070. $booksArray[] = [['title'=>$book['title']],['description'=>$book['description']],['url'=>$book['url']],['image'=>$book['image']]];
  1071. $promotionNum++;
  1072. }
  1073. //7,通过RebitMQ推送热门书籍到指定用户
  1074. $data = array();
  1075. $data['openid'] = $force_user['openid'];
  1076. $data['appid'] = $force_user['appid'];
  1077. $data['news_content'] = json_encode($booksArray);
  1078. $data['type'] = 'one_task';
  1079. $data['task_id'] = 3;
  1080. $data['send_time'] = date("Y-m-d H:i:s");
  1081. $send_data=array(
  1082. 'send_time'=>date("Y-m-d H:i:s"),
  1083. 'data' => $data
  1084. );
  1085. $delay = 0;
  1086. $job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_news_list');
  1087. dispatch($job);
  1088. }
  1089. }
  1090. /**
  1091. * 付费用户每天智能推送(定时任务 每天8点推送一次昨天的用户)
  1092. * 需要开关控制
  1093. */
  1094. static function add_template_pay_push_daily() {
  1095. // 从动态推送表中获取
  1096. $customPushConfig = CustomPushConfigs::customPushConfigs('pay_daily_push');
  1097. //\Log::info($customPushConfig);
  1098. //\Log::info('add_template_pay_push_daily_customPushConfig:'.json_encode($customPushConfig));
  1099. // 随机文案
  1100. $config_contents = CustomPushConfigContents::getCustomPushConfigContents();
  1101. // \Log::info($config_contents);
  1102. $template_content_num = rand(0,count($config_contents)-1);
  1103. // $template_content_num = 9;
  1104. $template_content = $common_template_id = '';
  1105. foreach($config_contents as $key=>$config_content){
  1106. if($template_content_num == $key){
  1107. $template_content = $config_content['template_format_content'];
  1108. $common_template_id = $config_content['common_template_id'];
  1109. break;
  1110. }
  1111. // \Log::info('add_template_pay_push_daily_$key:'.$key.' template_content:'.$config_content);
  1112. }
  1113. \Log::info('add_template_pay_push_daily_template_content_num:'.$template_content_num.' template_content:'.$template_content);
  1114. if(empty($template_content)){
  1115. $template_content = $customPushConfig['template_content'];
  1116. }
  1117. if(empty($common_template_id)){
  1118. $common_template_id = $customPushConfig['common_template_id'];
  1119. }
  1120. // $template_content = $customPushConfig['template_content'];
  1121. $name = $customPushConfig['name'];
  1122. $is_all_push = $customPushConfig['is_all_push'];
  1123. $remark = $customPushConfig['remark'];
  1124. $redirect_url = $customPushConfig['redirect_url'];
  1125. //1,遍历渠道表
  1126. $distribution_channels = ChannelService::getAllChannels();
  1127. for ($k=0; $k < count($distribution_channels); $k++) {
  1128. //2,判断该渠道下有没有开启 付费用户每天智能推送
  1129. $distribution_channel_id = $distribution_channels[$k]['id'];
  1130. \Log::info('add_template_pay_push_daily_start:'.$distribution_channel_id);
  1131. $customMsgSwitch = CustomMsgSwitchsMsgs::customMsgSwitchsByChannelCate($distribution_channel_id,'pay_daily_push');
  1132. if (!empty($customMsgSwitch)) {
  1133. if ($customMsgSwitch['status'] == '1') {
  1134. \Log::info('add_template_pay_push_daily_open_start:'.$distribution_channel_id);
  1135. $official_accounts = OfficialAccount::officialAuthAccountBydistributionChannelId($distribution_channel_id,1,1);
  1136. $appids = array();
  1137. foreach($official_accounts as $official_account){
  1138. $appids[] = $official_account->appid;
  1139. }
  1140. $appids_str = implode(',', $appids);
  1141. $wechatTemplateMsgs['appid'] = $appids_str;// 由渠道得到appids
  1142. $wechatTemplateMsgs['common_template_id'] = $common_template_id;
  1143. $wechatTemplateMsgs['name'] = $name;
  1144. $wechatTemplateMsgs['send_time'] = date('Y-m-d H:i:s',strtotime('+1 minute'));// 马上执行
  1145. $wechatTemplateMsgs['template_content'] = $template_content;
  1146. //用户最近阅读链接
  1147. $continueReadUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com/'.$redirect_url;
  1148. $wechatTemplateMsgs['redirect_url'] = $continueReadUrl;
  1149. $wechatTemplateMsgs['distribution_channel_id'] = $distribution_channel_id;
  1150. $wechatTemplateMsgs['remark'] = $remark;
  1151. $wechatTemplateMsgs['is_show_list'] = 0;// 界面不展示
  1152. $wechatTemplateMsgs['subscribe_time'] = 'z';// 关注时间不限
  1153. $wechatTemplateMsgs['sex'] = 'z';
  1154. $wechatTemplateMsgs['balance'] = 'z';
  1155. $wechatTemplateMsgs['order_type'] = 'd';// 昨天消费
  1156. $wechatTemplateMsgs['category_id'] = 'z';
  1157. //\Log::info('$wechatTemplateMsgs:'.json_encode($wechatTemplateMsgs));
  1158. $resultStatus = WechatTemplateService::addWechatTemplateMsg($wechatTemplateMsgs);
  1159. }
  1160. }
  1161. }
  1162. }
  1163. /**
  1164. * 客服消息智能推送方法--公用版
  1165. * 以下3个板块找到对应的推送
  1166. * 1、用户
  1167. * 2、内容
  1168. * 3、队列类型
  1169. */
  1170. static function SmartPush($category_type){
  1171. if(empty($category_type)) return false;
  1172. $base_category_type = self::get_base_category_type($category_type);
  1173. \Log::info('smart_push:'.$category_type.' base_category_type:'.$base_category_type);
  1174. // 先更新书库和测书信息,避免频繁查询
  1175. BookConfigService::resetBookLibRedis($category_type);
  1176. //1,遍历渠道表
  1177. $distribution_channels = ChannelService::getAllChannels();
  1178. for ($k=0; $k < count($distribution_channels); $k++) {
  1179. //2,判断该渠道下有没有开启 未支付订单的智能推送
  1180. $distribution_channel_id = $distribution_channels[$k]['id'];
  1181. $customMsgSwitch = CustomMsgSwitchsMsgs::customMsgSwitchsByChannelCate($distribution_channel_id,$base_category_type);
  1182. if (!empty($customMsgSwitch)) {
  1183. if ($customMsgSwitch['status'] == '1') {
  1184. \Log::info('SmartPush_channel_id:'.$distribution_channel_id.' category_type:'.$category_type.' is_self_content:'.$customMsgSwitch['is_self_content']);
  1185. // 如果自定义内容发送
  1186. $sexs = array('z');
  1187. if($customMsgSwitch['is_self_content']){
  1188. $sexs = array('a','b');
  1189. }
  1190. $user_total_count = 0;
  1191. foreach($sexs as $sex){
  1192. \Log::info('smart_push_sex:'.$sex);
  1193. $smartPushsMsg = array();
  1194. $smartPushsMsg['distribution_channel_id'] = $distribution_channel_id;
  1195. $smartPushsMsg['category_type'] = $category_type;
  1196. $smartPushsMsg['sex'] = $sex;
  1197. $send_content = '';
  1198. if($customMsgSwitch['is_self_content']){
  1199. $smartPushsMsgData = SmartPushMsgService::smartPushByDistributionChannelIdAndCategorySex($smartPushsMsg);
  1200. //\Log::info('$smartPushsMsgData:');
  1201. //\Log::info($smartPushsMsgData);
  1202. if(!empty($smartPushsMsgData)){
  1203. // 带上参数
  1204. $send_content = TemplateCustomSendService::convert_custom_content($smartPushsMsgData->content, 'smart_push_'.$base_category_type, $distribution_channel_id,0);
  1205. $send_content = json_decode($send_content);
  1206. }
  1207. }
  1208. $skip = 0;
  1209. $loop = 0;
  1210. while ( $skip <= 250000) {
  1211. $loop++;
  1212. $force_user = null;
  1213. $force_user = self::get_send_force_users($distribution_channel_id,$category_type,$sex,$skip);
  1214. \Log::info('SmartPush_usernum:'.count($force_user).' loop:'.$loop.' distribution_channel_id:'.$distribution_channel_id.' memory:'.memory_get_usage());
  1215. $force_user_num = count($force_user);
  1216. $skip += $force_user_num;
  1217. $user_total_count += $force_user_num;
  1218. if($force_user_num > 0){
  1219. for ($i=0; $i < $force_user_num; $i++) {
  1220. $uid = $force_user[$i]['uid'];
  1221. $bid = $force_user[$i]['bid'];
  1222. if(empty($bid)) $bid=1;// TODO
  1223. // 非自定义,则根据bid取系统推荐的书籍
  1224. if(empty($customMsgSwitch['is_self_content'])){
  1225. \Log::info('get_user_send_content_start');
  1226. $send_content = null;
  1227. $send_content = self::get_user_send_content($bid,$distribution_channel_id,$category_type,$uid);
  1228. }else{
  1229. \Log::info('get_user_send_content_not_start:'.$uid.' memory:'.memory_get_usage());
  1230. }
  1231. if(empty($send_content)){
  1232. \Log::info('smart_push:openid:'.$force_user[$i]['openid'].' appid:'.$force_user[$i]['appid'].' content_is_null');
  1233. continue;
  1234. }
  1235. $smart_push_queue = self::get_smart_push_queue($category_type);
  1236. $data = array();
  1237. $data['openid'] = $force_user[$i]['openid'];
  1238. $data['appid'] = $force_user[$i]['appid'];
  1239. if (isset($smart_push_queue['is_news']) && $smart_push_queue['is_news']){
  1240. $data[$smart_push_queue['content']] = json_encode($send_content);
  1241. }else{
  1242. $data[$smart_push_queue['content']] = $send_content;
  1243. }
  1244. $data['type'] = 'one_task';
  1245. $data['task_id'] = 3;
  1246. $data['send_time'] = date("Y-m-d H:i:s");
  1247. $send_data=array(
  1248. 'send_time'=>date("Y-m-d H:i:s"),
  1249. 'data' => $data
  1250. );
  1251. $delay = 0;
  1252. // \Log::info('smart_push:'.json_encode($data));
  1253. if (isset($smart_push_queue['is_news']) && $smart_push_queue['is_news']){
  1254. $job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue($smart_push_queue['queue']);
  1255. }else{
  1256. $job = (new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue($smart_push_queue['queue']);
  1257. }
  1258. dispatch($job);
  1259. \Log::info('smart_push_user_end:'.$uid.' memory:'.memory_get_usage());
  1260. // 释放内存
  1261. $send_data = $data = null;
  1262. }//end users
  1263. if($force_user_num < 10000)//低于单次获取的10000条,所以下次不必获取,必须与单次获取的数值保持一致!
  1264. {
  1265. \Log::info('smart_push user num less than once get num, so break ,distribution_channel_id:'.$distribution_channel_id.' category_type:'. $category_type.' sex:'.$sex.' loop:'.$loop.' memory:'.memory_get_usage());
  1266. break;
  1267. }
  1268. }else{
  1269. \Log::info('smart_push_has_no_user,distribution_channel_id:'.$distribution_channel_id.' category_type:'. $category_type.' sex:'.$sex.' loop:'.$loop.' memory:'.memory_get_usage());
  1270. break;
  1271. }
  1272. }
  1273. } // end sex
  1274. \Log::info('SmartPush_end:'.' distribution_channel_id:'.$distribution_channel_id.' memory:'.memory_get_usage());
  1275. // 后面计数
  1276. $customSendStatsCount = CustomSendDayStats::customSendDayStatsByChannelAndFromAndDay($distribution_channel_id,$base_category_type,date("Y-m-d"));
  1277. if (empty($customSendStatsCount)) {
  1278. $customSendDayStats['distribution_channel_id'] = $distribution_channel_id;
  1279. $customSendDayStats['from'] = $base_category_type;
  1280. $customSendDayStats['push_user_num'] = $user_total_count;
  1281. $customSendDayStats['date'] = date("Y-m-d");
  1282. CustomSendDayStats::create($customSendDayStats);
  1283. }else{
  1284. $customSendStatsCount['push_user_num'] = $customSendStatsCount['push_user_num']+$user_total_count;
  1285. $customSendStatsCount->save();
  1286. }
  1287. }
  1288. }// end 开关
  1289. }
  1290. }
  1291. static function get_base_category_type($category_type){
  1292. if($category_type == 'unpaid_hot_push' || $category_type =='all_hot_push'){
  1293. return 'hot_push';
  1294. }
  1295. if($category_type == 'paid_point_push' || $category_type =='unpaid_point_push'){
  1296. return 'point_push';
  1297. }
  1298. if($category_type == 'unpaid_send_big_activity'){
  1299. return 'unpaid_send_activity';
  1300. }
  1301. return $category_type;
  1302. }
  1303. static function get_smart_push_queue($category_type){
  1304. $smart_push_queue = array();
  1305. $text_queue = array('unpaid_send_activity','unpaid_send_big_activity','pay_daily_push');
  1306. if(in_array($category_type,$text_queue)){
  1307. $smart_push_queue['is_news'] = false;
  1308. $smart_push_queue['content'] = 'content';
  1309. $smart_push_queue['queue'] = 'send_texts_list';
  1310. }else{
  1311. $smart_push_queue['is_news'] = true;
  1312. $smart_push_queue['content'] = 'news_content';
  1313. $smart_push_queue['queue'] = 'send_news_list';
  1314. }
  1315. // \Log::info('$smart_push_queue');\Log::info($smart_push_queue);
  1316. return $smart_push_queue;
  1317. }
  1318. static function get_base_custom_param(){
  1319. $param = array();
  1320. $param['appids'] = array();
  1321. $param['subscribe_time'] = 'z';
  1322. $param['sex'] = 'z';
  1323. $param['balance'] = 'z';
  1324. $param['category_id'] = 'z';
  1325. $param['order_type'] = 'z';
  1326. $param['sign_time'] = 'z';
  1327. //$param['skip'] = 0;
  1328. return $param;
  1329. }
  1330. static function get_appids_str($distribution_channel_id){
  1331. $official_accounts = OfficialAccount::officialAuthAccountBydistributionChannelId($distribution_channel_id,1,1);
  1332. $appids = array();
  1333. foreach($official_accounts as $official_account){
  1334. $appids[] = $official_account->appid;
  1335. }
  1336. $appids_str = implode(',', $appids);
  1337. return $appids_str;
  1338. }
  1339. /**
  1340. * 去重筛选的用户,已经推送过的不再推送
  1341. * @param unknown_type $force_users
  1342. * @param unknown_type $redis_pre_str
  1343. */
  1344. static function get_unique_users($force_users,$redis_pre_str,$expire_time=3600){
  1345. // 去重
  1346. if(!empty($force_users)){
  1347. \Log::info('before_'.$redis_pre_str.'_quchong::'.count($force_users));
  1348. foreach($force_users as $key=>$force_user){
  1349. $uid = $force_user['uid'];
  1350. $user_key = $redis_pre_str.':'.$uid;
  1351. if(Redis::get($user_key)){
  1352. unset($force_users[$key]);
  1353. // \Log::info($redis_pre_str.'_exist_user:'.$uid);
  1354. continue;
  1355. }else{
  1356. // \Log::info($redis_pre_str.'_push_add_user:'.$uid);
  1357. Redis::set($user_key,date('Y-m-d H:i:s'));
  1358. Redis::expire($user_key,$expire_time);
  1359. }
  1360. }
  1361. \Log::info('after_'.$redis_pre_str.'_push_quchong::'.count($force_users));
  1362. }
  1363. return $force_users;
  1364. }
  1365. /**
  1366. * 得到指定类型智能推送发送的用户列表
  1367. */
  1368. static function get_send_force_users($distribution_channel_id,$category_type,$sex,$skip=0){
  1369. $force_users = array();
  1370. $custom_param = self::get_base_custom_param();
  1371. $custom_param['skip'] = $skip;//skip传进来
  1372. $custom_param['distribution_channel_id'] = $distribution_channel_id;
  1373. $custom_param['appids'] = self::get_appids_str($distribution_channel_id);
  1374. $custom_param['sex'] = $sex;
  1375. // 签到推送
  1376. if($category_type == 'sign_push'){
  1377. $custom_param['sign_time'] = 'a';// 60-120分钟内
  1378. $custom_param['subscribe_time'] = 'g';// 1天前老用户
  1379. $force_users = ForceSubscribeService::forceUserCountByCustomPrams($custom_param['appids'],$custom_param['distribution_channel_id'],$custom_param['subscribe_time'],$custom_param['sex'],$custom_param['balance'],$custom_param['category_id'],$custom_param['order_type'],$custom_param['sign_time'],$custom_param['skip']);
  1380. $force_users = self::get_unique_users($force_users,'sign_push_user',7200);
  1381. }
  1382. // 关注推送
  1383. elseif($category_type == 'subscribe_push'){
  1384. $custom_param['subscribe_time'] = 'h';// 10-25分钟内
  1385. $force_users = ForceSubscribeService::forceUserCountByCustomPrams($custom_param['appids'],$custom_param['distribution_channel_id'],$custom_param['subscribe_time'],$custom_param['sex'],$custom_param['balance'],$custom_param['category_id'],$custom_param['order_type'],$custom_param['sign_time'],$custom_param['skip']);
  1386. $force_users = self::get_unique_users($force_users,'subscribe_push_user',3600);
  1387. }
  1388. // 全量热门书籍推送
  1389. elseif($category_type == 'all_hot_push'){
  1390. $custom_param['subscribe_time'] = 'l';//12-48小时
  1391. $custom_param['order_type'] = 'z';//全量
  1392. $force_users = ForceSubscribeService::forceUserCountByCustomPrams($custom_param['appids'],$custom_param['distribution_channel_id'],$custom_param['subscribe_time'],$custom_param['sex'],$custom_param['balance'],$custom_param['category_id'],$custom_param['order_type'],$custom_param['sign_time'],$custom_param['skip']);
  1393. $force_users = self::get_unique_users($force_users,'all_hot_push',3600*48);
  1394. }
  1395. // 已付费定制书籍推送
  1396. elseif($category_type == 'paid_point_push'){
  1397. $custom_param['subscribe_time'] = 'k';//42-48小时
  1398. $custom_param['order_type'] = 'b';//已充值
  1399. $force_users = ForceSubscribeService::forceUserCountByCustomPrams($custom_param['appids'],$custom_param['distribution_channel_id'],$custom_param['subscribe_time'],$custom_param['sex'],$custom_param['balance'],$custom_param['category_id'],$custom_param['order_type'],$custom_param['sign_time'],$custom_param['skip']);
  1400. $force_users = self::get_unique_users($force_users,'paid_point_push',3600*48);
  1401. // 排除测试的用户
  1402. // $force_users = self::get_minus_test_users($force_users);
  1403. }
  1404. // 未付费热门书籍推送
  1405. elseif($category_type == 'unpaid_hot_push'){
  1406. $custom_param['subscribe_time'] = 'f';//24-48小时
  1407. $custom_param['order_type'] = 'a';//未充值
  1408. $force_users = ForceSubscribeService::forceUserCountByCustomPrams($custom_param['appids'],$custom_param['distribution_channel_id'],$custom_param['subscribe_time'],$custom_param['sex'],$custom_param['balance'],$custom_param['category_id'],$custom_param['order_type'],$custom_param['sign_time'],$custom_param['skip']);
  1409. $force_users = self::get_unique_users($force_users,'unpaid_hot_push',3600*48);
  1410. }
  1411. // 未付费定制书籍推送
  1412. elseif($category_type == 'unpaid_point_push'){
  1413. $custom_param['subscribe_time'] = 'k';//42-48小时
  1414. $custom_param['order_type'] = 'a';//未充值
  1415. $force_users = ForceSubscribeService::forceUserCountByCustomPrams($custom_param['appids'],$custom_param['distribution_channel_id'],$custom_param['subscribe_time'],$custom_param['sex'],$custom_param['balance'],$custom_param['category_id'],$custom_param['order_type'],$custom_param['sign_time'],$custom_param['skip']);
  1416. $force_users = self::get_unique_users($force_users,'unpaid_point_push',3600*48);
  1417. // 排除测试的用户
  1418. // $force_users = self::get_minus_test_users($force_users);
  1419. }
  1420. // 未付费优惠充值活动推送(多选项)
  1421. elseif($category_type == 'unpaid_send_big_activity'){
  1422. $custom_param['subscribe_time'] = 'j';//20-48小时
  1423. $custom_param['order_type'] = 'a';//未充值
  1424. $force_users = ForceSubscribeService::forceUserCountByCustomPrams($custom_param['appids'],$custom_param['distribution_channel_id'],$custom_param['subscribe_time'],$custom_param['sex'],$custom_param['balance'],$custom_param['category_id'],$custom_param['order_type'],$custom_param['sign_time'],$custom_param['skip']);
  1425. $force_users = self::get_unique_users($force_users,'unpaid_send_big_activity',3600*48);
  1426. }
  1427. // 未付费9.9活动推送
  1428. elseif($category_type == 'unpaid_send_activity'){
  1429. $custom_param['subscribe_time'] = 'i';//36-48小时
  1430. $custom_param['order_type'] = 'a';//未充值
  1431. $force_users = ForceSubscribeService::forceUserCountByCustomPrams($custom_param['appids'],$custom_param['distribution_channel_id'],$custom_param['subscribe_time'],$custom_param['sex'],$custom_param['balance'],$custom_param['category_id'],$custom_param['order_type'],$custom_param['sign_time'],$custom_param['skip']);
  1432. $force_users = self::get_unique_users($force_users,'unpaid_send_activity',3600*48);
  1433. }
  1434. // 付费用户推送
  1435. elseif($category_type == 'pay_daily_push'){
  1436. $custom_param['order_type'] = 'd';//6-13小时充值,考虑到0-6点不推送
  1437. $force_users = ForceSubscribeService::forceUserCountByCustomPrams($custom_param['appids'],$custom_param['distribution_channel_id'],$custom_param['subscribe_time'],$custom_param['sex'],$custom_param['balance'],$custom_param['category_id'],$custom_param['order_type'],$custom_param['sign_time'],$custom_param['skip']);
  1438. $force_users = self::get_unique_users($force_users,'pay_daily_push',3600*24);
  1439. }
  1440. else{
  1441. }
  1442. // key重新排序
  1443. $temp_force_users = array();
  1444. if(!empty($force_users)){
  1445. foreach($force_users as $force_user){
  1446. $temp_force_users[] = $force_user;
  1447. }
  1448. }
  1449. return $temp_force_users;
  1450. }
  1451. /**
  1452. * 得到指定用户,发送的图文内容
  1453. */
  1454. static function get_user_send_content($bid,$distribution_channel_id,$category_type,$uid){
  1455. $send_content = '';
  1456. $recommend_book_category_types = array('sign_push','subscribe_push');
  1457. // 改版,书籍数量只能选1本
  1458. $send_book_num = 1;
  1459. // 推荐图书
  1460. if(in_array($category_type,$recommend_book_category_types)){
  1461. $send_content = self::get_user_recommend_books($bid,$distribution_channel_id,$send_book_num,$category_type,$uid);
  1462. }
  1463. // 已付费热门书籍推送 12小时
  1464. elseif($category_type == 'all_hot_push'){
  1465. $send_content = self::get_user_recommend_books($bid,$distribution_channel_id,$send_book_num,$category_type,$uid);
  1466. }
  1467. // 未付费热门书籍推送 24小时
  1468. elseif($category_type == 'unpaid_hot_push'){
  1469. // 优惠充值活动
  1470. $send_content = array();
  1471. $send_content[] = [
  1472. ['title'=>'新用户专享:点此领取25000书币'],
  1473. ['description'=>'新用户专享:点此领取25000书币'],
  1474. ['url'=> env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com/sale/newUserSale?fromtype=unpaid_send_activity&send_time='.time()],
  1475. ['image'=>'https://zhuishuyun.oss-cn-hangzhou.aliyuncs.com/smart_push/citiao.jpg']
  1476. ];
  1477. }
  1478. // 已付费定制书籍推送 42小时
  1479. elseif($category_type == 'paid_point_push'){
  1480. $send_content = self::get_user_recommend_books($bid,$distribution_channel_id,$send_book_num,$category_type,$uid);
  1481. }
  1482. // 未付费定制书籍推送 42小时
  1483. elseif($category_type == 'unpaid_point_push'){
  1484. $send_content = array();
  1485. if(in_array($distribution_channel_id,array('14'))){
  1486. // 头条为终身充值活动
  1487. $first_activity = [
  1488. ['title'=>'全年免费看书的机会来啦!限时优惠'],
  1489. ['description'=>'全年免费看书的机会来啦!限时优惠'],
  1490. ['url'=> env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com/sale/seYearActivity?fromtype=unpaid_send_activity&send_time='.time()],
  1491. ['image'=>'https://cdn-novel.iycdm.com/h5/2018111201.jpg']
  1492. ];
  1493. }else{
  1494. // 头条为9.9元活动
  1495. $first_activity = [
  1496. ['title'=>'充9.9元,得2000书币!'],
  1497. ['description'=>'充9.9元,得2000书币,只有一次机会哦!过期失效,不要错过!'],
  1498. ['url'=> env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com/sale/newUserActivity?fromtype=unpaid_send_activity&send_time='.time()],
  1499. ['image'=>'https://zhuishuyun.oss-cn-hangzhou.aliyuncs.com/smart_push/toutiao.jpg']
  1500. ];
  1501. }
  1502. $send_content[] = $first_activity;
  1503. }
  1504. // 未付费优惠充值活动推送(多选项)
  1505. elseif($category_type == 'unpaid_send_big_activity'){
  1506. //活动链接
  1507. $continueReadUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com/sale/newUserSale?fromtype=unpaid_send_activity&send_time='.time();
  1508. $send_content = '您的新用户专享礼包已送达'."\n\n".'送25000书币及优惠券,畅读全站小说无压力!'."\n\n".'过期失效,只有1次机会哦!'."\n\n".'<a href="'.$continueReadUrl.'">点击立即领取> ></a>'."\n\n".'为方便下次阅读,请'.'<a href="https://help.leyuee.com/top.html">'.'置顶公众号'.'</a>';
  1509. }
  1510. // 未付费9.9活动推送
  1511. elseif($category_type == 'unpaid_send_activity'){
  1512. //活动链接
  1513. $continueReadUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com/sale/newUserActivity?fromtype=unpaid_send_activity&send_time='.time();
  1514. $send_content = '您的新用户专享礼包已送达'."\n\n".'充9.9元,得2000书币,只有一次机会哦!'."\n\n".'过期失效,不要错过!'."\n\n".'<a href="'.$continueReadUrl.'">点击立即领取> ></a>'."\n\n".'为方便下次阅读,请'.'<a href="https://help.leyuee.com/top.html">'.'置顶公众号'.'</a>';
  1515. }
  1516. // 付费用户推送
  1517. elseif($category_type == 'pay_daily_push'){
  1518. $userBook = ReadRecordService::getFirstReadRecord($uid);
  1519. $book_name = isset($userBook['book_name'])?$userBook['book_name']:'';
  1520. $user = UserService::getById($uid);
  1521. $balance = isset($user->balance)?$user->balance:'0';
  1522. $nickname = isset($user->nickname)?$user->nickname:'';
  1523. //用户最近阅读链接
  1524. $continueReadUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com/continue?fromtype=pay_daily_push';
  1525. //拼接文案
  1526. if(empty($book_name)){
  1527. $send_content = '尊敬的会员:'.$nickname."\n\n".'您的账户余额:'.$balance."\n\n".'<a href="'.$continueReadUrl.'">点击继续阅读~</a>'."\n";
  1528. }else{
  1529. $send_content = '尊敬的会员:'.$nickname."\n\n".'您的账户余额:'.$balance."\n\n".'最近阅读书籍:《'.$book_name.'》'."\n\n".'<a href="'.$continueReadUrl.'">点击继续阅读~</a>'."\n";
  1530. }
  1531. \Log::info('pay_daily_push_send_content:'.$uid);\Log::info($send_content);
  1532. }
  1533. // TODO最近阅读?
  1534. else{}
  1535. return $send_content;
  1536. }
  1537. /**
  1538. * 得到指定用户,发送的图文内容
  1539. * 多条图文版--老版
  1540. */
  1541. static function get_user_send_content_multy($bid,$distribution_channel_id,$category_type,$uid){
  1542. $send_content = '';
  1543. $recommend_book_category_types = array('sign_push','subscribe_push');
  1544. // 推荐图书
  1545. if(in_array($category_type,$recommend_book_category_types)){
  1546. $send_content = self::get_user_recommend_books($bid,$distribution_channel_id,4,$category_type,$uid);
  1547. }
  1548. // 已付费热门书籍推送 12小时
  1549. elseif($category_type == 'all_hot_push'){
  1550. $send_content = self::get_user_recommend_books($bid,$distribution_channel_id,5,$category_type,$uid);
  1551. }
  1552. // 未付费热门书籍推送 24小时
  1553. elseif($category_type == 'unpaid_hot_push'){
  1554. // 次条增加优惠充值活动
  1555. $send_content_temp = self::get_user_recommend_books($bid,$distribution_channel_id,4,$category_type,$uid);
  1556. $send_content = array();
  1557. $send_content[0] = $send_content_temp[0];
  1558. $send_content[1] = [
  1559. ['title'=>'新用户专享:点此领取25000书币'],
  1560. ['description'=>'新用户专享:点此领取25000书币'],
  1561. ['url'=> env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com/sale/newUserSale?fromtype=unpaid_send_activity&send_time='.time()],
  1562. ['image'=>'https://zhuishuyun.oss-cn-hangzhou.aliyuncs.com/smart_push/citiao.jpg']
  1563. ];
  1564. $send_content[2] = $send_content_temp[1];
  1565. $send_content[3] = $send_content_temp[2];
  1566. $send_content[4] = $send_content_temp[3];
  1567. }
  1568. // 已付费定制书籍推送 42小时
  1569. elseif($category_type == 'paid_point_push'){
  1570. // 头条为最近阅读记录链接
  1571. $recent_read_book = self::get_user_recent_read_books($uid,$distribution_channel_id,$category_type);
  1572. $send_content = self::get_user_recommend_books($bid,$distribution_channel_id,4,$category_type,$uid);
  1573. array_unshift($send_content,$recent_read_book);// 前插
  1574. }
  1575. // 未付费定制书籍推送 42小时
  1576. elseif($category_type == 'unpaid_point_push'){
  1577. $send_content = self::get_user_recommend_books($bid,$distribution_channel_id,3,$category_type,$uid);
  1578. // 次条为最近阅读记录链接
  1579. $recent_read_book = self::get_user_recent_read_books($uid,$distribution_channel_id,$category_type);
  1580. array_unshift($send_content,$recent_read_book);
  1581. // 头条为9.9元活动
  1582. $first_activity = [
  1583. ['title'=>'充9.9元,得2000书币!'],
  1584. ['description'=>'充9.9元,得2000书币,只有一次机会哦!过期失效,不要错过!'],
  1585. ['url'=> env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com/sale/newUserActivity?fromtype=unpaid_send_activity&send_time='.time()],
  1586. ['image'=>'https://zhuishuyun.oss-cn-hangzhou.aliyuncs.com/smart_push/toutiao.jpg']
  1587. ];
  1588. array_unshift($send_content,$first_activity);
  1589. }
  1590. // 未付费优惠充值活动推送(多选项)
  1591. elseif($category_type == 'unpaid_send_big_activity'){
  1592. //活动链接
  1593. $continueReadUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com/sale/newUserSale?fromtype=unpaid_send_activity&send_time='.time();
  1594. $send_content = '您的新用户专享礼包已送达'."\n\n".'送25000书币及优惠券,畅读全站小说无压力!'."\n\n".'过期失效,只有1次机会哦!'."\n\n".'<a href="'.$continueReadUrl.'">点击立即领取> ></a>'."\n\n".'为方便下次阅读,请'.'<a href="https://help.leyuee.com/top.html">'.'置顶公众号'.'</a>';
  1595. }
  1596. // 未付费9.9活动推送
  1597. elseif($category_type == 'unpaid_send_activity'){
  1598. //活动链接
  1599. $continueReadUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com/sale/newUserActivity?fromtype=unpaid_send_activity&send_time='.time();
  1600. $send_content = '您的新用户专享礼包已送达'."\n\n".'充9.9元,得2000书币,只有一次机会哦!'."\n\n".'过期失效,不要错过!'."\n\n".'<a href="'.$continueReadUrl.'">点击立即领取> ></a>'."\n\n".'为方便下次阅读,请'.'<a href="https://help.leyuee.com/top.html">'.'置顶公众号'.'</a>';
  1601. }
  1602. // TODO最近阅读?
  1603. else{}
  1604. return $send_content;
  1605. }
  1606. /**
  1607. * 得到指定用户,系统推荐的书籍列表
  1608. */
  1609. static function get_user_recommend_books($bid,$distribution_channel_id,$book_num=4,$category_type,$uid){
  1610. $base_category_type = self::get_base_category_type($category_type);
  1611. \Log::info('get_user_recommend_books,bid:'.$bid.' distribution_channel_id:'.$distribution_channel_id.' book_num:'.$book_num.' uid:'.$uid.' base_category_type:'.$base_category_type.' category_type:'.$category_type);
  1612. // $books = BookConfigService::getSimpleChannelBook($bid,$book_num);
  1613. $books = BookConfigService::getSimpleChannelBookLoop($bid,$book_num,$uid);
  1614. $bookUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com';
  1615. $booksArray = [];
  1616. // 标题加余额
  1617. $pre_title = '';
  1618. if(in_array($distribution_channel_id,array('123','211','14'))){
  1619. $user = UserService::getById($uid);
  1620. $balance = isset($user->balance)?$user->balance:'0';
  1621. if($balance >= 100){
  1622. $pre_title = '您的账户余额:'.$balance.'书币 ';
  1623. }
  1624. }
  1625. //5,获取男频女频的标签文案
  1626. $womanPromotion = PromotionService::getRandomHeadline(2,count($books));
  1627. $manPromotion = PromotionService::getRandomHeadline(1,count($books));
  1628. $promotionNum = 0;
  1629. if ($books) {
  1630. foreach ($books as $key => $value) {
  1631. // 标题图片id标识
  1632. $title_image = [];
  1633. $channelName = $value->channel_name;
  1634. $cover = $value->cover;
  1635. if($channelName == '男频'){
  1636. $book['title'] = $manPromotion[$promotionNum]['title'];
  1637. $book['description'] = $manPromotion[$promotionNum]['title'];
  1638. $title_image['title_id'] = $manPromotion[$promotionNum]['id'];
  1639. }else{
  1640. $book['title'] = $womanPromotion[$promotionNum]['title'];
  1641. $book['description'] = $womanPromotion[$promotionNum]['title'];
  1642. $title_image['title_id'] = $womanPromotion[$promotionNum]['id'];
  1643. }
  1644. if ($promotionNum == 0) {
  1645. //第一条图片选用图库中的图片地址
  1646. $promotionImages = PromotionService::getMessageRandomDocumentCovers(3);
  1647. if ($promotionImages) {
  1648. $book['image'] = $promotionImages[0]['link'];
  1649. $title_image['image_id'] = $promotionImages[0]['id'];
  1650. }else{
  1651. $book['image'] = $cover;
  1652. $title_image['image_id'] = 0;
  1653. }
  1654. }else{
  1655. $book['image'] = $cover;
  1656. $title_image['image_id'] = 0;
  1657. }
  1658. // 记录标题图片推送的redis
  1659. $title_image_key = $title_image['title_id'].'_'.$title_image['image_id'];
  1660. \Log::info('title_image_smart_push:'.$base_category_type.' uid:'.$uid.' title_image_key:'.$title_image_key);
  1661. $title_image_send_uv_key = 'smart_push_from_title_image_send_uv:'.$title_image_key;
  1662. $title_image_statistics_key = 'smart_push_from_title_image:'.$title_image_key;
  1663. Redis::sadd($title_image_send_uv_key,$uid);
  1664. Redis::HINCRBY($title_image_statistics_key,'send_num',1);
  1665. //通过判断书籍是男频还是女频拼接书籍信息的不同标签文案
  1666. if(strpos($bookUrl.$value->url,'?') > -1){
  1667. $urlBook = $value->url = $bookUrl.$value->url.'&fromtype='.$base_category_type.'&source=wechatmsg&fromsource=smart_push&from_title_image='.$title_image_key;
  1668. }else{
  1669. $urlBook = $value->url = $bookUrl.$value->url.'?fromtype='.$base_category_type.'&source=wechatmsg&fromsource=smart_push&from_title_image='.$title_image_key;
  1670. }
  1671. $book['url'] = $urlBook;
  1672. // $book['image'] = $cover;
  1673. if(!empty($pre_title)){
  1674. $book['title'] = $pre_title.$book['title'];
  1675. }
  1676. $booksArray[$key] = [['title'=>$book['title']],['description'=>$book['description']],['url'=>$book['url']],['image'=>$book['image']]];
  1677. $promotionNum++;
  1678. }
  1679. }
  1680. return $booksArray;
  1681. }
  1682. /**
  1683. * 得到用户最近阅读的记录
  1684. */
  1685. static function get_user_recent_read_books($uid,$distribution_channel_id,$category_type){
  1686. \Log::info('get_user_recent_read_books,distribution_channel_id:'.$distribution_channel_id.' uid:'.$uid);
  1687. $base_category_type = self::get_base_category_type($category_type);
  1688. $booksArray = [];
  1689. // 获取用户最近阅读的书籍信息
  1690. $userBook = ReadRecordService::getFirstReadRecord($uid);
  1691. if ($userBook) {
  1692. $book = array();
  1693. $book['url'] = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com/continue?fromtype='.$base_category_type;
  1694. $channelName = $userBook['channel_name'];
  1695. //获取男频女频的标签文案
  1696. $womanPromotion = PromotionService::getRandomHeadline(2,1);
  1697. $manPromotion = PromotionService::getRandomHeadline(1,1);
  1698. if($channelName == '男频'){
  1699. $book['title'] = $manPromotion[0]['title'];
  1700. $book['description'] = $manPromotion[0]['title'];
  1701. }else{
  1702. $book['title'] = $womanPromotion[0]['title'];
  1703. $book['description'] = $womanPromotion[0]['title'];
  1704. }
  1705. //头条的图片从财哥个图库中随机取一张
  1706. $promotionImages = PromotionService::getMessageRandomDocumentCovers(3);
  1707. if ($promotionImages) {
  1708. $book['image'] = $promotionImages[0]['link'];
  1709. }else{
  1710. $book['image'] = $userBook['cover'];
  1711. }
  1712. $booksArray = [['title'=>$book['title']],['description'=>$book['description']],['url'=>$book['url']],['image'=>$book['image']]];
  1713. }
  1714. return $booksArray;
  1715. }
  1716. /**
  1717. * 得到指定书籍的所有信息
  1718. */
  1719. static function get_book_full_infos($books,$category_type=''){
  1720. $booksArray = [];
  1721. //5,获取男频女频的标签文案
  1722. $womanPromotion = PromotionService::getRandomHeadline(2,count($books));
  1723. $manPromotion = PromotionService::getRandomHeadline(1,count($books));
  1724. $promotionNum = 0;
  1725. if ($books) {
  1726. foreach ($books as $key => $value) {
  1727. $channelName = $value->channel_name;
  1728. $cover = $value->cover;
  1729. if($channelName == '男频'){
  1730. $book['title'] = $manPromotion[$promotionNum]['title'];
  1731. $book['description'] = $manPromotion[$promotionNum]['title'];
  1732. }else{
  1733. $book['title'] = $womanPromotion[$promotionNum]['title'];
  1734. $book['description'] = $womanPromotion[$promotionNum]['title'];
  1735. }
  1736. //通过判断书籍是男频还是女频拼接书籍信息的不同标签文案
  1737. // if(strpos($bookUrl.$value->url,'?') > -1){
  1738. // $urlBook = $value->url = $bookUrl.$value->url.'&fromtype='.$category_type.'&source=wechatmsg';
  1739. // }else{
  1740. // $urlBook = $value->url = $bookUrl.$value->url.'?fromtype='.$category_type.'&source=wechatmsg';
  1741. // }
  1742. $book['url'] = $value->url;
  1743. // $book['image'] = $cover;
  1744. if ($promotionNum == 0) {
  1745. //第一条图片选用图库中的图片地址
  1746. $promotionImages = PromotionService::getMessageRandomDocumentCovers(3);
  1747. if ($promotionImages) {
  1748. $book['image'] = $promotionImages[0]['link'];
  1749. }else{
  1750. $book['image'] = $cover;
  1751. }
  1752. }else{
  1753. $book['image'] = $cover;
  1754. }
  1755. $booksArray[$key] = [['title'=>$book['title']],['description'=>$book['description']],['url'=>$book['url']],['image'=>$book['image']]];
  1756. $promotionNum++;
  1757. }
  1758. }
  1759. return $booksArray;
  1760. }
  1761. //-------------------------------------------------客服消息配置开关Start---------------------------CustomMsgSwitchs
  1762. /**
  1763. * 开启或关闭客服消息
  1764. */
  1765. static function customMsgSwitchSetting($distribution_channel_id,$custom_category,$status,$is_self_content=0)
  1766. {
  1767. try {
  1768. //获取分销渠道下指定的一个智能推送
  1769. $customMsgSwitch = CustomMsgSwitchsMsgs::customMsgSwitchsByChannelCate($distribution_channel_id,$custom_category);
  1770. if (!empty($customMsgSwitch)) {
  1771. //设置推送状态 1,开启 2,关闭
  1772. $customMsgSwitch['status'] = $status;
  1773. $customMsgSwitch['is_self_content'] = $is_self_content;
  1774. $customMsgSwitch->save();
  1775. return 1;
  1776. }else{
  1777. return 0;
  1778. }
  1779. } catch (\Exception $e) {
  1780. return 2;
  1781. }
  1782. }
  1783. /**
  1784. * 根据distribution_channel_id获取客服消息开关
  1785. */
  1786. static function customMsgSwitchsByChannelId($distribution_channel_id)
  1787. {
  1788. \Log::info('customMsgSwitchsByChannelId_start:'.$distribution_channel_id);
  1789. $result = array();
  1790. // 渠道个性配置
  1791. $custom_msg_switch_infos = self::GetCustomMsgSwitchsByChannelId($distribution_channel_id);
  1792. // 基础素材配置
  1793. $customMsgSwitchs = CustomMsgSwitchs::getCustomMsgSwitchByCategorys();
  1794. \Log::info('$custom_msg_switch_infos');\Log::info($custom_msg_switch_infos);
  1795. \Log::info('$customMsgSwitchs');\Log::info($customMsgSwitchs);
  1796. // 组装素材,全部后台配置
  1797. foreach($custom_msg_switch_infos as $key => $custom_msg_switch_info){
  1798. // \Log::info($custom_msg_switch_info);
  1799. $customMsgSwitch = $customMsgSwitchs[$custom_msg_switch_info->custom_category];
  1800. // 不展示
  1801. if($customMsgSwitch['status'] == 0){
  1802. continue;
  1803. }
  1804. $result[$key]['status'] = $custom_msg_switch_info->status;
  1805. $result[$key]['id'] = $custom_msg_switch_info->id;
  1806. $result[$key]['custom_category'] = $custom_msg_switch_info->custom_category;
  1807. $result[$key]['distribution_channel_id'] = $custom_msg_switch_info->distribution_channel_id;
  1808. $result[$key]['title'] = $customMsgSwitch['title'];
  1809. $result[$key]['desc_content'] = $customMsgSwitch['desc_content'];
  1810. $result[$key]['img_url'] = $customMsgSwitch['img_url'];
  1811. $result[$key]['common_is_self_content'] = $customMsgSwitch['is_self_content'];
  1812. $result[$key]['is_self_content'] = $custom_msg_switch_info->is_self_content;
  1813. }
  1814. return $result;
  1815. }
  1816. /**
  1817. * 根据distribution_channel_id获取客服消息开关
  1818. */
  1819. static function GetCustomMsgSwitchsByChannelId($distribution_channel_id)
  1820. {
  1821. //1,首先尝试获取该渠道下的智能推送开关
  1822. $customMsgSwitchsMsgs = CustomMsgSwitchsMsgs::customMsgSwitchsMsgsByChannelId($distribution_channel_id);
  1823. if (count($customMsgSwitchsMsgs)>0) {
  1824. //2,该渠道下有智能推送的配置信息,开始对比过滤
  1825. $customMsgSwitchsMsgsArray = [];
  1826. //3,获取智能推送基础配置表的推送开关信息
  1827. $customMsgSwitchs = CustomMsgSwitchs::customMsgSwitchs();
  1828. //4,判断如果渠道下的只能推送开关数量和基础配置下的相同,则直接返回查到的所有客服开关
  1829. if (count($customMsgSwitchsMsgs) == count($customMsgSwitchs)) {
  1830. return $customMsgSwitchsMsgs;
  1831. }else{
  1832. //5,如果数量不一致,则渠道下需要更新智能开关的数量,与配置表保持一致,少补,多的保留
  1833. //先循环渠道下所有的只能客服推送数量,通过custom_category关联基础配置表,
  1834. //如果渠道下所查询到的custom_category在基础表中不存在,则说明该智能客服推送配置已删除,渠道下也要删除多余的配置项
  1835. for ($i=0; $i < count($customMsgSwitchsMsgs); $i++) {
  1836. $customMsgSwitchsSerch = CustomMsgSwitchs::customMsgSwitchsBySwitchId($customMsgSwitchsMsgs[$i]['custom_category']);
  1837. // 有些是需要特殊开着的,多的留着
  1838. if (empty($customMsgSwitchsSerch)) {
  1839. // $customMsgSwitchsMsgs[$i]->delete();
  1840. }else{
  1841. $customMsgSwitchsMsgsArray[] = $customMsgSwitchsMsgs[$i];
  1842. }
  1843. }
  1844. //再循环基础配置表获取custom_category,看在渠道的智能客服推送配置中是否存在,
  1845. //如果不存在,则说明这是最新配置的智能客服推送项,该渠道需要新增一条默认为关闭的智能推送开关
  1846. for ($i=0; $i < count($customMsgSwitchs); $i++) {
  1847. $customMsgSwitchsMsg = CustomMsgSwitchsMsgs::customMsgSwitchsByChannelCate($distribution_channel_id,$customMsgSwitchs[$i]['custom_category']);
  1848. if (empty($customMsgSwitchsMsg)) {
  1849. # code...
  1850. $customMsg['custom_category'] = $customMsgSwitchs[$i]['custom_category'];
  1851. if($customMsg['custom_category'] == 'auto_custom_trusteeship'){
  1852. $customMsg['status'] = '2';// 客服消息托管
  1853. }else{
  1854. $customMsg['status'] = '1';// 默认开
  1855. }
  1856. $customMsg['distribution_channel_id'] = $distribution_channel_id;
  1857. $customMsgSwitchsMsgNew = CustomMsgSwitchsMsgs::create($customMsg);
  1858. $customMsgSwitchsMsgsArray[] = $customMsgSwitchsMsgNew;
  1859. }
  1860. }
  1861. //6,同步完成后,返回正确的客服推送信息
  1862. return $customMsgSwitchsMsgsArray;
  1863. }
  1864. }else{
  1865. //7,如果该渠道没有智能开关配置,则说明该渠道是新增的渠道,
  1866. //通过基础配置表为该渠道创建相应的智能客服推送,默认关闭
  1867. $customMsgSwitchsMsgsArray = [];
  1868. $customMsgSwitchs = CustomMsgSwitchs::customMsgSwitchs();
  1869. if (count($customMsgSwitchs)>0) {
  1870. for ($i=0; $i < count($customMsgSwitchs); $i++) {
  1871. $customMsgSwitchsMsg['custom_category'] = $customMsgSwitchs[$i]['custom_category'];
  1872. if($customMsgSwitchsMsg['custom_category'] == 'auto_custom_trusteeship'){
  1873. $customMsgSwitchsMsg['status'] = '2';// 客服消息托管
  1874. }else{
  1875. $customMsgSwitchsMsg['status'] = '1';// 默认开
  1876. }
  1877. $customMsgSwitchsMsg['distribution_channel_id'] = $distribution_channel_id;
  1878. $customMsgSwitchsMsgNew = CustomMsgSwitchsMsgs::create($customMsgSwitchsMsg);
  1879. $customMsgSwitchsMsgsArray[] = $customMsgSwitchsMsgNew;
  1880. }
  1881. }
  1882. return $customMsgSwitchsMsgsArray;
  1883. }
  1884. }
  1885. /**
  1886. * 获取指定客服消息开关
  1887. */
  1888. static function customMsgSwitchsByChannelCate($distribution_channel_id,$custom_category)
  1889. {
  1890. return CustomMsgSwitchsMsgs::customMsgSwitchsByChannelCate($distribution_channel_id,$custom_category);
  1891. }
  1892. //-------------------------------------------------客服消息配置开关End---------------------------CustomMsgSwitchs
  1893. //-------------------------------------------------编辑发送客服消息Start---------------------------CustomSendMsg
  1894. /**
  1895. * 根据distribution_channel_id获取发送客服消息列表
  1896. */
  1897. static function customSendMsgsByChannelId($distribution_channel_id)
  1898. {
  1899. return CustomSendMsgs::customSendMsgsByChannelId($distribution_channel_id);
  1900. }
  1901. /**
  1902. * 搜索
  1903. */
  1904. static function searchCustomSendMsgs($param,$is_all = false)
  1905. {
  1906. return CustomSendMsgs::searchCustomSendMsgs($param,$is_all);
  1907. }
  1908. /**
  1909. * 根据distribution_channel_id和托管信息获取发送客服消息列表
  1910. */
  1911. static function customSendMsgsByChannelIdAndTrusteeship($customerPrams)
  1912. {
  1913. return CustomSendMsgs::customSendMsgsByChannelIdAndTrusteeship($customerPrams['distribution_channel_id'],$customerPrams['trusteeship']);
  1914. }
  1915. /**
  1916. * 根据托管信息获取发送客服消息列表
  1917. */
  1918. static function customSendMsgsByTrusteeship($customerPrams)
  1919. {
  1920. return CustomSendMsgs::customSendMsgsByTrusteeship($customerPrams['trusteeship']);
  1921. }
  1922. /**
  1923. * 托管信息信息
  1924. * @param $day
  1925. * @return mixed
  1926. */
  1927. public static function customSendMsgsByTrusteeshipGroup($day){
  1928. return CustomSendMsgs::where('trusteeship',1)
  1929. ->where('del_flag',0)
  1930. ->where('send_time','>=',$day)
  1931. ->where('send_time','<=',$day.' 23:59:59')
  1932. ->groupBy('distribution_channel_id')
  1933. ->select('distribution_channel_id',DB::raw('GROUP_CONCAT(id) as ids'),DB::raw('sum(user_num) as user_num'))
  1934. ->get();
  1935. }
  1936. /**
  1937. * 托管信息信息
  1938. * @param $day
  1939. * @return mixed
  1940. */
  1941. public static function customSendMsgsByTrusteeshipByChannelId($distribution_channel_id,$date){
  1942. return CustomSendMsgs::where('trusteeship',1)
  1943. ->where('del_flag',0)
  1944. ->where('distribution_channel_id','=',$distribution_channel_id)
  1945. ->where('send_time','>=',$date)
  1946. ->groupBy('distribution_channel_id')
  1947. ->select(DB::raw('GROUP_CONCAT(id) as ids'),DB::raw('sum(user_num) as user_num'))
  1948. ->first();
  1949. }
  1950. /**
  1951. * 根据托管信息获取发送客服消息列表
  1952. */
  1953. static function customSendMsgsByTrusteeshipAndBacthNo($customerPrams)
  1954. {
  1955. return CustomSendMsgs::customSendMsgsByTrusteeshipAndBacthNo($customerPrams['trusteeship'],$customerPrams['batch_no']);
  1956. }
  1957. //判断1小时内是否有相同的客服消息插入过
  1958. static function isSendCustomerAtSameTime($customerPrams){
  1959. return CustomSendMsgs::isSendCustomerAtSameTime($customerPrams['distribution_channel_id'],$customerPrams['appid'],$customerPrams['send_time']);
  1960. }
  1961. //判断1小时内是否有相同的客服消息插入过
  1962. static function isSendCustomerAtSameTimeAndSex($customerPrams){
  1963. return CustomSendMsgs::isSendCustomerAtSameTimeAndSex($customerPrams['distribution_channel_id'],$customerPrams['appid'],$customerPrams['send_time'],$customerPrams['sex']);
  1964. }
  1965. /**
  1966. * 根据task_id获取发送客服消息
  1967. */
  1968. static function customSendMsgsByTaskid($task_id)
  1969. {
  1970. return CustomSendMsgs::customSendMsgsByTaskid($task_id);
  1971. }
  1972. /**
  1973. * 根据id获取发送客服消息
  1974. */
  1975. static function customSendMsgsById($id)
  1976. {
  1977. return CustomSendMsgs::customSendMsgsById($id);
  1978. }
  1979. /**
  1980. * 停止客服消息发送
  1981. */
  1982. static function updateCustomSendMsgsStatus($id)
  1983. {
  1984. try {
  1985. $customSendMsgs = CustomSendMsgs::customSendMsgsById($id);
  1986. if (!empty($customSendMsgs)) {
  1987. if($customSendMsgs['status'] == '8'){
  1988. \Log::info('该客服已经在发送状态,不允许编辑'.$customSendMsgs['task_id']);
  1989. return 4;
  1990. }
  1991. $customSendMsgs['status'] = '4';
  1992. $customSendMsgs->save();
  1993. $officialAccountArray = $customSendMsgs->toArray();
  1994. Redis::hset('send_wechat_msg:task_id:'.$customSendMsgs['task_id'], 'wechat_msg', json_encode($officialAccountArray));
  1995. return 1;
  1996. }else{
  1997. return 2;
  1998. }
  1999. } catch (\Exception $e) {
  2000. \Log::info('updateCustomSendMsgsStatus_ept:'.$e->getMessage());
  2001. return 0;
  2002. }
  2003. }
  2004. /**
  2005. * 更新客服消息发送,兼容删除
  2006. */
  2007. static function updateCustomSendMsgsStatusMulty($id,$status,$del_flag=0)
  2008. {
  2009. try {
  2010. $customSendMsgs = CustomSendMsgs::customSendMsgsById($id);
  2011. if (!empty($customSendMsgs)) {
  2012. // 由于批次状态暂时不更新,所以子渠道强制更新状态
  2013. // if($customSendMsgs['status'] == '8'){
  2014. // \Log::info('updateCustomSendMsgsStatusMulty,status=8:'.$customSendMsgs['task_id']);
  2015. // return 4;
  2016. // }
  2017. $customSendMsgs['status'] = $status;
  2018. $customSendMsgs['del_flag'] = $del_flag;
  2019. $customSendMsgs->save();
  2020. $officialAccountArray = $customSendMsgs->toArray();
  2021. Redis::hset('send_wechat_msg:task_id:'.$customSendMsgs['task_id'], 'wechat_msg', json_encode($officialAccountArray));
  2022. return 1;
  2023. }else{
  2024. return 2;
  2025. }
  2026. } catch (\Exception $e) {
  2027. \Log::info('updateCustomSendMsgsStatusMulty_ept:'.$e->getMessage());
  2028. return 0;
  2029. }
  2030. }
  2031. /**
  2032. * 发送客服消息
  2033. */
  2034. static function addCustomSendMsgs($customSendMsgsPrams)
  2035. {
  2036. try {
  2037. $appids = $customSendMsgsPrams['appid'];
  2038. $appidArray = explode(',', $appids);
  2039. $usercountnum = 0;
  2040. $customSendMsgsPrams['task_id'] = 'custom_#'.$customSendMsgsPrams['distribution_channel_id'].'#'.md5(uniqid());
  2041. //4,先在数据库创建一条新的客服消息
  2042. $customSendMsgsPrams['user_num'] = 0;
  2043. $customSendMsgsPrams['del_flag'] = 0;
  2044. $customSendMsgs = CustomSendMsgs::create($customSendMsgsPrams);
  2045. $customSendMsgsPrams['id'] = $customSendMsgs['id'];
  2046. $officialAccountArray = $customSendMsgs->toArray();
  2047. Redis::hset('send_wechat_msg:task_id:'.$customSendMsgs['task_id'], 'wechat_msg', json_encode($officialAccountArray));
  2048. return 1;
  2049. } catch (\Exception $e) {
  2050. \Log::info('===================================发送客服消息报错');
  2051. \Log::info($e->getMessage());
  2052. return 0;
  2053. }
  2054. }
  2055. /**
  2056. * 修改客服消息
  2057. */
  2058. static function editCustomSendMsgs($customSendMsgsPrams)
  2059. {
  2060. try {
  2061. $customSendMsgs = CustomSendMsgs::customSendMsgsById($customSendMsgsPrams['id']);
  2062. if ($customSendMsgs) {
  2063. if($customSendMsgs['status'] == '8'){
  2064. \Log::info('该客服已经在发送状态,不允许编辑'.$customSendMsgs['task_id']);
  2065. return 4;
  2066. }
  2067. //1,先把原来的客服消息停止并删除
  2068. $customSendMsgs['status'] = '4';
  2069. $customSendMsgs['del_flag'] = 1;
  2070. $customSendMsgs->save();
  2071. $officialAccountArray = $customSendMsgs->toArray();
  2072. Redis::hset('send_wechat_msg:task_id:'.$customSendMsgs['task_id'], 'wechat_msg', json_encode($officialAccountArray));
  2073. // Redis::hset('send_wechat_msg:task_id:'.$customSendMsgs['task_id'], 'wechat_msg', '[{user_num:'.$customSendMsgs['user_num'].'},{appid:'.$customSendMsgs['appid'].'},{name:'.$customSendMsgs['name'].'},{send_time:'.$customSendMsgs['send_time'].'},{content:'.$customSendMsgs['content'].'},{redirect_url:'.$customSendMsgs['redirect_url'].'},{distribution_channel_id:'.$customSendMsgs['distribution_channel_id'].'},{subscribe_time:'.$customSendMsgs['subscribe_time'].'},{sex:'.$customSendMsgs['sex'].'},{balance:'.$customSendMsgs['balance'].'},{order_type:'.$customSendMsgs['order_type'].'},{category_id:'.$customSendMsgs['category_id'].'}]');
  2074. $isSendCustomer = self::isSendCustomerAtSameTime($customSendMsgsPrams);
  2075. if (!empty($isSendCustomer)) {
  2076. \Log::info('已经发送过该客服消息');
  2077. \Log::info($isSendCustomer['id']);
  2078. \Log::info($isSendCustomer['status']);
  2079. \Log::info($isSendCustomer['del_flag']);
  2080. return 3;
  2081. }
  2082. //2,创建一条新的模板消息
  2083. $appids = $customSendMsgsPrams['appid'];
  2084. $appidArray = explode(',', $appids);
  2085. $usercountnum = 0;
  2086. $customSendMsgsPrams['task_id'] = 'custom_#'.$customSendMsgsPrams['distribution_channel_id'].'#'.md5(uniqid());
  2087. //4,先在数据库创建一条新的客服消息
  2088. $customSendMsgsPrams['user_num'] = 0;
  2089. $customSendMsgsPrams['del_flag'] = 0;
  2090. $customSendMsgs = CustomSendMsgs::create($customSendMsgsPrams);
  2091. $customSendMsgsPrams['id'] = $customSendMsgs['id'];
  2092. $officialAccountArray = $customSendMsgs->toArray();
  2093. Redis::hset('send_wechat_msg:task_id:'.$customSendMsgs['task_id'], 'wechat_msg', json_encode($officialAccountArray));
  2094. return 1;
  2095. }else{
  2096. return 2;
  2097. }
  2098. } catch (\Exception $e) {
  2099. \Log::info('===================================发送客服消息报错');
  2100. \Log::info($e->getMessage());
  2101. return 0;
  2102. }
  2103. }
  2104. /**
  2105. * 删除客服消息
  2106. */
  2107. static function delCustomSendMsgs($customSendMsgsPrams)
  2108. {
  2109. try {
  2110. $customSendMsgs = CustomSendMsgs::customSendMsgsById($customSendMsgsPrams['id']);
  2111. if ($customSendMsgs) {
  2112. if($customSendMsgs['status'] == '8'){
  2113. \Log::info('该客服已经在发送状态,不允许编辑'.$customSendMsgs['task_id']);
  2114. return 4;
  2115. }
  2116. //1,先把原来的客服消息停止并删除
  2117. $customSendMsgs['status'] = '4';
  2118. $customSendMsgs['del_flag'] = 1;
  2119. $customSendMsgs->save();
  2120. $officialAccountArray = $customSendMsgs->toArray();
  2121. Redis::hset('send_wechat_msg:task_id:'.$customSendMsgs['task_id'], 'wechat_msg', json_encode($officialAccountArray));
  2122. return 1;
  2123. }else{
  2124. return 2;
  2125. }
  2126. } catch (\Exception $e) {
  2127. \Log::info('===================================发送客服消息报错');
  2128. \Log::info($e->getMessage());
  2129. return 0;
  2130. }
  2131. }
  2132. /**
  2133. * 测试发送客服消息
  2134. */
  2135. static function testAddCustomSendMsgs($customSendMsgsPrams)
  2136. {
  2137. try {
  2138. // 测试链接加上域名
  2139. $customSendMsgsPrams['content'] = TemplateCustomSendService::convert_custom_content($customSendMsgsPrams['content'], 'test', $customSendMsgsPrams['distribution_channel_id'],0);
  2140. $appids = $customSendMsgsPrams['appid'];
  2141. $appidArray = explode(',', $appids);
  2142. for ($j=0; $j < count($appidArray); $j++) {
  2143. \Log::info('testAddCustomSendMsgs_appid:'.$appidArray[$j]);
  2144. //1,获取指定的用户
  2145. $forceSubscribeUserPram['uid'] = $customSendMsgsPrams['uid'];
  2146. $forceSubscribeUserPram['appid'] = $appidArray[$j];
  2147. $forceSubscribeUserPram['distribution_channel_id'] = $customSendMsgsPrams['distribution_channel_id'];
  2148. $force_user = array();
  2149. $temp_force_user = ForceSubscribeService::forceSubscribeUsersByUidAndAppidAndChannelId($forceSubscribeUserPram);
  2150. \Log::info('temp_force_user');\Log::info($temp_force_user);
  2151. if(count($temp_force_user) == 0){
  2152. \Log::info('testAddCustomSendMsgs_has_no_user:'.$customSendMsgsPrams['uid']);
  2153. $direct_user = array('openid'=>$customSendMsgsPrams['uid']);
  2154. $force_user[] = $direct_user;
  2155. }else{
  2156. $force_user = $temp_force_user;
  2157. }
  2158. \Log::info('force_user');\Log::info($force_user);
  2159. if (count($force_user)>0) {
  2160. for ($i=0; $i < count($force_user); $i++) {
  2161. //2,测试循环用户发送客服消息
  2162. $data = array();
  2163. $data['openid'] = $force_user[$i]['openid'];
  2164. $data['appid'] = $appidArray[$j];
  2165. $data['news_content'] = $customSendMsgsPrams['content'];
  2166. $data['type'] = 'last_task';
  2167. $data['task_id'] = 'custom_f69556c4eb5274cefdb9ba0a477736366';
  2168. $data['send_time'] = date("Y-m-d H:i:s");
  2169. $send_data=array(
  2170. 'send_time'=>date("Y-m-d H:i:s"),
  2171. 'data' => $data
  2172. );
  2173. $delay = 0;
  2174. // \Log::info('客服消息找到用户并发送===================================');
  2175. \Log::info('test_custom_msg_send:'.$data['openid']);
  2176. // \Log::info($data['appid']);
  2177. \Log::info($data['news_content']);
  2178. $job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_news_list');
  2179. dispatch($job);
  2180. }
  2181. }
  2182. }
  2183. return 1;
  2184. } catch (\Exception $e) {
  2185. \Log::info('===================================');
  2186. \Log::info($e->getMessage());
  2187. return 0;
  2188. }
  2189. }
  2190. //-------------------------------------------------编辑发送客服消息End---------------------------CustomSendMsg
  2191. //-------------------------------------------------生成图文链接Start---------------------------
  2192. /**
  2193. * 上传微信图片
  2194. * @param unknown_type $gzh_app_id
  2195. * @param unknown_type $img_url
  2196. * @param unknown_type $group_nick
  2197. * @param unknown_type $timestamp
  2198. */
  2199. static function upload_material_img($param) {
  2200. $params = array("gzh_app_id"=>$param['gzh_app_id'],"img_url"=>$param['img_url'],"group_nick"=>$param['group_nick'],"timestamp"=>$param['timestamp']);
  2201. $sign = get_sign($params);
  2202. //获取封面图片media_id
  2203. $mediaCustomerJson = self::getUserClient()->request("GET","upload_material_img/",
  2204. ['query'=>['gzh_app_id'=>$param['gzh_app_id'],'img_url'=>$param['img_url'],'group_nick'=>$param['group_nick'],'timestamp'=>$param['timestamp'],'sign'=>$sign],'connect_timeout' => 3]
  2205. )->getBody()->getContents();
  2206. \Log::info("alter_upload_material_img:".$param['gzh_app_id'].' img_url:'.$param['img_url']);
  2207. \Log::info($mediaCustomerJson);
  2208. $mediaCustomerArray = json_decode($mediaCustomerJson,true);
  2209. return $mediaCustomerArray;
  2210. }
  2211. public static function multy_upload_material_imgs($content,$last_urls,$base_param){
  2212. \Log::info('multy_upload_material_imgs_start');
  2213. \Log::info('last_urls');\Log::info($last_urls);
  2214. $multy_urls = [];
  2215. foreach($last_urls as $last_url){
  2216. if(strpos($last_url,'mmbiz.qpic.cn') > -1){
  2217. \Log::info('wx_url_continue:'.$last_url);
  2218. continue;
  2219. }
  2220. $last_url = str_replace(array("&amp;","&quot;","amp;","quot;"), "", htmlspecialchars($last_url));
  2221. $params = array("gzh_app_id"=>$base_param['gzh_app_id'],"img_url"=>$last_url,"group_nick"=>$base_param['group_nick'],"timestamp"=>$base_param['timestamp']);
  2222. $sign = get_sign($params);
  2223. $params['sign'] = $sign;
  2224. $url = env('MEDIA_API_BASE_URI').'upload_material_img?'.http_build_query($params);
  2225. $multy_url = [];
  2226. $multy_url['request_url'] = $url;
  2227. $multy_url['old_url'] = $last_url;
  2228. $multy_urls[] = $multy_url;
  2229. }
  2230. // 最多尝试3次
  2231. $try_time = 0;
  2232. while($try_time < 3){
  2233. try{
  2234. $url_mappings = self::multiRequest($multy_urls);
  2235. \Log::info('$url_mappings');\Log::info($url_mappings);
  2236. if(!empty($url_mappings)){
  2237. \Log::info('multiRequest_success');
  2238. break;
  2239. }
  2240. }catch(\Exception $e){
  2241. \Log::info('multiRequest_ept:'.$e->getMessage());
  2242. }
  2243. $try_time++;
  2244. }
  2245. if($try_time == 3){
  2246. \Log::info('multiRequest_error_return:');
  2247. return '';
  2248. }
  2249. foreach($url_mappings as $key=>$url_mapping){
  2250. \Log::info('replace_old_url:'.$url_mapping['old_url'].' new_url:'.$url_mapping['new_url']);
  2251. $content = str_replace($url_mapping['old_url'], $url_mapping['new_url'], $content);
  2252. }
  2253. return $content;
  2254. }
  2255. static function multiRequest($data){
  2256. $client = new Client(
  2257. ['allow_redirects'=>true,
  2258. 'timeout'=>120000]);
  2259. $promises = [];
  2260. $mappings = [];
  2261. for($i=0;$i<count($data);$i++){
  2262. \Log::info('async_url:'.$data[$i]['request_url']);
  2263. $promises[$i] = $client->getAsync($data[$i]['request_url']);
  2264. }
  2265. $results = Promise\unwrap($promises);
  2266. // \Log::info('multy_$results');\Log::info($results);
  2267. $j = 0;
  2268. foreach($data as $key=>&$v){
  2269. $res = $results[$key]->getBody()->getContents();
  2270. $res = json_decode($res,true);
  2271. \Log::info('res:'.$key.' res:'.json_encode($res));
  2272. $mappings[$key]['old_url'] = $data[$key]['old_url'];
  2273. if($res['code'] == 1){
  2274. $mappings[$key]['new_url'] = $res['data']['url'];
  2275. }else{
  2276. \Log::info('multiRequest_error_return:'.$data[$key]['old_url'].' res:'.json_encode($res).' request_url:'.$data[$key]['request_url']);
  2277. return null;
  2278. }
  2279. }
  2280. return $mappings;
  2281. }
  2282. // 单个链接上传替换
  2283. static function one_upload_material_imgs($content,$last_urls){
  2284. \Log::info('one_upload_material_imgs_start');
  2285. foreach($last_urls as $key=>$last_url){
  2286. \Log::info('last_urls_count:'.count($last_urls).' now:'.$key);
  2287. if(strpos($last_url,'mmbiz.qpic.cn') > -1){
  2288. \Log::info('wx_url_continue:'.$last_url);
  2289. continue;
  2290. }
  2291. $last_url = str_replace(array("&amp;","&quot;","amp;","quot;"), "", htmlspecialchars($last_url));
  2292. // TODO 加try 3次?
  2293. $i = 0;
  2294. while($i < 3){
  2295. try{
  2296. \Log::info('child_upload_material_img_start:i'.$i.' last_url:'.$last_url);
  2297. $param['img_url'] = $last_url;
  2298. $upload_res = self::upload_material_img($param);
  2299. \Log::info('$upload_res');\Log::info($upload_res);
  2300. if($upload_res['code'] == 1){
  2301. $content = str_replace($last_url, $upload_res['data']['url'], $content);
  2302. break;
  2303. }
  2304. }catch(\Exception $e){
  2305. \Log::info('upload_matrial_img_ept:'.$e->getMessage());
  2306. sleep(1);
  2307. }
  2308. $i++;
  2309. }
  2310. if($i == 3){
  2311. \Log::info('upload_matrial_img_return');
  2312. return null;
  2313. }
  2314. }
  2315. return $content;
  2316. }
  2317. /**
  2318. * 转化章节图片为微信的url
  2319. * @param unknown_type $content
  2320. */
  2321. static function convert_content_wechat_url($content,$param){
  2322. \Log::info('convert_content_wechat_url_start:'.json_encode($param));
  2323. // 获取url list
  2324. // 防封模式、第一条封面图要去掉
  2325. //<img src="http://yueduyun.oss-cn-hangzhou.aliyuncs.com/book_chapter_image_test/5pNo6A7wqQmB1WgQygDjkOM9VZn2vXeY/1/1_0.jpg"
  2326. preg_match_all('/<img src="(.*)"/U',$content,$ff_matchs);
  2327. $ff_urls = isset($ff_matchs[1])?$ff_matchs[1]:array();
  2328. if(isset($ff_urls[0])){
  2329. unset($ff_urls[0]);
  2330. }
  2331. \Log::info('ff_matchs:');
  2332. \Log::info($ff_urls);
  2333. // 背景图模式
  2334. //background: url(&quot;http://yueduyun.oss-cn-hangzhou.aliyuncs.com/book_chapter_image_test/5pNo6A7wqQmB1WgQygDjkOM9VZn2vXeY/1/1_0.jpg&quot;)
  2335. preg_match_all('/background: url\((.*)\)/U',$content,$bg_matchs);
  2336. $bg_urls = isset($bg_matchs[1])?$bg_matchs[1]:array();
  2337. \Log::info('bg_urls:');
  2338. \Log::info($bg_urls);
  2339. $last_urls = array_merge($ff_urls,$bg_urls);
  2340. \Log::info('$last_urls');\Log::info($last_urls);
  2341. if(!empty($last_urls)){
  2342. \Log::info('before_convert:');\Log::info($content);
  2343. $content = self::multy_upload_material_imgs($content,$last_urls,$param);
  2344. \Log::info('after_convert:');\Log::info($content);
  2345. }
  2346. return $content;
  2347. }
  2348. /**
  2349. * 生成图文链接
  2350. */
  2351. static function createOfficialImgTxtUrl($officialImgTxtUrlPrams)
  2352. {
  2353. $img_url = $officialImgTxtUrlPrams['img_url'];
  2354. $content = $officialImgTxtUrlPrams['content'];
  2355. $title = $officialImgTxtUrlPrams['title'];
  2356. $distribution_channel_id = $officialImgTxtUrlPrams['distribution_channel_id'];
  2357. $book_name = $officialImgTxtUrlPrams['book_name'];
  2358. $chapter_sequence = $officialImgTxtUrlPrams['chapter_sequence'];
  2359. $send_orders_id = $officialImgTxtUrlPrams['send_orders_id'];
  2360. $send_orders_name = $officialImgTxtUrlPrams['send_orders_name'];
  2361. $send_orders_url = $officialImgTxtUrlPrams['send_orders_url'];
  2362. $show_cover_pic = 1;
  2363. $gzh_app_id = '';
  2364. $digest = '';
  2365. $author = '';
  2366. $thumb_media_id = '';
  2367. $officialAccountprams['distribution_channel_id'] = $distribution_channel_id;
  2368. $officialAccounts = OfficialAccountService::officialAuthAccountBydistributionChannelId($officialAccountprams);
  2369. if (count($officialAccounts)>0) {
  2370. $gzh_app_id = $officialAccounts[0]['appid'];
  2371. // $gzh_app_id = 'wxb2aaa55b088f12e9';
  2372. $timestamp = (time());
  2373. $group_nick = env('GROUP_NICK');
  2374. $param = [
  2375. 'timestamp'=>$timestamp,
  2376. 'group_nick'=>$group_nick,
  2377. 'gzh_app_id'=>$gzh_app_id,
  2378. 'img_url'=>$img_url
  2379. ];
  2380. // 内容的链接转化为微信的官方链接,否则文章上传会被过滤掉
  2381. $content = self::convert_content_wechat_url($content,$param);
  2382. // 转化异常直接返回
  2383. if(empty($content)){
  2384. return '';
  2385. }
  2386. try {
  2387. $mediaCustomerArray = self::upload_material_img($param);
  2388. if ($mediaCustomerArray['code'] == 1) {
  2389. $mediaCustomerData = $mediaCustomerArray['data'];
  2390. if (!empty($mediaCustomerData)) {
  2391. $thumb_media_id = $mediaCustomerData['media_id'];
  2392. $param2s = array("show_cover_pic"=>$show_cover_pic,"author"=>$author,"digest"=>$digest,"content"=>$content,"title"=>$title,"thumb_media_id"=>$thumb_media_id,"gzh_app_id"=>$gzh_app_id,"group_nick"=>$group_nick,"timestamp"=>$timestamp);
  2393. $sign2 = get_sign($param2s);
  2394. \Log::info("============================开始请求创建素材URL接口");
  2395. //获取图文素材链接
  2396. $officialImgTxtUrlJson = self::getUserClient()->request("POST","upload_gzh_article/",
  2397. ['form_params'=>["show_cover_pic"=>$show_cover_pic,"author"=>$author,"digest"=>$digest,"content"=>$content,"title"=>$title,"thumb_media_id"=>$thumb_media_id,"gzh_app_id"=>$gzh_app_id,'group_nick'=>env('GROUP_NICK'),'timestamp'=>$timestamp,'sign'=>$sign2],'connect_timeout' => 3]
  2398. )->getBody()->getContents();
  2399. \Log::info("============================获取素材图文链接 通过接口整合获得 content_source_url");
  2400. \Log::info($officialImgTxtUrlJson);
  2401. $officialImgTxtUrlArray = json_decode($officialImgTxtUrlJson,true);
  2402. if ($officialImgTxtUrlArray['code'] == 1) {
  2403. $officialImgTxtUrlData = $officialImgTxtUrlArray['data'];
  2404. if (!empty($officialImgTxtUrlData)) {
  2405. $content_source_url = $officialImgTxtUrlData['chapter_url'];
  2406. $officialImgTxtUrls['img_url'] = $img_url;
  2407. $officialImgTxtUrls['content'] = $content;
  2408. $officialImgTxtUrls['title'] = $title;
  2409. $officialImgTxtUrls['distribution_channel_id'] = $distribution_channel_id;
  2410. $officialImgTxtUrls['book_name'] = $book_name;
  2411. $officialImgTxtUrls['chapter_sequence'] = $chapter_sequence;
  2412. $officialImgTxtUrls['gzh_app_id'] = $gzh_app_id;
  2413. $officialImgTxtUrls['digest'] = $digest;
  2414. $officialImgTxtUrls['author'] = $author;
  2415. $officialImgTxtUrls['thumb_media_id'] = $thumb_media_id;
  2416. $officialImgTxtUrls['content_source_url'] = $content_source_url;
  2417. $officialImgTxtUrls['send_orders_id'] = $send_orders_id;
  2418. $officialImgTxtUrls['send_orders_name'] = $send_orders_name;
  2419. $officialImgTxtUrls['send_orders_url'] = $send_orders_url;
  2420. $officialImgTxtUrls['del_flag'] = 0;
  2421. OfficialImgtextUrls::create($officialImgTxtUrls);
  2422. return urldecode(str_replace("\/","/",$content_source_url));
  2423. }
  2424. }else{
  2425. return '';
  2426. }
  2427. }
  2428. }else{
  2429. return '';
  2430. }
  2431. } catch (\Exception $e) {
  2432. \Log::info("createOfficialImgTxtUrl_ept:".$e->getMessage());
  2433. return '';
  2434. }
  2435. }
  2436. }
  2437. /**
  2438. * 获取图文列表
  2439. */
  2440. static function getOfficialImgTxtUrl($distribution_channel_id)
  2441. {
  2442. return OfficialImgtextUrls::officialImgtextUrlsByChannelId($distribution_channel_id);
  2443. }
  2444. /**
  2445. * 根据标题获取图文列表
  2446. */
  2447. static function getOfficialImgTxtUrlByBookName($distribution_channel_id,$book_name)
  2448. {
  2449. return OfficialImgtextUrls::getOfficialImgTxtUrlByBookName($distribution_channel_id,$book_name);
  2450. }
  2451. /**
  2452. * 上传二维码图片
  2453. */
  2454. static function uploadQrcodeImgToWechat($officialImgTxtUrlPrams)
  2455. {
  2456. $distribution_channel_id = $officialImgTxtUrlPrams['distribution_channel_id'];
  2457. $img_url = $officialImgTxtUrlPrams['qrcode_Img'];
  2458. $officialAccountprams['distribution_channel_id'] = $distribution_channel_id;
  2459. $officialAccounts = OfficialAccountService::officialAuthAccountBydistributionChannelId($officialAccountprams);
  2460. if (count($officialAccounts)>0) {
  2461. $gzh_app_id = $officialAccounts[0]['appid'];
  2462. // $gzh_app_id = 'wxb2aaa55b088f12e9';
  2463. $timestamp = (time());
  2464. $group_nick = env('GROUP_NICK');
  2465. try {
  2466. $params = array("gzh_app_id"=>$gzh_app_id,"img_url"=>$img_url,"group_nick"=>$group_nick,"timestamp"=>$timestamp);
  2467. $sign = get_sign($params);
  2468. //获取封面图片media_id
  2469. $mediaCustomerJson = self::getUserClient()->request("GET","upload_material_img/",
  2470. ['query'=>['gzh_app_id'=>$gzh_app_id,'img_url'=>$img_url,'group_nick'=>env('GROUP_NICK'),'timestamp'=>$timestamp,'sign'=>$sign],'connect_timeout' => 3]
  2471. )->getBody()->getContents();
  2472. \Log::info("============================上传二维码图片 通过图片+appid获取media_id");
  2473. \Log::info($mediaCustomerJson);
  2474. $mediaCustomerArray = json_decode($mediaCustomerJson,true);
  2475. if ($mediaCustomerArray['code'] == 1) {
  2476. $mediaCustomerData = $mediaCustomerArray['data'];
  2477. if (!empty($mediaCustomerData)) {
  2478. $thumb_media_id = $mediaCustomerData['media_id'];
  2479. $url = $mediaCustomerData['url'];
  2480. $wechatQrcodeImg['media_id'] = $thumb_media_id;
  2481. $wechatQrcodeImg['url'] = $url;
  2482. return $wechatQrcodeImg;
  2483. }
  2484. }else{
  2485. return false;
  2486. }
  2487. } catch (\Exception $e) {
  2488. \Log::info("============================上传二维码图片");
  2489. \Log::info($e->getMessage());
  2490. return false;
  2491. }
  2492. }
  2493. }
  2494. /**
  2495. * 删除图文素材
  2496. */
  2497. static function delOfficialImgTxtUrl($id)
  2498. {
  2499. try {
  2500. $officialImgtextUrl = OfficialImgtextUrls::officialImgtextUrlsById($id);
  2501. if (!empty($officialImgtextUrl)) {
  2502. $officialImgtextUrl['del_flag'] = 1;
  2503. $officialImgtextUrl->save();
  2504. return 1;
  2505. }else{
  2506. return 2;
  2507. }
  2508. } catch (\Exception $e) {
  2509. return 0;
  2510. }
  2511. }
  2512. //-------------------------------------------------生成图文链接End---------------------------
  2513. static public function getUserClient(){
  2514. // return new Client(['base_uri' => 'http://118.31.133.29/api/']);
  2515. return new Client(['base_uri' => env('MEDIA_API_BASE_URI')]);
  2516. }
  2517. /**
  2518. * 客服消息智能推送方法--简单公用版
  2519. * 以下3个板块找到对应的推送
  2520. * 1、用户
  2521. * 2、内容
  2522. * 3、队列类型
  2523. */
  2524. static function SimpleSmartPush($category_type){
  2525. if(empty($category_type)) return false;
  2526. \Log::info('simple_smart_push:'.$category_type);
  2527. // 应该不区分男女,通过内容来区分
  2528. $sexs = array('z');
  2529. $user_total_count = 0;
  2530. foreach($sexs as $sex){
  2531. \Log::info('smart_push_sex:'.$sex);
  2532. $send_content = '';
  2533. $skip = 0;
  2534. $loop = 0;
  2535. while ( $skip <= 250000) {
  2536. $loop++;
  2537. $force_user = null;
  2538. $force_user = self::get_simple_send_users($category_type,$sex,$skip);
  2539. \Log::info('SmartPush_usernum:'.count($force_user).' loop:'.$loop.' memory:'.memory_get_usage());
  2540. \Log::info('$force_user:'.json_encode($force_user));
  2541. $force_user_num = count($force_user);
  2542. $skip += $force_user_num;
  2543. $user_total_count += $force_user_num;
  2544. if($force_user_num > 0){
  2545. for ($i=0; $i < $force_user_num; $i++) {
  2546. $uid = $force_user[$i]['uid'];
  2547. $bid = $force_user[$i]['bid'];
  2548. $template_id = $force_user[$i]['template_id'];
  2549. $distribution_channel_id = $force_user[$i]['distribution_channel_id'];
  2550. $send_content = self::get_simple_user_send_content($category_type,$uid,$distribution_channel_id);
  2551. if(empty($send_content)){
  2552. \Log::info('smart_push:openid:'.$force_user[$i]['openid'].' appid:'.$force_user[$i]['appid'].' content_is_null');
  2553. continue;
  2554. }
  2555. $smart_push_queue = self::get_simple_smart_push_queue($category_type);
  2556. $data = array();
  2557. $data['openid'] = $force_user[$i]['openid'];
  2558. $data['appid'] = $force_user[$i]['appid'];
  2559. $data['type'] = 'one_task';
  2560. $data['task_id'] = 3;
  2561. $data['send_time'] = date("Y-m-d H:i:s");
  2562. if ($smart_push_queue['queue'] == 'send_template_list'){
  2563. $data['statusstr'] = (time());
  2564. $data['template_id'] = $template_id;
  2565. $data['url'] = $send_content['url'];
  2566. $data['template_content'] = json_encode($send_content['content']);
  2567. }elseif ($smart_push_queue['queue'] == 'send_news_list'){
  2568. $data['news_content'] = json_encode($send_content['content']);
  2569. }else{
  2570. $data['content'] = $send_content['content'];
  2571. }
  2572. $send_data=array(
  2573. 'send_time'=>date("Y-m-d H:i:s"),
  2574. 'data' => $data
  2575. );
  2576. $delay = 0;
  2577. // \Log::info('smart_push:'.json_encode($data));
  2578. if ($smart_push_queue['queue'] == 'send_news_list'){
  2579. $job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue($smart_push_queue['queue']);
  2580. }elseif ($smart_push_queue['queue'] == 'send_template_list'){
  2581. $job = (new SendTemplate($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue($smart_push_queue['queue']);
  2582. }else{
  2583. $job = (new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue($smart_push_queue['queue']);
  2584. }
  2585. dispatch($job);
  2586. \Log::info('smart_push_user_end:'.$uid.' memory:'.memory_get_usage());
  2587. // 释放内存
  2588. $send_data = $data = null;
  2589. }//end users
  2590. if($force_user_num < 10000)//低于单次获取的10000条,所以下次不必获取,必须与单次获取的数值保持一致!
  2591. {
  2592. \Log::info('simple_smart_push user num less than once get num, so break category_type:'. $category_type.' sex:'.$sex.' loop:'.$loop.' memory:'.memory_get_usage());
  2593. break;
  2594. }
  2595. }
  2596. else{
  2597. \Log::info('simple_smart_push_has_no_user category_type:'. $category_type.' sex:'.$sex.' loop:'.$loop.' memory:'.memory_get_usage());
  2598. break;
  2599. }
  2600. }//end loop
  2601. } // end sex
  2602. }
  2603. /**
  2604. * 得到指定用户,发送的图文内容
  2605. */
  2606. static function get_simple_user_send_content($category_type,$uid,$distribution_channel_id){
  2607. $send_content = null;
  2608. // 推荐图书
  2609. if($category_type == 'template_short_long_chapter'){
  2610. $book_config = DataAnalysisBookConfig::getUserOneNotOrderBook($uid);
  2611. //
  2612. $template_content = [
  2613. 'first'=>$book_config['template_title'],
  2614. 'keyword1'=>'《'.$book_config['book_name'].'》',
  2615. 'keyword2'=>'今日',
  2616. 'remark'=>'点击查看详情'
  2617. ];
  2618. $send_content['content'][] = $template_content;
  2619. $bookUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com';
  2620. $bookUrl = $bookUrl. '/reader?bid='. Hashids::encode($book_config['bid']).'&cid='.$book_config['first_cid'].'&fromtype=test_long_short_chapter';
  2621. $send_content['url'] = $bookUrl;
  2622. }elseif($category_type == 'custom_short_long_chapter'){
  2623. $book_config = DataAnalysisBookConfig::getUserOneNotOrderBook($uid);
  2624. //
  2625. $promotionImages = PromotionService::getMessageRandomDocumentCovers(3);
  2626. $book['image'] = $promotionImages[0]['link'];
  2627. $bookUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('CUSTOM_HOST').'.com';
  2628. $bookUrl = $bookUrl. '/reader?bid='. Hashids::encode($book_config['bid']).'&cid='.$book_config['first_cid'].'&fromtype=test_long_short_chapter';
  2629. $booksArray[0] = [['title'=>$book_config->custom_title],['description'=>$book_config->custom_title],['url'=>$bookUrl],['image'=>$book['image']]];
  2630. $send_content['content'] = $booksArray;
  2631. }
  2632. else{}
  2633. \Log::info('get_simple_user_send_content_last:'.$uid);
  2634. \Log::info($send_content);
  2635. return $send_content;
  2636. }
  2637. /**
  2638. * 得到指定类型智能推送发送的用户列表
  2639. */
  2640. static function get_simple_send_users($category_type,$sex,$skip=0){
  2641. $force_users = [];
  2642. $param = [];
  2643. // 客服消息
  2644. if($category_type == 'custom_short_long_chapter'){
  2645. $uids = self::get_test_users();
  2646. // 42小时后
  2647. $param['subscribe_time'] = 'k';
  2648. $force_users = ForceSubscribeService::simpleForceUserCountByPrams($uids, $param,$skip);
  2649. $force_users = self::get_unique_users($force_users,'custom_short_long_chapter',7200);
  2650. }
  2651. elseif($category_type == 'template_short_long_chapter'){
  2652. $uids = self::get_test_users();
  2653. // 60小时后
  2654. $param['subscribe_time'] = 'm';
  2655. $param['common_template_id'] = 'OPENTM401671396';
  2656. \Log::info('start_simple_param:'.json_encode($uids).' param:'.json_encode($param));
  2657. $force_users = ForceSubscribeService::simpleForceUserCountByPrams($uids, $param,$skip);
  2658. \Log::info('before_unique_user:'.json_encode($force_users));
  2659. $force_users = self::get_unique_users($force_users,'custom_short_long_chapter',7200);
  2660. }
  2661. // key重新排序
  2662. $temp_force_users = array();
  2663. if(!empty($force_users)){
  2664. foreach($force_users as $force_user){
  2665. $temp_force_users[] = $force_user;
  2666. }
  2667. }
  2668. return $temp_force_users;
  2669. }
  2670. static function get_simple_smart_push_queue($category_type){
  2671. $smart_push_queue = array();
  2672. $template_queue = array('template_short_long_chapter');
  2673. $custom_queue = array('custom_short_long_chapter');
  2674. $text_queue = array();
  2675. if(in_array($category_type,$template_queue)){
  2676. $smart_push_queue['is_news'] = false;
  2677. $smart_push_queue['queue'] = 'send_template_list';
  2678. }elseif(in_array($category_type,$custom_queue)){
  2679. $smart_push_queue['is_news'] = true;
  2680. $smart_push_queue['queue'] = 'send_news_list';
  2681. }else{
  2682. $smart_push_queue['is_news'] = false;
  2683. $smart_push_queue['queue'] = 'send_texts_list';
  2684. }
  2685. \Log::info('$smart_push_queue');\Log::info($smart_push_queue);
  2686. return $smart_push_queue;
  2687. }
  2688. static function get_minus_test_users($force_users){
  2689. $test_uids = self::get_test_users();
  2690. \Log::info('get_minus_test_users,force_users:'.json_encode($force_users).' $test_uids:'.json_encode($test_uids));
  2691. if(!empty($force_users)){
  2692. foreach($force_users as $key=>$force_user){
  2693. if(in_array($force_user->uid,$test_uids)){
  2694. \Log::info('get_minus_test_users_exist:'.$force_user->uid);
  2695. unset($force_users[$key]);
  2696. }else{
  2697. \Log::info('get_minus_test_users_not_exist:'.$force_user->uid);
  2698. }
  2699. }
  2700. }
  2701. \Log::info('get_minus_test_users after_force_users:'.json_encode($force_users));
  2702. return $force_users;
  2703. }
  2704. static function get_test_users(){
  2705. // 获取测试的用户
  2706. $types = [2,5];
  2707. $test_uids = DataAnalysisChapterService::getByTypes($types);
  2708. return $test_uids;
  2709. }
  2710. }