PromotionController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. ->leftJoin('miniprogram', 'miniprogram.id', '=', 'promotions.miniprogram_id')
  33. ->where(['promotions.is_enabled' => 1, 'promotions.uid' => $this->getLoginUserId()])
  34. ->when($miniprogramId, function ($query, $miniprogramId) {
  35. return $query->where('promotions.miniprogram_id', $miniprogramId);
  36. })
  37. ->when($isConfig, function ($query) {
  38. return $query->where('promotions.callback_config_id', '<>', 0);
  39. }, function ($query) {
  40. return $query->where('promotions.callback_config_id', 0);
  41. })->when($name, function ($query, $name){
  42. return $query->where('promotions.name', 'like', '%'. $name . '%');
  43. })->when($id, function ($query, $id) {
  44. return $query->where('promotions.id', $id);
  45. })->when($videoName, function ($query, $videoName) {
  46. return $query->where('videos.name', 'like', '%'. $videoName . '%');
  47. })->when($starTime, function ($query, $startTime) {
  48. return $query->where('promotions.created_at', '>=', $startTime);
  49. })->when($endTime, function ($query, $endTime){
  50. return $query->where('promotions.created_at', '<=', $endTime . ' 23:59:59');
  51. })->orderBy('created_at', 'desc')
  52. ->select('promotions.id', 'promotions.name', 'promotions.created_at',
  53. 'videos.name as video_name', 'promotions.series_sequence', 'promotions.callback_type',
  54. 'promotions.callback_config_id', 'promotions.video_id', 'promotions.remark', 'promotions.status',
  55. 'promotions.first_charge_template_id', 'promotions.not_first_charge_template_id')
  56. ->paginate($request->input('limit', 15));
  57. $chargeTemplateIds = $result->pluck('first_charge_template_id')->merge($result->pluck('not_first_charge_template_id'))->unique();
  58. $payTemplates = DB::table('pay_templates')->whereIn('id', $chargeTemplateIds)
  59. ->select('id', 'name')
  60. ->get()->keyBy('id')->toArray();
  61. foreach ($result as $item) {
  62. $item->status_str = $item->status ? '启用':'禁用';
  63. $item->series_sequence_name = '第'. $item->series_sequence . '集';
  64. $item->callback_type_str = $callbackTypeMap[$item->callback_type] ?? '';
  65. $item->promotion_path = config('tuiguang.tuiguang.url') . DIRECTORY_SEPARATOR . 'api/promotion/index?ranse_id='. $item->id;
  66. $item->track_url = config('tuiguang.tuiguang.trackUrl') .
  67. '/track?dycallback=1&connection_id='.$item->id.
  68. '&link_source=' .( $linkSourceMap[$item->callback_type] ?? '') .
  69. '&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__';
  70. $item->first_charge_template_str = $payTemplates[$item->first_charge_template_id]->name ?? '';
  71. $item->not_first_charge_template_str = $payTemplates[$item->not_first_charge_template_id]->name ?? '';
  72. }
  73. return $result;
  74. }
  75. public function delete(Request $request) {
  76. $this->validate($request, ['id' => 'required']);
  77. DB::table('promotions')
  78. ->where(['id' => $request->input('id'), 'uid' => $this->getLoginUserId(), 'is_enabled' => 1])
  79. ->update(['is_enabled' => 0, 'updated_at' => date('Y-m-d H:i:s')]);
  80. return 'ok';
  81. }
  82. public function updateSeriesSequence(Request $request) {
  83. $this->validate($request, ['id' => 'required',
  84. 'series_sequence' => 'required', 'remark' => 'nullable|string|max:140']);
  85. DB::table('promotions')
  86. ->where(['id' => $request->input('id'), 'uid' => $this->getLoginUserId(), 'is_enabled' => 1])
  87. ->update(['series_sequence' => $request->input('series_sequence'),
  88. 'remark' => $request->input('remark') ?? '',
  89. 'first_charge_template_id' => $request->input('first_charge_template_id'),
  90. 'not_first_charge_template_id' => $request->input('not_first_charge_template_id'),
  91. 'updated_at' => date('Y-m-d H:i:s')]);
  92. return 'ok';
  93. }
  94. public function add(Request $request) {
  95. $this->validate($request, [
  96. 'video_id' => 'required',
  97. 'series_sequence' => 'required',
  98. 'name' => 'required',
  99. 'miniprogram_id' => 'required',
  100. ]);
  101. $now = date('Y-m-d H:i:s');
  102. DB::table('promotions')
  103. ->insert([
  104. 'uid' => $this->getLoginUserId(),
  105. 'miniprogram_id' => $request->input('miniprogram_id'),
  106. 'name' => $request->input('name'),
  107. 'video_id' => $request->input('video_id'),
  108. 'series_sequence' => $request->input('series_sequence'),
  109. 'created_at' => $now,
  110. 'updated_at' => $now,
  111. ]);
  112. return 'ok';
  113. }
  114. /**
  115. * 更新推广的回传配置
  116. * 1, 如果是巨量账户级,推广id和巨量账户唯一绑定
  117. * @param Request $request
  118. * @return string
  119. * @throws \Illuminate\Validation\ValidationException
  120. */
  121. public function updateCallbackConfig(Request $request) {
  122. $this->validate($request, [
  123. 'id' => 'required',
  124. 'callback_type' => 'required|in:1',
  125. 'callback_config_id' => 'required',
  126. 'remark' => 'nullable|string|max:140',
  127. ]);
  128. if(1 == $request->input('callback_type')) {
  129. $exist = DB::table('promotions')
  130. ->where(['is_enabled' => 1, 'callback_type' => $request->input('callback_type'),
  131. 'callback_config_id' => $request->input('callback_config_id'),
  132. 'status' => 1])
  133. ->first();
  134. if($exist && $exist->id != $request->input('id')) {
  135. CommonBusinessException::throwError(Errors::JULIANG_ACCOUNT_PROMOTION_UNIQUE);
  136. }
  137. }
  138. DB::table('promotions')
  139. ->where(['id' => $request->input('id'), 'uid' => $this->getLoginUserId(), 'is_enabled' => 1])
  140. ->update([
  141. 'callback_type' => $request->input('callback_type'),
  142. 'callback_config_id' => $request->input('callback_config_id'),
  143. 'remark' => $request->input('remark', ''),
  144. 'updated_at' => date('Y-m-d')
  145. ]);
  146. return 'ok';
  147. }
  148. }