OfficialInteractiveEventController.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. $role_book = BookConfigService::getBookByRole($content);
  194. if ($role_book) {
  195. $booksInfo[] = [
  196. 'book_name' => $role_book->book_name,
  197. 'intro' => '',
  198. 'cover' => $role_book->cover,
  199. 'bid' => $role_book->bid,
  200. 'book_url' => '/detail?id=' . Hashids::encode($role_book->bid),
  201. ];
  202. }
  203. }
  204. } catch (\Exception $e) {
  205. }
  206. }
  207. if (count($booksInfo) == 0) {
  208. if (preg_match('/^[0-9]+$/', $content)) {
  209. $channel_sex = ChannelService::getChannelCompanySex($distribution_channel_id);
  210. $feedback['text'] = "尊敬的会员:\n\n <a href='" . env('PROTOCOL') . "://site" . $encode_distribution_channel_id . "." . $WECHAT_CUSTOM_HOST . ".com/recent'>☞ 点我继续上次阅读 </a>";
  211. if ($channel_sex != 1) {
  212. //热门书籍推荐
  213. $sex = ForceSubscribeService::getUserRealSexByUid($uid);
  214. $channel_name = $sex == 1 ? '男频' : '女频';
  215. $recomm_books = BookConfigService::getSignRecommendBooksFromCache([], $channel_name, 2);
  216. $recent_read_reco_content = '';
  217. if ($recomm_books) {
  218. // 判断是否不展示书名
  219. $self_config = ChannelService::check_channel_account_priv($distribution_channel_id, 'hide_book_name');
  220. $hide_type = isset($self_config->type) ? $self_config->type : 'not_hide';
  221. $recommend_titles = [];
  222. if ($hide_type == 'hide_book_name') {
  223. \Log::info('hide_book_name reco:' . $distribution_channel_id . ' uid:' . $uid);
  224. $recommend_titles = PromotionService::getRandomHeadline($sex, count($recomm_books));
  225. }
  226. $recent_read_reco_content .= "\n\n" . '热门书籍推荐~';
  227. foreach ($recomm_books as $key => $book) {
  228. $book_name = $book->book_name;
  229. if ($hide_type == 'hide_book_name') {
  230. $book_name = isset($recommend_titles[$key]->title) ? $recommend_titles[$key]->title : $book->book_name;
  231. }
  232. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($book->bid) . '&cid=' . $book->first_cid;
  233. $recent_read_reco_content .= "\n\n" . '<a href="' . $url . '"> ☞ 《' . $book_name . '》</a>';
  234. }
  235. }
  236. $feedback['text'] .= $recent_read_reco_content;
  237. }
  238. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $feedback['text'], 'recent_read');
  239. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  240. }
  241. $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>';
  242. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $feedback['text'], 'text_search');
  243. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  244. } else {
  245. $data = [];
  246. for ($i = 0; $i < count($booksInfo); $i++) {
  247. $bid = $booksInfo[$i]['bid'];
  248. $base_url = env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . $WECHAT_CUSTOM_HOST . '.com' . $booksInfo[$i]['book_url'];
  249. $instro_len = strlen($booksInfo[$i]['intro']);
  250. \Log::info('wechat_log_info:len:' . $instro_len . ' getURL-----' . $base_url);
  251. if ($instro_len > 100) {
  252. $booksInfo[$i]['intro'] = mb_substr($booksInfo[$i]['intro'], 0, 100, 'utf-8');
  253. $booksInfo[$i]['intro'] .= '......';
  254. \Log::info('search_instro_too_long:' . $booksInfo[$i]['intro']);
  255. }
  256. $data[$i] = ['url' => $base_url,
  257. 'title' => $booksInfo[$i]['book_name'],
  258. 'description' => $booksInfo[$i]['intro'],
  259. 'image' => $booksInfo[$i]['cover']];
  260. // 返回一条
  261. break;
  262. }
  263. $datatext['text'] = $data;
  264. $datatext['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $datatext['text'], 'text_search');
  265. return response()->success($datatext);
  266. }
  267. } elseif ($event == "scan") {
  268. // 有自定义不回复内容的渠道
  269. $distribution_self_define_config = DistributionSelfDefineConfig::getDistributionSelfDefineConfig($distribution_channel_id, 'force_subscribe_noreply');
  270. if (!empty($distribution_self_define_config)) {
  271. \Log::info('distribution_self_define_config: distribution_channel_id:' . $distribution_channel_id);
  272. $feedback['text'] = "";
  273. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  274. }
  275. $feedback['text'] = "<a href='" . env('PROTOCOL') . "://site" . $encode_distribution_channel_id . "." . $WECHAT_CUSTOM_HOST . ".com/continue'>点此继续阅读 </a> ";
  276. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $feedback['text'], 'scan');
  277. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  278. } elseif ($event == "click") {
  279. \Log::info('content:' . $content);
  280. if ($content == "contact_customer" && !empty($appid)) {
  281. if (config('common.click_type')) {
  282. # code...
  283. \Log::info('wechat_log_info:getURL-----进入方法');
  284. $mediaCustomer = MediaCustomer::mediaCustomerByAppid($appid);
  285. if (count($mediaCustomer) > 0) {
  286. # code...
  287. // $send_event_contents = array();
  288. for ($i = 0; $i < count($mediaCustomer); $i++) {
  289. # code...
  290. $send_event_content = array();
  291. $send_event_content['image'] = array('media_id' => $mediaCustomer[$i]['media_id']);
  292. // $send_event_content['text'] = 'test_zhoulj';
  293. return response()->success($send_event_content);
  294. // $send_event_contents[$i] = $send_event_content;
  295. }
  296. } else {
  297. $timestamp = (time());
  298. $gzh_app_id = $appid;
  299. $group_nick = env('GROUP_NICK');
  300. $officialAccountPrams['appid'] = $appid;
  301. $officialAccount = (object)OfficialAccountService::officialAccountByAppid($officialAccountPrams);
  302. \Log::info('++++++++++++=====GET CUSTOM IMG [1] =====+++++++++++++');
  303. if (!empty($officialAccount)) {
  304. $distributionChannelSetting = CustomMsgService::customerImgUrlByChannelId($officialAccount->distribution_channel_id);
  305. \Log::info('++++++++++++=====GET CUSTOM IMG [2] =====+++++++++++++');
  306. if (!empty($distributionChannelSetting)) {
  307. # code...
  308. $params = array("gzh_app_id" => $appid, "group_nick" => $group_nick, "is_default" => 0, "img_url" => $distributionChannelSetting['customer_img_url'], "timestamp" => $timestamp);
  309. $sign = get_sign($params);
  310. $mediaCustomerJson = '';
  311. try {
  312. $mediaCustomerJson = $this->getUserClient()->request("GET", "upload_gzh_img/",
  313. ['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]
  314. )->getBody()->getContents();
  315. } catch (\Exception $e) {
  316. \Log::info('++++++++++++=====GET CUSTOM IMG [0] =====+++++++++++++');
  317. }
  318. $mediaCustomerArray = json_decode($mediaCustomerJson, true);
  319. \Log::info('++++++++++++=====GET CUSTOM IMG [3] =====+++++++++++++');
  320. \Log::info($mediaCustomerArray);
  321. // \Log($mediaCustomerArray);
  322. // dd($mediaCustomerArray);
  323. if ($mediaCustomerArray['code'] == 1) {
  324. # code...
  325. $mediaCustomerData = $mediaCustomerArray['data'];
  326. if (empty($mediaCustomerDat)) {
  327. # code...
  328. $mediaCustomerCreate['media_id'] = $mediaCustomerData['media_id'];
  329. $mediaCustomerCreate['url'] = $mediaCustomerData['url'];
  330. $mediaCustomerCreate['appid'] = $appid;
  331. $mediaCustomerCreate['distribution_channel_id'] = $officialAccount->distribution_channel_id;
  332. MediaCustomer::create($mediaCustomerCreate);
  333. $send_event_content = array();
  334. $send_event_content['image'] = array('media_id' => $mediaCustomerData['media_id']);
  335. // $send_event_content['text'] = 'test_zhoulj';
  336. return response()->success($send_event_content);
  337. }
  338. } else {
  339. $feedback['text'] = env('CUSTOM_RETURN_MSG');
  340. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  341. }
  342. } else {
  343. $params = array("gzh_app_id" => $appid, "group_nick" => $group_nick, "timestamp" => $timestamp);
  344. $sign = get_sign($params);
  345. \Log::info('wechat_log_info:getURL-----' . env('MEDIA_API_BASE_URI'));
  346. \Log::info('wechat_log_info:getURL-----' . $gzh_app_id . '___' . $timestamp . '___' . $sign);
  347. $mediaCustomerJson = '';
  348. try {
  349. $mediaCustomerJson = $this->getUserClient()->request("GET", "upload_gzh_img/",
  350. ['query' => ['gzh_app_id' => $appid, 'group_nick' => env('GROUP_NICK'), 'timestamp' => $timestamp, 'sign' => $sign], 'connect_timeout' => 3]
  351. )->getBody()->getContents();
  352. } catch (\Exception $e) {
  353. \Log::info('wechat_log_info:Exception-----' . $e->getMessage());
  354. }
  355. \Log::info('wechat_log_info:getURL-----' . env('MEDIA_API_BASE_URI'));
  356. $mediaCustomerArray = json_decode($mediaCustomerJson, true);
  357. // \Log($mediaCustomerArray);
  358. // dd($mediaCustomerArray);
  359. if ($mediaCustomerArray['code'] == 1) {
  360. # code...
  361. $mediaCustomerData = $mediaCustomerArray['data'];
  362. if (empty($mediaCustomerDat)) {
  363. # code...
  364. $mediaCustomerCreate['media_id'] = $mediaCustomerData['media_id'];
  365. $mediaCustomerCreate['url'] = $mediaCustomerData['url'];
  366. $mediaCustomerCreate['appid'] = $appid;
  367. $mediaCustomerCreate['distribution_channel_id'] = $officialAccount->distribution_channel_id;
  368. MediaCustomer::create($mediaCustomerCreate);
  369. $send_event_content = array();
  370. $send_event_content['image'] = array('media_id' => $mediaCustomerData['media_id']);
  371. // $send_event_content['text'] = 'test_zhoulj';
  372. return response()->success($send_event_content);
  373. }
  374. } else {
  375. $feedback['text'] = env('CUSTOM_RETURN_MSG');
  376. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  377. }
  378. }
  379. }
  380. }
  381. } else {
  382. $feedback['text'] = env('CUSTOM_RETURN_MSG');
  383. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  384. }
  385. } elseif ($content == "daily_sign" && !empty($openid)) {
  386. //调洋哥的接口发送签到客服消息
  387. \Log::info('========================进入洋哥签到回调');
  388. try {
  389. $feedback['text'] = UserSignService::userSignReturnContent3($openid, $distribution_channel_id, $WECHAT_CUSTOM_HOST);
  390. } catch (\Exception $e) {
  391. \Log::error('sign_activity_error:');
  392. \Log::error($e);
  393. }
  394. $top_url = OfficialAccountService::getTopUrl($officialAccount->service_type_info);
  395. if ($officialAccount->service_type_info == 1) {
  396. $top_content = "\n\n" . '为方便阅读,请<a href="' . $top_url . '">设为星标</a>';
  397. } else {
  398. $top_content = "\n\n" . '为方便阅读,请<a href="' . $top_url . '">置顶公众号</a>';
  399. }
  400. $feedback['text'] .= $top_content;
  401. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $feedback['text'], 'daily_sign');
  402. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  403. } elseif ($content == "recent_read" && !empty($openid)) {
  404. // $channel_sex = ChannelService::getChannelCompanySex($distribution_channel_id);
  405. $channelInfo = ChannelService::getById($distribution_channel_id);
  406. $property = isset($channelInfo->property) ? $channelInfo->property : '';
  407. $channel_sex = 3;
  408. if ($property === ChannelConst::PROPERTY_MALE) {
  409. $channel_sex = 1;
  410. }
  411. if ($property === ChannelConst::PROPERTY_FEMALE) {
  412. $channel_sex = 2;
  413. }
  414. \Log::info('channel_sex:' . $channel_sex . ' distribution_channel_id:' . $distribution_channel_id . ' property:' . $property);
  415. $feedback['text'] = "尊敬的会员:\n\n <a href='" . env('PROTOCOL') . "://site" . $encode_distribution_channel_id . "." . $WECHAT_CUSTOM_HOST . ".com/recent'>☞ 点我继续上次阅读 </a>";
  416. if ($channel_sex != 1) {
  417. // 根据用户性别确定男女频
  418. $sex = ForceSubscribeService::getUserRealSexByUid($uid);
  419. $channel_name = $sex == 1 ? '男频' : '女频';
  420. // 站点属性优先
  421. if ($channel_sex == 2) {
  422. $channel_name = '女频';
  423. }
  424. $recomm_books = BookConfigService::getSignRecommendBooksFromCache([], $channel_name, 2);
  425. $recent_read_reco_content = '';
  426. if ($recomm_books) {
  427. // 判断是否不展示书名
  428. $self_config = ChannelService::check_channel_account_priv($distribution_channel_id, 'hide_book_name');
  429. $hide_type = isset($self_config->type) ? $self_config->type : 'not_hide';
  430. $recommend_titles = [];
  431. if ($hide_type == 'hide_book_name') {
  432. \Log::info('hide_book_name reco:' . $distribution_channel_id . ' uid:' . $uid);
  433. $recommend_titles = PromotionService::getRandomHeadline($sex, count($recomm_books));
  434. }
  435. $recent_read_reco_content .= "\n\n" . '热门书籍推荐~';
  436. foreach ($recomm_books as $key => $book) {
  437. $book_name = $book->book_name;
  438. if ($hide_type == 'hide_book_name') {
  439. $book_name = isset($recommend_titles[$key]->title) ? $recommend_titles[$key]->title : $book->book_name;
  440. }
  441. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($book->bid) . '&cid=' . $book->first_cid;
  442. $recent_read_reco_content .= "\n\n" . '<a href="' . $url . '"> ☞ 《' . $book_name . '》</a>';
  443. }
  444. }
  445. $feedback['text'] .= $recent_read_reco_content;
  446. } else {
  447. // 男频站点
  448. // 1本在测的,1本优质书,共2本,如果没有在测的书,则返回2本优质书
  449. // 用户按照15天注册后的用户
  450. $sex = ForceSubscribeService::getUserRealSexByUid($uid);
  451. [$bids, $test_bid] = BookConfig::getLeftRecomBids($uid, '男频', 2);
  452. \Log::info('bids and test_bid:' . $uid . ' data:' . json_encode([$bids, $test_bid]));
  453. if ($bids) {
  454. $recomm_books = BookService::getSimpleBooksByIds($bids);
  455. $recent_read_reco_content = '';
  456. if ($recomm_books) {
  457. // 判断是否不展示书名
  458. $self_config = ChannelService::check_channel_account_priv($distribution_channel_id, 'hide_book_name');
  459. $hide_type = isset($self_config->type) ? $self_config->type : 'not_hide';
  460. $recommend_titles = [];
  461. if ($hide_type == 'hide_book_name') {
  462. \Log::info('hide_book_name reco:' . $distribution_channel_id . ' uid:' . $uid);
  463. $recommend_titles = PromotionService::getRandomHeadline($sex, count($recomm_books));
  464. }
  465. $recent_read_reco_content .= "\n\n" . '热门书籍推荐~';
  466. foreach ($recomm_books as $key => $book) {
  467. $book_name = $book->name;
  468. if ($hide_type == 'hide_book_name') {
  469. $book_name = isset($recommend_titles[$key]->title) ? $recommend_titles[$key]->title : $book_name;
  470. }
  471. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/reader?bid=' . Hashids::encode($book->id) . '&cid=' . $book->first_cid;
  472. if ($book->id == $test_bid) {
  473. $url .= '&fromsource=smart_push';
  474. }
  475. $recent_read_reco_content .= "\n\n" . '<a href="' . $url . '"> ☞ 《' . $book_name . '》</a>';
  476. }
  477. }
  478. $feedback['text'] .= $recent_read_reco_content;
  479. }
  480. }
  481. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $feedback['text'], 'recent_read');
  482. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  483. } // 个别渠道是事件
  484. elseif ($content == "user_center" && !empty($openid)) {
  485. \Log::info('click_user_center:' . $openid);
  486. $feedback['text'] = "☞ <a href='" . $base_url . "/person'>个人中心</a>\n\n";
  487. $feedback['text'] .= "☞ <a href='" . $base_url . "/'>书城首页</a>\n\n";
  488. $feedback['text'] .= "☞ <a href='" . $base_url . "/pay'>优惠充值</a>\n\n";
  489. $feedback['text'] .= "☞ <a href='" . $help_url . "'>帮助中心</a>\n";
  490. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $feedback['text'], 'user_center');
  491. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  492. } elseif ($content == "qrcode_main" && !empty($openid)) {
  493. $msg = OfficialAccountMsgService::getEventMsg(['appid' => $appid, 'apply_event' => 'click', 'apply_event_key' => 'qrcode_main']);
  494. if ($msg) {
  495. $send_event_content = array();
  496. $send_event_content['image'] = array('media_id' => $msg->return_content);
  497. return response()->success($send_event_content);
  498. } else {
  499. $feedback['text'] = '配置有误';
  500. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  501. }
  502. }
  503. } elseif ($event == "unsubscribe") {
  504. $feedback['text'] = "";
  505. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  506. } elseif ($event == "subscribe") {
  507. // 统计
  508. $sub_statistic_key = 'gzh_actual_sub:' . $appid . ':' . date('Y-m-d');
  509. \Log::info('sub_statistic_key:' . $sub_statistic_key);
  510. Redis::sadd($sub_statistic_key, $openid);
  511. Redis::expire($sub_statistic_key, 30 * 3600);
  512. if (!empty($openid)) {
  513. # code...
  514. \Log::info('subscribe:' . $openid . ' distribution_channel_id:' . $distribution_channel_id);
  515. $params['openid'] = $openid;
  516. $forceSubscribeUser = ForceSubscribeService::forceSubscribeUsersByOpenid($params);
  517. \Log::info($forceSubscribeUser);
  518. // 有自定义回复内容的渠道
  519. // $distribution_self_define_content = DistributionSelfDefineContent::getDistributionSelfDefineContent($distribution_channel_id);
  520. // 从redis取
  521. $distribution_self_define_content = MsgService::getCurrentReply($distribution_channel_id);
  522. // 判断渠道,如果是场景值关注过来的,则普通回复,否则自定义回复
  523. $subscribe_from_scene = DistributionSelfDefineConfig::getDistributionSelfDefineConfig($distribution_channel_id, 'subscribe_from_scene');
  524. if (!empty($subscribe_from_scene)) {
  525. if (!empty($content) && strpos($content, 'qrscene_') === 0) {
  526. $distribution_self_define_content = null;
  527. \Log::info('subscribe_from_scene:' . $distribution_channel_id);
  528. }
  529. }
  530. if (is_null($distribution_self_define_content)) {
  531. $recommend_content = '';
  532. if (!empty($forceSubscribeUser)) {
  533. # code...
  534. \Log::info('exist_force_user:' . $openid);
  535. //通过uid获取该用户最近阅读记录的书籍
  536. $uid = isset($forceSubscribeUser->uid) ? $forceSubscribeUser->uid : '';
  537. $userBook = ReadRecordService::getFirstReadRecord($uid);
  538. $bookName = isset($userBook['book_name']) ? $userBook['book_name'] : '最近阅读';
  539. \Log::info('bookName:' . $bookName);
  540. if (!empty($recommend_content)) {
  541. $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>';
  542. $feedback['text'] .= $recommend_content;
  543. $feedback['text'] .= "\n" . "\n" . '为方便下次阅读,请' . '<a href="https://help.leyuee.com/top.html">' . '置顶公众号' . '</a>';
  544. } else {
  545. $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" . '明日继续签到得书币哦!';
  546. }
  547. } else {
  548. $feedback['text'] = "<a href='" . env('PROTOCOL') . "://site" . $encode_distribution_channel_id . "." . $WECHAT_CUSTOM_HOST . ".com/continue'>点击继续阅读 </a> ";
  549. $feedback['text'] .= $recommend_content;
  550. }
  551. // 外站关注的直接回复图文qrscene_outer:123_12
  552. if (strpos($content, 'outer') > -1) {
  553. $scene_content = str_replace('qrscene_outer:', '', $content);
  554. $bid_cid = explode('_', $scene_content);
  555. $bid = isset($bid_cid[0]) ? $bid_cid[0] : '';
  556. $cid = isset($bid_cid[1]) ? $bid_cid[1] : '';
  557. if (!empty($bid) && !empty($cid)) {
  558. $book_conf = BookConfig::getBookById($bid);
  559. \Log::info('out_book_conf');
  560. \Log::info($book_conf);
  561. if (!empty($book_conf)) {
  562. $data = array();
  563. $encode_bid = Hashids::encode($bid);
  564. $data[] = ['url' => env('PROTOCOL') . '://site' . $encode_distribution_channel_id . '.' . $WECHAT_CUSTOM_HOST . '.com' . '/reader?bid=' . $encode_bid . '&cid=' . $cid . '&fromtype=outer_' . $scene_content,
  565. 'title' => $book_conf['book_name'],
  566. 'description' => $book_conf['intro'],
  567. 'image' => $book_conf['cover']
  568. ];
  569. \Log::info('outer:' . json_encode($data));
  570. $feedback['text'] = $data;
  571. }
  572. }
  573. }
  574. } else {
  575. // 判断是否图文
  576. if (isset($distribution_self_define_content->is_pic) && $distribution_self_define_content->is_pic) {
  577. $feedback['text'] = ImageNewsToArray($distribution_self_define_content->content);
  578. } else {
  579. $feedback['text'] = isset($distribution_self_define_content->content) ? $distribution_self_define_content->content : '';
  580. }
  581. }
  582. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $feedback['text'], 'subscribe');
  583. // 判断是否有关注回复第二条,有则推送
  584. MsgService::pushChannelCurrentSecondReply($distribution_channel_id, $appid, $openid, 'subscribe_second_reply');
  585. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  586. } else {
  587. $feedback['text'] = "<a href='" . env('PROTOCOL') . "://site" . $encode_distribution_channel_id . "." . $WECHAT_CUSTOM_HOST . ".com/continue'>点击继续阅读 </a> ";
  588. $feedback['text'] = ChannelService::convertChannelReplyUrl($is_outer_site, $appid, $openid, $feedback['text'], 'subscribe');
  589. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  590. }
  591. } elseif ($event == "TEMPLATESENDJOBFINISH") {
  592. $feedback['text'] = "";
  593. return response()->item(new OfficialInteractiveEventTransformer(), (object)$feedback);
  594. } else {
  595. return response()->error('EVENT_NOT_FOUND');
  596. }
  597. }
  598. public function getUserClient()
  599. {
  600. return new Client(['base_uri' => env('MEDIA_API_BASE_URI')]);
  601. }
  602. }