Index.php
8.4 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
/**
* 首页接口
*/
class Index extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
/**
* @ApiTitle (首页项目列表)
* @ApiSummary (农场项目列表)
* @ApiMethod (POST)
* @ApiRoute (/api/index/index)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="type", type="int", required=false, description="类型(不传或者1为已收获2将收获3培育中)")
* @ApiParams (name="page", type="inter", required=false, description="当前页(默认1)")
* @ApiParams (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
*
* @ApiReturn({
"code": 1,
"msg": "SUCCESS",
"time": "1553839125",
"data": {
"id":项目ID,
"farm_id":农场ID
'project_name'://项目名称,
"image"://项目图片
'farm_name'://农场名称,
"avatar"://头像
'look_num'://浏览量,
"good_num"://点赞量
'thumbnail'://图片
"address"://项目地址
"status"://是否点过赞(1已经点过2没有点过)
"hong_status"://0未授权1已经授权
'createtime'://创建时间
}
})
*/
public function index()
{
$token = $this->request->header('token');
$type = $this->request->param('type');
$page = $this->request->param('page',1,'intval');
$pageNum = $this->request->param('pageNum',6,'intval');
//判断用户是否登录
if($token){
$user_id = $this->getUserId();
if(empty($type) || $type == 1){
$data = Db::name('project')
->where('status',2)
->field('id,farm_id,project_name,address,image,good_num')
->order('good_num desc,id desc')
->page($page,$pageNum)
->select();
foreach ($data as &$v){
$v['image'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$v['image'];
$zan = Db::name('good')
->where('project_id',$v['id'])
->where('user_id',$user_id)
->find();
if($zan){
$v['statu'] = 1;
}else{
$v['statu'] = 2;
}
$farm = Db::name('farm')
->alias('a')
->join('user b','a.user_id = b.id')
->field('a.name,b.avatar')
->where('a.id',$v['farm_id'])
->find();
$v['avatar'] = $farm['avatar'];
$v['farm_name'] = $farm['name'];
$v['hong_status'] = 1;
}
$this->success('success',$data);
}elseif ($type == 2){
$data = Db::name('project')
->where('status',1)
->field('id,farm_id,project_name,address,image,good_num')
->order('good_num desc,id desc')
->page($page,$pageNum)
->select();
foreach ($data as &$v){
$v['image'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$v['image'];
$zan = Db::name('good')
->where('project_id',$v['id'])
->where('user_id',$user_id)
->find();
if($zan){
$v['statu'] = 1;
}else{
$v['statu'] = 2;
}
$farm = Db::name('farm')
->alias('a')
->join('user b','a.user_id = b.id')
->field('a.name,b.avatar')
->where('a.id',$v['farm_id'])
->find();
$v['avatar'] = $farm['avatar'];
$v['farm_name'] = $farm['name'];
$v['hong_status'] = 1;
}
$this->success('success',$data);
}elseif ($type == 3){
$data = Db::name('project')
->where('status',0)
->field('id,farm_id,project_name,address,image,good_num')
->order('good_num desc,id desc')
->page($page,$pageNum)
->select();
foreach ($data as &$v){
$v['image'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$v['image'];
$zan = Db::name('good')
->where('project_id',$v['id'])
->where('user_id',$user_id)
->find();
if($zan){
$v['statu'] = 1;
}else{
$v['statu'] = 2;
}
$farm = Db::name('farm')
->alias('a')
->join('user b','a.user_id = b.id')
->field('a.name,b.avatar')
->where('a.id',$v['farm_id'])
->find();
$v['avatar'] = $farm['avatar'];
$v['farm_name'] = $farm['name'];
$v['hong_status'] = 1;
}
$this->success('success',$data);
}else{
$this->error('类型错误');
}
}else{
if(empty($type) || $type == 1){
$data = Db::name('project')
->where('status',2)
->field('id,farm_id,project_name,address,image,good_num')
->order('good_num desc,id desc')
->page($page,$pageNum)
->select();
foreach ($data as &$v){
$v['image'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$v['image'];
$farm = Db::name('farm')
->alias('a')
->join('user b','a.user_id = b.id')
->field('a.name,b.avatar')
->where('a.id',$v['farm_id'])
->find();
$v['avatar'] = $farm['avatar'];
$v['farm_name'] = $farm['name'];
$v['hong_status'] = 0;
}
$this->success('success',$data);
}elseif ($type == 2){
$data = Db::name('project')
->where('status',1)
->field('id,farm_id,project_name,address,image,good_num')
->order('good_num desc,id desc')
->page($page,$pageNum)
->select();
foreach ($data as &$v){
$v['image'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$v['image'];
$farm = Db::name('farm')
->alias('a')
->join('user b','a.user_id = b.id')
->field('a.name,b.avatar')
->where('a.id',$v['farm_id'])
->find();
$v['avatar'] = $farm['avatar'];
$v['farm_name'] = $farm['name'];
$v['hong_status'] = 0;
}
$this->success('success',$data);
}elseif ($type == 3){
$data = Db::name('project')
->where('status',0)
->field('id,farm_id,project_name,address,image,good_num')
->order('good_num desc,id desc')
->page($page,$pageNum)
->select();
foreach ($data as &$v){
$v['image'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$v['image'];
$farm = Db::name('farm')
->alias('a')
->join('user b','a.user_id = b.id')
->field('a.name,b.avatar')
->where('a.id',$v['farm_id'])
->find();
$v['avatar'] = $farm['avatar'];
$v['farm_name'] = $farm['name'];
$v['hong_status'] = 0;
}
$this->success('success',$data);
}else{
$this->error('类型错误');
}
}
}
}