PrivateBookPage.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Redis;
  4. use app\main\constants\CacheConstants;
  5. use think\Model;
  6. class PrivateBookPage extends Model
  7. {
  8. // 表名
  9. protected $table = 'private_book_page';
  10. // 自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. protected $updateTime = 'updatetime';
  15. protected $redisKeyPrefix = 'SGRS:';
  16. // 追加属性
  17. protected $append = [
  18. ];
  19. public function getStatusList()
  20. {
  21. return ['1' => __('正常'),'0' => __('隐藏')];
  22. }
  23. /**
  24. * 拉取内推页内容
  25. * @param $id
  26. * @param bool $fromDb
  27. * @return array|mixed
  28. */
  29. public function getResourceList($id, $fromDb = false)
  30. {
  31. $redisKey = CacheConstants::getSinglePushResourceCache($id);
  32. if ($fromDb == false) {
  33. if ($redisVal = Redis::instance()->get($redisKey)) {
  34. $data = json_decode($redisVal, true);
  35. if ( empty($data) ) {
  36. $fromDb = true;
  37. }
  38. } else {
  39. $fromDb = true;
  40. }
  41. }
  42. if ($fromDb) {
  43. //库里查询
  44. $page = $this->where('id', 'eq', $id)->find();
  45. if ($page) {
  46. //拉取资源
  47. $rows = model("PrivateBookPageResource")->where('page_id', 'eq', $id)->order('weight desc')->limit(3)->select();
  48. $list = [];
  49. if ($rows) {
  50. foreach ($rows as $row) {
  51. $item = $row->toArray();
  52. unset($item['createtime']);
  53. unset($item['updatetime']);
  54. $list[] = $item;
  55. }
  56. }
  57. $data = [
  58. 'id' => $page['id'],
  59. 'top_img' => $page['top_img'],
  60. 'end_time' => $page['end_time'],
  61. 'status' => $page['status'],
  62. 'bottom_tips'=>$page['bottom_tips'],
  63. 'resource' => $list,
  64. ];
  65. }else{
  66. $data['id'] = 0;
  67. }
  68. //写入redis
  69. Redis::instance()->set($redisKey, json_encode($data, JSON_UNESCAPED_UNICODE), 600);
  70. }
  71. if ( !$data['id'] ){
  72. $data = [];
  73. }
  74. return $data;
  75. }
  76. }