OfficialInteractiveEventController.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. <?php
  2. namespace App\Http\Controllers\Wechat\OfficialAccount;
  3. use App\Consts\Channel\ChannelConst;
  4. use App\Http\Controllers\Controller;
  5. use App\Modules\Book\Services\BookService;
  6. use App\Modules\Channel\Services\ChannelService;
  7. use App\Modules\Book\Services\BookSearchStatService;
  8. use App\Modules\Channel\Models\Channel;
  9. use App\Modules\Channel\Models\YqMoveChannel;
  10. use App\Modules\OfficialAccount\Models\MediaCustomer;
  11. use App\Modules\OfficialAccount\Models\OfficialAccount;
  12. use App\Modules\OfficialAccount\Models\OfficialInteractiveEvent;
  13. use App\Modules\OfficialAccount\Models\WechatKeywordMsgs;
  14. use App\Modules\OfficialAccount\Models\DistributionReply;
  15. use App\Modules\OfficialAccount\Models\DistributionSelfDefineContent;
  16. use App\Modules\OfficialAccount\Models\DistributionSelfDefineConfig;
  17. use App\Modules\OfficialAccount\Services\OfficialAccountMsgService;
  18. use App\Modules\OfficialAccount\Services\OfficialAccountService;
  19. use App\Modules\OfficialAccount\Services\CustomMsgService;
  20. use App\Modules\OfficialAccount\Services\MsgService;
  21. use App\Modules\User\Services\ReadRecordService;
  22. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  23. use App\Http\Controllers\Wechat\OfficialAccount\Transformers\OfficialInteractiveEventTransformer;
  24. use App\Http\Controllers\Wechat\OfficialAccount\Transformers\OfficialInteractiveEventTextTransformer;
  25. use App\Modules\Book\Services\BookConfigService;
  26. use App\Modules\User\Services\UserSignService;
  27. use Illuminate\Http\Request;
  28. use GuzzleHttp\Client;
  29. use App\Libs\OSS;
  30. use Hashids;
  31. use Redis;
  32. use DB;
  33. use App\Modules\Book\Models\BookConfig;
  34. use App\Modules\Promotion\Services\PromotionService;
  35. class OfficialInteractiveEventController extends Controller
  36. {
  37. /**
  38. * @apiDefine OfficialAccount 公众号
  39. */
  40. /**
  41. * @apiVersion 1.0.0
  42. * @api {GET} OfficialAccount/OfficialInteractiveEventFeedback 公众号交互事件推送
  43. * @apiGroup OfficialAccount
  44. * @apiName OfficialInteractiveEventFeedback
  45. * @apiParam {String} appid 公众号的appid.
  46. * @apiParam {String} event 事件类型.
  47. * @apiParam {String} content text类型下的搜索条件.
  48. * @apiSuccess {String} text 返回的文字信息.
  49. * @apiSuccess {String} title 搜索出的书籍标题.
  50. * @apiSuccess {String} description 搜索出的书籍简介.
  51. * @apiSuccess {String} url 搜索出的书籍链接.
  52. * @apiSuccess {String} image 搜索出的书籍封面地址.
  53. * @apiSuccessExample {json} Success-Response:
  54. *
  55. * {
  56. * "code": 0,
  57. * "msg": "",
  58. * "data": {
  59. * "text": "用户取消强制关注"
  60. * }
  61. * }
  62. *
  63. * {
  64. * "code": 0,
  65. * "msg": "",
  66. * "data": [
  67. * {
  68. * "title": "肌缘巧合",
  69. * "description": "&nbsp;&nbsp;&nbsp;&nbsp;他是权势倾天,纵横商界的王者,却偏偏钟情于她,一宠成瘾。“女人,我要你......只要你能满足我,别墅、游轮、支票,你随便挑。”她羞涩的半低着头:“我只想要你。”他挑眉,“你野心不小啊!”她妩媚一笑,解开他的领带,“难道你不愿意!”他宠她爱她,给她所有想要的。只是有一天她终于忍不住暴走,“靠,你有没有节制呀?我要离婚。”",
  70. * "url": "test2.ycsd.cn?bid=5",
  71. * "image": "https:\/\/leyue-bucket.oss-cn-hangzhou.aliyuncs.com\/ycsd_cover\/covermiddle\/0\/8.jpg"
  72. * },
  73. * {
  74. * "title": "等你爱我",
  75. * "description": "&nbsp;&nbsp;&nbsp;&nbsp;“只要你愿意成全我,给你所有……”黑暗中,她为救他,成了他的女人,他却隔天清晨匆匆离去。六年后,她进入他的公司,与他擦肩而过,却互不相识,但一切却悄然发生改变,他有了自己爱的人,她有了爱自己的人……她带着女儿疲于奔命,他重新进入她的生活,当他决定娶她时,她却淡淡一笑,转身离开……",
  76. * "url": "test2.ycsd.cn?bid=9",
  77. * "image": "https:\/\/leyue-bucket.oss-cn-hangzhou.aliyuncs.com\/ycsd_cover\/covermiddle\/0\/4.jpg"
  78. * }
  79. * ]
  80. * }
  81. */
  82. function officialInteractiveEventFeedback(Request $request)
  83. {
  84. $appid = $request->has('appid') ? $request->input('appid') : '';
  85. $event = $request->has('event') ? $request->input('event') : '';
  86. $uid = $request->has('uid') ? $request->input('uid') : '';
  87. $openid = $request->has('openid') ? $request->input('openid') : '';
  88. if (empty($event)) {
  89. return response()->error("PARAM_EMPTY");
  90. }
  91. $content = $request->has('content') ? $request->input('content') : '';
  92. $officialAccount = OfficialAccount::officialAccountByAppid($appid);
  93. $distribution_channel_id = '';
  94. if (!empty($officialAccount)) {
  95. $distribution_channel_id = $officialAccount['distribution_channel_id'];
  96. } else {
  97. $distribution_channel_id = '';
  98. }
  99. if ($event == "text") {
  100. if (empty($content)) {
  101. return response()->error("PARAM_EMPTY");
  102. }
  103. }
  104. $encode_distribution_channel_id = encodeDistributionChannelId($distribution_channel_id);
  105. // 判断渠道是否是外站派单(需要我们帮生成强关用户)
  106. $channel = Channel::getById($distribution_channel_id);
  107. $is_outer_site = isset($channel->is_outer_site) ? $channel->is_outer_site : '0';
  108. $is_domain_simple = isset($channel->is_domain_simple) ? $channel->is_domain_simple : '0';
  109. if ($is_domain_simple == '1') {
  110. $encode_distribution_channel_id = $distribution_channel_id;
  111. }
  112. $params['openid'] = $openid;
  113. $forceSubscribeUser = ForceSubscribeService::forceSubscribeUsersByOpenid($params);
  114. $uid = isset($forceSubscribeUser->uid) ? $forceSubscribeUser->uid : '0';
  115. $WECHAT_CUSTOM_HOST = env('WECHAT_CUSTOM_HOST');
  116. $base_url = env('PROTOCOL') . "://site" . $encode_distribution_channel_id . "." . $WECHAT_CUSTOM_HOST . ".com";
  117. $help_url = env('PROTOCOL') . '://help.' . $WECHAT_CUSTOM_HOST . '.com?distribution_channel_id=' . $encode_distribution_channel_id;
  118. if ($event == "text") {
  119. // 有自定义不回复文本内容的渠道
  120. $distribution_self_define_config = DistributionSelfDefineConfig::getDistributionSelfDefineConfig($distribution_channel_id, 'force_subscribe_notext_reply');
  121. if (!empty($distribution_self_define_config)) {
  122. $feedback['text'] = "";
  123. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  124. }
  125. // 搜索写入
  126. if ($content != 'search_get_recommend_book') {
  127. if (strlen($content) <= 100) {
  128. BookSearchStatService::create($uid, $openid, 'wechat', $content);
  129. }
  130. }
  131. // 单独推送一本智能推荐的书
  132. if ($content == 'search_get_recommend_book') {
  133. \Log::info('get_recommend_book:' . $openid);
  134. $feedback['text'] = "";
  135. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  136. $bid = isset($forceSubscribeUser->bid) ? $forceSubscribeUser->bid : '0';
  137. $params['openid'] = $openid;
  138. $recommend_book = BookConfigService::getSimpleChannelBookLoop($bid, 1, $uid);
  139. $feedback['text'] = '';
  140. if (!empty($recommend_book)) {
  141. $bookUrl = env('PROTOCOL') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . $WECHAT_CUSTOM_HOST . '.com';
  142. $bookUrl = $bookUrl . $recommend_book[0]->url . '&fromtype=' . $content . '&source=wechatmsg&fromsource=smart_push';
  143. $feedback['text'] = '新书推荐: <a href="' . $bookUrl . '"> 《' . $recommend_book[0]->book_name . '》</a>';
  144. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $feedback['text'], 'get_one_recommend_book');
  145. }
  146. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  147. }
  148. if ($content == '签到') {
  149. //调洋哥的接口发送签到客服消息
  150. \Log::info('text_sign:' . $openid);
  151. $feedback['text'] = UserSignService::userSignReturnContent3($openid, $distribution_channel_id, $WECHAT_CUSTOM_HOST);
  152. $top_url = OfficialAccountService::getTopUrl($officialAccount->service_type_info);
  153. if ($officialAccount->service_type_info == 1) {
  154. $top_content = "\n\n" . '为方便阅读,请<a href="' . $top_url . '">设为星标</a>';
  155. } else {
  156. $top_content = "\n\n" . '为方便阅读,请<a href="' . $top_url . '">置顶公众号</a>';
  157. }
  158. $feedback['text'] .= $top_content;
  159. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $feedback['text'], 'text_sign');
  160. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  161. }
  162. $booksInfo = array();
  163. // 判断是否自定义关键字
  164. $special_keyword = WechatKeywordMsgs::wechatKeywordByDistributionChannelIdAndKeyword($distribution_channel_id, $content, 1);
  165. if (!empty($special_keyword)) {
  166. \Log::info('officialInteractiveEventFeedback_has_special_keyword:' . $distribution_channel_id . ' content:' . $content);
  167. \Log::info($special_keyword);
  168. $bid = Hashids::decode($special_keyword['bid'])[0];
  169. \Log::info('bid:' . $bid);
  170. $book_conf = BookConfig::getBookById($bid);
  171. if (!empty($book_conf)) {
  172. $special_keyword_new = array();
  173. $special_keyword_new['book_name'] = isset($special_keyword['send_title']) && !empty($special_keyword['send_title']) ? $special_keyword['send_title'] : $book_conf['book_name'];
  174. $special_keyword_new['cover'] = isset($special_keyword['send_cover']) && !empty($special_keyword['send_cover']) ? $special_keyword['send_cover'] : $book_conf['cover'];
  175. \Log::info('keyword_book_name:' . $special_keyword_new['book_name'] . ' cover:' . $special_keyword_new['cover']);
  176. $special_keyword_new['intro'] = $book_conf['intro'];
  177. // 有派单id的关键字不做统计
  178. if (isset($special_keyword['send_order_id']) && $special_keyword['send_order_id'] > 0) {
  179. $special_keyword_new['book_url'] = '/yun/' . $special_keyword['send_order_id'];
  180. } else {
  181. $special_keyword_new['book_url'] = '/reader?bid=' . $special_keyword['bid'] . '&cid=' . $special_keyword['cid'] . '&fromtype=keyword_' . $special_keyword['id'] . '&source=wechatmsg';
  182. }
  183. $special_keyword_new['bid'] = $special_keyword['bid'];
  184. $booksInfo[] = $special_keyword_new;
  185. } else {
  186. \Log::info('book_conf_null:' . $distribution_channel_id . ' content:' . $content);
  187. }
  188. } else {
  189. $booksInfo = BookConfig::getBooksByKey($content, 1);
  190. try {
  191. if (count($booksInfo) == 0) {
  192. $role_book = BookConfig::getFromRole($content);
  193. if ($role_book) {
  194. $booksInfo[] = [
  195. 'book_name' => $role_book->book_name,
  196. 'intro' => '',
  197. 'cover' => $role_book->cover,
  198. 'bid' => $role_book->bid,
  199. 'book_url' => '/detail?id=' . Hashids::encode($role_book->bid),
  200. ];
  201. }
  202. }
  203. } catch (\Exception $e) {
  204. }
  205. }
  206. if (count($booksInfo) == 0) {
  207. if (preg_match('/^[0-9]+$/', $content)) {
  208. $channel_sex = ChannelService::getChannelCompanySex($distribution_channel_id);
  209. $feedback['text'] = "尊敬的会员:\n\n <a href='" . env('PROTOCOL') . "://site" . $encode_distribution_channel_id . "." . $WECHAT_CUSTOM_HOST . ".com/recent'>☞ 点我继续上次阅读 </a>";
  210. if ($channel_sex != 1) {
  211. //热门书籍推荐
  212. $sex = ForceSubscribeService::getUserRealSexByUid($uid);
  213. $channel_name = $sex == 1 ? '男频' : '女频';
  214. $recomm_books = BookConfigService::getSignRecommendBooksFromCache([], $channel_name, 2);
  215. $recent_read_reco_content = '';
  216. if ($recomm_books) {
  217. // 判断是否不展示书名
  218. $self_config = ChannelService::check_channel_account_priv($distribution_channel_id, 'hide_book_name');
  219. $hide_type = isset($self_config->type) ? $self_config->type : 'not_hide';
  220. $recommend_titles = [];
  221. if ($hide_type == 'hide_book_name') {
  222. \Log::info('hide_book_name reco:' . $distribution_channel_id . ' uid:' . $uid);
  223. $recommend_titles = PromotionService::getRandomHeadline($sex, count($recomm_books));
  224. }
  225. $recent_read_reco_content .= "\n\n" . '热门书籍推荐~';
  226. foreach ($recomm_books as $key => $book) {
  227. $book_name = $book->book_name;
  228. if ($hide_type == 'hide_book_name') {
  229. $book_name = isset($recommend_titles[$key]->title) ? $recommend_titles[$key]->title : $book->book_name;
  230. }
  231. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($book->bid) . '&cid=' . $book->first_cid;
  232. $recent_read_reco_content .= "\n\n" . '<a href="' . $url . '"> ☞ 《' . $book_name . '》</a>';
  233. }
  234. }
  235. $feedback['text'] .= $recent_read_reco_content;
  236. }
  237. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $feedback['text'], 'recent_read');
  238. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  239. }
  240. $feedback['text'] = '未找到相关小说,' . "\n" . '您可以试试:' . "\n" . "\n" . '<a href="' . env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . $WECHAT_CUSTOM_HOST . '.com/recent">查看阅读记录 >> </a> ' . "\n" . "\n" . '<a href="' . env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . $WECHAT_CUSTOM_HOST . '.com">去书城首页看看 >> </a> ' . "\n" . "\n" . '点此<a href="https://help.zhuishuyun.com/?distribution_channel_id=' . encodeDistributionChannelId($distribution_channel_id) . '&down=1">联系客服</a>';
  241. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $feedback['text'], 'text_search');
  242. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  243. } else {
  244. $data = [];
  245. for ($i = 0; $i < count($booksInfo); $i++) {
  246. $bid = $booksInfo[$i]['bid'];
  247. $base_url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . $WECHAT_CUSTOM_HOST . '.com' . $booksInfo[$i]['book_url'];
  248. $instro_len = strlen($booksInfo[$i]['intro']);
  249. \Log::info('wechat_log_info:len:' . $instro_len . ' getURL-----' . $base_url);
  250. if ($instro_len > 100) {
  251. $booksInfo[$i]['intro'] = mb_substr($booksInfo[$i]['intro'], 0, 100, 'utf-8');
  252. $booksInfo[$i]['intro'] .= '......';
  253. \Log::info('search_instro_too_long:' . $booksInfo[$i]['intro']);
  254. }
  255. $data[$i] = ['url' => $base_url,
  256. 'title' => $booksInfo[$i]['book_name'],
  257. 'description' => $booksInfo[$i]['intro'],
  258. 'image' => $booksInfo[$i]['cover']];
  259. // 返回一条
  260. break;
  261. }
  262. $datatext['text'] = $data;
  263. $datatext['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $datatext['text'], 'text_search');
  264. return response()->success($datatext);
  265. }
  266. } elseif ($event == "scan") {
  267. // 有自定义不回复内容的渠道
  268. $distribution_self_define_config = DistributionSelfDefineConfig::getDistributionSelfDefineConfig($distribution_channel_id, 'force_subscribe_noreply');
  269. if (!empty($distribution_self_define_config)) {
  270. \Log::info('distribution_self_define_config: distribution_channel_id:' . $distribution_channel_id);
  271. $feedback['text'] = "";
  272. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  273. }
  274. $feedback['text'] = "<a href='" . env('PROTOCOL') . "://site" . $encode_distribution_channel_id . "." . $WECHAT_CUSTOM_HOST . ".com/continue'>点此继续阅读 </a> ";
  275. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $feedback['text'], 'scan');
  276. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  277. } elseif ($event == "click") {
  278. \Log::info('content:' . $content);
  279. if ($content == "contact_customer" && !empty($appid)) {
  280. if (config('common.click_type')) {
  281. # code...
  282. \Log::info('wechat_log_info:getURL-----进入方法');
  283. $mediaCustomer = MediaCustomer::mediaCustomerByAppid($appid);
  284. if (count($mediaCustomer) > 0) {
  285. # code...
  286. // $send_event_contents = array();
  287. for ($i = 0; $i < count($mediaCustomer); $i++) {
  288. # code...
  289. $send_event_content = array();
  290. $send_event_content['image'] = array('media_id' => $mediaCustomer[$i]['media_id']);
  291. // $send_event_content['text'] = 'test_zhoulj';
  292. return response()->success($send_event_content);
  293. // $send_event_contents[$i] = $send_event_content;
  294. }
  295. } else {
  296. $timestamp = (time());
  297. $gzh_app_id = $appid;
  298. $group_nick = env('GROUP_NICK');
  299. $officialAccountPrams['appid'] = $appid;
  300. $officialAccount = (object)OfficialAccountService::officialAccountByAppid($officialAccountPrams);
  301. \Log::info('++++++++++++=====GET CUSTOM IMG [1] =====+++++++++++++');
  302. if (!empty($officialAccount)) {
  303. $distributionChannelSetting = CustomMsgService::customerImgUrlByChannelId($officialAccount->distribution_channel_id);
  304. \Log::info('++++++++++++=====GET CUSTOM IMG [2] =====+++++++++++++');
  305. if (!empty($distributionChannelSetting)) {
  306. # code...
  307. $params = array("gzh_app_id" => $appid, "group_nick" => $group_nick, "is_default" => 0, "img_url" => $distributionChannelSetting['customer_img_url'], "timestamp" => $timestamp);
  308. $sign = get_sign($params);
  309. $mediaCustomerJson = '';
  310. try {
  311. $mediaCustomerJson = $this->getUserClient()->request("GET", "upload_gzh_img/",
  312. ['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]
  313. )->getBody()->getContents();
  314. } catch (\Exception $e) {
  315. \Log::info('++++++++++++=====GET CUSTOM IMG [0] =====+++++++++++++');
  316. }
  317. $mediaCustomerArray = json_decode($mediaCustomerJson, true);
  318. \Log::info('++++++++++++=====GET CUSTOM IMG [3] =====+++++++++++++');
  319. \Log::info($mediaCustomerArray);
  320. // \Log($mediaCustomerArray);
  321. // dd($mediaCustomerArray);
  322. if ($mediaCustomerArray['code'] == 1) {
  323. # code...
  324. $mediaCustomerData = $mediaCustomerArray['data'];
  325. if (empty($mediaCustomerDat)) {
  326. # code...
  327. $mediaCustomerCreate['media_id'] = $mediaCustomerData['media_id'];
  328. $mediaCustomerCreate['url'] = $mediaCustomerData['url'];
  329. $mediaCustomerCreate['appid'] = $appid;
  330. $mediaCustomerCreate['distribution_channel_id'] = $officialAccount->distribution_channel_id;
  331. MediaCustomer::create($mediaCustomerCreate);
  332. $send_event_content = array();
  333. $send_event_content['image'] = array('media_id' => $mediaCustomerData['media_id']);
  334. // $send_event_content['text'] = 'test_zhoulj';
  335. return response()->success($send_event_content);
  336. }
  337. } else {
  338. $feedback['text'] = env('CUSTOM_RETURN_MSG');
  339. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  340. }
  341. } else {
  342. $params = array("gzh_app_id" => $appid, "group_nick" => $group_nick, "timestamp" => $timestamp);
  343. $sign = get_sign($params);
  344. \Log::info('wechat_log_info:getURL-----' . env('MEDIA_API_BASE_URI'));
  345. \Log::info('wechat_log_info:getURL-----' . $gzh_app_id . '___' . $timestamp . '___' . $sign);
  346. $mediaCustomerJson = '';
  347. try {
  348. $mediaCustomerJson = $this->getUserClient()->request("GET", "upload_gzh_img/",
  349. ['query' => ['gzh_app_id' => $appid, 'group_nick' => env('GROUP_NICK'), 'timestamp' => $timestamp, 'sign' => $sign], 'connect_timeout' => 3]
  350. )->getBody()->getContents();
  351. } catch (\Exception $e) {
  352. \Log::info('wechat_log_info:Exception-----' . $e->getMessage());
  353. }
  354. \Log::info('wechat_log_info:getURL-----' . env('MEDIA_API_BASE_URI'));
  355. $mediaCustomerArray = json_decode($mediaCustomerJson, true);
  356. // \Log($mediaCustomerArray);
  357. // dd($mediaCustomerArray);
  358. if ($mediaCustomerArray['code'] == 1) {
  359. # code...
  360. $mediaCustomerData = $mediaCustomerArray['data'];
  361. if (empty($mediaCustomerDat)) {
  362. # code...
  363. $mediaCustomerCreate['media_id'] = $mediaCustomerData['media_id'];
  364. $mediaCustomerCreate['url'] = $mediaCustomerData['url'];
  365. $mediaCustomerCreate['appid'] = $appid;
  366. $mediaCustomerCreate['distribution_channel_id'] = $officialAccount->distribution_channel_id;
  367. MediaCustomer::create($mediaCustomerCreate);
  368. $send_event_content = array();
  369. $send_event_content['image'] = array('media_id' => $mediaCustomerData['media_id']);
  370. // $send_event_content['text'] = 'test_zhoulj';
  371. return response()->success($send_event_content);
  372. }
  373. } else {
  374. $feedback['text'] = env('CUSTOM_RETURN_MSG');
  375. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  376. }
  377. }
  378. }
  379. }
  380. } else {
  381. $feedback['text'] = env('CUSTOM_RETURN_MSG');
  382. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  383. }
  384. } elseif ($content == "daily_sign" && !empty($openid)) {
  385. //调洋哥的接口发送签到客服消息
  386. \Log::info('========================进入洋哥签到回调');
  387. try {
  388. $feedback['text'] = UserSignService::userSignReturnContent3($openid, $distribution_channel_id, $WECHAT_CUSTOM_HOST);
  389. } catch (\Exception $e) {
  390. \Log::error('sign_activity_error:');
  391. \Log::error($e);
  392. }
  393. $top_url = OfficialAccountService::getTopUrl($officialAccount->service_type_info);
  394. if ($officialAccount->service_type_info == 1) {
  395. $top_content = "\n\n" . '为方便阅读,请<a href="' . $top_url . '">设为星标</a>';
  396. } else {
  397. $top_content = "\n\n" . '为方便阅读,请<a href="' . $top_url . '">置顶公众号</a>';
  398. }
  399. $feedback['text'] .= $top_content;
  400. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $feedback['text'], 'daily_sign');
  401. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  402. } elseif ($content == "recent_read" && !empty($openid)) {
  403. // $channel_sex = ChannelService::getChannelCompanySex($distribution_channel_id);
  404. $channelInfo = ChannelService::getById($distribution_channel_id);
  405. $property = isset($channelInfo->property) ? $channelInfo->property : '';
  406. $channel_sex = 3;
  407. if ($property === ChannelConst::PROPERTY_MALE) {
  408. $channel_sex = 1;
  409. }
  410. if ($property === ChannelConst::PROPERTY_FEMALE) {
  411. $channel_sex = 2;
  412. }
  413. \Log::info('channel_sex:' . $channel_sex . ' distribution_channel_id:' . $distribution_channel_id . ' property:' . $property);
  414. $feedback['text'] = "尊敬的会员:\n\n <a href='" . env('PROTOCOL') . "://site" . $encode_distribution_channel_id . "." . $WECHAT_CUSTOM_HOST . ".com/recent'>☞ 点我继续上次阅读 </a>";
  415. if ($channel_sex != 1) {
  416. // 根据用户性别确定男女频
  417. $sex = ForceSubscribeService::getUserRealSexByUid($uid);
  418. $channel_name = $sex == 1 ? '男频' : '女频';
  419. // 站点属性优先
  420. if ($channel_sex == 2) {
  421. $channel_name = '女频';
  422. }
  423. $recomm_books = BookConfigService::getSignRecommendBooksFromCache([], $channel_name, 2);
  424. $recent_read_reco_content = '';
  425. if ($recomm_books) {
  426. // 判断是否不展示书名
  427. $self_config = ChannelService::check_channel_account_priv($distribution_channel_id, 'hide_book_name');
  428. $hide_type = isset($self_config->type) ? $self_config->type : 'not_hide';
  429. $recommend_titles = [];
  430. if ($hide_type == 'hide_book_name') {
  431. \Log::info('hide_book_name reco:' . $distribution_channel_id . ' uid:' . $uid);
  432. $recommend_titles = PromotionService::getRandomHeadline($sex, count($recomm_books));
  433. }
  434. $recent_read_reco_content .= "\n\n" . '热门书籍推荐~';
  435. foreach ($recomm_books as $key => $book) {
  436. $book_name = $book->book_name;
  437. if ($hide_type == 'hide_book_name') {
  438. $book_name = isset($recommend_titles[$key]->title) ? $recommend_titles[$key]->title : $book->book_name;
  439. }
  440. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($book->bid) . '&cid=' . $book->first_cid;
  441. $recent_read_reco_content .= "\n\n" . '<a href="' . $url . '"> ☞ 《' . $book_name . '》</a>';
  442. }
  443. }
  444. $feedback['text'] .= $recent_read_reco_content;
  445. } else {
  446. // 男频站点
  447. // 1本在测的,1本优质书,共2本,如果没有在测的书,则返回2本优质书
  448. // 用户按照15天注册后的用户
  449. $sex = ForceSubscribeService::getUserRealSexByUid($uid);
  450. [$bids, $test_bid] = BookConfig::getLeftRecomBids($uid, '男频', 2);
  451. \Log::info('bids and test_bid:' . $uid . ' data:' . json_encode([$bids, $test_bid]));
  452. if ($bids) {
  453. $recomm_books = BookService::getSimpleBooksByIds($bids);
  454. $recent_read_reco_content = '';
  455. if ($recomm_books) {
  456. // 判断是否不展示书名
  457. $self_config = ChannelService::check_channel_account_priv($distribution_channel_id, 'hide_book_name');
  458. $hide_type = isset($self_config->type) ? $self_config->type : 'not_hide';
  459. $recommend_titles = [];
  460. if ($hide_type == 'hide_book_name') {
  461. \Log::info('hide_book_name reco:' . $distribution_channel_id . ' uid:' . $uid);
  462. $recommend_titles = PromotionService::getRandomHeadline($sex, count($recomm_books));
  463. }
  464. $recent_read_reco_content .= "\n\n" . '热门书籍推荐~';
  465. foreach ($recomm_books as $key => $book) {
  466. $book_name = $book->name;
  467. if ($hide_type == 'hide_book_name') {
  468. $book_name = isset($recommend_titles[$key]->title) ? $recommend_titles[$key]->title : $book_name;
  469. }
  470. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($book->id) . '&cid=' . $book->first_cid;
  471. if ($book->id == $test_bid) {
  472. $url .= '&fromsource=smart_push';
  473. }
  474. $recent_read_reco_content .= "\n\n" . '<a href="' . $url . '"> ☞ 《' . $book_name . '》</a>';
  475. }
  476. }
  477. $feedback['text'] .= $recent_read_reco_content;
  478. }
  479. }
  480. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $feedback['text'], 'recent_read');
  481. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  482. } // 个别渠道是事件
  483. elseif ($content == "user_center" && !empty($openid)) {
  484. \Log::info('click_user_center:' . $openid);
  485. $feedback['text'] = "☞ <a href='" . $base_url . "/person'>个人中心</a>\n\n";
  486. $feedback['text'] .= "☞ <a href='" . $base_url . "/'>书城首页</a>\n\n";
  487. $feedback['text'] .= "☞ <a href='" . $base_url . "/pay'>优惠充值</a>\n\n";
  488. $feedback['text'] .= "☞ <a href='" . $help_url . "'>帮助中心</a>\n";
  489. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $feedback['text'], 'user_center');
  490. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  491. } elseif ($content == "qrcode_main" && !empty($openid)) {
  492. $msg = OfficialAccountMsgService::getEventMsg(['appid' => $appid, 'apply_event' => 'click', 'apply_event_key' => 'qrcode_main']);
  493. if ($msg) {
  494. $send_event_content = array();
  495. $send_event_content['image'] = array('media_id' => $msg->return_content);
  496. return response()->success($send_event_content);
  497. } else {
  498. $feedback['text'] = '配置有误';
  499. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  500. }
  501. }
  502. } elseif ($event == "unsubscribe") {
  503. $feedback['text'] = "";
  504. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  505. } elseif ($event == "subscribe") {
  506. // 统计
  507. $sub_statistic_key = 'gzh_actual_sub:' . $appid . ':' . date('Y-m-d');
  508. \Log::info('sub_statistic_key:' . $sub_statistic_key);
  509. Redis::sadd($sub_statistic_key, $openid);
  510. Redis::expire($sub_statistic_key, 30 * 3600);
  511. // 有自定义不回复内容的渠道
  512. $distribution_self_define_config = DistributionSelfDefineConfig::getDistributionSelfDefineConfig($distribution_channel_id, 'force_subscribe_noreply');
  513. if (!empty($distribution_self_define_config)) {
  514. \Log::info('distribution_self_define_config: distribution_channel_id:' . $distribution_channel_id);
  515. $feedback['text'] = "";
  516. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  517. }
  518. if (!empty($openid)) {
  519. # code...
  520. \Log::info('subscribe:' . $openid . ' distribution_channel_id:' . $distribution_channel_id);
  521. $params['openid'] = $openid;
  522. $forceSubscribeUser = ForceSubscribeService::forceSubscribeUsersByOpenid($params);
  523. \Log::info($forceSubscribeUser);
  524. // 有自定义回复内容的渠道
  525. // $distribution_self_define_content = DistributionSelfDefineContent::getDistributionSelfDefineContent($distribution_channel_id);
  526. // 从redis取
  527. $distribution_self_define_content = MsgService::getCurrentReply($distribution_channel_id);
  528. // 判断渠道,如果是场景值关注过来的,则普通回复,否则自定义回复
  529. $subscribe_from_scene = DistributionSelfDefineConfig::getDistributionSelfDefineConfig($distribution_channel_id, 'subscribe_from_scene');
  530. if (!empty($subscribe_from_scene)) {
  531. if (!empty($content) && strpos($content, 'qrscene_') === 0) {
  532. $distribution_self_define_content = null;
  533. \Log::info('subscribe_from_scene:' . $distribution_channel_id);
  534. }
  535. }
  536. if (is_null($distribution_self_define_content)) {
  537. $recommend_content = '';
  538. if (!empty($forceSubscribeUser)) {
  539. # code...
  540. \Log::info('exist_force_user:' . $openid);
  541. //通过uid获取该用户最近阅读记录的书籍
  542. $uid = isset($forceSubscribeUser->uid) ? $forceSubscribeUser->uid : '';
  543. $userBook = ReadRecordService::getFirstReadRecord($uid);
  544. $bookName = isset($userBook['book_name']) ? $userBook['book_name'] : '最近阅读';
  545. \Log::info('bookName:' . $bookName);
  546. if (!empty($recommend_content)) {
  547. $feedback['text'] = '亲,终于等到你了!' . "\n" . "\n" . '<a href="' . env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . $WECHAT_CUSTOM_HOST . '.com/continue">点我继续阅读 </a> ' . "\n" . "\n" . '阅读记录:' . "\n" . "\n" . '><a href="' . env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . $WECHAT_CUSTOM_HOST . '.com/continue">' . $bookName . '</a>';
  548. $feedback['text'] .= $recommend_content;
  549. $feedback['text'] .= "\n" . "\n" . '为方便下次阅读,请' . '<a href="https://help.leyuee.com/top.html">' . '置顶公众号' . '</a>';
  550. } else {
  551. $feedback['text'] = '亲,终于等到你了!' . "\n" . "\n" . '<a href="' . env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . $WECHAT_CUSTOM_HOST . '.com/continue">点我继续阅读 </a> ' . "\n" . "\n" . '阅读记录:' . "\n" . "\n" . '><a href="' . env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . $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 . '.' . $WECHAT_CUSTOM_HOST . '.com/person">个人中心 </a>查看!' . "\n\n" . '明日继续签到得书币哦!';
  552. }
  553. } else {
  554. $feedback['text'] = "<a href='" . env('PROTOCOL') . "://site" . $encode_distribution_channel_id . "." . $WECHAT_CUSTOM_HOST . ".com/continue'>点击继续阅读 </a> ";
  555. $feedback['text'] .= $recommend_content;
  556. }
  557. // 外站关注的直接回复图文qrscene_outer:123_12
  558. if (strpos($content, 'outer') > -1) {
  559. $scene_content = str_replace('qrscene_outer:', '', $content);
  560. $bid_cid = explode('_', $scene_content);
  561. $bid = isset($bid_cid[0]) ? $bid_cid[0] : '';
  562. $cid = isset($bid_cid[1]) ? $bid_cid[1] : '';
  563. if (!empty($bid) && !empty($cid)) {
  564. $book_conf = BookConfig::getBookById($bid);
  565. \Log::info('out_book_conf');
  566. \Log::info($book_conf);
  567. if (!empty($book_conf)) {
  568. $data = array();
  569. $encode_bid = Hashids::encode($bid);
  570. $data[] = ['url' => env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . $WECHAT_CUSTOM_HOST . '.com' . '/reader?bid=' . $encode_bid . '&cid=' . $cid . '&fromtype=outer_' . $scene_content,
  571. 'title' => $book_conf['book_name'],
  572. 'description' => $book_conf['intro'],
  573. 'image' => $book_conf['cover']
  574. ];
  575. \Log::info('outer:' . json_encode($data));
  576. $feedback['text'] = $data;
  577. }
  578. }
  579. }
  580. } else {
  581. // 判断是否图文
  582. if (isset($distribution_self_define_content->is_pic) && $distribution_self_define_content->is_pic) {
  583. $feedback['text'] = ImageNewsToArray($distribution_self_define_content->content);
  584. } else {
  585. $feedback['text'] = isset($distribution_self_define_content->content) ? $distribution_self_define_content->content : '';
  586. }
  587. }
  588. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $feedback['text'], 'subscribe');
  589. // 判断是否有关注回复第二条,有则推送
  590. MsgService::pushChannelCurrentSecondReply($distribution_channel_id, $appid, $openid, 'subscribe_second_reply');
  591. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  592. } else {
  593. $feedback['text'] = "<a href='" . env('PROTOCOL') . "://site" . $encode_distribution_channel_id . "." . $WECHAT_CUSTOM_HOST . ".com/continue'>点击继续阅读 </a> ";
  594. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $feedback['text'], 'subscribe');
  595. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  596. }
  597. } elseif ($event == "TEMPLATESENDJOBFINISH") {
  598. $feedback['text'] = "";
  599. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  600. } else {
  601. return response()->error('EVENT_NOT_FOUND');
  602. }
  603. }
  604. public function getUserClient()
  605. {
  606. return new Client(['base_uri' => env('MEDIA_API_BASE_URI')]);
  607. }
  608. }