CustomMsgService.php 175 KB

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