UserController.php
20.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/11/13
* Time: 17:04
*/
namespace api\index\controller;
use cmf\controller\RestBaseController;
use think\Cache;
use think\cache\driver\Redis;
use think\Validate;
use think\Db;
/**
* @title 个人中心
* @description
*/
class UserController extends RestBaseController
{
/**
* @title 登录注册
* @description 接口说明
* @author Guosheng
* @url /index/User/login
* @method POST
*
* @header name:XX-Token require:1 default: desc:token
*
* @param name:phone type:int require:1 default: other: desc:手机号
* @param name:code type:string require:1 default: other: desc:验证码
*
*/
public function login()
{
$user_id = $this->getUserId();
$param = $this->request->param();
$param['create_time'] = time();
$param['user_id'] = $user_id;
$validate = new Validate([
'phone' => 'require|max:11',
'code'=>'require'
]);
if (!$validate->check($param)) {
$this->error(['code'=>40005,'msg'=>$validate->getError()]);
}
$code = Cache::get($param['phone']);
if($code == $param['code']){
$data = Db::name('integral')
->insert($param);
if($data){
$this->success('SUCCESS');
}
}else{
$this->error(['code'=>40002,'msg'=>'验证码错误或者失效,请重新发送']);
}
}
/**
* @title 获取验证码
* @description
* @author Guosheng
* @url /index/User/getcode
* @method POST
*
* @param name:phone type:string require:1 other: desc:手机号码
*
* @return code:验证码
*/
public function getcode()
{
$phone = $this->request->param('phone');
if (empty($phone)) {
$this->error(['code' => 40005, 'msg' => '缺少必要参数']);
}
if (!preg_match('/^1[0-9]{10}$/', $phone)) {
$this->error(['code' => 40005, 'msg' => "请输入正确的手机格式!"]);
}
//生成验证码
$number = generateCode();
//发送验证码
$data = array(
'content' => "【垃圾回收变现】您的验证码是:" . $number."十分钟之内有效",//短信内容
'mobile' => $phone,//手机号码
'productid' => '676767',//产品id
'xh' => ''//小号
);
$result = send_sms($data);
if (substr($result, 0, strpos($result, ',')) != "1") {
$this->error(['code' => 42001, 'msg' => $result]);
}
Cache::set($phone,$number,600);
// $code = Db::name('code')
// ->where('phone',$phone)
// ->find();
// if (empty($code)) {
// $arr['phone'] = $phone;
// $arr['create_time'] = time();
// $arr['expire_time'] = time() + 60;
// $arr['code'] = $number;
// $result = Db::name('code')
// ->insertGetId($arr);
// } else {
// $arr['code'] = $number;
// $arr['update_time'] = time();
// $arr['expire_time'] = time() + 60;
// $result = Db::name('code')
// ->where('phone',$phone)
// ->update($arr);
// }
// if (empty($result)) {
// $this->error(['code' => 40006, 'msg' => 'sql执行失败']);
// }
$this->success('SUCCESS', ['code' => $number]);
}
/**
* @title 判断用户身份
* @description 接口说明
* @author Guosheng
* @url /index/User/judge
* @method POST
*
* @header name:XX-Token require:1 default: desc:token
*
* @return is_set:0否 1是
*/
public function judge()
{
$user_id = $this->getUserId();
$data = Db::name('user')
->where('id',$user_id)
->field('id,is_set')
->find();
$this->success('success',$data['is_set']);
}
/**
* @title 我的首页
* @description 接口说明
* @author Guosheng
* @url /index/User/userInfo
* @method POST
*
* @header name:XX-Token require:1 default: desc:token
*
* @return user_nickname:昵称
* @return avatar:头像
* @return now:当前积分
* @return num:参与次数
* @return add_total:累计积分
*
*/
public function userInfo()
{
$user_id = $this->getUserId();
$info = Db::name('integral')
->alias('a')
->join('user b','a.user_id = b.id')
->field('a.*,b.user_nickname,b.avatar')
->where('a.user_id',$user_id)
->find();
if(empty($info)){
$this->error(['code'=>40006,'msg'=>'您还没有注册,请先注册!']);
}
$this->success('SUCCESS',$info);
}
/**
* @title 充值积分列表
* @description 接口说明
* @author Guosheng
* @url /index/User/pay
* @method POST
*
* @header name:XX-Token require:1 default: desc:token
*
* @return integral:积分
* @return money:金额
*
*/
public function pay(){
$user_id = $this->getUserId();
$data = Db::name('pay')
->where('delete_time',0)
->select();
$this->success('SUCCESS',$data);
}
/**
* @title 提现金额列表
* @description 接口说明
* @author Guosheng
* @url /index/User/money
* @method POST
*
* @header name:XX-Token require:1 default: desc:token
*
* @return integral:积分
* @return money:金额
*
*/
public function money(){
$user_id = $this->getUserId();
$data = Db::name('money')
->where('delete_time',0)
->select();
$this->success('SUCCESS',$data);
}
/**
* @title 我的回收订单
* @description 我的回收订单
* @author Guosheng
* @url /index/User/recycleorder
* @method POST
*
* @header name:XX-Token require:1 default: desc:token
*
* @param name:status require:1 other desc: 订单状态(不传或者为0是 已预约,1已接单,2已完成)
*
* @return id:订单ID
* @return num:订单号
* @return rgoods_name:商品名称
* @return create_time:创建时间
* @return sub_time:预约时间
*
*/
public function recycleorder()
{
$user_id = $this->getUserId();
$status = $this->request->param('status',0,'intval');
if(empty($status) || $status == 0){
//已预约的回收订单
$data = Db::name('subscribe')
->where('user_id',$user_id)
->where('status',0)
->field('id,num,create_time,sub_time,attr_id')
->select();
$res = $this->r_foreach($data);
$this->success('SUCCESS',$res);
}elseif ($status == 1){
//已接单的回收订单
$data = Db::name('subscribe')
->where('user_id',$user_id)
->where('status',1)
->field('id,num,create_time,sub_time,attr_id')
->select();
$res = $this->r_foreach($data);
$this->success('SUCCESS',$res);
}elseif ($status == 2){
//已完成的回收订单
$data = Db::name('subscribe')
->where('user_id',$user_id)
->where('status',2)
->field('id,num,create_time,sub_time,attr_id')
->select();
$res = $this->r_foreach($data);
$this->success('SUCCESS',$res);
}
}
//封装回收订单循环逻辑
public function r_foreach($res){
foreach ($res as &$v){
$v['rgoods_name'] = [];
$v['attr_id'] = explode(',',$v['attr_id']);
foreach ($v['attr_id'] as &$v1){
$v1 = Db::name('attr')
->alias('a')
->join('rgoods r','a.rgoods_id=r.id')
->field('r.rgoods_name')
->where('id',$v1)
->find();
array_push($v['rgoods_name'],$v1);
}
}
return $res;
}
/**
* @title 取消回收订单
* @description 取消回收订单
* @author Guosheng
* @url /index/User/cancleorder
* @method POST
*
* @header name:XX-Token require:1 default: desc:token
*
* @param name:order_id require:1 other desc: 订单id
*
*
*/
public function cancleorder(){
$id = $this->request->param('order_id');
if(empty($id)){
$this->error(['code'=>41006,'msg'=>'缺少必要参数']);
}
$data = Db::name('subscribe')
->where('id',$id)
->delete();
if($data){
$this->success('SUCCESS');
}else{
$this->error('sql语句执行失败');
}
}
/**
* @title 回收订单详情
* @description
* @author GuoSheng
* @url /index/Recycle/orderDetail
* @method GET
*
* @header name:XX-Token require:1 default: desc:token
*
* @param name:order_id type:int require:1 default: desc:回收订单ID
*
* @return status:状态(0已预约,1已接单,2已完成)
* @return num:订单号
* @return create_time:下单时间
* @return sub_time:预约时间
* @return image:回收物品图片
* @return content:留言备注
* @return attr_id:[attr_name:属性名称 rgoods_name:商品名称]
* @return serve:服务费
*
*/
public function orderDetail()
{
$user_id = $this->getUserId();
//接收订单ID
$id = $this->request->param('order_id');
if(empty($id)){
$this->error(['code'=>41006,'msg'=>'缺少必要参数']);
}
$res = Db::name('subscribe')
->where('id',$id)
->find();
$data['status'] = $res['status'];
$data['num'] = $res['num'];
$data['create_time'] = date('Y-m-d H:i:s',$res['create_time']);
$data['sub_time'] = date('Y-m-d H:i:s',$res['sub_time']);
$data['recycle_id'] = Db::name('recycle')->where('id',$res['recycle_id'])->column('address');
$data['attr_id'] = explode(',',$res['attr_id']);
foreach ($data['attr_id'] as &$v){
$v = Db::name('attr')
->alias('a')
->join('rgoods_id r','a.rgoods_id = r.id')
->where('a.id',$v)
->field('a.attr_name,r.rgoods_name')
->find();
}
$data['image'] = explode(',',$res['image']);
foreach($data['image'] as &$vo){
$vo = cmf_get_image_url($vo);
}
$data['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($res['content']));
$data['serve'] = Db::name('recyclefee')->where('id',1)->column('fee');
$this->success('SUCCESS',$data);
}
/**
* @title 我的商城订单
* @description 我的商城订单
* @author Guosheng
* @url /index/User/shoporder
* @method POST
*
* @header name:XX-Token require:1 default: desc:token
*
* @param name:status require:1 other desc: 订单状态(不传或者为0是 已兑换,1已发货,2已完成)
*
* @return id:订单ID
* @return num:订单号
* @return goods_name:商品名称
* @return create_time:创建时间
* @return num:购买数量
* @return total:总价
*
*/
public function shoporder()
{
$user_id = $this->getUserId();
$status = $this->request->param('status',0,'intval');
if(empty($status) || $status == 0){
//已兑换的商城订单
$data = Db::name('shoporder')
->where('user_id',$user_id)
->where('status',0)
->field('id,number,create_time,num,shopgoods_id,total')
->select();
$data = $this->s_foreach($data);
$this->success('SUCCESS',$data);
}elseif ($status == 1){
//已发货的商城订单
$data = Db::name('shoporder')
->where('user_id',$user_id)
->where('status',1)
->field('id,number,create_time,num,shopgoods_id,total')
->select();
$data = $this->s_foreach($data);
$this->success('SUCCESS',$data);
}elseif ($status == 2){
//已完成的商城订单
$data = Db::name('shoporder')
->where('user_id',$user_id)
->where('status',2)
->field('id,number,create_time,num,shopgoods_id,total')
->select();
$data = $this->s_foreach($data);
$this->success('SUCCESS',$data);
}
}
//封装商城订单循环逻辑
public function s_foreach($res){
foreach ($res as &$v){
$v['shopgoods_id'] = Db::name('shopgoods')
->where('id',$v)
->field('goods_name,thumbnail')
->find();
$v['thumbnail'] = cmf_get_image_preview_url($v['shopgoods_id']['thumbnail']);
}
return $res;
}
/**
* @title 商城订单详情
* @description 商城订单详情
* @author GuoSheng
* @url /index/User/sorderDetail
* @method GET
*
* @header name:XX-Token require:1 default: desc:token
*
* @param name:order_id type:int require:1 default: desc:商城订单ID
*
* @return status:状态(0已下单,1已发货,2已完成)
* @return number:订单号
* @return create_time:下单时间
* @return express_num:快递单号
* @return shoprecycle_id:收货地址
* @return thumbnail:商品图片
* @return goods_name:商品名称
* @return content:留言备注
* @return freight:运费
* @return total:价格
* @return num:购买数量
* @return total_sum:总计
* @return shopgoods_id:商品信息
*
*/
public function sorderDetail()
{
$user_id = $this->getUserId();
//接收商品订单ID
$id = $this->request->param('order_id');
if(empty($id)){
$this->error(['code'=>41006,'msg'=>'缺少必要参数']);
}
$res = Db::name('shoporder')
->where('id',$id)
->find();
$data['status'] = $res['status'];
$data['number'] = $res['number'];
$data['create_time'] = date('Y-m-d H:i:s',$res['create_time']);
$data['express_num'] = $res['express_num'];
$data['recycle_id'] = Db::name('shoprecycle')->where('id',$res['shoprecycle_id'])->column('address');
$shopgoods_id = Db::name('shopgoods')
->where('id',$res['shopgoods_id'])
->field('id,goods_name,thumbnail,freight')
->find();
$data['freight'] = $shopgoods_id['freight'];
$data['total'] = $res['total'];
$data['total_sum'] = $data['total'] + $data['freight'];
$data['thumbnail'] = cmf_get_image_preview_url($shopgoods_id['thumbnail']);
$data['goods_name'] = $shopgoods_id['goods_name'];
$data['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($res['content']));
$this->success('SUCCESS',$data);
}
/**
* @title 我的家政预约订单
* @description 我的家政预约订单
* @author Guosheng
* @url /index/User/subhomeorder
* @method POST
*
* @header name:XX-Token require:1 default: desc:token
*
* @param name:status require:1 other desc: 订单状态(1已预约,2已接单,3已完成)
*
* @return id:订单ID
* @return num:订单号
* @return home_id:家政公司名称
* @return service_id:服务项目名称
* @return price:价格
* @return sub_time:预约时间
*
*/
public function subhomeorder()
{
$user_id = $this->getUserId();
$status = $this->request->param('status',0,'intval');
if(empty($status) || $status == 1){
//已预约的回收订单
$data = Db::name('subhome')
->where('user_id',$user_id)
->where('status',1)
->field('id,num,create_time,sub_time,home_id,service_id')
->select();
$home = Db::name('home')->where('id',$data['home_id'])->column('home_name');
$service = Db::name('service')->where('id',$data['service_id'])->field('sever_name,price')->find();
$data['service_id'] = $service['sever_name'];
$data['price'] = $service['price'];
$data['home_id'] = $home['home_name'];
$this->success('SUCCESS',$data);
}elseif ($status == 1){
//已接单的回收订单
$data = Db::name('subhome')
->where('user_id',$user_id)
->where('status',2)
->field('id,num,create_time,sub_time,home_id,service_id')
->select();
$home = Db::name('home')->where('id',$data['home_id'])->column('home_name');
$service = Db::name('service')->where('id',$data['service_id'])->field('sever_name,price')->find();
$data['service_id'] = $service['sever_name'];
$data['price'] = $service['price'];
$data['home_id'] = $home['home_name'];
$this->success('SUCCESS',$data);
}elseif ($status == 2){
//已完成的回收订单
$data = Db::name('subhome')
->where('user_id',$user_id)
->where('status',3)
->field('id,num,create_time,sub_time,home_id,service_id')
->select();
$home = Db::name('home')->where('id',$data['home_id'])->column('home_name');
$service = Db::name('service')->where('id',$data['service_id'])->field('sever_name,price')->find();
$data['service_id'] = $service['sever_name'];
$data['price'] = $service['price'];
$data['home_id'] = $home['home_name'];
$this->success('SUCCESS',$data);
}
}
/**
* @title 家政预约订单详情
* @description 家政预约订单详情
* @author GuoSheng
* @url /index/User/homeOrderDetail
* @method GET
*
* @header name:XX-Token require:1 default: desc:token
*
* @param name:order_id type:int require:1 default: desc:家政预约订单ID
*
* @return status:状态(1已预约,2已接单,3已完成)
* @return num:订单号
* @return create_time:下单时间
* @return sub_time:预约时间
* @return homeaddress_id:预约地址
* @return home_id:家政公司名称
* @return thumbnail:家政公司缩略图
* @return price:价格
* @return service_id:服务名称
* @return content:留言备注
*
*/
public function homeOrderDetail()
{
$user_id = $this->getUserId();
//接收订单ID
$id = $this->request->param('order_id');
if(empty($id)){
$this->error(['code'=>41006,'msg'=>'缺少必要参数']);
}
$res = Db::name('subhome')
->where('id',$id)
->find();
$data['status'] = $res['status'];
$data['num'] = $res['num'];
$data['create_time'] = date('Y-m-d H:i:s',$res['create_time']);
$data['sub_time'] = date('Y-m-d H:i:s',$res['sub_time']);
$homeaddress_id = Db::name('homeadress')->where('id',$res['homeaddress_id'])->column('name');
$data['homeaddress_id'] = $homeaddress_id['name'];
$home = Db::name('home')->where('id',$data['home_id'])->field('id,home_name,thumbnail')->find();
$service = Db::name('service')->where('id',$data['service_id'])->field('sever_name,price')->find();
$data['thumbnail'] = $home['thumbnail'];
$data['service_id'] = $service['sever_name'];
$data['price'] = $service['price'];
$data['home_id'] = $home['home_name'];
$data['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($res['content']));
$data['serve'] = Db::name('recyclefee')->where('id',1)->column('fee');
$this->success('SUCCESS',$data);
}
}