OfficialInteractiveEventController.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. <?php
  2. namespace App\Http\Controllers\Wechat\OfficialAccount;
  3. use App\Modules\Channel\Services\ChannelService;
  4. use App\Modules\Book\Services\BookSearchStatService;
  5. use Illuminate\Routing\Controller as ChannelBaseController;
  6. use App\Modules\Channel\Models\Channel;
  7. use App\Modules\Channel\Models\YqMoveChannel;
  8. use App\Modules\OfficialAccount\Models\MediaCustomer;
  9. use App\Modules\OfficialAccount\Models\OfficialAccount;
  10. use App\Modules\OfficialAccount\Models\OfficialInteractiveEvent;
  11. use App\Modules\OfficialAccount\Models\WechatKeywordMsgs;
  12. use App\Modules\OfficialAccount\Models\DistributionReply;
  13. use App\Modules\OfficialAccount\Models\DistributionSelfDefineContent;
  14. use App\Modules\OfficialAccount\Models\DistributionSelfDefineConfig;
  15. use App\Modules\OfficialAccount\Services\OfficialAccountService;
  16. use App\Modules\OfficialAccount\Services\CustomMsgService;
  17. use App\Modules\OfficialAccount\Services\MsgService;
  18. use App\Modules\User\Services\ReadRecordService;
  19. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  20. use App\Http\Controllers\Wechat\OfficialAccount\Transformers\OfficialInteractiveEventTransformer;
  21. use App\Http\Controllers\Wechat\OfficialAccount\Transformers\OfficialInteractiveEventTextTransformer;
  22. use App\Modules\Book\Services\BookConfigService;
  23. use App\Modules\User\Services\UserSignService;
  24. use Illuminate\Http\Request;
  25. use GuzzleHttp\Client;
  26. use App\Libs\OSS;
  27. use Hashids;
  28. use App\Modules\Book\Models\BookConfig;
  29. class OfficialInteractiveEventController extends ChannelBaseController
  30. {
  31. /**
  32. * @apiDefine OfficialAccount 公众号
  33. */
  34. /**
  35. * @apiVersion 1.0.0
  36. * @api {GET} OfficialAccount/OfficialInteractiveEventFeedback 公众号交互事件推送
  37. * @apiGroup OfficialAccount
  38. * @apiName OfficialInteractiveEventFeedback
  39. * @apiParam {String} appid 公众号的appid.
  40. * @apiParam {String} event 事件类型.
  41. * @apiParam {String} content text类型下的搜索条件.
  42. * @apiSuccess {String} text 返回的文字信息.
  43. * @apiSuccess {String} title 搜索出的书籍标题.
  44. * @apiSuccess {String} description 搜索出的书籍简介.
  45. * @apiSuccess {String} url 搜索出的书籍链接.
  46. * @apiSuccess {String} image 搜索出的书籍封面地址.
  47. * @apiSuccessExample {json} Success-Response:
  48. *
  49. * {
  50. * "code": 0,
  51. * "msg": "",
  52. * "data": {
  53. * "text": "用户取消强制关注"
  54. * }
  55. * }
  56. *
  57. * {
  58. * "code": 0,
  59. * "msg": "",
  60. * "data": [
  61. * {
  62. * "title": "肌缘巧合",
  63. * "description": "&nbsp;&nbsp;&nbsp;&nbsp;他是权势倾天,纵横商界的王者,却偏偏钟情于她,一宠成瘾。“女人,我要你......只要你能满足我,别墅、游轮、支票,你随便挑。”她羞涩的半低着头:“我只想要你。”他挑眉,“你野心不小啊!”她妩媚一笑,解开他的领带,“难道你不愿意!”他宠她爱她,给她所有想要的。只是有一天她终于忍不住暴走,“靠,你有没有节制呀?我要离婚。”",
  64. * "url": "test2.ycsd.cn?bid=5",
  65. * "image": "https:\/\/leyue-bucket.oss-cn-hangzhou.aliyuncs.com\/ycsd_cover\/covermiddle\/0\/8.jpg"
  66. * },
  67. * {
  68. * "title": "等你爱我",
  69. * "description": "&nbsp;&nbsp;&nbsp;&nbsp;“只要你愿意成全我,给你所有……”黑暗中,她为救他,成了他的女人,他却隔天清晨匆匆离去。六年后,她进入他的公司,与他擦肩而过,却互不相识,但一切却悄然发生改变,他有了自己爱的人,她有了爱自己的人……她带着女儿疲于奔命,他重新进入她的生活,当他决定娶她时,她却淡淡一笑,转身离开……",
  70. * "url": "test2.ycsd.cn?bid=9",
  71. * "image": "https:\/\/leyue-bucket.oss-cn-hangzhou.aliyuncs.com\/ycsd_cover\/covermiddle\/0\/4.jpg"
  72. * }
  73. * ]
  74. * }
  75. */
  76. function officialInteractiveEventFeedback(Request $request)
  77. {
  78. $appid = $request->has('appid') ? $request->input('appid') : '';
  79. // if(empty($appid)) {
  80. // return response()->error("PARAM_EMPTY");
  81. // }
  82. $event = $request->has('event') ? $request->input('event') : '';
  83. $uid = $request->has('uid') ? $request->input('uid') : '';
  84. $openid = $request->has('openid') ? $request->input('openid') : '';
  85. \Log::info('-------------------------');
  86. \Log::info($openid);
  87. if(empty($event)) {
  88. return response()->error("PARAM_EMPTY");
  89. }
  90. $content = $request->has('content') ? $request->input('content') : '';
  91. $officialAccount = OfficialAccount::officialAccountByAppid($appid);
  92. $distribution_channel_id = '';
  93. if (!empty($officialAccount)) {
  94. $distribution_channel_id = $officialAccount['distribution_channel_id'];
  95. }else{
  96. $distribution_channel_id = '';
  97. }
  98. if($event == "text"){
  99. if(empty($content)) {
  100. return response()->error("PARAM_EMPTY");
  101. }
  102. }
  103. $encode_distribution_channel_id = encodeDistributionChannelId($distribution_channel_id);
  104. // 判断渠道是否是外站派单(需要我们帮生成强关用户)
  105. $channel = Channel::getById($distribution_channel_id);
  106. $is_outer_site = isset($channel->is_outer_site)?$channel->is_outer_site:'0';
  107. $is_domain_simple = isset($channel->is_domain_simple)?$channel->is_domain_simple:'0';
  108. if($is_domain_simple == '1'){
  109. $encode_distribution_channel_id = $distribution_channel_id;
  110. }
  111. $is_yq_move = '0';
  112. // 新站
  113. if(isset($channel->is_yq_move) && $channel->is_yq_move){
  114. \Log::info('new_has_move_site:'.$distribution_channel_id);
  115. $is_yq_move = 1;
  116. }
  117. // 已经迁移过的站,回复也要有老站
  118. $yqMoveChannel = YqMoveChannel::get_yq_move_channel($distribution_channel_id,1);
  119. if(!empty($yqMoveChannel)){
  120. \Log::info('old_has_move_site:'.$distribution_channel_id);
  121. $is_yq_move = 1;
  122. }
  123. $params['openid'] = $openid;
  124. $forceSubscribeUser = ForceSubscribeService::forceSubscribeUsersByOpenid($params);
  125. $uid = isset($forceSubscribeUser->uid)?$forceSubscribeUser->uid:'0';
  126. \Log::info('distribution_channel_id:'.$distribution_channel_id.' is_outer_site:'.$is_outer_site.' is_yq_move:'.$is_yq_move.' uid:'.$uid);
  127. if ($event == "text") {
  128. // 搜索写入
  129. if($content != 'search_get_recommend_book'){
  130. if(strlen($content) <= 100){
  131. BookSearchStatService::create($uid,$openid,'wechat',$content);
  132. }
  133. }
  134. // 单独推送一本智能推荐的书
  135. if($content == 'search_get_recommend_book'){
  136. \Log::info('get_recommend_book:'.$openid);
  137. $feedback['text'] = "";
  138. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  139. // 有自定义不回复内容的渠道
  140. $distribution_self_define_config = DistributionSelfDefineConfig::getDistributionSelfDefineConfig($distribution_channel_id,'recommend_book_noreply');
  141. if(!empty($distribution_self_define_config)){
  142. \Log::info('distribution_self_define_config_no_recommend_book: distribution_channel_id:'.$distribution_channel_id);
  143. $feedback['text'] = "";
  144. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  145. }
  146. $bid = isset($forceSubscribeUser->bid)?$forceSubscribeUser->bid:'0';
  147. $params['openid'] = $openid;
  148. $recommend_book = BookConfigService::getSimpleChannelBookLoop($bid,1,$uid);
  149. $feedback['text'] = '';
  150. if(!empty($recommend_book)){
  151. $bookUrl = env('PROTOCOL').'://site'.encodeDistributionChannelId($distribution_channel_id).'.'.env('WECHAT_CUSTOM_HOST').'.com';
  152. $bookUrl = $bookUrl.$recommend_book[0]->url.'&fromtype='.$content.'&source=wechatmsg&fromsource=smart_push';
  153. $feedback['text'] = '新书推荐: <a href="' . $bookUrl . '"> 《' . $recommend_book[0]->book_name . '》</a>';
  154. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site,$appid, $openid, $feedback['text'],'get_one_recommend_book');
  155. }
  156. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  157. }
  158. if($content == '签到'){
  159. //调洋哥的接口发送签到客服消息
  160. \Log::info('text_sign:'.$openid);
  161. $feedback['text'] = UserSignService::userSignReturnContent3($openid,$distribution_channel_id);
  162. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site,$appid, $openid, $feedback['text'],'text_sign');
  163. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  164. }
  165. $booksInfo = array();
  166. // 判断是否自定义关键字
  167. $special_keyword = WechatKeywordMsgs::wechatKeywordByDistributionChannelIdAndKeyword($distribution_channel_id,$content,1);
  168. if(!empty($special_keyword)){
  169. \Log::info('officialInteractiveEventFeedback_has_special_keyword:'.$distribution_channel_id.' content:'.$content);
  170. \Log::info($special_keyword);
  171. $bid = Hashids::decode($special_keyword['bid'])[0];
  172. \Log::info('bid:'.$bid);
  173. $book_conf = BookConfig::getBookById($bid);
  174. \Log::info('$book_conf');\Log::info($book_conf);
  175. if(!empty($book_conf)){
  176. $special_keyword_new = array();
  177. $special_keyword_new['book_name'] = isset($special_keyword['send_title']) && !empty($special_keyword['send_title'])?$special_keyword['send_title']:$book_conf['book_name'];
  178. $special_keyword_new['cover'] = isset($special_keyword['send_cover']) && !empty($special_keyword['send_cover'])?$special_keyword['send_cover']:$book_conf['cover'];
  179. \Log::info('keyword_book_name:'.$special_keyword_new['book_name'].' cover:'.$special_keyword_new['cover']);
  180. $special_keyword_new['intro'] = $book_conf['intro'];
  181. // 此处为自定义的章节链接https://site163.leyuee.com/reader?bid=N948JQDWpb7mx0K6zaKV5j2zOeZABdaL&cid=382260&fromtype=custom_3070&soid=30771
  182. // 有派单id的关键字不做统计
  183. if(isset($special_keyword['send_order_id']) && $special_keyword['send_order_id'] > 0){
  184. $special_keyword_new['book_url'] = '/yun/'.$special_keyword['send_order_id'];
  185. }else{
  186. $special_keyword_new['book_url'] = '/reader?bid='.$special_keyword['bid'].'&cid='.$special_keyword['cid'].'&fromtype=keyword_'.$special_keyword['id'];
  187. }
  188. $special_keyword_new['bid'] = $special_keyword['bid'];
  189. $booksInfo[] = $special_keyword_new;
  190. }else{
  191. \Log::info('book_conf_null:'.$distribution_channel_id.' content:'.$content);
  192. }
  193. }else{
  194. $booksInfo = BookConfig::getBooksByKey($content,3);
  195. }
  196. \Log::info('event_booksInfo');
  197. \Log::info($booksInfo);
  198. if(count($booksInfo) == 0){
  199. $feedback['text'] = '未找到相关小说,'."\n".'您可以试试:'."\n"."\n".'<a href="'.env('PROTOCOL').'://site'.$encode_distribution_channel_id.'.'.env('WECHAT_CUSTOM_HOST').'.com/recent">查看阅读记录 >> </a> '."\n"."\n".'<a href="'.env('PROTOCOL').'://site'.$encode_distribution_channel_id.'.'.env('WECHAT_CUSTOM_HOST').'.com">去书城首页看看 >> </a> '."\n"."\n".'联系客服:'."\n".'点击菜单栏 “用户中心“ > ”联系客服”';
  200. // $feedback['text'] = "未找到您想要的书\n<a href='".env('PROTOCOL')."://site".$distribution_channel_id.".".env('WECHAT_CUSTOM_HOST').".com'>点我进入书城</a>\n\n";
  201. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site,$appid, $openid, $feedback['text'],'text_search');
  202. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  203. }else{
  204. $data=[];
  205. for ($i=0; $i < count($booksInfo); $i++) {
  206. $bid = $booksInfo[$i]['bid'];
  207. \Log::info('wechat_log_info:book');\Log::info($booksInfo[$i]);
  208. $base_url = env('PROTOCOL').'://site'.$encode_distribution_channel_id.'.'.env('WECHAT_CUSTOM_HOST').'.com'.$booksInfo[$i]['book_url'];
  209. $instro_len = strlen($booksInfo[$i]['intro']);
  210. \Log::info('wechat_log_info:len:'.$instro_len.' getURL-----'.$base_url);
  211. if( $instro_len > 100){
  212. $booksInfo[$i]['intro'] = mb_substr($booksInfo[$i]['intro'], 0, 100, 'utf-8');
  213. $booksInfo[$i]['intro'] .= '......';
  214. \Log::info('search_instro_too_long:'.$booksInfo[$i]['intro']);
  215. }
  216. $data[$i]=['url'=>$base_url,
  217. 'title'=>$booksInfo[$i]['book_name'],
  218. 'description'=>$booksInfo[$i]['intro'],
  219. 'image'=>$booksInfo[$i]['cover']];
  220. // 返回一条
  221. break;
  222. }
  223. $datatext['text'] = $data;
  224. $datatext['text'] = ChannelService::convertChannelReplyUrl($is_outer_site,$appid, $openid, $datatext['text'],'text_search');
  225. return response()->success($datatext);
  226. // return response()->collection(new OfficialInteractiveEventTextTransformer(),$data);
  227. }
  228. }
  229. elseif($event == "scan"){
  230. // 有自定义不回复内容的渠道
  231. $distribution_self_define_config = DistributionSelfDefineConfig::getDistributionSelfDefineConfig($distribution_channel_id,'force_subscribe_noreply');
  232. if(!empty($distribution_self_define_config)){
  233. \Log::info('distribution_self_define_config: distribution_channel_id:'.$distribution_channel_id);
  234. $feedback['text'] = "";
  235. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  236. }
  237. $feedback['text'] = "<a href='".env('PROTOCOL')."://site".$encode_distribution_channel_id.".".env('WECHAT_CUSTOM_HOST').".com/continue'>点此继续阅读 </a> ";
  238. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site,$appid, $openid, $feedback['text'],'scan');
  239. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  240. }
  241. elseif($event == "click"){
  242. if($content == "contact_customer" && !empty($appid)){
  243. if (config('common.click_type')) {
  244. # code...
  245. \Log::info('wechat_log_info:getURL-----进入方法');
  246. $mediaCustomer = MediaCustomer::mediaCustomerByAppid($appid);
  247. if (count($mediaCustomer)>0) {
  248. # code...
  249. // $send_event_contents = array();
  250. for ($i=0; $i < count($mediaCustomer); $i++) {
  251. # code...
  252. $send_event_content = array();
  253. $send_event_content['image'] = array('media_id'=>$mediaCustomer[$i]['media_id']);
  254. // $send_event_content['text'] = 'test_zhoulj';
  255. return response()->success($send_event_content);
  256. // $send_event_contents[$i] = $send_event_content;
  257. }
  258. }else{
  259. $timestamp = (time());
  260. $gzh_app_id = $appid;
  261. $group_nick = env('GROUP_NICK');
  262. $officialAccountPrams['appid'] = $appid;
  263. $officialAccount = (object)OfficialAccountService::officialAccountByAppid($officialAccountPrams);
  264. \Log::info('++++++++++++=====GET CUSTOM IMG [1] =====+++++++++++++');
  265. if (!empty($officialAccount)) {
  266. $distributionChannelSetting = CustomMsgService::customerImgUrlByChannelId($officialAccount->distribution_channel_id);
  267. \Log::info('++++++++++++=====GET CUSTOM IMG [2] =====+++++++++++++');
  268. if (!empty($distributionChannelSetting)) {
  269. # code...
  270. $params = array("gzh_app_id"=>$appid,"group_nick"=>$group_nick,"is_default"=>0,"img_url"=>$distributionChannelSetting['customer_img_url'],"timestamp"=>$timestamp);
  271. $sign = get_sign($params);
  272. $mediaCustomerJson ='';
  273. try {
  274. $mediaCustomerJson = $this->getUserClient()->request("GET","upload_gzh_img/",
  275. ['query'=>['gzh_app_id'=>$appid,'group_nick'=>env('GROUP_NICK'),"is_default"=>0,"img_url"=>$distributionChannelSetting['customer_img_url'],'timestamp'=>$timestamp,'sign'=>$sign],'connect_timeout' => 3]
  276. )->getBody()->getContents();
  277. } catch (\Exception $e) {
  278. \Log::info('++++++++++++=====GET CUSTOM IMG [0] =====+++++++++++++');
  279. }
  280. $mediaCustomerArray = json_decode($mediaCustomerJson,true);
  281. \Log::info('++++++++++++=====GET CUSTOM IMG [3] =====+++++++++++++');
  282. \Log::info($mediaCustomerArray);
  283. // \Log($mediaCustomerArray);
  284. // dd($mediaCustomerArray);
  285. if ($mediaCustomerArray['code'] == 1) {
  286. # code...
  287. $mediaCustomerData = $mediaCustomerArray['data'];
  288. if (empty($mediaCustomerDat)) {
  289. # code...
  290. $mediaCustomerCreate['media_id'] = $mediaCustomerData['media_id'];
  291. $mediaCustomerCreate['url'] = $mediaCustomerData['url'];
  292. $mediaCustomerCreate['appid'] = $appid;
  293. $mediaCustomerCreate['distribution_channel_id'] = $officialAccount->distribution_channel_id;
  294. MediaCustomer::create($mediaCustomerCreate);
  295. $send_event_content = array();
  296. $send_event_content['image'] = array('media_id'=>$mediaCustomerData['media_id']);
  297. // $send_event_content['text'] = 'test_zhoulj';
  298. return response()->success($send_event_content);
  299. }
  300. }else{
  301. $feedback['text'] = "客服QQ:3389644904\n客服电话:0571-56680019\n在线时间:工作日9:00-21:00\n";
  302. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  303. }
  304. }else{
  305. $params = array("gzh_app_id"=>$appid,"group_nick"=>$group_nick,"timestamp"=>$timestamp);
  306. $sign = get_sign($params);
  307. \Log::info('wechat_log_info:getURL-----'.env('MEDIA_API_BASE_URI'));
  308. \Log::info('wechat_log_info:getURL-----'.$gzh_app_id.'___'.$timestamp.'___'.$sign);
  309. $mediaCustomerJson ='';
  310. try {
  311. $mediaCustomerJson = $this->getUserClient()->request("GET","upload_gzh_img/",
  312. ['query'=>['gzh_app_id'=>$appid,'group_nick'=>env('GROUP_NICK'),'timestamp'=>$timestamp,'sign'=>$sign],'connect_timeout' => 3]
  313. )->getBody()->getContents();
  314. } catch (\Exception $e) {
  315. \Log::info('wechat_log_info:Exception-----'.$e->getMessage());
  316. }
  317. \Log::info('wechat_log_info:getURL-----'.env('MEDIA_API_BASE_URI'));
  318. $mediaCustomerArray = json_decode($mediaCustomerJson,true);
  319. // \Log($mediaCustomerArray);
  320. // dd($mediaCustomerArray);
  321. if ($mediaCustomerArray['code'] == 1) {
  322. # code...
  323. $mediaCustomerData = $mediaCustomerArray['data'];
  324. if (empty($mediaCustomerDat)) {
  325. # code...
  326. $mediaCustomerCreate['media_id'] = $mediaCustomerData['media_id'];
  327. $mediaCustomerCreate['url'] = $mediaCustomerData['url'];
  328. $mediaCustomerCreate['appid'] = $appid;
  329. $mediaCustomerCreate['distribution_channel_id'] = $officialAccount->distribution_channel_id;
  330. MediaCustomer::create($mediaCustomerCreate);
  331. $send_event_content = array();
  332. $send_event_content['image'] = array('media_id'=>$mediaCustomerData['media_id']);
  333. // $send_event_content['text'] = 'test_zhoulj';
  334. return response()->success($send_event_content);
  335. }
  336. }else{
  337. $feedback['text'] = "客服QQ:3389644904\n客服电话:0571-56680019\n在线时间:工作日9:00-21:00\n";
  338. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  339. }
  340. }
  341. }
  342. }
  343. }else{
  344. $feedback['text'] = "客服QQ:3389644904\n在线时间:工作日9:00-21:00\n";
  345. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  346. }
  347. }elseif ($content == "daily_sign" && !empty($openid)) {
  348. //调洋哥的接口发送签到客服消息
  349. \Log::info('========================进入洋哥签到回调');
  350. $feedback['text'] = UserSignService::userSignReturnContent3($openid,$distribution_channel_id);
  351. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site,$appid, $openid, $feedback['text'],'daily_sign');
  352. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  353. }elseif ($content == "recent_read" && !empty($openid)) {
  354. if($is_yq_move){
  355. $feedback['text'] = ForceSubscribeService::getYqMoveReply($distribution_channel_id,$openid);
  356. }
  357. else
  358. {
  359. $feedback['text'] = "<a href='".env('PROTOCOL')."://site".$encode_distribution_channel_id.".".env('WECHAT_CUSTOM_HOST').".com/recent'>点击查看最近阅读 </a>\n\n ";
  360. // 添加优质书库
  361. $sex = ForceSubscribeService::getSexByOpenid($distribution_channel_id,$openid);
  362. $sex_type = $sex_content = '';
  363. if($sex == 1){
  364. $sex_type = 'boy';
  365. $sex_content = '☞ 都市、玄幻、历史、军事、网游、灵异、科幻等海量书库任你看';
  366. }else{
  367. $sex_type = 'girl';
  368. $sex_content = '☞ 豪门、总裁、虐恋、婚宠、女尊、宫斗、经商、灵异等海量书库任你看';
  369. }
  370. $feedback['text'] .= "<a href='".env('PROTOCOL')."://site".$encode_distribution_channel_id.".".env('WECHAT_CUSTOM_HOST').".com/stock?sex=".$sex_type."'>".$sex_content."</a>";
  371. }
  372. // 追加热门书籍
  373. $feedback['text'] .= BookConfigService::getHotRandomRecommendBookText($distribution_channel_id,$uid, 2);
  374. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site,$appid, $openid, $feedback['text'],'recent_read');
  375. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  376. }
  377. // $feedback['text'] = "客服QQ:3389644904\n客服电话:0571-56680019\n在线时间:工作日9:00-21:00\n";
  378. // return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  379. }
  380. elseif($event == "unsubscribe"){
  381. $feedback['text'] = "";
  382. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  383. }
  384. elseif($event == "subscribe"){
  385. // 有自定义不回复内容的渠道
  386. $distribution_self_define_config = DistributionSelfDefineConfig::getDistributionSelfDefineConfig($distribution_channel_id,'force_subscribe_noreply');
  387. if(!empty($distribution_self_define_config)){
  388. \Log::info('distribution_self_define_config: distribution_channel_id:'.$distribution_channel_id);
  389. $feedback['text'] = "";
  390. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  391. }
  392. /**
  393. * 第 三 版
  394. */
  395. \Log::info('000000000000');
  396. if (!empty($openid)) {
  397. # code...
  398. \Log::info('subscribe:'.$openid.' distribution_channel_id:'.$distribution_channel_id);
  399. $params['openid'] = $openid;
  400. $forceSubscribeUser = ForceSubscribeService::forceSubscribeUsersByOpenid($params);
  401. \Log::info($forceSubscribeUser);
  402. // 有自定义回复内容的渠道
  403. // $distribution_self_define_content = DistributionSelfDefineContent::getDistributionSelfDefineContent($distribution_channel_id);
  404. // 从redis取
  405. $distribution_self_define_content = MsgService::getCurrentReply($distribution_channel_id);
  406. \Log::info('distribution_self_define_content:'.json_encode($distribution_self_define_content));
  407. // \Log::info($distribution_self_define_content);
  408. if(is_null($distribution_self_define_content)){
  409. // 获取特殊渠道的关注回复追加热门书籍推荐
  410. $distribution_replay = DistributionReply::getDistributionReply($distribution_channel_id);
  411. \Log::info('distribution_replay:'.json_encode($distribution_replay));
  412. $recommend_content = '';
  413. if(!is_null($distribution_replay)){
  414. $special_bids = explode(',', $distribution_replay->reply_bids);
  415. $recomm_books = BookConfigService::getBidRecommendBooks($special_bids);
  416. if (!is_null($recomm_books)) {
  417. $recommend_content = "\n\n" . '热门书籍推荐';
  418. foreach ($recomm_books as $book) {
  419. $url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . env('WECHAT_CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($book->bid) . '&cid=' . $book->first_cid;
  420. $recommend_content .= "\n\n" . '<a href="' . $url . '"> >>' . $book->book_name . '</a>';
  421. }
  422. }
  423. }
  424. if (!empty($forceSubscribeUser)) {
  425. # code...
  426. \Log::info('exist_force_user:'.$openid);
  427. //通过uid获取该用户最近阅读记录的书籍
  428. $uid = isset($forceSubscribeUser->uid)?$forceSubscribeUser->uid:'';
  429. $userBook = ReadRecordService::getFirstReadRecord($uid);
  430. $bookName = isset($userBook['book_name'])?$userBook['book_name']:'最近阅读';
  431. \Log::info('bookName:'.$bookName);
  432. if(!empty($recommend_content)){
  433. $feedback['text'] = '亲,终于等到你了!'."\n"."\n".'<a href="'.env('PROTOCOL').'://site'.$encode_distribution_channel_id.'.'.env('WECHAT_CUSTOM_HOST').'.com/continue">点我继续阅读 </a> '."\n"."\n".'阅读记录:'."\n"."\n".'><a href="'.env('PROTOCOL').'://site'.$encode_distribution_channel_id.'.'.env('WECHAT_CUSTOM_HOST').'.com/continue">'.$bookName.'</a>';
  434. $feedback['text'] .= $recommend_content;
  435. $feedback['text'] .= "\n"."\n".'为方便下次阅读,请'.'<a href="https://help.leyuee.com/top.html">'.'置顶公众号'.'</a>';
  436. }else{
  437. $feedback['text'] = '亲,终于等到你了!'."\n"."\n".'<a href="'.env('PROTOCOL').'://site'.$encode_distribution_channel_id.'.'.env('WECHAT_CUSTOM_HOST').'.com/continue">点我继续阅读 </a> '."\n"."\n".'阅读记录:'."\n"."\n".'><a href="'.env('PROTOCOL').'://site'.$encode_distribution_channel_id.'.'.env('WECHAT_CUSTOM_HOST').'.com/continue">'.$bookName.'</a>'."\n"."\n".'为方便下次阅读,请'.'<a href="https://help.leyuee.com/top.html">'.'置顶公众号'.'</a>'."\n\n".'今日签到成功,赠送30书币,连续签到2日后,赠送书币增加至50哦~,进入<a href="'.env('PROTOCOL').'://site'.$encode_distribution_channel_id.'.'.env('WECHAT_CUSTOM_HOST').'.com/person">个人中心 </a>查看!'."\n\n".'明日继续签到得书币哦!';
  438. }
  439. }else{
  440. $feedback['text'] = "<a href='".env('PROTOCOL')."://site".$encode_distribution_channel_id.".".env('WECHAT_CUSTOM_HOST').".com/continue'>点击继续阅读 </a> ";
  441. $feedback['text'] .= $recommend_content;
  442. }
  443. // 外站关注的直接回复图文qrscene_outer:123_12
  444. if(strpos($content,'outer') > -1){
  445. $scene_content = str_replace('qrscene_outer:','',$content);
  446. $bid_cid = explode('_', $scene_content);
  447. $bid = isset($bid_cid[0])?$bid_cid[0]:'';
  448. $cid = isset($bid_cid[1])?$bid_cid[1]:'';
  449. if(!empty($bid) && !empty($cid)){
  450. $book_conf = BookConfig::getBookById($bid);
  451. \Log::info('out_book_conf');\Log::info($book_conf);
  452. if(!empty($book_conf)){
  453. $data = array();
  454. $encode_bid = Hashids::encode($bid);
  455. $data[]=['url'=>env('PROTOCOL').'://site'.$encode_distribution_channel_id.'.'.env('WECHAT_CUSTOM_HOST').'.com'.'/reader?bid='.$encode_bid.'&cid='.$cid.'&fromtype=outer_'.$scene_content,
  456. 'title'=>$book_conf['book_name'],
  457. 'description'=>$book_conf['intro'],
  458. 'image'=>$book_conf['cover']
  459. ];
  460. \Log::info('outer:'.json_encode($data));
  461. $feedback['text'] = $data;
  462. }
  463. }
  464. }
  465. }else{
  466. // 判断是否图文
  467. if(isset($distribution_self_define_content->is_pic) && $distribution_self_define_content->is_pic){
  468. $feedback['text'] = ImageNewsToArray($distribution_self_define_content->content);
  469. }else{
  470. $feedback['text'] = isset($distribution_self_define_content->content)?$distribution_self_define_content->content:'';
  471. }
  472. }
  473. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site,$appid, $openid, $feedback['text'],'subscribe');
  474. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  475. }else{
  476. $feedback['text'] = "<a href='".env('PROTOCOL')."://site".$encode_distribution_channel_id.".".env('WECHAT_CUSTOM_HOST').".com/continue'>点击继续阅读 </a> ";
  477. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site,$appid, $openid, $feedback['text'],'subscribe');
  478. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  479. }
  480. }elseif($event == "TEMPLATESENDJOBFINISH"){
  481. $feedback['text'] = "";
  482. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  483. }else{
  484. // $feedback['text'] = "事件未配置";
  485. // return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  486. return response()->error('EVENT_NOT_FOUND');
  487. }
  488. }
  489. public function getUserClient(){
  490. // return new Client(['base_uri' => 'http://118.31.133.29/api/']);
  491. return new Client(['base_uri' => env('MEDIA_API_BASE_URI')]);
  492. }
  493. }