Custom.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\CustomMediaPush;
  4. use app\admin\model\CustomUrl;
  5. use app\common\controller\Api;
  6. use think\Db;
  7. use think\Exception;
  8. use think\Log;
  9. /**
  10. * Custom 相关接口
  11. */
  12. class Custom extends Api
  13. {
  14. //如果$noNeedLogin为空表示所有接口都需要登录才能请求
  15. //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
  16. //如果接口已经设置无需登录,那也就无需鉴权了
  17. //
  18. // 无需登录的接口,*表示全部
  19. protected $noNeedLogin = ['handlestatusapi', 'subscribedelayapi'];
  20. // 无需鉴权的接口,*表示全部
  21. protected $noNeedRight = ['handlestatusapi', 'subscribedelayapi'];
  22. /**
  23. * @param ids 需要更改的custom_id(array)
  24. * @param status : (send => 发送中 , hidden => 发送完成)
  25. * @throws \Exception
  26. */
  27. public function handlestatusapi()
  28. {
  29. $custom_model = new \app\admin\model\Custom();
  30. $customUrl_model = new CustomUrl();
  31. $customMediaPush_model = new CustomMediaPush();
  32. $params = input();
  33. $custom_list = $params['custom_list'] ?? [];
  34. $status = $params['status'] ?? '';
  35. Log::info('==== handlestatusapi start ====');
  36. Log::info(print_r($params, true));
  37. if ($custom_list && $status) {
  38. $status_origin = [
  39. \app\common\constants\Custom::CUSTOM_STATUE_SEND,
  40. \app\common\constants\Custom::CUSTOM_STATUE_HIDDEN
  41. ];
  42. if (in_array($status, $status_origin)) {
  43. $custom_ids_str = implode(',', array_column($custom_list, 'id'));
  44. if ($status == \app\common\constants\Custom::CUSTOM_STATUE_SEND) {
  45. # 1.根据参数 media_id,更改custom 表中的状态 ( 如果是status == send , 只需要更改 statue 字段)
  46. try {
  47. $custom_model->where('id', 'in', $custom_ids_str)
  48. ->update(['statue' => $status]);
  49. } catch (Exception $e) {
  50. Log::error($e->getMessage());
  51. $this->error($e->getMessage());
  52. }
  53. $custom_push_status = \app\common\constants\Custom::CUSTOM_MEDIA_PUSH_STATUS_SENDING;
  54. } else {
  55. # 1.根据参数 media_id,更改custom 表中的状态
  56. foreach ($custom_list as $k => $item) {
  57. try {
  58. $send_num = $item['send_num'] ?? 0;
  59. $custom_model->where('id', 'eq', $item['id'])
  60. ->update(['statue' => $status, 'send_num' => $send_num]);
  61. Log::info('custom 表中的数据statue send_num 更新完成');
  62. } catch (Exception $e) {
  63. Log::error($e->getMessage());
  64. $this->error($e->getMessage());
  65. }
  66. }
  67. $custom_push_status = \app\common\constants\Custom::CUSTOM_MEDIA_PUSH_STATUS_SENT;
  68. # 5. 更改 custom_url.sendstatus
  69. if ($status == \app\common\constants\Custom::CUSTOM_STATUE_HIDDEN) {
  70. try {
  71. $customUrl_model->where('custom_id', 'in', $custom_ids_str)
  72. ->update(['sendstatus' => \app\common\constants\Custom::CUSTOM_URL_SENDSTATUS_SENT]);
  73. Log::info('custom_url 表中的数据更新完成');
  74. } catch (Exception $e) {
  75. Log::error($e->getMessage());
  76. $this->error($e->getMessage());
  77. }
  78. }
  79. }
  80. # 2.根据参数 media_id,获取需要更改状态的 custom_media_push_id
  81. $push_ids = $custom_model->where('id', 'in', $custom_ids_str)
  82. ->column('custom_media_push_id');
  83. # 3.去掉重复的custom_media_push_id
  84. $push_ids = array_unique($push_ids);
  85. if ($push_ids) {
  86. $custom_push_ids_str = implode(',', $push_ids);
  87. # 4. 更改 custom_media_push 的状态
  88. try {
  89. $customMediaPush_model->where('id', 'in', $custom_push_ids_str)
  90. ->update(['status' => $custom_push_status]);
  91. Log::info('custom_media_push 表中的数据更新完成');
  92. } catch (Exception $e) {
  93. Log::error($e->getMessage());
  94. $this->error($e->getMessage());
  95. }
  96. }
  97. } else {
  98. Log::info('参数status错误');
  99. $this->error('参数status错误');
  100. }
  101. } else {
  102. $this->error('参数custom_list或status不存在');
  103. }
  104. Log::info('handlestatusapi Success');
  105. Log::info('==== handlestatusapi end ====');
  106. $this->success('成功');
  107. }
  108. /**
  109. * 关注延时推送的配置
  110. */
  111. public function subscribedelayapi()
  112. {
  113. $channelId = 0;
  114. $params = json_decode(file_get_contents("php://input"), true);
  115. if ($params) {
  116. $channelId = $params['channel_id'] ?? 0;
  117. }
  118. if (!$channelId) {
  119. $channelId = $this->request->param('channel_id', 0);
  120. }
  121. $data = model("SubscribeDelayPush")->getInfo($channelId);
  122. $result = [
  123. 'code' => 0,
  124. 'msg' => 'success',
  125. 'data' => $data
  126. ];
  127. exit(json_encode($result, JSON_UNESCAPED_SLASHES));
  128. }
  129. }