OfficialInteractiveEventController.php 51 KB

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