Category.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. public function setFlagAttr($value, $data)
  20. {
  21. return is_array($value) ? implode(',', $value) : $value;
  22. }
  23. /**
  24. * 读取分类类型
  25. * @return array
  26. */
  27. public static function getTypeList()
  28. {
  29. $typeList = config('site.categorytype');
  30. foreach ($typeList as $k => &$v)
  31. {
  32. $v = __($v);
  33. }
  34. return $typeList;
  35. }
  36. public function getTypeTextAttr($value, $data)
  37. {
  38. $value = $value ? $value : $data['type'];
  39. $list = $this->getTypeList();
  40. return isset($list[$value]) ? $list[$value] : '';
  41. }
  42. public function getFlagList()
  43. {
  44. return ['hot' => __('Hot'), 'index' => __('Index'), 'recommend' => __('Recommend')];
  45. }
  46. public function getFlagTextAttr($value, $data)
  47. {
  48. $value = $value ? $value : $data['flag'];
  49. $valueArr = explode(',', $value);
  50. $list = $this->getFlagList();
  51. return implode(',', array_intersect_key($list, array_flip($valueArr)));
  52. }
  53. /**
  54. * 读取分类列表
  55. * @param string $type 指定类型
  56. * @param string $status 指定状态
  57. * @return array
  58. */
  59. public static function getCategoryArray($type = NULL, $status = NULL)
  60. {
  61. $list = collection(self::where(function($query) use($type, $status) {
  62. if (!is_null($type))
  63. {
  64. $query->where('type', '=', $type);
  65. }
  66. if (!is_null($status))
  67. {
  68. $query->where('status', '=', $status);
  69. }
  70. })->order('weigh', 'desc')->select())->toArray();
  71. return $list;
  72. }
  73. }