Category.php 2.0 KB

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