OfficialInteractiveEventController.php 51 KB

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