PromotionController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace Modules\Tuiguang\Http\Controllers;
  3. use Catch\Base\CatchController;
  4. use Illuminate\Foundation\Validation\ValidatesRequests;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. use Modules\Common\Errors\Errors;
  8. use Modules\Common\Exceptions\CommonBusinessException;
  9. use Modules\User\Http\Controllers\UserTrait;
  10. class PromotionController extends CatchController
  11. {
  12. use UserTrait;
  13. use ValidatesRequests;
  14. public function list(Request $request) {
  15. $callbackTypeMap = [
  16. '0' => '无',
  17. '1' => '巨量账户级回传',
  18. ];
  19. $linkSourceMap = [
  20. '0' => '',
  21. '1' => 'tiktok'
  22. ];
  23. $name = $request->input('name');
  24. $isConfig = $request->input('is_config', 1);
  25. $id = $request->input('id');
  26. $videoName = $request->input('video_name');
  27. $starTime = $request->input('start_time');
  28. $endTime = $request->input('end_time');
  29. $miniprogramId = $request->input('miniprogram_id');
  30. $result = DB::table('promotions')
  31. ->leftJoin('videos', 'videos.id', '=', 'promotions.video_id')
  32. ->where(['promotions.is_enabled' => 1, 'promotions.uid' => $this->getLoginUserId()])
  33. ->when($miniprogramId, function ($query, $miniprogramId) {
  34. return $query->where('promotions.miniprogram_id', $miniprogramId);
  35. })
  36. ->when($isConfig, function ($query) {
  37. return $query->where('promotions.callback_config_id', '<>', 0);
  38. }, function ($query) {
  39. return $query->where('promotions.callback_config_id', 0);
  40. })->when($name, function ($query, $name){
  41. return $query->where('promotions.name', 'like', '%'. $name . '%');
  42. })->when($id, function ($query, $id) {
  43. return $query->where('promotions.id', $id);
  44. })->when($videoName, function ($query, $videoName) {
  45. return $query->where('videos.name', 'like', '%'. $videoName . '%');
  46. })->when($starTime, function ($query, $startTime) {
  47. return $query->where('promotions.created_at', '>=', $startTime);
  48. })->when($endTime, function ($query, $endTime){
  49. return $query->where('promotions.created_at', '<=', $endTime . ' 23:59:59');
  50. })->orderBy('created_at', 'desc')
  51. ->select('promotions.id', 'promotions.name', 'promotions.created_at',
  52. 'videos.name as video_name', 'promotions.series_sequence', 'promotions.callback_type',
  53. 'promotions.callback_config_id', 'promotions.video_id', 'promotions.remark', 'promotions.status',
  54. 'promotions.first_charge_template_id', 'promotions.not_first_charge_template_id')
  55. ->paginate($request->input('limit', 15));
  56. foreach ($result as $item) {
  57. $item->status_str = $item->status ? '启用':'禁用';
  58. $item->series_sequence_name = '第'. $item->series_sequence . '集';
  59. $item->callback_type_str = $callbackTypeMap[$item->callback_type] ?? '';
  60. $item->promotion_path = config('tuiguang.tuiguang.url') . DIRECTORY_SEPARATOR . 'api/promotion/index?ranse_id='. $item->id;
  61. $item->track_url = config('tuiguang.tuiguang.trackUrl') .
  62. '/track?dycallback=1&connection_id='.$item->id.
  63. '&link_source=' .( $linkSourceMap[$item->callback_type] ?? '') .
  64. '&adid=__AID__&cid=__CID__&imei=__IMEI__&oaid=__OAID__&mac=__MAC__&os=__OS__&ip=__IP__&androidid=__ANDROIDID__&ua=__UA__&timestamp=__TS__&callback_url=__CALLBACK_URL__&projectid=__PROJECT_ID__&promotionid=__PROMOTION_ID__&advertiser_id=__ADVERTISER_ID__&campaign_id=__CAMPAIGN_ID__';
  65. }
  66. return $result;
  67. }
  68. public function delete(Request $request) {
  69. $this->validate($request, ['id' => 'required']);
  70. DB::table('promotions')
  71. ->where(['id' => $request->input('id'), 'uid' => $this->getLoginUserId(), 'is_enabled' => 1])
  72. ->update(['is_enabled' => 0, 'updated_at' => date('Y-m-d H:i:s')]);
  73. return 'ok';
  74. }
  75. public function updateSeriesSequence(Request $request) {
  76. $this->validate($request, ['id' => 'required',
  77. 'series_sequence' => 'required', 'remark' => 'nullable|string|max:140',
  78. 'first_charge_template_id' => 'required',
  79. 'not_first_charge_template_id' => 'required',]);
  80. DB::table('promotions')
  81. ->where(['id' => $request->input('id'), 'uid' => $this->getLoginUserId(), 'is_enabled' => 1])
  82. ->update(['series_sequence' => $request->input('series_sequence'),
  83. 'remark' => $request->input('remark', ''),
  84. 'first_charge_template_id' => $request->input('first_charge_template_id'),
  85. 'not_first_charge_template_id' => $request->input('not_first_charge_template_id'),
  86. 'updated_at' => date('Y-m-d H:i:s')]);
  87. return 'ok';
  88. }
  89. public function add(Request $request) {
  90. $this->validate($request, [
  91. 'video_id' => 'required',
  92. 'series_sequence' => 'required',
  93. 'name' => 'required',
  94. 'miniprogram_id' => 'required',
  95. 'first_charge_template_id' => 'required',
  96. 'not_first_charge_template_id' => 'required',
  97. ]);
  98. $now = date('Y-m-d H:i:s');
  99. DB::table('promotions')
  100. ->insert([
  101. 'uid' => $this->getLoginUserId(),
  102. 'miniprogram_id' => $request->input('miniprogram_id'),
  103. 'name' => $request->input('name'),
  104. 'video_id' => $request->input('video_id'),
  105. 'series_sequence' => $request->input('series_sequence'),
  106. 'first_charge_template_id' => $request->input('first_charge_template_id'),
  107. 'not_first_charge_template_id' => $request->input('not_first_charge_template_id'),
  108. 'created_at' => $now,
  109. 'updated_at' => $now,
  110. ]);
  111. return 'ok';
  112. }
  113. /**
  114. * 更新推广的回传配置
  115. * 1, 如果是巨量账户级,推广id和巨量账户唯一绑定
  116. * @param Request $request
  117. * @return string
  118. * @throws \Illuminate\Validation\ValidationException
  119. */
  120. public function updateCallbackConfig(Request $request) {
  121. $this->validate($request, [
  122. 'id' => 'required',
  123. 'callback_type' => 'required|in:1',
  124. 'callback_config_id' => 'required',
  125. 'remark' => 'nullable|string|max:140',
  126. ]);
  127. if(1 == $request->input('callback_type')) {
  128. $exist = DB::table('promotions')
  129. ->where(['is_enabled' => 1, 'callback_type' => $request->input('callback_type'),
  130. 'callback_config_id' => $request->input('callback_config_id'),
  131. 'status' => 1])
  132. ->first();
  133. if($exist && $exist->id != $request->input('id')) {
  134. CommonBusinessException::throwError(Errors::JULIANG_ACCOUNT_PROMOTION_UNIQUE);
  135. }
  136. }
  137. DB::table('promotions')
  138. ->where(['id' => $request->input('id'), 'uid' => $this->getLoginUserId(), 'is_enabled' => 1])
  139. ->update([
  140. 'callback_type' => $request->input('callback_type'),
  141. 'callback_config_id' => $request->input('callback_config_id'),
  142. 'remark' => $request->input('remark', ''),
  143. 'updated_at' => date('Y-m-d')
  144. ]);
  145. return 'ok';
  146. }
  147. }