1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace app\common\model;
- use app\common\library\Redis;
- use app\main\constants\CacheConstants;
- use think\Model;
- class PrivateBookPage extends Model
- {
- // 表名
- protected $table = 'private_book_page';
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $redisKeyPrefix = 'SGRS:';
- // 追加属性
- protected $append = [
- ];
- public function getStatusList()
- {
- return ['1' => __('正常'),'0' => __('隐藏')];
- }
- /**
- * 拉取内推页内容
- * @param $id
- * @param bool $fromDb
- * @return array|mixed
- */
- public function getResourceList($id, $fromDb = false)
- {
- $redisKey = CacheConstants::getSinglePushResourceCache($id);
- if ($fromDb == false) {
- if ($redisVal = Redis::instance()->get($redisKey)) {
- $data = json_decode($redisVal, true);
- if ( empty($data) ) {
- $fromDb = true;
- }
- } else {
- $fromDb = true;
- }
- }
- if ($fromDb) {
- //库里查询
- $page = $this->where('id', 'eq', $id)->find();
- if ($page) {
- //拉取资源
- $rows = model("PrivateBookPageResource")->where('page_id', 'eq', $id)->order('weight desc')->limit(3)->select();
- $list = [];
- if ($rows) {
- foreach ($rows as $row) {
- $item = $row->toArray();
- unset($item['createtime']);
- unset($item['updatetime']);
- $list[] = $item;
- }
- }
- $data = [
- 'id' => $page['id'],
- 'top_img' => $page['top_img'],
- 'end_time' => $page['end_time'],
- 'status' => $page['status'],
- 'bottom_tips'=>$page['bottom_tips'],
- 'resource' => $list,
- ];
- }else{
- $data['id'] = 0;
- }
- //写入redis
- Redis::instance()->set($redisKey, json_encode($data, JSON_UNESCAPED_UNICODE), 600);
- }
- if ( !$data['id'] ){
- $data = [];
- }
- return $data;
- }
- }
|