CustomMsgService.php 150 KB

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