Media.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. <?php
  2. namespace App\Http\Controllers\Fpdx;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Redis;
  5. use App\Http\Controllers\Core\Ding;
  6. use App\Http\Controllers\Controller;
  7. use App\Models\Fpdx\ActivityModel;
  8. use App\Models\Fpdx\MediaModel;
  9. use App\Models\Fpdx\PairModel;
  10. use App\Models\Fpdx\MediaNotiveModel;
  11. use App\Models\Fpdx\MediaOrderModel;
  12. class Media extends Controller
  13. {
  14. private $model;
  15. public function __construct()
  16. {
  17. $this->model = new MediaModel();
  18. }
  19. /**
  20. * @param Request $request
  21. * @return array
  22. */
  23. public function createLink(Request $request)
  24. {
  25. $data = array(
  26. 'public_name' => $request->post('public_name'),
  27. 'attach' => json_encode($request->toArray())
  28. );
  29. $data['media_id'] = "kd_" . bin2hex(\openssl_random_pseudo_bytes(6));
  30. $media = $this->model->fill($data);
  31. if ($media->save()) {
  32. Redis::lpush("{pair_hides}", $data['media_id']);
  33. return [
  34. 'code' => 200,
  35. 'message' => 'success',
  36. 'data' => $media
  37. ];
  38. } else {
  39. return [
  40. 'code' => 500,
  41. 'message' => 'fail',
  42. ];
  43. }
  44. }
  45. /**
  46. * 自动结算任务
  47. * @param int $stage_id
  48. */
  49. public static function autoCpAccount(int $stage_id = 0)
  50. {
  51. if ($stage_id == 0) {
  52. $activitys = ActivityModel::getActivitys();
  53. $stage_id = $activitys['last'];
  54. }
  55. PairModel::where('stage_id', $stage_id)->whereIn('state', [401, 402, 403, 404])->increment('state', 400);
  56. $pairs = PairModel::where(array(
  57. ['stage_id', $stage_id],
  58. ['fx_money', '=', 0]
  59. ))->where("completed_at", 0)->whereBetween('state', [100, 899])->get();
  60. foreach ($pairs as $pair) {
  61. $pair->completed_at = time();
  62. $pair->save();
  63. $media = new Media();
  64. if (
  65. in_array($pair->media_id, [
  66. 'kd_b598cb7474d8',
  67. 'kd_5edc93aa148e',
  68. 'gh_01c089b58dda',
  69. 'gh_c94c95866ca5',
  70. 'gh_5edc93aa148e',
  71. 'gh_b598cb7474d8'
  72. ])
  73. || strpos("#" . $pair->media_id, 'gh_') != 1
  74. ) {
  75. $media->kolFenxiao($pair->id);
  76. } else {
  77. $media->cpFenxiao($pair->id);
  78. }
  79. }
  80. Ding::robot([
  81. 'title' => '活动分销提醒',
  82. 'text' => "分配对象第「{$stage_id}」期结算任务完成"
  83. ]);
  84. }
  85. /**
  86. * 分配对象分销
  87. * @param int pair_id 匹配报名的id
  88. * @return void
  89. */
  90. public function cpFenxiao(int $pair_id)
  91. {
  92. $pair = PairModel::find($pair_id);
  93. if (collect($pair)->isEmpty()) {
  94. return;
  95. }
  96. if ($pair->state > 800 && $pair->state < 899) {
  97. if ($pair->sex == 2) {
  98. $pair->fx_money = 3.5;
  99. } else {
  100. $pair->fx_money = 2.5;
  101. }
  102. } else {
  103. $pair->fx_money = 0.5;
  104. }
  105. if ($pair->save()) {
  106. $this->fenxiao($pair->media_id, $pair->fx_money, $column = 'fpdx_money');
  107. MediaOrderModel::create([
  108. 'media_id' => $pair->media_id,
  109. 'type' => 'income',
  110. 'amount' => $pair->fx_money * 100,
  111. 'tag' => '分配对象',
  112. 'describe' => "分配对象活动第{$pair->stage_id}期结算(订单ID:{$pair->order_id})"
  113. ]);
  114. }
  115. }
  116. /**
  117. * kol分销
  118. * @param int $pair_id 匹配报名id
  119. * @return void
  120. */
  121. public function kolFenxiao(int $pair_id)
  122. {
  123. $pair = PairModel::find($pair_id);
  124. if (collect($pair)->isEmpty()) {
  125. return;
  126. }
  127. if ($pair->state > 800 && $pair->state < 899) {
  128. $pair->fx_money = $pair->pay;
  129. } else {
  130. $pair->fx_money = 0;
  131. }
  132. if ($pair->save()) {
  133. $this->fenxiao($pair->media_id, $pair->fx_money, $column = 'fpdx_money');
  134. if ($pair->fx_money > 0) {
  135. MediaOrderModel::create([
  136. 'media_id' => $pair->media_id,
  137. 'type' => 'income',
  138. 'amount' => $pair->fx_money * 100,
  139. 'tag' => '分配对象',
  140. 'describe' => "分配对象活动第{$pair->stage_id}期结算(订单ID:{$pair->order_id})"
  141. ]);
  142. }
  143. }
  144. }
  145. /**
  146. * 公众号分销,增加账户收入
  147. * @param string $media_id
  148. * @param float money
  149. * @param string $column
  150. * @return void
  151. */
  152. private function fenxiao(string $media_id, float $money, $column = 'gold_flower')
  153. {
  154. $media = $this->model->where('media_id', $media_id)->first();
  155. if (collect($media)->isEmpty()) {
  156. $media = $this->model->fill([
  157. 'media_id' => $media_id,
  158. $column => $money
  159. ]);
  160. $media->save();
  161. } else {
  162. $this->model->where('media_id', $media_id)->increment($column, $money);
  163. }
  164. return;
  165. }
  166. /**
  167. * 广播通知
  168. * @param Request $request
  169. * @return array
  170. */
  171. public function broadcastNotive(Request $request)
  172. {
  173. $medias = $this->model->where()->pluck('media_id');
  174. array_map(function ($media_id) use ($request, &$datas) {
  175. array_push($datas, [
  176. 'created_at' => time(),
  177. 'media_id' => $media_id,
  178. 'content' => $request->input('content'),
  179. 'type' => 1
  180. ]);
  181. if (count($datas) > 100) {
  182. \DB::table('kdgx_fpdx_media_notive')->insert($datas);
  183. $datas = array();
  184. }
  185. }, $medias);
  186. return [
  187. 'code' => 200,
  188. 'message' => 'success'
  189. ];
  190. }
  191. /**
  192. * 获取未读消息数
  193. * @param string $media_id 公众号id
  194. * @return array [
  195. * 'code' => 200,
  196. * 'message' => 'success',
  197. * 'data' => [
  198. * 'count' => 未读消息数量,
  199. * 'list' => [
  200. * 'id' => 消息id,
  201. * 'created_at' => 创建时间,
  202. * 'content' => 内容,
  203. * 'read_at' => 阅读时间,
  204. * 'type' => 消息类型
  205. * ],
  206. * ],
  207. * ]
  208. */
  209. public function unread($media_id)
  210. {
  211. $datas = MediaNotiveModel::where('media_id', $media_id)->whereNull('read_at')->get([
  212. 'id',
  213. 'created_at',
  214. 'content',
  215. 'read_at',
  216. 'type'
  217. ]);
  218. return [
  219. 'code' => 200,
  220. 'message' => 'success',
  221. 'data' => [
  222. 'count' => $datas->count(),
  223. 'list' => $datas
  224. ],
  225. ];
  226. }
  227. /**
  228. * 获取消息列表
  229. * @param string $media_id 公众号id
  230. * @param int page 页数?
  231. * @return array [
  232. * 'code' => 200,
  233. * 'message' => 'success',
  234. * 'data' => [
  235. * 'page' => [
  236. * 'count' => 总数,
  237. * 'limit' => 每页数量
  238. * ],
  239. * 'list' => [
  240. * 'id' => 消息id,
  241. * 'created_at' => 创建时间,
  242. * 'content' => 内容,
  243. * 'read_at' => 阅读时间,
  244. * 'type' => 消息类型
  245. * ],
  246. * ],
  247. * ]
  248. */
  249. public function notivelist($media_id, $page = 1)
  250. {
  251. $count = MediaNotiveModel::where('media_id', $media_id)->count();
  252. $datas = MediaNotiveModel::where('media_id', $media_id)->skip(($page - 1) * 20)->take(20)->get([
  253. 'id',
  254. 'created_at',
  255. 'content',
  256. 'read_at',
  257. 'type'
  258. ]);
  259. return [
  260. 'code' => 200,
  261. 'message' => 'success',
  262. 'data' => [
  263. 'page' => [
  264. 'count' => $count,
  265. 'limit' => 20,
  266. ],
  267. 'list' => $datas
  268. ],
  269. ];
  270. }
  271. /**
  272. * 阅读通知
  273. * @param int $notive_id
  274. * @return array
  275. */
  276. public function readNotive(int $notive_id)
  277. {
  278. $notive = MediaNotiveModel::find($notive_id);
  279. $notive->read_at = time();
  280. $notive->save();
  281. return [
  282. 'code' => 200,
  283. 'message' => 'success'
  284. ];
  285. }
  286. /**
  287. * 获取分配对象收益明细
  288. * @param Request $request
  289. * @param string $media_id 公众号id
  290. * @return array [
  291. * 'code' => 200,
  292. * 'message' => 'success',
  293. * 'data' => [
  294. * 'total' => [
  295. * 'count' => 累计报名人数,
  296. * 'fx_money' => 累计收益,
  297. * 'expect_money' => 预期收益
  298. * ],
  299. * 'data' => [
  300. * [
  301. * 'uid' => 用户id,
  302. * 'stage_id' => 期数,
  303. * 'sex' => 性别,
  304. * 'create_time' => 报名时间,
  305. * 'fx_money' => 本次收益,
  306. * 'state' => 结算状态,
  307. * 'user' => [
  308. * 'uid' => 用户id,
  309. * 'nickanem' => '用户昵称'
  310. * ],
  311. * ],
  312. * ],
  313. * ],
  314. * ]
  315. */
  316. public function fxIncome(Request $request, string $media_id)
  317. {
  318. $page = $request->page;
  319. $pairModel = new PairModel();
  320. $datas = $pairModel->fxIncome($media_id);
  321. $total = array(
  322. 'count' => $datas->count(),
  323. 'fx_money' => $datas->sum('fx_money'),
  324. 'expect_money' => $datas->where('sex', 1)->filter(function ($item) {
  325. return $item->state > 100 && $item->state < 299;
  326. })->count() * 2.5 + $datas->where('sex', 2)->filter(function ($item) {
  327. return $item->state > 100 && $item->state < 299;
  328. })->count() * 3.5
  329. );
  330. $pages = array(
  331. 'count' => $datas->count(),
  332. 'limit' => 40,
  333. 'page' => $page
  334. );
  335. $tmp = $datas->slice(($page - 1) * $pages['limit'])->take($pages['limit']);
  336. $list = array();
  337. $tmp->each(function ($item, $key) use (&$list) {
  338. array_push($list, $item);
  339. });
  340. return [
  341. 'code' => 200,
  342. 'message' => 'success',
  343. 'data' => [
  344. 'total' => $total,
  345. 'pages' => $pages,
  346. 'list' => $list
  347. ],
  348. ];
  349. }
  350. /**
  351. * fpdx公众号收益总计
  352. * @param string $media_id 公众号id
  353. * @return array [
  354. * 'code' => 200,
  355. * 'message' => 'success',
  356. * 'data' => [
  357. * 'count' => 累计报名人数,
  358. * 'fx_money' => 累计收益,
  359. * 'expect_money' => 预期收益
  360. * ],
  361. * ]
  362. */
  363. public function fpdxIncome(string $media_id)
  364. {
  365. $pairModel = new PairModel();
  366. $datas = $pairModel->fxIncome($media_id);
  367. $data = array(
  368. 'count' => $datas->count(),
  369. 'fx_money' => $datas->sum('fx_money'),
  370. );
  371. $res = MediaModel::where("media_id", $media_id)->first(['fpdx_money']);
  372. $data['fpdx_money'] = $res->fpdx_money;
  373. $activitys = ActivityModel::getActivitys();
  374. $datas = $pairModel->fxrank($activitys['next']);
  375. $rank = 0;
  376. foreach ($datas as $value) {
  377. $rank++;
  378. if ($value->media_id == $media_id) {
  379. break;
  380. }
  381. }
  382. $data['rank'] = $rank;
  383. return [
  384. 'code' => 200,
  385. 'message' => 'success',
  386. 'data' => $data
  387. ];
  388. }
  389. /**
  390. * msy公众号收益总计
  391. * @param string $media_id 公众号id
  392. * @return array [
  393. * 'code' => 200,
  394. * 'message' => 'success',
  395. * 'data' => [
  396. * 'count' => 累计消费笔数,
  397. * 'fx_money' => 累计收益,
  398. * 'today_count' => 今日笔数
  399. * ],
  400. * ]
  401. */
  402. public function msyIncome(string $media_id)
  403. {
  404. $data = $this->model->incomeData($media_id);
  405. return [
  406. 'code' => 200,
  407. 'message' => 'success',
  408. 'data' => $data
  409. ];
  410. }
  411. /**
  412. * @param string $media_id
  413. * @return array
  414. */
  415. public function isOpen(string $media_id)
  416. {
  417. $wx_fpdx = $this->model->isOpen($media_id, 'fpdx', 'weixiao');
  418. $kd_fpdx = $this->model->isOpen($media_id, 'fpdx', 'koudai');
  419. $wx_msy = $this->model->isOpen($media_id, 'msy', 'weixiao');
  420. $kd_msy = $this->model->isOpen($media_id, 'msy', 'koudai');
  421. return [
  422. 'code' => 200,
  423. 'message' => 'successs',
  424. 'data' => [
  425. 'fpdx' => [
  426. 'isopen' => $wx_fpdx || $kd_fpdx
  427. ],
  428. 'msy' => [
  429. 'isopen' => $wx_msy || $kd_msy
  430. ],
  431. ],
  432. ];
  433. }
  434. /**
  435. * @param Request $request
  436. * @param string $media_id 公众号id
  437. * @return array
  438. * [
  439. * "code" => 200,
  440. * 'message' => 'success',
  441. * 'data' => [
  442. *
  443. * ]
  444. * ]
  445. * @throws \ApiException
  446. */
  447. public function info(Request $request, string $media_id)
  448. {
  449. $app = $request->get('app');
  450. $data = $this->model->mediaInfo($media_id, $app);
  451. return [
  452. 'code' => 200,
  453. 'message' => 'success',
  454. 'data' => $data
  455. ];
  456. }
  457. }