CustomMsgService.php 156 KB

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