ClientConfig.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Redis;
  4. use think\Model;
  5. class ClientConfig extends Model
  6. {
  7. // 表名
  8. protected $table = 'client_config';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. // 追加属性
  15. protected $append = [
  16. 'fun_type_text',
  17. 'type_text',
  18. 'user_pay_type_text',
  19. 'status_text',
  20. 'position_text',
  21. ];
  22. public function getFunTypeList()
  23. {
  24. return ['1' => __('Fun_type 1'),'2' => __('Fun_type 2'),'3' => __('Fun_type 3')];
  25. }
  26. public function getTypeList()
  27. {
  28. return ['1' => __('Type 1'),'2' => __('Type 2'),'3' => __('Type 3'),'4' => __('Type 4')];
  29. }
  30. public function getUserPayTypeList()
  31. {
  32. return ['0' => __('User_pay_type 0'),'1' => __('User_pay_type 1'),'2' => __('User_pay_type 2')];
  33. }
  34. public function getStatusList()
  35. {
  36. return ['1' => __('Status 1'),'0' => __('Status 0')];
  37. }
  38. public function getPositionList()
  39. {
  40. return ['1' => __('书架'),'2' => __('个人中心'), '3' => __('书城首页')];
  41. }
  42. public function getPositionTextAttr($value, $data)
  43. {
  44. $value = $value ? $value : $data['position'];
  45. $arr = array_filter(explode(',', $value));
  46. if (!empty($arr)) {
  47. $list = $this->getPositionList();
  48. $str = '';
  49. foreach ($arr as $row) {
  50. $str .= $list[$row]. ',';
  51. }
  52. $str = trim($str, ',');
  53. }
  54. return isset($str) ? $str : '';
  55. }
  56. public function getFunTypeTextAttr($value, $data)
  57. {
  58. $value = $value ? $value : $data['fun_type'];
  59. $list = $this->getFunTypeList();
  60. return isset($list[$value]) ? $list[$value] : '';
  61. }
  62. public function getTypeTextAttr($value, $data)
  63. {
  64. $value = $value ? $value : $data['type'];
  65. $list = $this->getTypeList();
  66. return isset($list[$value]) ? $list[$value] : '';
  67. }
  68. public function getUserPayTypeTextAttr($value, $data)
  69. {
  70. $value = $value ? $value : $data['user_pay_type'];
  71. $list = $this->getUserPayTypeList();
  72. return isset($list[$value]) ? $list[$value] : '';
  73. }
  74. public function getStatusTextAttr($value, $data)
  75. {
  76. $value = $value ? $value : $data['status'];
  77. $list = $this->getStatusList();
  78. return isset($list[$value]) ? $list[$value] : '';
  79. }
  80. /**
  81. * 获取配置的内容
  82. * @param null $id
  83. */
  84. public function getOne($id = null)
  85. {
  86. $row = [];
  87. $redisKey = 'CCP:'.$id;
  88. if (Redis::instance()->exists($redisKey)) {
  89. $row = Redis::instance()->hGetAll($redisKey);
  90. } else {
  91. $row = $this->where('id', 'eq', $id)->find();
  92. if ($row) {
  93. $row = $row->toArray();
  94. Redis::instance()->hMSet($redisKey, $row);
  95. Redis::instance()->expire($redisKey, 3600);
  96. }
  97. }
  98. return $row;
  99. }
  100. /**
  101. * 删除缓存
  102. * @param null $id
  103. */
  104. public function delCache($id = null)
  105. {
  106. $redisKey = 'CCP:'.$id;
  107. return Redis::instance()->del($redisKey);
  108. }
  109. }