Resource.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Redis;
  4. use think\Model;
  5. class Resource extends Model
  6. {
  7. // 表名
  8. protected $table = 'resource';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. // 追加属性
  15. protected $append = [
  16. 'show_type_text',
  17. 'type_text',
  18. 'status_text',
  19. 'classify_text',
  20. 'pop_range_text'
  21. ];
  22. protected static function init()
  23. {
  24. self::afterInsert(function ($row) {
  25. $pk = $row->getPk();
  26. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  27. });
  28. }
  29. public function getShowTypeList()
  30. {
  31. return ['0' => __('全部用户'),'1' => __('未付费'),'2' => __('普通付费'),'3' => __('年费会员')];
  32. }
  33. public function getTypeList()
  34. {
  35. return ['0' => __('书币充值'),'1' => __('VIP充值'),'2'=>__('书币赠送')];
  36. }
  37. public function getClassifyList()
  38. {
  39. return ['0' => __('微信充值'),'1' => __('支付宝充值')];
  40. }
  41. public function getStatusList()
  42. {
  43. return ['1' => __('开启'),'2' => __('关闭')];
  44. }
  45. public function getPopRangeList()
  46. {
  47. return ['0' => __('最近阅读'),'1' => __('男生'),'2' => __('女生'),'3' => __('个人中心'),'4' => __('分类'),'5' => __('免费'),'6' => __('完本')];
  48. }
  49. public function getShowTypeTextAttr($value, $data)
  50. {
  51. $value = $value ? $value : $data['show_type'];
  52. $list = $this->getShowTypeList();
  53. return isset($list[$value]) ? $list[$value] : '';
  54. }
  55. public function getClassifyTextAttr($value, $data)
  56. {
  57. $value = $value ? $value : $data['classify'];
  58. $list = $this->getClassifyList();
  59. return isset($list[$value]) ? $list[$value] : '';
  60. }
  61. public function getTypeTextAttr($value, $data)
  62. {
  63. $value = $value ? $value : $data['type'];
  64. $list = $this->getTypeList();
  65. return isset($list[$value]) ? $list[$value] : '';
  66. }
  67. public function getStatusTextAttr($value, $data)
  68. {
  69. $value = $value ? $value : $data['status'];
  70. $list = $this->getStatusList();
  71. return isset($list[$value]) ? $list[$value] : '';
  72. }
  73. public function getPopRangeTextAttr($value, $data)
  74. {
  75. $value = $value ? $value : $data['pop_range'];
  76. $valueArr = explode(',', $value);
  77. $list = $this->getPopRangeList();
  78. return implode(',', array_intersect_key($list, array_flip($valueArr)));
  79. }
  80. protected function setPopRangeAttr($value)
  81. {
  82. return is_array($value) ? implode(',', $value) : $value;
  83. }
  84. /**
  85. * 获取基本信息
  86. * @param $id
  87. * @return array
  88. * @throws \think\Exception
  89. */
  90. public function getInfo($id)
  91. {
  92. $row = [];
  93. $key = 'R-I:'.$id;
  94. $redis = Redis::instance();
  95. if ($redis->exists($key)) {
  96. $redis_key_type = $redis->type($key);
  97. if($redis_key_type == \Redis::REDIS_STRING){
  98. $row = [];
  99. }else{
  100. $row = $redis->hGetAll($key);
  101. }
  102. } else {
  103. $rowModel = model('Resource')->where('id', $id)->find();
  104. if ($rowModel) {
  105. $row = $rowModel->toArray();
  106. $redis->hMSet($key, $row);
  107. $redis->expire($key, 600);
  108. } else {
  109. $redis->set($key, 'null', 600);
  110. }
  111. }
  112. return $row;
  113. }
  114. }