Category.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. /**
  5. * 分类模型
  6. */
  7. class Category Extends Model
  8. {
  9. // 开启自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. // 追加属性
  15. protected $append = [
  16. 'type_text',
  17. 'flag_text',
  18. ];
  19. /**
  20. * 读取分类类型
  21. * @return array
  22. */
  23. public static function getTypeList()
  24. {
  25. $typelist = config('site.categorytype');
  26. return $typelist;
  27. }
  28. public function getTypeTextAttr($value, $data)
  29. {
  30. $value = $value ? $value : $data['type'];
  31. $list = $this->getTypeList();
  32. return isset($list[$value]) ? $list[$value] : '';
  33. }
  34. public function getFlagList()
  35. {
  36. return ['hot' => __('Hot'), 'index' => __('Index'), 'recommend' => __('Recommend')];
  37. }
  38. public function getFlagTextAttr($value, $data)
  39. {
  40. $value = $value ? $value : $data['flag'];
  41. $valueArr = explode(',', $value);
  42. $list = $this->getFlagList();
  43. return implode(',', array_intersect_key($list, array_flip($valueArr)));
  44. }
  45. /**
  46. * 读取分类列表
  47. * @param string $type 指定类型
  48. * @param string $status 指定状态
  49. * @return array
  50. */
  51. public static function getCategoryArray($type = NULL, $status = NULL)
  52. {
  53. $list = collection(self::where(function($query) use($type, $status) {
  54. if (!is_null($type))
  55. {
  56. $query->where('type', '=', $type);
  57. }
  58. if (!is_null($status))
  59. {
  60. $query->where('status', '=', $status);
  61. }
  62. })->order('weigh', 'desc')->select())->toArray();
  63. return $list;
  64. }
  65. }