Category.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. protected static function init()
  20. {
  21. self::afterInsert(function ($row) {
  22. $row->save(['weigh' => $row['id']]);
  23. });
  24. }
  25. public function setFlagAttr($value, $data)
  26. {
  27. return is_array($value) ? implode(',', $value) : $value;
  28. }
  29. /**
  30. * 读取分类类型
  31. * @return array
  32. */
  33. public static function getTypeList()
  34. {
  35. $typeList = config('site.categorytype');
  36. $typeList = array (
  37. 'all'=> '总分类'
  38. // 'chanpinpaihang' => '首页产品排行分类',
  39. // 'gerenxian' => '首页个人险分类',
  40. // 'qiyexian' => '首页企业险分类',
  41. // 'caichanxian' => '首页财产险分类',
  42. // 'jibingyiwaiyiliao' => '首页疾病意外医疗险推荐排行榜分类',
  43. // 'yiwaishanghaixian' => '首页意外伤害险推荐排行榜分类',
  44. // 'allbaoxian' => '导出保险分类',
  45. // 'tuijianchaxunyijifenlei' => '推荐查询分类'
  46. );
  47. foreach ($typeList as $k => &$v) {
  48. $v = __($v);
  49. }
  50. return $typeList;
  51. }
  52. public function getTypeTextAttr($value, $data)
  53. {
  54. $value = $value ? $value : $data['type'];
  55. $list = $this->getTypeList();
  56. return isset($list[$value]) ? $list[$value] : '';
  57. }
  58. public function getFlagList()
  59. {
  60. return ['hot' => __('Hot'), 'index' => __('Index'), 'recommend' => __('Recommend')];
  61. }
  62. public function getFlagTextAttr($value, $data)
  63. {
  64. $value = $value ? $value : $data['flag'];
  65. $valueArr = explode(',', $value);
  66. $list = $this->getFlagList();
  67. return implode(',', array_intersect_key($list, array_flip($valueArr)));
  68. }
  69. /**
  70. * 读取分类列表
  71. * @param string $type 指定类型
  72. * @param string $status 指定状态
  73. * @return array
  74. */
  75. public static function getCategoryArray($type = null, $status = null)
  76. {
  77. $list = collection(self::where(function ($query) use ($type, $status) {
  78. if (!is_null($type)) {
  79. $query->where('type', '=', $type);
  80. }
  81. if (!is_null($status)) {
  82. $query->where('status', '=', $status);
  83. }
  84. })->order('weigh', 'desc')->select())->toArray();
  85. return $list;
  86. }
  87. }