ActivityController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. namespace App\Http\Controllers\Channel\Activity;
  3. use App\Http\Controllers\Channel\Activity\Transformers\ActivityChargeInfoTransformer;
  4. use App\Http\Controllers\Channel\Activity\Transformers\ActivityTransformer;
  5. use App\Http\Controllers\Channel\BaseController;
  6. use App\Modules\Activity\Services\ActivityService;
  7. use App\Modules\Activity\Services\ActivitySwitchService;
  8. use App\Modules\Trade\Models\Order;
  9. use App\Modules\Trade\Services\OrderService;
  10. use DB;
  11. use Hashids;
  12. use Illuminate\Http\Request;
  13. use Log;
  14. use Storage;
  15. /**
  16. * 活动
  17. * Class ActivityController
  18. * @package App\Http\Controllers\Channel\Activity
  19. */
  20. class ActivityController extends BaseController
  21. {
  22. /**
  23. * @apiDefine Activity 活动模块
  24. */
  25. /**
  26. * @apiVersion 1.0.0
  27. * @api {GET} activity/getActivities 获取活动信息
  28. * @apiGroup Activity
  29. * @apiName getActivities
  30. * @apiParam {Number} [activity_id] 活动ID
  31. * @apiParam {String} [begin_time] 开始时间
  32. * @apiParam {String} [end_time] 结束时间
  33. * @apiSuccess {Number} id 活动ID.
  34. * @apiSuccess {String} name 活动名称
  35. * @apiSuccess {String} activity_page 活动页面
  36. * @apiSuccess {String} created_at 创建时间
  37. * @apiSuccess {String} start_time 开始时间
  38. * @apiSuccess {String} end_time 结束时间
  39. * @apiSuccess {Number} price 活动单价
  40. * @apiSuccess {Number} totalChargeAmount 活动的总充值额
  41. * @apiSuccess {Number} orderCount 订单数
  42. * @apiSuccessExample {json} Success-Response:
  43. *
  44. * {
  45. * "code": 0,
  46. * "msg": "",
  47. * "data": [
  48. * {
  49. * "id": 2,
  50. * "name": "活动1222",
  51. * "activity_page": "http://www.baidu.com",
  52. * "created_at": "2017-12-01 10:20:04",
  53. * "start_time": "2017-12-01 10:20:04",
  54. * "end_time": "2017-12-01 10:20:04",
  55. * "price": 30,
  56. * "totalChargeAmount": 1000,
  57. * "orderCount": 50
  58. * }
  59. * ]
  60. * }
  61. */
  62. function getActivities(Request $request)
  63. {
  64. $params = [];
  65. $request->has('end_time') && $request->input('end_time') && $params['end_time'] = trim($request->input('end_time'));
  66. $request->has('begin_time') && $request->input('begin_time') && $params['begin_time'] = $request->input('begin_time');
  67. $request->has('activity_id') && $request->input('activity_id') && $params['activity_id'] = $request->input('activity_id');
  68. $params['distribution_channel_id'] = [0];
  69. //\Log::info('achor:start_query_activity_info:'.time());
  70. $activities = ActivityService::search($params);
  71. //\Log::info('achor:end_query_activity_info:'.time());
  72. $channel_id = $this->getChannelId();
  73. //\Log::info('channel_id:'.$channel_id);
  74. foreach ($activities as $item) {
  75. $url = $item->activity_page;
  76. $item->activity_page = env('PROTOCOL') . '://site' . encodeDistributionChannelId($channel_id) . '.' . env('CUSTOM_HOST') . '.com' . $url;
  77. //select premium
  78. /*$item->totalChargeAmount = OrderService::getAmount(['activity_id' => $item->id, 'distribution_channel_id' => $channel_id]);
  79. $item->orderCount = OrderService::getActivityOrderNum(['activity_id' => $item->id, 'status' => 'PAID', 'distribution_channel_id' => $channel_id]);*/
  80. //\Log::info('achor:start_query_order_info:'.($item->id).':'.time());
  81. $order_info = Order::select(DB::raw('count(*) as order_count,sum(price) as total_charge_amount'))
  82. ->where([
  83. ['activity_id' ,'=', $item->id],
  84. ['status','=', 'PAID'],
  85. ['distribution_channel_id','=',$channel_id]
  86. ])->first();
  87. //\Log::info('achor:end_query_order_info:'.($item->id).':'.time());
  88. $item->totalChargeAmount = $order_info?$order_info->total_charge_amount:0;
  89. $item->orderCount = $order_info?$order_info->order_count:0;
  90. if ($item->id == 3) {
  91. $item->price = '50元/108元';
  92. }
  93. if ($item->id == 860) {
  94. $item->price = '17元/30元/58元';
  95. }
  96. if($item->id == 1224){
  97. $item->price = '11元/17元/58元';
  98. }
  99. $is_over = 0;
  100. if($item->end_time < date('Y-m-d H:i:s')){
  101. $is_over = 1;
  102. }
  103. $item->is_over = $is_over;
  104. //\Log::info('achor:start_query_ActivitySwitch:'.($item->id).':'.time());
  105. $item->is_reader_page_show = ActivitySwitchService::isShowInPage($item->id, $channel_id, 'reader');
  106. $item->is_sign_message_show = ActivitySwitchService::isShowInPage($item->id, $channel_id, 'sign');
  107. //\Log::info('achor:end_query_ActivitySwitch:'.($item->id).':'.time());
  108. }
  109. return response()->pagination(new ActivityTransformer(), $activities);
  110. }
  111. /**
  112. * @apiVersion 1.0.0
  113. * @api {GET} activity/getActivityRechargeInfo 获取活动的充值明细
  114. * @apiGroup Activity
  115. * @apiName getActivityRechargeInfo
  116. * @apiParam {Number} [activity_id] 活动ID
  117. * @apiSuccess {Number} uid 用户id.
  118. * @apiSuccess {Number} price 充值金额.
  119. * @apiSuccess {String} trade_no 平台交易ID.
  120. * @apiSuccess {String} pay_end_at 支付完成时间.
  121. * @apiSuccess {String} activity_id 活动ID
  122. * @apiSuccess {Number} send_order_id 派单id
  123. * @apiSuccess {String} send_order_name 派单名称
  124. * @apiSuccess {String} created_at 创建时间
  125. * @apiSuccessExample {json} Success-Response:
  126. *
  127. * {
  128. * "code": 0,
  129. * "msg": "",
  130. * "data": [
  131. * {
  132. * "uid": 1,
  133. * "price": "1.00",
  134. * "trade_no": "201711301125401585459852216605",
  135. * "pay_end_at": "2017-12-01 10:20:04",
  136. * "activity_id": 1,
  137. * "send_order_id": 0,
  138. * "send_order_name": "",
  139. * "created_at": 1512094804,
  140. * }
  141. * ]
  142. * }
  143. */
  144. function getActivityRechargeInfo(Request $request)
  145. {
  146. $activity_id = $request->has('activity_id') ? $request->input('activity_id') : '';
  147. if (empty($activity_id)) {
  148. return response()->error("PARAM_EMPTY");
  149. }
  150. if (!is_numeric($activity_id)) {
  151. return response()->error("PARAM_ERROR");
  152. }
  153. $activitiesRechargeInfo = ActivityService::getActivityRechargeInfo(['activity_id' => $activity_id, 'status' => 'PAID']);
  154. return response()->pagination(new ActivityChargeInfoTransformer(), $activitiesRechargeInfo);
  155. }
  156. /**
  157. * @apiVersion 1.0.0
  158. * @api {GET} activity/pushinfo 快速创建客服消息
  159. * @apiGroup Activity
  160. * @apiName getActivityCustomerPushInfo
  161. * @apiParam {Number} activity_id 活动ID
  162. * @apiSuccess {Number} data.name 活动名称.
  163. * @apiSuccess {Number} data.redirect_url 跳转地址.
  164. * @apiSuccess {String} data.pic 图片地址
  165. * @apiSuccess {String} data.title 标题
  166. * @apiSuccessExample {json} Success-Response:
  167. * {
  168. * code: 0,
  169. * msg: "",
  170. * data: {
  171. * id: 3,
  172. * name: "春节活动",
  173. * created_at: "2018-02-07 19:41:47",
  174. * updated_at: "2018-02-07 19:41:50",
  175. * start_time: "2018-02-03 19:41:52",
  176. * end_time: "2018-02-07 19:41:54",
  177. * activity_page: "http://site2.myzsy.com/sale/springactivity?token=1JMfs0yjYR1CV6Bsf8ZWlp34WIYsg61d",
  178. * product_id: 0,
  179. * token: "1JMfs0yjYR1CV6Bsf8ZWlp34WIYsg61d",
  180. * default_template_id: 1,
  181. * customer_msg: "{"pic":"https:\/\/yueduyun.oss-cn-hangzhou.aliyuncs.com\/distribution\/activity_pic\/2018020701.jpg","title":"\u3010\u6625\u8282\u7ea2\u5305\u3011\u6625\u8282\u5927\u793c\u5305\uff1a\u514550\u900150 \u3001\u5145108\u9001108\uff01\u4e66\u5e01\u7ffb\u500d\u7b49\u4f60\u6765\u53d6\uff01"} ",
  182. * redirect_url: "http://site2.myzsy.com/sale/springactivity?token=1JMfs0yjYR1CV6Bsf8ZWlp34WIYsg61d",
  183. * pic: "https://yueduyun.oss-cn-hangzhou.aliyuncs.com/distribution/activity_pic/2018020701.jpg",
  184. * title: "【春节红包】春节大礼包:充50送50 、充108送108!书币翻倍等你来取!"
  185. * }
  186. * }
  187. */
  188. function getActivityCustomerPushInfo(Request $request)
  189. {
  190. $activity_id = $request->has('activity_id') ? $request->input('activity_id') : '';
  191. if (empty($activity_id)) {
  192. return response()->error("PARAM_EMPTY");
  193. }
  194. $channel_id = $this->getChannelId();
  195. $activity = ActivityService::getById($activity_id);
  196. $activity->activity_page = env('PROTOCOL') . '://site' . encodeDistributionChannelId($channel_id) . '.' . env('CUSTOM_HOST') . '.com' . $activity->activity_page;
  197. $activity->redirect_url = $activity->activity_page;
  198. if ($activity->customer_msg) {
  199. $push_info = json_decode($activity->customer_msg, 1);
  200. $activity->pic = $push_info['pic'];
  201. $activity->title = $push_info['title'];
  202. } else {
  203. $activity->pic = '';
  204. $activity->title = '';
  205. }
  206. return response()->success($activity);
  207. }
  208. /**
  209. * @apiVersion 1.0.0
  210. * @api {GET} activity/quickCreatedTemplateMsg 快速创建模板消息
  211. * @apiGroup Activity
  212. * @apiName quickCreatedTemplateMsg
  213. * @apiParam {Number} activity_id 活动ID
  214. * @apiSuccess {Number} data.name 活动名称.
  215. * @apiSuccess {Number} data.example 模板示例
  216. * @apiSuccess {String} data.title 标题
  217. * @apiSuccess {String} data.content 内容
  218. * @apiSuccess {String} data.redirect_url 跳转链接
  219. * @apiSuccessExample {json} Success-Response:
  220. * {
  221. * code: 0,
  222. * msg: "",
  223. * data: {
  224. * name: "春节活动",
  225. * activity_page: "http://site2.myzsy.com/sale/springactivity?token=1JMfs0yjYR1CV6Bsf8ZWlp34WIYsg61d",
  226. * example: "尊敬的会员:[微信名称] 恭喜您抽中双节双重大礼包! 会员卡号:充50送50 、充108送108!书币翻倍读不停! 有效期:活动时间2月14日-2月21日 点击【详情】,立即领取限时大礼包>>>",
  227. * common_template_id: "TM405959659",
  228. * title: "任务到期通知",
  229. * content: "{{first.DATA}} 会员卡号:{{keynote1.DATA}} 有效期:{{keynote2.DATA}} {{remark.DATA}}",
  230. * redirect_url: "http://site2.myzsy.com/sale/springactivity?token=1JMfs0yjYR1CV6Bsf8ZWlp34WIYsg61d"
  231. * }
  232. * }
  233. */
  234. function quickCreatedTemplateMsg(Request $request)
  235. {
  236. $activity_id = $request->has('activity_id') ? $request->input('activity_id') : '';
  237. if (empty($activity_id)) {
  238. return response()->error("PARAM_EMPTY");
  239. }
  240. $res = DB::table('activity')
  241. //->join('default_template_themes', 'activity.id', '=', 'default_template_themes.activity_id')
  242. ->leftjoin('default_template_themes', 'activity.default_template_id', '=', 'default_template_themes.id')
  243. ->leftjoin('wechat_public_templates', 'default_template_themes.template_id', '=', 'wechat_public_templates.id')
  244. ->select('activity.name', 'activity.activity_page', 'default_template_themes.template_content',
  245. 'wechat_public_templates.common_template_id', 'wechat_public_templates.title'
  246. , 'wechat_public_templates.content', 'wechat_public_templates.example', 'default_template_themes.title')
  247. ->where('activity.id', $activity_id)
  248. ->first();
  249. if ($res) {
  250. $channel_id = $this->getChannelId();
  251. $res->activity_page = env('PROTOCOL') . '://site' . encodeDistributionChannelId($channel_id) . '.' . env('CUSTOM_HOST') . '.com' . $res->activity_page;
  252. $res->redirect_url = $res->activity_page;
  253. } else {
  254. $res = [];
  255. }
  256. return response()->success((array)$res);
  257. }
  258. /**
  259. * @apiVersion 1.0.0
  260. * @api {GET} activity/longActivityTemplate 长期活动模板
  261. * @apiGroup Activity
  262. * @apiName longActivityTemplate
  263. * @apiSuccess {Number} data.name 活动名称.
  264. * @apiSuccess {Number} data.price单价
  265. * @apiSuccess {String} data.get 获得书币
  266. * @apiSuccess {String} data.product_id product_id
  267. * @apiSuccessExample {json} Success-Response:
  268. * {
  269. * code: 0,
  270. * msg: "",
  271. * data: {
  272. * [
  273. * {
  274. * name: "充68.00元得13600书币",
  275. * price: "68.00",
  276. * get: 13600,
  277. * product_id: 744
  278. * }
  279. * ]
  280. * }
  281. * }
  282. */
  283. function longActivityTemplate()
  284. {
  285. $product_ids = env('LONG_ACTIVITY_PRODUCT_ID');
  286. if (!$product_ids)
  287. return response()->success();
  288. $product_id_arr = explode(',', $product_ids);
  289. $product_info = DB::table('products')->whereIn('id', $product_id_arr)->get();
  290. if (!$product_info)
  291. return response()->success();
  292. $activity = [];
  293. foreach ($product_info as $v) {
  294. $fee = $v->price * 100 + $v->given;
  295. $activity[] = ['name' => '充' . $v->price . '元得' . $fee . '书币', 'price' => $v->price, 'get' => $fee, 'product_id' => $v->id];
  296. }
  297. return response()->success((array)$activity);
  298. }
  299. /**
  300. * @apiVersion 1.0.0
  301. * @api {POST} activity/createActivity 创建活动
  302. * @apiGroup Activity
  303. * @apiName createActivity
  304. * @apiParam {String} name 活动名称.
  305. * @apiParam {String} start_time 开始时间
  306. * @apiParam {String} end_time 结束时间
  307. * @apiParam {String} product_id product_id
  308. * @apiSuccessExample {json} Success-Response:
  309. * {
  310. * code: 0,
  311. * msg: "",
  312. * data: {
  313. * }
  314. * }
  315. */
  316. function createActivity(Request $request)
  317. {
  318. $name = $request->input('name');
  319. $start_time = $request->input('start_time');
  320. $end_time = $request->input('end_time');
  321. $product_id = $request->input('product_id');
  322. $distribution_channel_id = $this->getChannelId();
  323. $check = checkParam($request->except('_url'), ['name', 'start_time', 'end_time', 'product_id']);
  324. if ($check) {
  325. return response()->error("PARAM_EMPTY", ['lack' => $check]);
  326. }
  327. $end_time = $end_time . ' 23:59:59';
  328. $token = str_random(32);
  329. $product = DB::table('products')->find($product_id);
  330. if ($product && $product->price == '9.9') {
  331. $activity_page = '/sale/channelsalenine?token=' . $token;
  332. } else {
  333. $activity_page = '/sale/channelsalesixty?token=' . $token;
  334. }
  335. $create = ActivityService::createActivity(compact('name', 'start_time', 'end_time', 'product_id', 'distribution_channel_id', 'token', 'activity_page'));
  336. return response()->success($create);
  337. }
  338. /**
  339. * @apiVersion 1.0.0
  340. * @api {GET} activity/channelActivity 活动列表
  341. * @apiGroup Activity
  342. * @apiName channelActivity
  343. * @apiSuccess {Number} id 活动ID.
  344. * @apiSuccess {String} name 活动名称
  345. * @apiSuccess {String} activity_page 活动页面
  346. * @apiSuccess {String} created_at 创建时间
  347. * @apiSuccess {String} start_time 开始时间
  348. * @apiSuccess {String} end_time 结束时间
  349. * @apiSuccess {Number} price 活动单价
  350. * @apiSuccess {Number} totalChargeAmount 活动的总充值额
  351. * @apiSuccess {Number} orderCount 订单总数
  352. * @apiSuccess {Number} successOrderCount 成功订单总数
  353. * @apiSuccess {Number} pageUserNum 访问人数
  354. * @apiSuccess {Number} successrate 成功率
  355. * @apiSuccessExample {json} Success-Response:
  356. *
  357. * {
  358. * "code": 0,
  359. * "msg": "",
  360. * "data": [
  361. * {
  362. * "id": 2,
  363. * "name": "活动1222",
  364. * "activity_page": "http://www.baidu.com",
  365. * "created_at": "2017-12-01 10:20:04",
  366. * "start_time": "2017-12-01 10:20:04",
  367. * "end_time": "2017-12-01 10:20:04",
  368. * "price": 30,
  369. * "totalChargeAmount": 1000,
  370. * "orderCount": 50
  371. * }
  372. * ]
  373. * }
  374. */
  375. function channelActivity(Request $request)
  376. {
  377. $params = [];
  378. $channel_id = $this->getChannelId();
  379. $params['distribution_channel_id'] = [$channel_id];
  380. $activities = ActivityService::search($params);
  381. foreach ($activities as &$item) {
  382. $url = $item->activity_page;
  383. $item->activity_page = env('PROTOCOL') . '://site' . encodeDistributionChannelId($channel_id) . '.' . env('CUSTOM_HOST') . '.com' . $url;
  384. $stats = ActivityService::getActivityUvPv($item->id, $channel_id);
  385. //compact('page_pv','page_uv','button_pv','button_uv','order_num','re_order','order_sum');
  386. $item->totalChargeAmount = $stats['order_sum'];
  387. $item->orderCount = $stats['button_pv'];
  388. $item->successOrderCount = $stats['order_num'];
  389. $item->pageUserNum = $stats['page_uv'];
  390. $item->successrate = '0%';
  391. if ($stats['button_pv']) {
  392. $item->successrate = (string)round(($stats['order_num'] / (int)$stats['button_pv']) * 100, 4) . '%';
  393. }
  394. $is_over = 0;
  395. if($item->end_time < date('Y-m-d H:i:s')){
  396. $is_over = 1;
  397. }
  398. $item->is_over = $is_over;
  399. }
  400. return response()->collection(new ActivityTransformer(), $activities);
  401. }
  402. }