123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace app\common\model;
- use app\common\library\Redis;
- use think\Model;
- class SubscribeDelayPush extends Model
- {
- // 表名
- protected $table = 'subscribe_delay_push';
-
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
-
- // 追加属性
- protected $append = [
- 'status_text'
- ];
-
- protected $caches = [];
-
- public function getStatusList()
- {
- return ['normal' => __('Status normal'),'hidden' => __('Status hidden')];
- }
- public function getStatusTextAttr($value, $data)
- {
- $value = $value ? $value : $data['status'];
- $list = $this->getStatusList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- /**
- * 清理缓存
- * @param null $admin_id
- */
- public function delCache($admin_id = null)
- {
- if ($admin_id) {
- $key = "WSDPS:".$admin_id;
- Redis::instance()->del($key);
- }
- $allKey = "WSDPS:0";
- Redis::instance()->del($allKey);
- }
- /**
- * 获取配置缓存
- * @param null $admin_id
- * @return array|bool|string
- */
- public function getInfo($admin_id = null)
- {
- if ($admin_id) {
- $key = "WSDPS:".$admin_id;
- } else {
- $key = "WSDPS:0";
- }
- if (isset($this->caches[$key])) {
- return $this->caches[$key];
- }
- $data = Redis::instance()->get($key);
- if (!!!$data) {
- //取存
- $data = [];
- $maps = [
- 'status' => ['eq', 'normal'],
- ];
- if ($admin_id) {
- //需要判断是否可以使用
- $adminConfig = model("AdminConfig")->getAdminInfoAll($admin_id);
- if (!isset($adminConfig['subscribe_delay']) || $adminConfig['subscribe_delay'] != "2") {
- //不存在、未设置、未开启 直接返回空数组
- Redis::instance()->set($key, json_encode($data), 600);
- $this->caches[$key] = $data;
- return $data;
- }
- //继续
- $maps['admin_id'] = ['eq', $admin_id];
- }
- $rows = model("SubscribeDelayPush")->field("id, admin_id, content, content_type, delay, type, updatetime, status")->where($maps)->select();
- if ($rows) {
- $adminIds = array_unique(array_column($rows, 'admin_id'));
- //过滤未开启渠道
- $adminIdRows = model("AdminConfig")->field('admin_id')->where('admin_id', 'in', $adminIds)->where('subscribe_delay', 'eq','2')->select();
- $adminIdsArr = array_column($adminIdRows, 'admin_id');
- foreach ($rows as $row) {
- if (in_array($row['admin_id'], $adminIdsArr)) {
- $content = json_decode($row['content'], true);
- // $custom_fields = ["description","image","title","url"];
- $user_fields = ['user_type','mobile_system','sex'];
- $user_json = ['user_type' => 1,'mobile_system'=>0,'sex'=>0];
- foreach ($content as $key=>$value) {
- if (in_array($key, $user_fields)) {
- $user_json[$key] = $value;
- unset($content[$key]);
- }
- }
- $data[] = [
- 'id' => $row['id'],
- 'content' => json_encode($content, JSON_UNESCAPED_UNICODE),
- 'user_json' => json_encode($user_json, JSON_UNESCAPED_UNICODE),
- 'content_type'=>$row['content_type'],
- 'channel_id' => $row['admin_id'],
- 'delay_secs' => $row['type'] == 1 ? $row['delay'] * 60 : ($row['type'] == 2 ? $row['delay'] * 3600 : 0),
- 'update_time' => $row['updatetime'],
- 'status' => $row['status'] == 'normal' ? 1 : 0,
- ];
- }
- }
- }
- Redis::instance()->set($key, json_encode($data), 600);
- } else {
- $data = json_decode($data, true);
- }
- $this->caches[$key] = $data;
- return $data;
- }
- }
|