123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- <?php
- namespace App\Http\Controllers\Channel\OfficialAccount;
- use App\Http\Controllers\Channel\BaseController as ChannelBaseController;
- use App\Http\Controllers\Channel\OfficialAccount\Transformers\WechatKeyWordsMsgTransformer;
- use Illuminate\Http\Request;
- use GuzzleHttp\Client;
- use App\Modules\OfficialAccount\Services\WechatKeywordMsgService;
- use App\Modules\Statistic\Services\SendStatistics;
- class WechatKeywordsController extends ChannelBaseController
- {
- /**
- * @apiDefine OfficialAccount 公众号
- */
- /**
- * @apiVersion 1.0.0
- * @api {GET} OfficialAccount/wechatKeywordMsgById 通过id获取关键字信息
- * @apiGroup OfficialAccount
- * @apiName wechatKeywordMsgById
- * @apiParam {String} id 关键字信息id.
- */
- function wechatKeywordMsgById(Request $request)
- {
- $id = $request->has('id') ? $request->input('id') : '';
- if(empty($id)) {
- return response()->error("PARAM_EMPTY");
- }
- $wechatKeywordMsg['id'] = $id;
- $wechatKeywordMsgService = WechatKeywordMsgService::wechatKeywordMsgById($wechatKeywordMsg);
- $wechatKeywordMsgService = self::get_keyword_statistics($wechatKeywordMsgService);
- return response()->item(new WechatKeyWordsMsgTransformer(), $wechatKeywordMsgService);
- }
- /**
- * @apiVersion 1.0.0
- * @api {GET} OfficialAccount/wechatKeywordMsgBydistributionChannelId 获取推送关键字信息列表
- * @apiGroup OfficialAccount
- * @apiName wechatKeywordMsgBydistributionChannelId
- * @apiParam {String} distribution_channel_id 渠道id.
- */
- function wechatKeywordMsgBydistributionChannelId(Request $request)
- {
- $distribution_channel_id = $this->getChannelId();
- $wechatKeywordsMsg['distribution_channel_id'] = $distribution_channel_id;
- $wechatKeywordsMsgService = WechatKeywordMsgService::wechatKeywordMsgBydistributionChannelId($wechatKeywordsMsg);
- foreach($wechatKeywordsMsgService as $key=>$wechatKeywordsMsg){
- $wechatKeywordsMsgService[$key] = self::get_keyword_statistics($wechatKeywordsMsgService[$key]);
- }
- return response()->pagination(new WechatKeyWordsMsgTransformer(), $wechatKeywordsMsgService);
- }
- /**
- * @apiVersion 1.0.0
- * @api {POST} OfficialAccount/addWechatKeywordMsg 保存关键字信息
- * @apiGroup OfficialAccount
- * @apiName addWechatKeywordMsg
- * @apiParam {String} appids 公众号列表,英文逗号隔开.
- * @apiParam {String} keywords 关键字列表,英文逗号隔开.
- * @apiParam {String} bid 书籍id.
- * @apiParam {String} cid 章节id.
- */
- function addWechatKeywordMsg(Request $request)
- {
- \Log::info('addWechatKeywordMsg_start');
- \Log::info($request->all());
-
- $appids = $request->has('appids') ? $request->input('appids') : '';
- if(empty($appids)) {
- return response()->error("PARAM_EMPTY");
- }
- $keywords = $request->has('keywords') ? $request->input('keywords') : '';
- if(empty($keywords)) {
- return response()->error("PARAM_EMPTY");
- }
- $bid = $request->has('bid') ? $request->input('bid') : '';
- if(empty($bid)) {
- return response()->error("PARAM_EMPTY");
- }
- $cid = $request->has('cid') ? $request->input('cid') : '';
- if(empty($cid)) {
- return response()->error("PARAM_EMPTY");
- }
-
- $book_name = $request->has('book_name') ? $request->input('book_name') : '';
- if(empty($book_name)) {
- return response()->error("PARAM_EMPTY");
- }
-
- $chapter_name = $request->has('chapter_name') ? $request->input('chapter_name') : '';
- if(empty($chapter_name)) {
- return response()->error("PARAM_EMPTY");
- }
-
- $send_title = $request->has('send_title') ? $request->input('send_title') : '';
- if(empty($send_title)) {
- return response()->error("PARAM_EMPTY");
- }
- $send_cover = $request->has('send_cover') ? $request->input('send_cover') : '';
- if(empty($send_cover)) {
- return response()->error("PARAM_EMPTY");
- }
- $send_order_id = $request->has('send_order_id') ? $request->input('send_order_id') : '';
-
- $distribution_channel_id = $this->getChannelId();
- $wechatKeywordMsgs['appids'] = $appids;
- $wechatKeywordMsgs['bid'] = $bid;
- $wechatKeywordMsgs['cid'] = $cid;
- $wechatKeywordMsgs['book_name'] = $book_name;
- $wechatKeywordMsgs['chapter_name'] = $chapter_name;
- $wechatKeywordMsgs['send_title'] = $send_title;
- $wechatKeywordMsgs['send_cover'] = $send_cover;
- $wechatKeywordMsgs['distribution_channel_id'] = $distribution_channel_id;
- $wechatKeywordMsgs['send_order_id'] = $send_order_id;
- $wechatKeywordMsgs['status'] = 1;
-
- $keywords = explode(',',$keywords);
- \Log::info($wechatKeywordMsgs);
- \Log::info($keywords);
- foreach($keywords as $keyword){
- $wechatKeywordMsgs['keyword'] = $keyword;
- $resultStatus = WechatKeywordMsgService::addWechatKeywordMsg($wechatKeywordMsgs);
- }
- if ($resultStatus == 1) {
- return response()->success();
- }elseif ($resultStatus == 2) {
- return response()->error('KEYWORDS_UNSUCCESSFUL');
- }elseif ($resultStatus == 3) {
- return response()->error('KEYWORDS_UNSUCCESSFUL');
- }
- elseif ($resultStatus == 0) {
- return response()->error('KEYWORDS_UNSUCCESSFUL');
- }else{
- return response()->error('KEYWORDS_UNSUCCESSFUL');
- }
- }
- /**
- * @apiVersion 1.0.0
- * @api {POST} OfficialAccount/updateWechatKeywordMsg 更新关键字信息
- * @apiGroup OfficialAccount
- * @apiName updateWechatKeywordMsg
- * @apiParam {String} id 关键字id.
- * @apiParam {String} appids 公众号列表,英文逗号隔开.
- * @apiParam {String} keywords 关键字列表,英文逗号隔开.
- * @apiParam {String} bid 书籍id.
- * @apiParam {String} cid 章节id.
- */
-
- function updateWechatKeywordMsg(Request $request)
- {
- \Log::info('updateWechatKeywordMsg_start');
- $id = $request->has('id') ? $request->input('id') : '';
- if(empty($id)) {
- return response()->error("PARAM_EMPTY");
- }
-
- $appids = $request->has('appids') ? $request->input('appids') : '';
- if(empty($appids)) {
- return response()->error("PARAM_EMPTY");
- }
- $keywords = $request->has('keywords') ? $request->input('keywords') : '';
- if(empty($keywords)) {
- return response()->error("PARAM_EMPTY");
- }
- $bid = $request->has('bid') ? $request->input('bid') : '';
- if(empty($bid)) {
- return response()->error("PARAM_EMPTY");
- }
- $cid = $request->has('cid') ? $request->input('cid') : '';
- if(empty($cid)) {
- return response()->error("PARAM_EMPTY");
- }
-
- $book_name = $request->has('book_name') ? $request->input('book_name') : '';
- if(empty($book_name)) {
- return response()->error("PARAM_EMPTY");
- }
-
- $chapter_name = $request->has('chapter_name') ? $request->input('chapter_name') : '';
- if(empty($chapter_name)) {
- return response()->error("PARAM_EMPTY");
- }
- $send_title = $request->has('send_title') ? $request->input('send_title') : '';
- if(empty($send_title)) {
- return response()->error("PARAM_EMPTY");
- }
- $send_cover = $request->has('send_cover') ? $request->input('send_cover') : '';
- if(empty($send_cover)) {
- return response()->error("PARAM_EMPTY");
- }
-
- $distribution_channel_id = $this->getChannelId();
- $wechatKeywordMsgs['id'] = $id;
- $wechatKeywordMsgs['appids'] = $appids;
- $wechatKeywordMsgs['bid'] = $bid;
- $wechatKeywordMsgs['cid'] = $cid;
- $wechatKeywordMsgs['send_title'] = $send_title;
- $wechatKeywordMsgs['send_cover'] = $send_cover;
- $wechatKeywordMsgs['book_name'] = $book_name;
- $wechatKeywordMsgs['chapter_name'] = $chapter_name;
- $wechatKeywordMsgs['distribution_channel_id'] = $distribution_channel_id;
- $wechatKeywordMsgs['status'] = 1;
-
- $keywords = explode(',',$keywords);
- \Log::info($wechatKeywordMsgs);
- \Log::info($keywords);
- foreach($keywords as $keyword){
- $wechatKeywordMsgs['keyword'] = $keyword;
- $resultStatus = WechatKeywordMsgService::updateWechatKeywordMsg($wechatKeywordMsgs);
- }
-
- if ($resultStatus == 1) {
- return response()->success();
- }elseif ($resultStatus == 2) {
- return response()->error('KEYWORDS_UNSUCCESSFUL');
- }elseif ($resultStatus == 3) {
- return response()->error('KEYWORDS_UNSUCCESSFUL');
- }
- elseif ($resultStatus == 0) {
- return response()->error('KEYWORDS_UNSUCCESSFUL');
- }else{
- return response()->error('KEYWORDS_UNSUCCESSFUL');
- }
-
- \Log::info('updateWechatKeywordMsg_end');
-
- }
-
- /**
- * @apiVersion 1.0.0
- * @api {GET} OfficialAccount/updateWechatKeywordMsgStatus 更新关键字信息状态
- * @apiGroup OfficialAccount
- * @apiName updateWechatKeywordMsgStatus
- * @apiParam {String} id 关键字信息id.
- * @apiParam {String} status 关键字信息状态 0关闭 1开启.
- * @apiSuccessExample {json} Success-Response:
- *
- * {
- * "code": 0,
- * "msg": "",
- * "data": {
- * }
- * }
- */
- function updateWechatKeywordMsgStatus(Request $request)
- {
- $id = $request->has('id') ? $request->input('id') : '';
- if(empty($id)) {
- return response()->error("PARAM_EMPTY");
- }
-
- $status = $request->has('status') ? $request->input('status') : '0';
- $distribution_channel_id = $this->getChannelId();
-
- $wechatKeywordMsg['id'] = $id;
- $wechatKeywordMsg['status'] = $status;
- $wechatKeywordMsg['distribution_channel_id'] = $distribution_channel_id;
- $resultStatus = WechatKeywordMsgService::updateWechatKeywordMsgStatus($wechatKeywordMsg);
- if ($resultStatus == 1) {
- return response()->success();
- }elseif ($resultStatus == 2) {
- return response()->error('KEYWORDS_UNSUCCESSFUL');
- }elseif ($resultStatus == 4) {
- return response()->error('KEYWORDS_UNSUCCESSFUL');
- }elseif ($resultStatus == 0) {
- return response()->error('KEYWORDS_UNSUCCESSFUL');
- }else{
- return response()->error('KEYWORDS_UNSUCCESSFUL');
- }
-
- }
-
- public static function get_keyword_statistics($wechatKeywordMsgService){
- $start_date = date('Y-m-d',strtotime('-7 day'));
- $end_date = date('Y-m-d',strtotime('+1 day'));
- $stat_type = 'keyword_'.$wechatKeywordMsgService->id;
- \Log::info('get_keyword_statistics:'.$wechatKeywordMsgService->distribution_channel_id.' start_date:'.$start_date.' end_date:'.$end_date.' stat_type:'.$stat_type);
- $user_pay_stat = SendStatistics::getWechatMsgUseRecord($wechatKeywordMsgService->distribution_channel_id,$stat_type,$start_date,$end_date,true);
- \Log::info('get_keyword_statistics_pay_stat');\Log::info($user_pay_stat);
- $wechatKeywordMsgService->uv = $user_pay_stat['uv'];
- $wechatKeywordMsgService->click_num = $user_pay_stat['pv'];
- $wechatKeywordMsgService->new_subscribe_num = '0';
- $wechatKeywordMsgService->charge_amount = $user_pay_stat['success_order_sum'];
- return $wechatKeywordMsgService;
- }
- }
|