User.php
22.9 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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
<?php
namespace app\api\controller;
use app\api\model\Tax;
use app\api\model\UserAddress;
use app\api\model\UserCoupon;
use app\api\model\UserInvitation;
use app\common\controller\Api;
use fast\Http;
use think\Config;
use think\Db;
/**
* 我的页面
*/
class User extends Api
{
// protected $noNeedLogin = ['*'];
protected $noNeedLogin = ['third'];
protected $noNeedRight = '*';
public function _initialize()
{
parent::_initialize();
if (!Config::get('fastadmin.usercenter')) {
$this->error(__('User center already closed'));
}
}
/**
* 会员中心
* @ApiReturn (
* data:{
* nickname 昵称
* avatar 头像
* pay 待支付
* wait_send 待发货
* wait_collect 待收货
* wait_comment 待评价
* }
* )
*/
public function index()
{
$data = [];
$data['nickname'] = $this->auth->nickname;
$data['avatar'] = cdnurl($this->auth->avatar,true);
$data['mobile'] = $this->auth->mobile;
$data['pay'] = Db::name('litestore_order')->where('user_id',$this->auth->id)->where('pay_status','10')->count();
$data['wait_send'] = Db::name('litestore_order')->where('user_id',$this->auth->id)->where('freight_status','10')->count();
$data['wait_collect'] = Db::name('litestore_order')->where('user_id',$this->auth->id)->where('receipt_status','10')->count();
$data['wait_comment'] = Db::name('litestore_order')->where('user_id',$this->auth->id)->where('receipt_status','20')->count();
$this->success('会员中心', ['welcome' => $this->auth->nickname]);
}
/**
* 修改会员个人信息
*
* @ApiMethod (POST)
* @param string $avatar 头像地址
* @param string $mobile 联系方式
* @param string $nickname 昵称
*/
public function profile()
{
$user = $this->auth->getUser();
$mobile = $this->request->post('mobile');
$nickname = $this->request->post('nickname');
$avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
if ($mobile) {
$user->mobile = $mobile;
}
if ($nickname) {
$user->nickname = $nickname;
}
if ($avatar) {
$user->avatar = $avatar;
}
$user->save();
$this->success();
}
/**
* 第三方登录
*
* @ApiMethod (POST)
* @param string $code Code码
* @param string $nickname 微信昵称
* @param string $avatar 微信头像
*/
public function third()
{
$code = $this->request->post('code');
$nickname = $this->request->post('nickname');
$avatar = $this->request->post('avatar');
if (!$code || !$nickname || !$avatar){
$this->error('后台所需参数缺失请完善参数');
}
$param = [];
$param['js_code'] = $code;
$param['grant_type'] = 'authorization_code';
$param['secret'] = Config::get('site.secret');
$param['appid'] = Config::get('site.appid');
$wxapi = Http::get('https://api.weixin.qq.com/sns/jscode2session',$param);//请求openid
$wxapi = json_decode($wxapi,true);
if (isset($wxapi['errcode'])){
$this->error($wxapi['errmsg']);
}
$third = new \app\api\model\Third();
$userid = $third->where('openid',$wxapi['openid'])->value('user_id');
if ($userid){
$this->auth->direct($userid);
$this->success('登录成功',['token'=>$this->auth->getToken(),'user_type'=>$this->auth->user_type]);
}else{
$userid = $this->auth->register($nickname,'','','',['avatar'=>$avatar]);
if ($userid){
$third->save(['openid'=>$wxapi['openid'],'user_id'=>$userid]);
$this->success('登录成功',['token'=>$this->auth->getToken(),'user_type'=>$this->auth->user_type]);
}else{
$this->error('注册失败');
}
}
}
/**
* @ApiTitle (用户优惠券)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="type", type="integer", required=true, description="0未使用1已使用2已过期")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
"data": [
{
"id": 1,
"user_id": 1,
"coupon_id": 1,
"name": "手动阀手动阀",
"price": "1.00", 优惠券金额
"full_price": "10.00", 满减金额
"createtime": 111122244,
"endtime": 1641869388,
"status": "0",
"endtime_text": "2022年01月11日到期"
}
]
})
*/
public function userCoupon()
{
$type = $this->request->post('type');
if (!in_array($type,[0,1,2])) $this->error('type参数不合法');
$model = new UserCoupon();
$list = $model->where('status',$type)->where('user_id',$this->auth->id)->select();
$this->success('用户优惠券列表',$list);
}
/**
* @ApiTitle (用户地址列表)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
"data": [
{
"id": 1,
"user_id": 1,
"username": "1", 收件人
"address": "阿松大", 地址
"address_detail": "阿松大", 详细地址
"normal_status": "0", 0不默认1默认
"lng": null, 经度
"lat": null, 纬度
"createtime": null,
"mobile_hide": "135****9988" 电话号码
"mobile": "13549059988" 电话号码
}
]
})
*/
public function userAddressList()
{
$model = new UserAddress();
$list = $model->where('user_id',$this->auth->id)->order('normal_status','desc')->select();
$this->success('用户地址列表',$list);
}
/**
* @ApiTitle (用户修改(删除)地址)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name=id, type=integer, required=true, description="地址id")
* @ApiParams (name=type, type=integer, required=true, description="1修改2删除")
* @ApiParams (name=username, type=string, required=false, description="收件人")
* @ApiParams (name=mobile, type=string, required=flase, description="手机号")
* @ApiParams (name=address, type=string, required=false, description="地址")
* @ApiParams (name=address_detail, type=string, required=false, description="详细地址")
* @ApiParams (name=normal, type=integer, required=false, description="默认状态 0不默认或1默认")
* @ApiParams (name=lat, type=float, required=false, description="纬度")
* @ApiParams (name=lng, type=float, required=false, description="经度")
* @ApiReturn ({
'code':'1',
'msg':'SUCCESS'
"data":
})
*/
public function userAddressEdit()
{
$id = $this->request->post('id');
$type = $this->request->post('type');
$username = $this->request->post('username');
$mobile = $this->request->post('mobile');
$address = $this->request->post('address');
$address_detail = $this->request->post('address_detail');
$normal = $this->request->post('normal');
$lat = $this->request->post('lat');
$lng = $this->request->post('lng');
$data = [
'type' => $type,
'mobile' => $mobile,
'id' => $id,
'normal' => $normal,
'lat' => $lat,
'lng' => $lng,
];
$rule = [
'type' => 'require|in:1,2',
'mobile' => 'regex:^1\d{10}$',
'id' => 'require|integer',
'normal' => 'in:0,1',
'lat' => 'float',
'lng' => 'float',
];
$msg = [
'type' => 'type参数不合法',
'mobile' => '电话号码格式不正确',
'id' => '地址id不合法',
'normal' => '默认状态参数不合法',
'lat' => '纬度请为浮点数格式',
'lng' => '经度请为浮点数格式',
];
$validate = new \think\Validate();
$validate->rule($rule);
$validate->message($msg);
if (!$validate->check($data)) $this->error($validate->getError());
$model = new UserAddress();
if ($type == 1){
$data = [
'mobile' => $mobile,
'normal_status' => $normal,
'lat' => $lat,
'lng' => $lng,
'username' => $username,
'address' => $address,
'address_detail' => $address_detail,
];
$model->where('id',$id)->isUpdate()->save($data,['id'=>$id]);
}else{
$model->where('id',$id)->delete();
}
$this->success('SUCCESS');
}
/**
* @ApiTitle (加入我们)
* @ApiMethod (POST)
* @ApiParams (name=work, type=string, required=true, description="从事行业")
* @ApiParams (name=years, type=string, required=true, description="从业时间")
* @ApiParams (name=username, type=string, required=true, description="姓名")
* @ApiParams (name=mobile, type=string, required=true, description="手机号")
* @ApiParams (name=money, type=string, required=true, description="预计可投入资金")
* @ApiParams (name=team, type=integer, required=true, description="团队拥有0没有1有")
* @ApiParams (name=city, type=string, required=true, description="意向城市")
* @ApiParams (name=content, type=string, required=true, description="自身优势")
* @ApiReturn ({
'code':'1',
'msg':'SUCCESS'
"data":
})
*/
public function joinUs()
{
$work = $this->request->post('work');
$years = $this->request->post('years');
$username = $this->request->post('username');
$mobile = $this->request->post('mobile');
$money = $this->request->post('money');
$team = $this->request->post('team');
$city = $this->request->post('city');
$content = $this->request->post('content');
$data = [
'work' => $work,
'mobile' => $mobile,
'username' => $username,
'years' => $years,
'money' => $money,
'team' => $team,
'city' => $city,
'content' => $content,
];
$rule = [
'work' => 'require',
'mobile' => 'require|regex:^1\d{10}$',
'years' => 'require',
'username' => 'require',
'money' => 'require',
'team' => 'require|in:0,1',
'city' => 'require',
'content' => 'require|max:100',
];
$msg = [
'work' => '请填写从事行业',
'mobile' => '请正确填写电话号码',
'years' => '请填写从业时间',
'username' => '请输入姓名',
'money' => '请输入可投入资金',
'team' => '请选择是否拥有团队',
'city' => '请填写意向城市',
'content' => '请填写描述控制在100字以内',
];
$validate = new \think\Validate();
$validate->rule($rule);
$validate->message($msg);
if (!$validate->check($data)) $this->error($validate->getError());
$data['createtime'] = time();
Db::name('join_us')->insert($data);
$this->success('SUCCESS');
}
/**
* @ApiTitle (帮助反馈列表)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="token")
* @ApiParams (name=page, type=integer, required=true, description="页数")
* @ApiReturn ({
'code':'1',
'msg':'SUCCESS'
"data":
"list": {
"total": 1,
"per_page": 15,
"current_page": 1,
"last_page": 1,
"data": [
{
"id": 1,
"title": "123", 标题
"content": "啊实打实大苏打", 内容
}
]
},
"rider_status": 0 申请骑手状态:0=无,1=申请中,2=成功,3=拒绝
})
*/
public function helpsList()
{
$page = $this->request->post('page');
if (!is_numeric($page)) $this->error('页数参数不合法');
$list = Db::name('helps')
->order('id','desc')
->paginate(15,false,['page'=>$page]);
$this->success('SUCCESS',['list'=>$list,'rider_status'=>0]);
}
/**
* @ApiTitle (意见反馈)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="token")
* @ApiParams (name=cotent, type=string, required=true, description="意见描述")
* @ApiParams (name=images, type=string, required=false, description="图片逗号隔开 不要域名")
* @ApiParams (name=mobile, type=string, required=true, description="联系方式")
* @ApiReturn ({
'code':'1',
'msg':'SUCCESS'
"data":
})
*/
public function opinionAdd()
{
$cotent = $this->request->post('cotent');
$images = $this->request->post('images');
$mobile = $this->request->post('mobile');
if (!$cotent) $this->error('请填写意见描述');
if (mb_strlen($cotent) > 100) $this->error('请将意见描述控制在100字以内');
if (!$mobile || !preg_match('/^1\d{10}$/',$mobile)) $this->error('请正确填写联系方式');
$data = [
'user_id' => $this->auth->id,
'content' => $cotent,
'images' => $images,
'mobile' => $mobile,
'createtime' => time()
];
Db::name('opinion')->insert($data);
$this->success('SUCCESS');
}
/**
* @ApiTitle (申请成为骑手)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="token")
* @ApiReturn ({
'code':'1',
'msg':'SUCCESS'
"data":
})
*/
public function riderApply()
{
if ($this->auth->rider > 0){
$this->error('您已申请,不可重复申请');
}
$user = $this->auth->getUser();
$user->rider = '1';
$user->save();
$this->success('SUCCESS');
}
/**
* @ApiTitle (邀请有奖)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="token")
* @ApiReturn ({
'code':'1',
'msg':'邀请有奖'
"data":
"price": "10.00", 优惠价格
"full_price": "100.00", 满减价格
"endtime": "2022年04月10日", 到期时间
"rule": "啊实打实大苏打", 规则
"user_list": [
{
"user": {
"nickname": "admin",
"avatar_text": ""
},
"createtime_text": ""
}
]
})
*/
public function inviteReward()
{
$model = new UserInvitation();
$coupon = Db::name('coupon')->field('price,full_price,endtime')->find();
$coupon['endtime'] = date('Y年m月d日',$coupon['endtime']);
$coupon['rule'] = Config::get('site.invite_rule');
$coupon['user_list'] = $model->with(['user'])
->where('user_id',$this->auth->id)
->where('status','1')
->limit(3)
->select();
foreach ($coupon['user_list'] as $key => $value){
$value->getRelation('user')->visible(['nickname']);
$value->visible(['user']);
}
$this->success('邀请有奖',$coupon);
}
/**
* @ApiTitle (发票管理列表)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="token")
* @ApiParams (name=page, type=integer, required=true, description="页数")
* @ApiReturn ({
'code':'1',
'msg':'发票管理列表'
"data":
"total": 1, 总条数
"per_page": 5, 每页数量
"current_page": 1, 当前页
"last_page": 1, 最后一页
"data": [
{
"id": "1",
"company_name": "asdsd", 公司名称
"username": "asdsa", 个人名称
"type": "1", 1个人发票2公司发票
"price": 0, 发票价格
"tax_time": 1654891234,
"tax_time_text": "2022年01月12日" 开票时间
}
]
})
*/
public function taxList()
{
$page = $this->request->post('page',1);
$model = new Tax();
$list = $model
->where('user_id',1)
->where('del_status','normal')
->field('id,company_name,username,type,price,tax_time')
->order('id','desc')
->paginate(5,false,['page'=>$page]);
$this->success('发票管理列表',$list);
}
/**
* @ApiTitle (申请发票)
* @ApiSummary (个人信息和企业信息必填二选一)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="token")
* @ApiParams (name=type, type=integer, required=true, description="1个人2企业单位")
* @ApiParams (name=username, type=string, required=false, description="个人名称")
* @ApiParams (name=company_name, type=string, required=false, description="公司名称")
* @ApiParams (name=tax_number, type=string, required=false, description="纳税识别号")
* @ApiParams (name=company_address, type=string, required=false, description="公司地址")
* @ApiParams (name=company_mobile, type=string, required=false, description="公司电话")
* @ApiParams (name=bank, type=string, required=false, description="开户银行")
* @ApiParams (name=mobile, type=string, required=true, description="收票人电话号码")
* @ApiParams (name=eamil, type=string, required=true, description="电子邮箱")
* @ApiParams (name=order_no, type=string, required=true, description="订单编号")
* @ApiReturn ({
'code':'1',
'msg':'SUCCESS'
"data":
})
*/
public function taxApply()
{
$type = $this->request->post('type');
$username = $this->request->post('username');
$company_name = $this->request->post('company_name');
$tax_number = $this->request->post('tax_number');
$company_address = $this->request->post('company_address');
$company_mobile = $this->request->post('company_mobile');
$bank = $this->request->post('bank');
$mobile = $this->request->post('mobile');
$eamil = $this->request->post('eamil');
$order_no = $this->request->post('order_no');
//校验数据
if (!in_array($type,[1,2])) $this->error('type参数不合法');
$data = [
'mobile' => $mobile,
'eamil' => $eamil,
'order_no' => $order_no,
];
$rule = [
'mobile' => 'regex:^1\d{10}$',
'eamil' => 'regex:^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$',
'order_no' => 'require',
];
$msg = [
'mobile' => '收票人电话号码格式不正确',
'eamil' => '电子邮箱格式不正确',
'order_no' => '请输入订单编号',
];
$validate = new \think\Validate();
$validate->rule($rule);
$validate->message($msg);
if (!$validate->check($data)) $this->error($validate->getError());
if ($type == 1){
$data = [
'company_name' => $company_name,
'company_mobile' => $company_mobile,
'tax_number' => $tax_number,
'company_address' => $company_address,
'bank' => $bank,
];
$rule = [
'company_name' => 'require',
'tax_number' => 'require',
'company_address' => 'require',
'bank' => 'require',
'company_mobile' => 'regex:^1\d{10}$',
];
$msg = [
'company_mobile' => '公司电话格式不正确',
'company_name' => '请填写公司名称',
'tax_number' => '请填写纳税人识别号',
'company_address' => '请填写公司地址',
'bank' => '请填写开户银行',
];
$validate->rule($rule);
$validate->message($msg);
if (!$validate->check($data)) $this->error($validate->getError());
}else{
$data = [
'username' => $username,
];
$rule = [
'company_name' => 'require',
];
$msg = [
'company_mobile' => '请填写个人/非企业单位名称',
];
$validate->rule($rule);
$validate->message($msg);
if (!$validate->check($data)) $this->error($validate->getError());
}
//校验订单编号
$orderModel = new \app\api\model\Order();
$order = $orderModel::get(['order_no'=>$order_no]);
if (!$order) $this->error('订单编号不存在');
if ($order['user_id'] != $this->auth->id) $this->error('当前用户无权使用此订单编号');
// 插入数据
$data = [
'company_name' => $company_name,
'company_mobile' => $company_mobile,
'tax_number' => $tax_number,
'company_address' => $company_address,
'bank' => $bank,
'mobile' => $mobile,
'eamil' => $eamil,
'order_no' => $order_no,
'type' => $type,
'order_id' => $order['id'],
'price' => $order_no['total_price'],
'user_id' => $this->auth->id,
];
$model = new Tax();
$model->allowField(true)->isUpdate(false)->save($data);
$this->success('SUCCESS');
}
/**
* @ApiTitle (发票删除)
* @ApiSummary (个人信息和企业信息必填二选一)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="token")
* @ApiParams (name=ids, type=string, required=true, description="发票id逗号隔开")
* @ApiReturn ({
'code':'1',
'msg':'SUCCESS'
"data":
})
*/
public function taxDelete()
{
$ids = $this->request->post('ids');
//校验数据
$ids = explode(',',$ids);
$data = [];
foreach ($ids as $key => $value){
if (!is_numeric($value)) $this->error('ids参数不合法');
$data[] = [
'id' => $value,
'del_status' => 'hidden'
];
}
$model = new Tax();
$model->isUpdate()->saveAll($data);
$this->success('SUCCESS');
}
}