Tiktok.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace app\common\library;
  3. use think\Env;
  4. /**
  5. * 抖音广告队列类
  6. * Class MakeReport
  7. */
  8. class Tiktok
  9. {
  10. private $_redis = null;
  11. public function __construct()
  12. {
  13. $this->_redis = new \Redis();
  14. $this->_redis->connect(Env::get('redis.host', '127.0.0.1'), Env::get('redis.port', 6379));
  15. if (Env::get('redis.password', '')) {
  16. $this->_redis->auth(Env::get('redis.password', ''));
  17. }
  18. $this->_redis->select(Env::get('redis.seelect_go', ''));
  19. }
  20. /**
  21. * 添加任务
  22. * @param $action string 和go协商好的名称用来识别哪个方法
  23. * @param $uid int 用户id
  24. * @param $extend array 扩展参数
  25. * @param $count int 重试次数
  26. * @param int $expire 相同任务多久内不重复生成
  27. *
  28. * @return boolean true:提交任务成功 ;false:提交失败
  29. */
  30. public function addTask($action, $uid, $extend = [], $count = 0, $expire = 60)
  31. {
  32. $task_data = array(
  33. 'Action' => $action,
  34. 'Uid' => $uid,
  35. 'Time' => getMillisecond(),
  36. 'Extend' => $extend,
  37. 'Count' => $count
  38. );
  39. $task_unique_id = strtolower(md5(sprintf(
  40. '%s|%s|%s',
  41. $task_data['Uid'],
  42. $task_data['Action'],
  43. json_encode($task_data['Extend'])
  44. )));
  45. $task_unique_id = 'tiktok:'. $task_unique_id;
  46. $has_task = $this->_redis->get($task_unique_id);
  47. // 防止重复生成报表
  48. if (empty($has_task)) {
  49. $this->_redis->zAdd('tiktok:task',getMillisecond(), json_encode($task_data));
  50. $this->_redis->setex($task_unique_id, $expire, 1);
  51. return true;
  52. } else {
  53. return false;
  54. }
  55. }
  56. }