WechatKeywordsController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace App\Http\Controllers\Channel\OfficialAccount;
  3. use App\Http\Controllers\Channel\BaseController as ChannelBaseController;
  4. use App\Http\Controllers\Channel\OfficialAccount\Transformers\WechatKeyWordsMsgTransformer;
  5. use Illuminate\Http\Request;
  6. use GuzzleHttp\Client;
  7. use App\Modules\OfficialAccount\Services\WechatKeywordMsgService;
  8. use App\Modules\Statistic\Services\SendStatistics;
  9. class WechatKeywordsController extends ChannelBaseController
  10. {
  11. /**
  12. * @apiDefine OfficialAccount 公众号
  13. */
  14. /**
  15. * @apiVersion 1.0.0
  16. * @api {GET} OfficialAccount/wechatKeywordMsgById 通过id获取关键字信息
  17. * @apiGroup OfficialAccount
  18. * @apiName wechatKeywordMsgById
  19. * @apiParam {String} id 关键字信息id.
  20. */
  21. function wechatKeywordMsgById(Request $request)
  22. {
  23. $id = $request->has('id') ? $request->input('id') : '';
  24. if(empty($id)) {
  25. return response()->error("PARAM_EMPTY");
  26. }
  27. $wechatKeywordMsg['id'] = $id;
  28. $wechatKeywordMsgService = WechatKeywordMsgService::wechatKeywordMsgById($wechatKeywordMsg);
  29. $wechatKeywordMsgService = self::get_keyword_statistics($wechatKeywordMsgService);
  30. return response()->item(new WechatKeyWordsMsgTransformer(), $wechatKeywordMsgService);
  31. }
  32. /**
  33. * @apiVersion 1.0.0
  34. * @api {GET} OfficialAccount/wechatKeywordMsgBydistributionChannelId 获取推送关键字信息列表
  35. * @apiGroup OfficialAccount
  36. * @apiName wechatKeywordMsgBydistributionChannelId
  37. * @apiParam {String} distribution_channel_id 渠道id.
  38. */
  39. function wechatKeywordMsgBydistributionChannelId(Request $request)
  40. {
  41. $distribution_channel_id = $this->getChannelId();
  42. $wechatKeywordsMsg['distribution_channel_id'] = $distribution_channel_id;
  43. $wechatKeywordsMsgService = WechatKeywordMsgService::wechatKeywordMsgBydistributionChannelId($wechatKeywordsMsg);
  44. foreach($wechatKeywordsMsgService as $key=>$wechatKeywordsMsg){
  45. $wechatKeywordsMsgService[$key] = self::get_keyword_statistics($wechatKeywordsMsgService[$key]);
  46. }
  47. return response()->pagination(new WechatKeyWordsMsgTransformer(), $wechatKeywordsMsgService);
  48. }
  49. /**
  50. * @apiVersion 1.0.0
  51. * @api {POST} OfficialAccount/addWechatKeywordMsg 保存关键字信息
  52. * @apiGroup OfficialAccount
  53. * @apiName addWechatKeywordMsg
  54. * @apiParam {String} appids 公众号列表,英文逗号隔开.
  55. * @apiParam {String} keywords 关键字列表,英文逗号隔开.
  56. * @apiParam {String} bid 书籍id.
  57. * @apiParam {String} cid 章节id.
  58. */
  59. function addWechatKeywordMsg(Request $request)
  60. {
  61. \Log::info('addWechatKeywordMsg_start');
  62. \Log::info($request->all());
  63. $appids = $request->has('appids') ? $request->input('appids') : '';
  64. if(empty($appids)) {
  65. return response()->error("PARAM_EMPTY");
  66. }
  67. $keywords = $request->has('keywords') ? $request->input('keywords') : '';
  68. if(empty($keywords)) {
  69. return response()->error("PARAM_EMPTY");
  70. }
  71. $bid = $request->has('bid') ? $request->input('bid') : '';
  72. if(empty($bid)) {
  73. return response()->error("PARAM_EMPTY");
  74. }
  75. $cid = $request->has('cid') ? $request->input('cid') : '';
  76. if(empty($cid)) {
  77. return response()->error("PARAM_EMPTY");
  78. }
  79. $book_name = $request->has('book_name') ? $request->input('book_name') : '';
  80. if(empty($book_name)) {
  81. return response()->error("PARAM_EMPTY");
  82. }
  83. $chapter_name = $request->has('chapter_name') ? $request->input('chapter_name') : '';
  84. if(empty($chapter_name)) {
  85. return response()->error("PARAM_EMPTY");
  86. }
  87. $send_title = $request->has('send_title') ? $request->input('send_title') : '';
  88. if(empty($send_title)) {
  89. return response()->error("PARAM_EMPTY");
  90. }
  91. $send_cover = $request->has('send_cover') ? $request->input('send_cover') : '';
  92. if(empty($send_cover)) {
  93. return response()->error("PARAM_EMPTY");
  94. }
  95. $send_order_id = $request->has('send_order_id') ? $request->input('send_order_id') : '';
  96. $distribution_channel_id = $this->getChannelId();
  97. $wechatKeywordMsgs['appids'] = $appids;
  98. $wechatKeywordMsgs['bid'] = $bid;
  99. $wechatKeywordMsgs['cid'] = $cid;
  100. $wechatKeywordMsgs['book_name'] = $book_name;
  101. $wechatKeywordMsgs['chapter_name'] = $chapter_name;
  102. $wechatKeywordMsgs['send_title'] = $send_title;
  103. $wechatKeywordMsgs['send_cover'] = $send_cover;
  104. $wechatKeywordMsgs['distribution_channel_id'] = $distribution_channel_id;
  105. $wechatKeywordMsgs['send_order_id'] = $send_order_id;
  106. $wechatKeywordMsgs['status'] = 1;
  107. $keywords = explode(',',$keywords);
  108. \Log::info($wechatKeywordMsgs);
  109. \Log::info($keywords);
  110. foreach($keywords as $keyword){
  111. $wechatKeywordMsgs['keyword'] = $keyword;
  112. $resultStatus = WechatKeywordMsgService::addWechatKeywordMsg($wechatKeywordMsgs);
  113. }
  114. if ($resultStatus == 1) {
  115. return response()->success();
  116. }elseif ($resultStatus == 2) {
  117. return response()->error('KEYWORDS_UNSUCCESSFUL');
  118. }elseif ($resultStatus == 3) {
  119. return response()->error('KEYWORDS_UNSUCCESSFUL');
  120. }
  121. elseif ($resultStatus == 0) {
  122. return response()->error('KEYWORDS_UNSUCCESSFUL');
  123. }else{
  124. return response()->error('KEYWORDS_UNSUCCESSFUL');
  125. }
  126. }
  127. /**
  128. * @apiVersion 1.0.0
  129. * @api {POST} OfficialAccount/updateWechatKeywordMsg 更新关键字信息
  130. * @apiGroup OfficialAccount
  131. * @apiName updateWechatKeywordMsg
  132. * @apiParam {String} id 关键字id.
  133. * @apiParam {String} appids 公众号列表,英文逗号隔开.
  134. * @apiParam {String} keywords 关键字列表,英文逗号隔开.
  135. * @apiParam {String} bid 书籍id.
  136. * @apiParam {String} cid 章节id.
  137. */
  138. function updateWechatKeywordMsg(Request $request)
  139. {
  140. \Log::info('updateWechatKeywordMsg_start');
  141. $id = $request->has('id') ? $request->input('id') : '';
  142. if(empty($id)) {
  143. return response()->error("PARAM_EMPTY");
  144. }
  145. $appids = $request->has('appids') ? $request->input('appids') : '';
  146. if(empty($appids)) {
  147. return response()->error("PARAM_EMPTY");
  148. }
  149. $keywords = $request->has('keywords') ? $request->input('keywords') : '';
  150. if(empty($keywords)) {
  151. return response()->error("PARAM_EMPTY");
  152. }
  153. $bid = $request->has('bid') ? $request->input('bid') : '';
  154. if(empty($bid)) {
  155. return response()->error("PARAM_EMPTY");
  156. }
  157. $cid = $request->has('cid') ? $request->input('cid') : '';
  158. if(empty($cid)) {
  159. return response()->error("PARAM_EMPTY");
  160. }
  161. $book_name = $request->has('book_name') ? $request->input('book_name') : '';
  162. if(empty($book_name)) {
  163. return response()->error("PARAM_EMPTY");
  164. }
  165. $chapter_name = $request->has('chapter_name') ? $request->input('chapter_name') : '';
  166. if(empty($chapter_name)) {
  167. return response()->error("PARAM_EMPTY");
  168. }
  169. $send_title = $request->has('send_title') ? $request->input('send_title') : '';
  170. if(empty($send_title)) {
  171. return response()->error("PARAM_EMPTY");
  172. }
  173. $send_cover = $request->has('send_cover') ? $request->input('send_cover') : '';
  174. if(empty($send_cover)) {
  175. return response()->error("PARAM_EMPTY");
  176. }
  177. $distribution_channel_id = $this->getChannelId();
  178. $wechatKeywordMsgs['id'] = $id;
  179. $wechatKeywordMsgs['appids'] = $appids;
  180. $wechatKeywordMsgs['bid'] = $bid;
  181. $wechatKeywordMsgs['cid'] = $cid;
  182. $wechatKeywordMsgs['send_title'] = $send_title;
  183. $wechatKeywordMsgs['send_cover'] = $send_cover;
  184. $wechatKeywordMsgs['book_name'] = $book_name;
  185. $wechatKeywordMsgs['chapter_name'] = $chapter_name;
  186. $wechatKeywordMsgs['distribution_channel_id'] = $distribution_channel_id;
  187. $wechatKeywordMsgs['status'] = 1;
  188. $keywords = explode(',',$keywords);
  189. \Log::info($wechatKeywordMsgs);
  190. \Log::info($keywords);
  191. foreach($keywords as $keyword){
  192. $wechatKeywordMsgs['keyword'] = $keyword;
  193. $resultStatus = WechatKeywordMsgService::updateWechatKeywordMsg($wechatKeywordMsgs);
  194. }
  195. if ($resultStatus == 1) {
  196. return response()->success();
  197. }elseif ($resultStatus == 2) {
  198. return response()->error('KEYWORDS_UNSUCCESSFUL');
  199. }elseif ($resultStatus == 3) {
  200. return response()->error('KEYWORDS_UNSUCCESSFUL');
  201. }
  202. elseif ($resultStatus == 0) {
  203. return response()->error('KEYWORDS_UNSUCCESSFUL');
  204. }else{
  205. return response()->error('KEYWORDS_UNSUCCESSFUL');
  206. }
  207. \Log::info('updateWechatKeywordMsg_end');
  208. }
  209. /**
  210. * @apiVersion 1.0.0
  211. * @api {GET} OfficialAccount/updateWechatKeywordMsgStatus 更新关键字信息状态
  212. * @apiGroup OfficialAccount
  213. * @apiName updateWechatKeywordMsgStatus
  214. * @apiParam {String} id 关键字信息id.
  215. * @apiParam {String} status 关键字信息状态 0关闭 1开启.
  216. * @apiSuccessExample {json} Success-Response:
  217. *
  218. * {
  219. * "code": 0,
  220. * "msg": "",
  221. * "data": {
  222. * }
  223. * }
  224. */
  225. function updateWechatKeywordMsgStatus(Request $request)
  226. {
  227. $id = $request->has('id') ? $request->input('id') : '';
  228. if(empty($id)) {
  229. return response()->error("PARAM_EMPTY");
  230. }
  231. $status = $request->has('status') ? $request->input('status') : '0';
  232. $distribution_channel_id = $this->getChannelId();
  233. $wechatKeywordMsg['id'] = $id;
  234. $wechatKeywordMsg['status'] = $status;
  235. $wechatKeywordMsg['distribution_channel_id'] = $distribution_channel_id;
  236. $resultStatus = WechatKeywordMsgService::updateWechatKeywordMsgStatus($wechatKeywordMsg);
  237. if ($resultStatus == 1) {
  238. return response()->success();
  239. }elseif ($resultStatus == 2) {
  240. return response()->error('KEYWORDS_UNSUCCESSFUL');
  241. }elseif ($resultStatus == 4) {
  242. return response()->error('KEYWORDS_UNSUCCESSFUL');
  243. }elseif ($resultStatus == 0) {
  244. return response()->error('KEYWORDS_UNSUCCESSFUL');
  245. }else{
  246. return response()->error('KEYWORDS_UNSUCCESSFUL');
  247. }
  248. }
  249. public static function get_keyword_statistics($wechatKeywordMsgService){
  250. $start_date = date('Y-m-d',strtotime('-7 day'));
  251. $end_date = date('Y-m-d',strtotime('+1 day'));
  252. $stat_type = 'keyword_'.$wechatKeywordMsgService->id;
  253. \Log::info('get_keyword_statistics:'.$wechatKeywordMsgService->distribution_channel_id.' start_date:'.$start_date.' end_date:'.$end_date.' stat_type:'.$stat_type);
  254. $user_pay_stat = SendStatistics::getWechatMsgUseRecord($wechatKeywordMsgService->distribution_channel_id,$stat_type,$start_date,$end_date,true);
  255. \Log::info('get_keyword_statistics_pay_stat');\Log::info($user_pay_stat);
  256. $wechatKeywordMsgService->uv = $user_pay_stat['uv'];
  257. $wechatKeywordMsgService->click_num = $user_pay_stat['pv'];
  258. $wechatKeywordMsgService->new_subscribe_num = '0';
  259. $wechatKeywordMsgService->charge_amount = $user_pay_stat['success_order_sum'];
  260. return $wechatKeywordMsgService;
  261. }
  262. }