Category.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 栏目模型
  13. // +----------------------------------------------------------------------
  14. namespace addons\cms\model;
  15. use think\Model;
  16. /**
  17. * 模型
  18. */
  19. class Category extends Model
  20. {
  21. protected $name = 'cms_category';
  22. public function getArrparentidAttr($value, $data)
  23. {
  24. return is_array($value) ? $value : ($value ? explode(',', $value) : []);
  25. }
  26. public function getArrchildidAttr($value, $data)
  27. {
  28. return is_array($value) ? $value : ($value ? explode(',', $value) : []);
  29. }
  30. public function getSettingAttr($value, $data)
  31. {
  32. return is_array($value) ? $value : (array) json_decode($data['setting'], true);
  33. }
  34. public function setSettingAttr($value)
  35. {
  36. return is_array($value) ? json_encode($value) : $value;
  37. }
  38. public function getOrderList($modelid, $tag)
  39. {
  40. $param = app()->request->get();
  41. $fields = ModelField::where(['modelid' => $modelid, 'isorder' => 1])
  42. ->order('listorder', 'desc')
  43. ->field('id,title,name')
  44. ->select()
  45. ->toArray();
  46. if (empty($fields)) {
  47. return [];
  48. }
  49. $sort = isset($param['sort']) && in_array($param['sort'], array_map(function ($vo) {return $vo['name'];}, $fields)) ? $param['sort'] : 'default';
  50. $order = isset($param['order']) && in_array(strtolower($param['order']), ['desc', 'asc']) ? $param['order'] : '';
  51. $isDefaultSort = $sort === 'default';
  52. // 构造默认排序
  53. $orderList = [
  54. 'data' => $isDefaultSort ? $tag['order'] : "$sort $order",
  55. 'list' => [[
  56. 'title' => '默认',
  57. 'field' => 'default',
  58. 'url' => '?' . http_build_query(array_merge($param, ['sort' => 'default'])),
  59. 'active' => $isDefaultSort,
  60. 'order' => $order,
  61. ]],
  62. ];
  63. foreach ($fields as $field) {
  64. $currentField = $field['name'];
  65. $isActive = $currentField === $sort;
  66. $tempOrder = $isActive && $order === 'desc' ? 'asc' : 'desc';
  67. $orderList['list'][] = [
  68. 'title' => $field['title'],
  69. 'field' => $currentField,
  70. 'url' => '?' . http_build_query(array_merge($param, ['sort' => $currentField, 'order' => $tempOrder])),
  71. 'active' => $isActive,
  72. 'order' => $tempOrder,
  73. ];
  74. }
  75. return $orderList;
  76. }
  77. //刷新栏目索引缓存
  78. public function category_cache()
  79. {
  80. $data = Category::order("listorder DESC")->column('catdir,parentid', 'id');
  81. cms_cache("Category", $data);
  82. return $data;
  83. }
  84. }