SubscribeDelayPush.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Redis;
  4. use think\Model;
  5. class SubscribeDelayPush extends Model
  6. {
  7. // 表名
  8. protected $table = 'subscribe_delay_push';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. // 追加属性
  15. protected $append = [
  16. 'status_text'
  17. ];
  18. protected $caches = [];
  19. public function getStatusList()
  20. {
  21. return ['normal' => __('Status normal'),'hidden' => __('Status hidden')];
  22. }
  23. public function getStatusTextAttr($value, $data)
  24. {
  25. $value = $value ? $value : $data['status'];
  26. $list = $this->getStatusList();
  27. return isset($list[$value]) ? $list[$value] : '';
  28. }
  29. /**
  30. * 清理缓存
  31. * @param null $admin_id
  32. */
  33. public function delCache($admin_id = null)
  34. {
  35. if ($admin_id) {
  36. $key = "WSDPS:".$admin_id;
  37. Redis::instance()->del($key);
  38. }
  39. $allKey = "WSDPS:0";
  40. Redis::instance()->del($allKey);
  41. }
  42. /**
  43. * 获取配置缓存
  44. * @param null $admin_id
  45. * @return array|bool|string
  46. */
  47. public function getInfo($admin_id = null)
  48. {
  49. if ($admin_id) {
  50. $key = "WSDPS:".$admin_id;
  51. } else {
  52. $key = "WSDPS:0";
  53. }
  54. if (isset($this->caches[$key])) {
  55. return $this->caches[$key];
  56. }
  57. $data = Redis::instance()->get($key);
  58. if (!!!$data) {
  59. //取存
  60. $data = [];
  61. $maps = [
  62. 'status' => ['eq', 'normal'],
  63. ];
  64. if ($admin_id) {
  65. //需要判断是否可以使用
  66. $adminConfig = model("AdminConfig")->getAdminInfoAll($admin_id);
  67. if (!isset($adminConfig['subscribe_delay']) || $adminConfig['subscribe_delay'] != "2") {
  68. //不存在、未设置、未开启 直接返回空数组
  69. Redis::instance()->set($key, json_encode($data), 600);
  70. $this->caches[$key] = $data;
  71. return $data;
  72. }
  73. //继续
  74. $maps['admin_id'] = ['eq', $admin_id];
  75. }
  76. $rows = model("SubscribeDelayPush")->field("id, admin_id, content, content_type, delay, type, updatetime, status")->where($maps)->select();
  77. if ($rows) {
  78. $adminIds = array_unique(array_column($rows, 'admin_id'));
  79. //过滤未开启渠道
  80. $adminIdRows = model("AdminConfig")->field('admin_id')->where('admin_id', 'in', $adminIds)->where('subscribe_delay', 'eq','2')->select();
  81. $adminIdsArr = array_column($adminIdRows, 'admin_id');
  82. foreach ($rows as $row) {
  83. if (in_array($row['admin_id'], $adminIdsArr)) {
  84. $content = json_decode($row['content'], true);
  85. // $custom_fields = ["description","image","title","url"];
  86. $user_fields = ['user_type','mobile_system','sex'];
  87. $user_json = ['user_type' => 1,'mobile_system'=>0,'sex'=>0];
  88. foreach ($content as $key=>$value) {
  89. if (in_array($key, $user_fields)) {
  90. $user_json[$key] = $value;
  91. unset($content[$key]);
  92. }
  93. }
  94. $data[] = [
  95. 'id' => $row['id'],
  96. 'content' => json_encode($content, JSON_UNESCAPED_UNICODE),
  97. 'user_json' => json_encode($user_json, JSON_UNESCAPED_UNICODE),
  98. 'content_type'=>$row['content_type'],
  99. 'channel_id' => $row['admin_id'],
  100. 'delay_secs' => $row['type'] == 1 ? $row['delay'] * 60 : ($row['type'] == 2 ? $row['delay'] * 3600 : 0),
  101. 'update_time' => $row['updatetime'],
  102. 'status' => $row['status'] == 'normal' ? 1 : 0,
  103. ];
  104. }
  105. }
  106. }
  107. Redis::instance()->set($key, json_encode($data), 600);
  108. } else {
  109. $data = json_decode($data, true);
  110. }
  111. $this->caches[$key] = $data;
  112. return $data;
  113. }
  114. }