123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace app\common\model;
- use app\common\library\Redis;
- use think\Model;
- class Resource extends Model
- {
- // 表名
- protected $table = 'resource';
-
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
-
- // 追加属性
- protected $append = [
- 'show_type_text',
- 'type_text',
- 'status_text',
- 'classify_text',
- 'pop_range_text'
- ];
- protected static function init()
- {
- self::afterInsert(function ($row) {
- $pk = $row->getPk();
- $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
- });
- }
-
-
- public function getShowTypeList()
- {
- return ['0' => __('全部用户'),'1' => __('未付费'),'2' => __('普通付费'),'3' => __('年费会员')];
- }
- public function getTypeList()
- {
- return ['0' => __('书币充值'),'1' => __('VIP充值'),'2'=>__('书币赠送')];
- }
- public function getClassifyList()
- {
- return ['0' => __('微信充值'),'1' => __('支付宝充值')];
- }
- public function getStatusList()
- {
- return ['1' => __('开启'),'2' => __('关闭')];
- }
- public function getPopRangeList()
- {
- return ['0' => __('最近阅读'),'1' => __('男生'),'2' => __('女生'),'3' => __('个人中心'),'4' => __('分类'),'5' => __('免费'),'6' => __('完本')];
- }
- public function getShowTypeTextAttr($value, $data)
- {
- $value = $value ? $value : $data['show_type'];
- $list = $this->getShowTypeList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getClassifyTextAttr($value, $data)
- {
- $value = $value ? $value : $data['classify'];
- $list = $this->getClassifyList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getTypeTextAttr($value, $data)
- {
- $value = $value ? $value : $data['type'];
- $list = $this->getTypeList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getStatusTextAttr($value, $data)
- {
- $value = $value ? $value : $data['status'];
- $list = $this->getStatusList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getPopRangeTextAttr($value, $data)
- {
- $value = $value ? $value : $data['pop_range'];
- $valueArr = explode(',', $value);
- $list = $this->getPopRangeList();
- return implode(',', array_intersect_key($list, array_flip($valueArr)));
- }
- protected function setPopRangeAttr($value)
- {
- return is_array($value) ? implode(',', $value) : $value;
- }
- /**
- * 获取基本信息
- * @param $id
- * @return array
- * @throws \think\Exception
- */
- public function getInfo($id)
- {
- $row = [];
- $key = 'R-I:'.$id;
- $redis = Redis::instance();
- if ($redis->exists($key)) {
- $redis_key_type = $redis->type($key);
- if($redis_key_type == \Redis::REDIS_STRING){
- $row = [];
- }else{
- $row = $redis->hGetAll($key);
- }
- } else {
- $rowModel = model('Resource')->where('id', $id)->find();
- if ($rowModel) {
- $row = $rowModel->toArray();
- $redis->hMSet($key, $row);
- $redis->expire($key, 600);
- } else {
- $redis->set($key, 'null', 600);
- }
- }
- return $row;
- }
- }
|