ListController.class.php
2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* Created by PhpStorm.
* User: liyifei2012it
* Date: 17/4/11
* Time: 16:31
*/
namespace Apicp\Controller\NewsClass;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Service\ClassService;
class ListController extends \Apicp\Controller\AbstractController
{
/**
* List
* @desc 分类列表
* @param int is_open 获取的分类启禁用类型(1=禁用,2=启用)
* @return array
* array(
* 'list' => array(
* array(
* 'class_id' => 1, // 分类ID
* 'class_name' => '分类1', // 分类名称
* 'is_open' => 1, // 启用分类(1=禁用,2=启用)
* 'order' => 1, // 排序
* 'child' => array( // 子分类
* array(
* 'class_id' => 2, // 分类ID
* 'class_name' => '分类2', // 分类名称
* 'is_open' => 1, // 启用分类(1=禁用,2=启用)
* 'order' => 1, // 排序
* )
* )
* )
* )
* )
*/
public function Index_post()
{
// 验证规则
$rules = [
'is_open' => 'integer',
];
// 验证数据
$validate = new PackageValidate($rules, [], array_keys($rules));
$postData = $validate->postData;
$classServ = new ClassService();
// 排序规则
$order_option = ['`order`' => 'asc'];
// 取一级分类
$classList = $classServ->list_by_conds(['parent_id' => 0], [], $order_option);
if (empty($classList)) {
return $this->_result = ['list' => []];
}
// 取二级分类
if (isset($postData['is_open']) && in_array($postData['is_open'], [Constant::CLASS_IS_OPEN_FALSE, Constant::CLASS_IS_OPEN_TRUE])) {
$conds = [
'parent_id > ?' => 0,
'is_open' => $postData['is_open'],
];
$childList = $classServ->list_by_conds($conds, [], $order_option);
} else {
$childList = $classServ->list_by_conds(['parent_id > ?' => 0], [], $order_option);
}
// 组合数据
foreach ($classList as $k => $v) {
if ($childList) {
foreach ($childList as $index => $info) {
if ($v['class_id'] == $info['parent_id']) {
$classList[$k]['child'][] = $info;
unset($childList[$index]);
}
}
}
}
$this->_result = ['list' => $classList];
}
}