Category.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 static function init()
  27. {
  28. self::afterInsert(function ($row) {
  29. $row->save(['weigh' => $row['id']]);
  30. });
  31. self::beforeDelete(function (self $category){
  32. if(in_array($category['id'],array_keys(self::$cantDel))){
  33. throw_user('不允许删除系统分类');
  34. }
  35. });
  36. self::beforeUpdate(function (self $category){
  37. if(in_array($category['id'],array_keys(self::$cantDel))){
  38. $category['name']=self::$cantDel[$category['id']];
  39. $category['status']='normal';
  40. }
  41. });
  42. }
  43. public function setFlagAttr($value, $data)
  44. {
  45. return is_array($value) ? implode(',', $value) : $value;
  46. }
  47. public function goods(){
  48. return $this->hasMany(Goods::class)->where('status',1);
  49. }
  50. /**
  51. * 读取分类类型
  52. * @return array
  53. */
  54. public static function getTypeList()
  55. {
  56. $typeList = config('site.categorytype');
  57. foreach ($typeList as $k => &$v) {
  58. $v = __($v);
  59. }
  60. return $typeList;
  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 getFlagList()
  69. {
  70. return ['hot' => __('Hot'), 'index' => __('Index'), 'recommend' => __('Recommend')];
  71. }
  72. public function getFlagTextAttr($value, $data)
  73. {
  74. $value = $value ? $value : $data['flag'];
  75. $valueArr = explode(',', $value);
  76. $list = $this->getFlagList();
  77. return implode(',', array_intersect_key($list, array_flip($valueArr)));
  78. }
  79. /**
  80. * 读取分类列表
  81. * @param string $type 指定类型
  82. * @param string $status 指定状态
  83. * @return array
  84. */
  85. public static function getCategoryArray($type = null, $status = null,$with=[])
  86. {
  87. $list = collection(self::where(function ($query) use ($type, $status) {
  88. if (!is_null($type)) {
  89. $query->where('type', '=', $type);
  90. }
  91. if (!is_null($status)) {
  92. $query->where('status', '=', $status);
  93. }
  94. })->order('weigh', 'desc')->with($with)->select())->toArray();
  95. foreach ($list as $k=>$v){
  96. if(!empty($v['goods'])){
  97. foreach ($v['goods'] as $ko=>$goods){
  98. $list[$k]['goods'][$ko]['sku'] = GoodsSku::where('goods_id',$goods['id'])->field('id,name')->select();
  99. }
  100. }
  101. }
  102. return $list;
  103. }
  104. public static function mall($with=[]){
  105. return self::getCategoryArray('mall','normal',$with);
  106. }
  107. }