User.php
11.0 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Db;
use think\Validate;
use fast\Http;
/**
* 个人中心
*/
class User extends Api
{
protected $noNeedLogin = ['login'];
protected $noNeedRight = '*';
protected $uid = '';
public function _initialize()
{
parent::_initialize();
$this->uid = $this->auth->getUserId();
}
/**
* @ApiTitle (小程序登录)
* @ApiSummary (小程序登录)
* @ApiMethod (POST)
* @ApiRoute (/api/user/login)
* @ApiParams (name="code", type="string", required=true, description="小程序code")
* @ApiParams (name="nickname", type="string", required=true, description="小程序昵称")
* @ApiParams (name="avatar", type="string", required=true, description="小程序头像")
* @ApiReturn({
"code": 1,
"msg": "登录成功",
"time": "1553839125",
"data": {
"token": "677afb39-1a4f-4492-84d3-0bcf32016b8a",//token
"user_id": 27,//用户id
"createtime": 1553839125,//登录时间
"expiretime": 1556431125,//token失效时间
"expires_in": 2592000//token失效剩余时间(单位s)
"openid": 1485212522522//openid
})
*/
public function login(){
if($this->request->isPost()){
//小程序配置
$config = config('verify.raw');
//小程序传递数据,包含昵称,头像,code
$raw_data = $this->request->post();
//验证表数据
$rule = config('verify.user');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check($raw_data)) {
$this->error($validate->getError());
}
$params = [
'appid' => $config['app_id'],
'secret' => $config['secret'],
'js_code' => $raw_data['code'],
'grant_type' => 'authorization_code'
];
$result = Http::sendRequest("https://api.weixin.qq.com/sns/jscode2session", $params, 'GET');
if ($result['ret']) {
$json = (array)json_decode($result['msg'], true);
if (isset($json['openid'])) {
$result = [
'openid' => $json['openid'],
'nickname' => $raw_data['nickname'],
'avatar' => $raw_data['avatar']
];
$ret = $this->auth->login($result);
if ($ret) {
$data = $this->auth->getUserinfo();
$this->success('登录成功', $data);
}else {
$this->error($this->auth->getError());
}
} else {
$this->error("登录失败",$json);
}
}
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (首页视频、图片、精美大片入口)
* @ApiSummary (首页视频、图片、精美大片入口)
* @ApiMethod (POST)
* @ApiRoute (/api/user/index)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"time": "1571492001",
"data": {
"video_thumbnail": http://mapwww.baidu.com 视频封面图地址
"pic_thumbnail": http://mapwww.baidu.com 图片地址
"wonderful_thumbnail": http://mapwww.baidu.com 精美大片地址
}
})
*/
public function index()
{
$rule = config('verify.path');
$data = Db::name('video')
->field('id,thumbnail')
->order('id desc')
->limit('1')
->find();
$pic = Db::name('pic')
->field('id,thumbnail')
->order('id desc')
->limit('1')
->find();
$wonderful = Db::name('wonderful')
->field('id,thumbnail')
->order('id desc')
->limit('1')
->find();
$this->auth;
$res['video_thumbnail'] = $rule.$data['thumbnail'];
$res['pic_thumbnail'] = $rule.$pic['thumbnail'];
$res['wonderful_thumbnail'] = $rule.$wonderful['thumbnail'];
$this->success('SUCCESS',$res);
}
/**
* @ApiTitle (地区分类)
* @ApiSummary (地区分类)
* @ApiMethod (POST)
* @ApiRoute (/api/user/type)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
"id": 4,//分类id
"area_name": //分类名称
"thumbnail": //图片
}
})
*/
public function type()
{
$rule = config('verify.path');
$data = Db::name('type')
->field('id,area_name,thumbnail')
->order('id desc')
->select();
foreach ($data as &$v){
$v['thumbnail'] = $rule.$v['thumbnail'];
}
$this->success('SUCCESS',$data);
}
/**
* @ApiTitle (搜索页)
* @ApiSummary (搜索页)
* @ApiMethod (POST)
* @ApiRoute (/api/user/search)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="type_id", type="inter", required=false, description="视频/图片 (视频为1 图片为2 精美大片为3)")
* @ApiParams (name="keyword", type="string", required=true, description="关键字")
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
"id": 4,//视频id/图片id,
"thumbnail": //视频封面图地址/图片地址,
"video": //视频路径/精美大片视频路径,
}
})
*/
public function search()
{
$rule = config('verify.path');
$type_id = $this->request->post('type_id');
$keyword = $this->request->post('keyword');
if(!empty($keyword)){
if($type_id == 1){
$where['keyword'] = ['like',"%$keyword%"];
$where['title'] = ['like',"%$keyword%"];
$data = Db::name('video')
->where($where)
->order('id desc')
->select();
foreach ($data as &$v){
$v['thumbnail'] = $rule.$v['thumbnail'];
$v['video'] = $rule.$v['video'];
}
$this->success('SUCCESS',$data);
}elseif($type_id == 2){
$where['keyword'] = ['like',"%$keyword%"];
$where['title'] = ['like',"%$keyword%"];
$data = Db::name('pic')
->where($where)
->order('id desc')
->select();
foreach ($data as &$v){
$v['thumbnail'] = $rule.$v['thumbnail'];
}
$this->success('SUCCESS',$data);
}else{
$where['keyword'] = ['like',"%$keyword%"];
$data = Db::name('wonderful')
->where($where)
->order('id desc')
->select();
foreach ($data as &$v){
$v['thumbnail'] = $rule.$v['thumbnail'];
$v['video'] = $rule.$v['video'];
}
$this->success('SUCCESS',$data);
}
}else{
$this->error(['code'=>2,'msg'=>'请输入关键字']);
}
}
/**
* @ApiTitle (图片列表)
* @ApiSummary (图片列表)
* @ApiMethod (POST)
* @ApiRoute (/api/user/pic)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="type_id", type="inter", required=false, description="地区ID")
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
"id": 4,//图片id,
"thumbnail": //缩略图,
}
})
*/
public function pic()
{
$rule = config('verify.path');
$type_id = $this->request->post('type_id');
if(empty($type_id)){
$data = Db::name('pic')
->field('id,thumbnail')
->order('id desc')
->select();
foreach ($data as &$v){
$v['thumbnail'] = $rule.$v['thumbnail'];
}
$this->success('SUCCESS',$data);
}else{
$type = ','.$type_id.',';
$where['type_ids'] = ['like',"%$type%"];
$data = Db::name('pic')
->where($where)
->field('id,thumbnail')
->order('id desc')
->select();
foreach ($data as &$v){
$v['thumbnail'] = $rule.$v['thumbnail'];
}
$this->success('SUCCESS',$data);
}
}
/**
* @ApiTitle (图片详情)
* @ApiSummary (图片详情)
* @ApiMethod (POST)
* @ApiRoute (/api/user/picdetail)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="pic_id", type="inter", required=true, description="图片ID")
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
"id": 4,//图片id,
"thumbnail": //缩略图,
"water_url": //小样带水印的图片地址,
"url": //高清地址,
"price"://正常价格,
"vip_price"://vip价格,
"content"://图片介绍,
"is_vip"://是否为会员(0否 1是)
}
})
*/
public function picdetail()
{
$uid = $this->uid;
$rule = config('verify.path');
$res = Db::name('user')
->where('id',$uid)
->field('id,is_vip')
->find();
if($res['is_vip'] == 0){
$pic_id = $this->request->post('pic_id');
if(empty($pic_id)){
$this->error(['code'=>2,'msg'=>'缺少必要参数']);
}
$data = Db::name('pic')
->where('id',$pic_id)
->find();
$data['thumbnail'] = $rule.$data['thumbnail'];
$data['water_url'] = $rule.$data['water_url'];
$data['is_vip'] = 0;
$this->success('SUCCESS',$data);
}elseif($res['is_vip'] == 1){
$pic_id = $this->request->post('pic_id');
if(empty($pic_id)){
$this->error(['code'=>2,'msg'=>'缺少必要参数']);
}
$data = Db::name('pic')
->where('id',$pic_id)
->find();
$data['is_vip'] = 1;
$data['thumbnail'] = $rule.$data['thumbnail'];
$data['water_url'] = $rule.$data['water_url'];
$this->success('SUCCESS',$data);
}
}
}