Category.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\common\model;
  3. use app\common\service\InstallService;
  4. use think\Model;
  5. use traits\model\SoftDelete;
  6. /**
  7. * 分类模型
  8. */
  9. class Category extends Model
  10. {
  11. use SoftDelete;
  12. // 开启自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'createtime';
  16. protected $updateTime = 'updatetime';
  17. // 追加属性
  18. protected $append = [
  19. ];
  20. const C_ZJ=7;
  21. const C_PEIJIAN=8;
  22. public static $cantDel=[
  23. self::C_ZJ=>'支架',
  24. self::C_PEIJIAN=>'配件',
  25. ];
  26. protected $type=[
  27. 'img_left'=>'array',
  28. ];
  29. protected static function init()
  30. {
  31. self::afterInsert(function ($row) {
  32. $row->save(['weigh' => $row['id']]);
  33. });
  34. self::beforeDelete(function (self $category){
  35. if(in_array($category['id'],array_keys(self::$cantDel))){
  36. throw_user('不允许删除系统分类');
  37. }
  38. });
  39. self::beforeUpdate(function (self $category){
  40. if(in_array($category['id'],array_keys(self::$cantDel))){
  41. $category['name']=self::$cantDel[$category['id']];
  42. $category['status']='normal';
  43. }
  44. });
  45. }
  46. public function setFlagAttr($value, $data)
  47. {
  48. return is_array($value) ? implode(',', $value) : $value;
  49. }
  50. public function goods(){
  51. return $this->hasMany(Goods::class)->where('status',1);
  52. }
  53. /**
  54. * 读取分类类型
  55. * @return array
  56. */
  57. public static function getTypeList()
  58. {
  59. $typeList = config('site.categorytype');
  60. foreach ($typeList as $k => &$v) {
  61. $v = __($v);
  62. }
  63. return $typeList;
  64. }
  65. public function getTypeTextAttr($value, $data)
  66. {
  67. $value = $value ? $value : $data['type'];
  68. $list = $this->getTypeList();
  69. return isset($list[$value]) ? $list[$value] : '';
  70. }
  71. public function getFlagList()
  72. {
  73. return ['hot' => __('Hot'), 'index' => __('Index'), 'recommend' => __('Recommend')];
  74. }
  75. public function getFlagTextAttr($value, $data)
  76. {
  77. $value = $value ? $value : $data['flag'];
  78. $valueArr = explode(',', $value);
  79. $list = $this->getFlagList();
  80. return implode(',', array_intersect_key($list, array_flip($valueArr)));
  81. }
  82. /**
  83. * 读取分类列表
  84. * @param string $type 指定类型
  85. * @param string $status 指定状态
  86. * @return array
  87. */
  88. public static function getCategoryArray($type = null, $status = null,$with=[])
  89. {
  90. $list = self::with($with)->where(function ($query) use ($type, $status) {
  91. if (!is_null($type)) {
  92. $query->where('type', '=', $type);
  93. }
  94. if (!is_null($status)) {
  95. $query->where('status', '=', $status);
  96. }
  97. })->order('weigh', 'desc')->select()->toArray();
  98. return $list;
  99. }
  100. public static function mall($with=[]){
  101. return self::getCategoryArray('mall','normal',$with);
  102. }
  103. }