User.php
34.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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
<?php
namespace app\api\controller;
use app\api\model\Market;
use app\api\model\UserScoreLog;
use app\api\model\UserMoneyLog;
use app\api\model\Order;
use app\api\model\OrderGrab;
use app\api\model\UserLicensePlate;
use app\api\model\Withdraw;
use app\api\model\ProblemCategory;
use app\api\model\Problem;
use app\api\model\Feedback;
use app\api\model\User as UserModel;
use app\common\controller\Wechat;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Validate;
use think\Exception;
use think\exception\PDOException;
use think\Db;
/**
* 会员接口
*/
class User extends Api
{
protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third', 'authorize', 'marketList'];
protected $noNeedRight = '*';
public function _initialize()
{
parent::_initialize();
}
/**
* @ApiWeigh (99)
* @ApiTitle (个人中心-首页)
* @ApiSummary (个人中心-首页)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1604741169",
"data": {
"id": 1, //用户ID
"nickname": "admin", //昵称
"mobile": "13888888888", //手机号
"avatar": "http://www.parking.top123456", //头像
"money": "32.00", //余额
"score": 0, //积分
"order_count": { //我的发布
"count1": 1, //待抢单数量
"count2": 0, //进行中数量
"count3": 0, //待确认数量
"count4": 0, //已结束数量
"count5": 0 //售后数量
"count6": 0 //已取消数量
},
"grab_count": { //抢单信息
"count1": 1, //抢单中数量
"count2": 0, //缴费中数量
"count3": 0, //待确认数量
"count4": 0, //已结束数量
"count5": 0 //售后数量
"count6": 0 //已取消数量
},
"url": "/u/1"
}
})
*/
public function index()
{
$user = $this->auth->getUser();
$user->avatar = cdnurl($user->avatar,true);
// 我的发布
$user->order_count = [
'count1' => $this->getOrderCount(['user_id'=>$user['id'],'status'=>'1']), // 待抢单
'count2' => $this->getOrderCount(['user_id'=>$user['id'],'status'=>'2']), // 进行中
'count3' => $this->getOrderCount(['user_id'=>$user['id'],'status'=>'3']), // 待确认
'count4' => $this->getOrderCount(['user_id'=>$user['id'],'status'=>['in','4,6']]), // 已完成/已拒绝
'count5' => $this->getOrderCount(['user_id'=>$user['id'],'status'=>'5']), // 售后
'count6' => $this->getOrderCount(['user_id'=>$user['id'],'status'=>['in','7,8']]), // 已取消
];
// 抢单信息
$user->grab_count = [
'count1' => $this->getGrabCount(['a.user_id'=>$user['id'],'a.status'=>'1']), // 抢单中
'count2' => $this->getGrabCount(['a.user_id'=>$user['id'],'a.status'=>'2']), // 缴费中
'count3' => $this->getGrabCount(['a.user_id'=>$user['id'],'a.status'=>'3']), // 待确认
'count4' => $this->getGrabCount(['a.user_id'=>$user['id'],'a.status'=>['in','4,6']]), // 已完成/已拒绝
'count5' => $this->getGrabCount(['a.user_id'=>$user['id'],'a.status'=>'5']), // 售后
'count6' => $this->getGrabCount(['a.user_id'=>$user['id'],'a.status'=>['in','7,8']]), // 已取消
];
$user->visible(['id','avatar','nickname','mobile','score','money','order_count','grab_count']);
$this->success('成功', $user);
}
/**
* 获取订单数量
*/
private function getOrderCount($where){
return Order::where($where)->count();
}
/**
* 获取抢单数量
*/
private function getGrabCount($where){
return OrderGrab::alias('a')
->join('order b','b.id = a.order_id')
->where($where)
->count();
}
/**
* 会员登录
*
* @param string $account 账号
* @param string $password 密码
*/
public function login()
{
$account = $this->request->request('account');
$password = $this->request->request('password');
if (!$account || !$password) {
$this->error(__('Invalid parameters'));
}
$ret = $this->auth->login($account, $password);
if ($ret) {
$data = ['userinfo' => $this->auth->getUserinfo()];
$this->success(__('Logged in successful'), $data);
} else {
$this->error($this->auth->getError());
}
}
/**
* 手机验证码登录
*
* @param string $mobile 手机号
* @param string $captcha 验证码
*/
public function mobilelogin()
{
$mobile = $this->request->request('mobile');
$captcha = $this->request->request('captcha');
if (!$mobile || !$captcha) {
$this->error(__('Invalid parameters'));
}
if (!Validate::regex($mobile, "^1\d{10}$")) {
$this->error(__('Mobile is incorrect'));
}
if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
$this->error(__('Captcha is incorrect'));
}
$user = \app\common\model\User::getByMobile($mobile);
if ($user) {
if ($user->status != 'normal') {
$this->error(__('Account is locked'));
}
//如果已经有账号则直接登录
$ret = $this->auth->direct($user->id);
} else {
$ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
}
if ($ret) {
Sms::flush($mobile, 'mobilelogin');
$data = ['userinfo' => $this->auth->getUserinfo()];
$this->success(__('Logged in successful'), $data);
} else {
$this->error($this->auth->getError());
}
}
/**
* 注册会员
*
* @param string $username 用户名
* @param string $password 密码
* @param string $email 邮箱
* @param string $mobile 手机号
* @param string $code 验证码
*/
public function register()
{
$username = $this->request->request('username');
$password = $this->request->request('password');
$email = $this->request->request('email');
$mobile = $this->request->request('mobile');
$code = $this->request->request('code');
if (!$username || !$password) {
$this->error(__('Invalid parameters'));
}
if ($email && !Validate::is($email, "email")) {
$this->error(__('Email is incorrect'));
}
if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
$this->error(__('Mobile is incorrect'));
}
$ret = Sms::check($mobile, $code, 'register');
if (!$ret) {
$this->error(__('Captcha is incorrect'));
}
$ret = $this->auth->register($username, $password, $email, $mobile, []);
if ($ret) {
$data = ['userinfo' => $this->auth->getUserinfo()];
$this->success(__('Sign up successful'), $data);
} else {
$this->error($this->auth->getError());
}
}
/**
* 退出登录
*/
public function logout()
{
$this->auth->logout();
$this->success(__('Logout successful'));
}
/**
* @ApiWeigh (97)
* @ApiTitle (编辑资料-展示数据)
* @ApiSummary (编辑资料-展示数据)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1604629301",
"data": {
"id": 1, //用户ID
"nickname": "admin", //昵称
"mobile": "13888888888", //手机号
"avatar": "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSIgaGVpZ2h0PSIxMDAiIHdpZHRoPSIxMDAiPjxyZWN0IGZpbGw9InJnYigxNjAsMTkwLDIyOSkiIHg9IjAiIHk9IjAiIHdpZHRoPSIxMDAiIGhlaWdodD0iMTAwIj48L3JlY3Q+PHRleHQgeD0iNTAiIHk9IjUwIiBmb250LXNpemU9IjUwIiB0ZXh0LWNvcHk9ImZhc3QiIGZpbGw9IiNmZmZmZmYiIHRleHQtYW5jaG9yPSJtaWRkbGUiIHRleHQtcmlnaHRzPSJhZG1pbiIgYWxpZ25tZW50LWJhc2VsaW5lPSJjZW50cmFsIj5BPC90ZXh0Pjwvc3ZnPg==", //头像
"birthday": "2017-04-15", //生日
"push_start_time": 0, //推送开始时间
"push_end_time": 0, //推送结束时间
"is_auth": "0", //是否已认证:0=否,1=是
"market_is_auth": "0", //商城是否已认证:0=否,1=是
"license_is_auth": "0", //车牌号是否已认证:0=否,1=是
"url": "/u/1"
}
})
*/
public function profileView()
{
$user = $this->auth->getUser();
$user->avatar = cdnurl($user->avatar,true);
$user->market_is_auth = !empty($user->market_ids) ? 1 : 0;
$license_count = UserLicensePlate::where('user_id',$user['id'])->count();
$user->license_is_auth = $license_count > 0 ? 1 : 0;
$user->visible(['id','avatar','nickname','mobile','birthday','push_start_time','push_end_time','is_auth'])->append(['market_is_auth','license_is_auth']);
$this->success('成功',$user);
}
/**
* @ApiWeigh (95)
* @ApiTitle (编辑资料)
* @ApiSummary (编辑资料)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="avatar", type="string", required=false, description="头像")
* @ApiParams (name="nickname", type="string", required=false, description="昵称")
* @ApiParams (name="birthday", type="string", required=false, description="生日")
* @ApiParams (name="push_start_time", type="string", required=false, description="推送开始时间")
* @ApiParams (name="push_end_time", type="string", required=false, description="推送结束时间")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1604282876",
"data": null
})
*/
public function profile()
{
$user = $this->auth->getUser();
$avatar = $this->request->param('avatar', '', 'trim,strip_tags,htmlspecialchars');
$nickname = $this->request->param('nickname');
$birthday = $this->request->param('birthday');
$push_start_time = $this->request->param('push_start_time');
$push_end_time = $this->request->param('push_end_time');
if(!$avatar && !$nickname && !$birthday && !$push_start_time && !$push_end_time){
$this->error('修改内容不合法');
}
if ($avatar) {
$user->avatar = $avatar;
}
if ($nickname) {
$user->nickname = $nickname;
}
if ($birthday) {
$user->birthday = $birthday;
}
if($push_start_time){
empty($push_end_time) && $this->error('请选择推送结束时间');
$user->push_start_time = $push_start_time;
}
if($push_end_time){
empty($push_start_time) && $this->error('请选择推送开始时间');
$user->push_end_time = $push_end_time;
}
$user->save();
$this->success();
}
/**
* @ApiWeigh (93)
* @ApiTitle (商城列表)
* @ApiSummary (商城列表)
* @ApiMethod (POST)
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1604629564",
"data": [{
"id": 1, //商城ID
"market_name": "你好商城", //名称
"is_bind": "0" //是否已绑定:0=否,1=是
}]
})
*/
public function marketList()
{
$market_id_arr = explode(',', $this->auth->market_ids);
$list = Market::field('id,market_name')->select();
foreach ($list as &$v) {
$v['is_bind'] = in_array($v['id'],$market_id_arr) ? 1 : 0;
}
$this->success('成功',$list);
}
/**
* @ApiWeigh (91)
* @ApiTitle (绑定商城)
* @ApiSummary (绑定商城)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="market_ids", type="inter", required=true, description="商城ID,多个ID用英文逗号分隔")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1604282876",
"data": null
})
*/
public function bindMarket()
{
$market_ids = $this->request->param('market_ids');
empty($market_ids) && $this->error('请选择商城');
$user = UserModel::get($this->auth->id);
$user->save(['market_ids'=>$market_ids]);
// 商城和车牌号都填写后,把用户认证状态改为已认证
if($user['is_auth'] == '0' && !empty($user['licensePlate'])){
$user->save(['is_auth'=>'1']);
}
$this->success('绑定成功');
}
/**
* @ApiWeigh (89)
* @ApiTitle (添加车牌号)
* @ApiSummary (添加车牌号)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="license_plate", type="string", required=true, description="车牌号")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1604282876",
"data": null
})
*/
public function licensePlateAdd()
{
$license_plate = $this->request->param('license_plate');
empty($license_plate) && $this->error('请填写车牌号');
!is_car_license($license_plate) && $this->error('车牌号格式不正确');
$user = $this->auth->getUser();
$has = UserLicensePlate::where('user_id',$user['id'])->where('license_plate',$license_plate)->find();
!empty($has) && $this->error('车牌已添加,请勿重复操作');
UserLicensePlate::create([
'user_id' => $user['id'],
'license_plate' => $license_plate
]);
// 商城和车牌号都填写后,把用户认证状态改为已认证
if($user['is_auth'] == '0' && !empty($user['market_ids'])){
$user->save(['is_auth'=>'1']);
}
$this->success('车牌号添加成功');
}
/**
* @ApiWeigh (87)
* @ApiTitle (删除车牌号)
* @ApiSummary (删除车牌号)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="user_license_plate_id", type="inter", required=true, description="用户车牌号ID")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1604282876",
"data": null
})
*/
public function licensePlateDel()
{
$user_license_plate_id = $this->request->param('user_license_plate_id');
empty($user_license_plate_id) && $this->error('缺少必需参数');
$info = UserLicensePlate::get($user_license_plate_id);
empty($info) && $this->error('车牌号不存在');
$count = UserLicensePlate::where('user_id',$this->auth->id)->count();
$count <= 1 && $this->error('请至少保留一个车牌号');
$info->delete();
$this->success('车牌号删除成功');
}
/**
* 修改邮箱
*
* @param string $email 邮箱
* @param string $captcha 验证码
*/
public function changeemail()
{
$user = $this->auth->getUser();
$email = $this->request->post('email');
$captcha = $this->request->request('captcha');
if (!$email || !$captcha) {
$this->error(__('Invalid parameters'));
}
if (!Validate::is($email, "email")) {
$this->error(__('Email is incorrect'));
}
if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
$this->error(__('Email already exists'));
}
$result = Ems::check($email, $captcha, 'changeemail');
if (!$result) {
$this->error(__('Captcha is incorrect'));
}
$verification = $user->verification;
$verification->email = 1;
$user->verification = $verification;
$user->email = $email;
$user->save();
Ems::flush($email, 'changeemail');
$this->success();
}
/**
* 修改手机号
*
* @param string $mobile 手机号
* @param string $captcha 验证码
*/
public function changemobile()
{
$user = $this->auth->getUser();
$mobile = $this->request->param('mobile');
$captcha = $this->request->param('captcha');
if (!$mobile || !$captcha) {
$this->error(__('Invalid parameters'));
}
if (!Validate::regex($mobile, "^1\d{10}$")) {
$this->error(__('Mobile is incorrect'));
}
if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
$this->error(__('Mobile already exists'));
}
// $result = \app\api\controller\Sms::check($mobile, $captcha, 'changemobile');
$result = Sms::check($mobile, $captcha, 'changemobile');
if (!$result) {
$this->error(__('Captcha is incorrect'));
}
$verification = $user->verification;
$verification->mobile = 1;
$user->verification = $verification;
$user->mobile = $mobile;
$user->save();
Sms::flush($mobile, 'changemobile');
$this->success();
}
/**
* 微信授权登录地址
* @ApiWeigh (99)
*
* @param string $redirect_uri 回调地址
*/
public function authorize()
{
$redirect_uri = $this->request->param('redirect_uri');
if(!$redirect_uri) {
$this->error('地址错误');
}
$redirect_uri = urlencode($redirect_uri);
$third = get_addon_config('third');
$appid = $third['wechat']['app_id'];
$wechat_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
$this->success('', ['wechat_url'=>$wechat_url]);
}
/**
* 第三方登录
* @ApiWeigh (99)
* @param string $platform 平台名称
* @param string $code Code码
*/
public function third()
{
$url = url('user/index');
$platform = $this->request->request("platform");
$code = $this->request->request("code");
$config = get_addon_config('third');
if (!$config || !isset($config[$platform])) {
$this->error(__('Invalid parameters'));
}
$app = new \addons\third\library\Application($config);
//通过code换access_token和绑定会员
session('state','STATE');
$result = $app->{$platform}->getUserInfo(['state' => 'STATE','code' => $code]);
if ($result) {
$loginret = \addons\third\library\Service::connect($platform, $result);
if ($loginret) {
$data = [
'token' => $this->auth->getToken(),
'userinfo' => $this->auth->getUserinfo(),
'thirdinfo' => $result
];
$this->success(__('Logged in successful'), $data);
}
}
$this->error(__('Operation failed'), $result);
}
/**
* 重置密码
*
* @param string $mobile 手机号
* @param string $newpassword 新密码
* @param string $captcha 验证码
*/
public function resetpwd()
{
$type = $this->request->request("type");
$mobile = $this->request->request("mobile");
$email = $this->request->request("email");
$newpassword = $this->request->request("newpassword");
$captcha = $this->request->request("captcha");
if (!$newpassword || !$captcha) {
$this->error(__('Invalid parameters'));
}
if ($type == 'mobile') {
if (!Validate::regex($mobile, "^1\d{10}$")) {
$this->error(__('Mobile is incorrect'));
}
$user = \app\common\model\User::getByMobile($mobile);
if (!$user) {
$this->error(__('User not found'));
}
$ret = Sms::check($mobile, $captcha, 'resetpwd');
if (!$ret) {
$this->error(__('Captcha is incorrect'));
}
Sms::flush($mobile, 'resetpwd');
} else {
if (!Validate::is($email, "email")) {
$this->error(__('Email is incorrect'));
}
$user = \app\common\model\User::getByEmail($email);
if (!$user) {
$this->error(__('User not found'));
}
$ret = Ems::check($email, $captcha, 'resetpwd');
if (!$ret) {
$this->error(__('Captcha is incorrect'));
}
Ems::flush($email, 'resetpwd');
}
//模拟一次登录
$this->auth->direct($user->id);
$ret = $this->auth->changepwd($newpassword, '', true);
if ($ret) {
$this->success(__('Reset password successful'));
} else {
$this->error($this->auth->getError());
}
}
/**
* @ApiWeigh (85)
* @ApiTitle (我的积分)
* @ApiSummary (我的积分)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1604282876",
"data": {
"score" : 50, //我的积分总数
}
})
*/
public function mySocre()
{
$this->success('成功',['score'=>$this->auth->score]);
}
/**
* @ApiWeigh (83)
* @ApiTitle (我的积分列表)
* @ApiSummary (我的积分列表)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="page", type="inter", required=false, description="当前页(默认1)")
* @ApiParams (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1604885966",
"data": {
"total": 1, //数据总数
"per_page": 15,
"current_page": 1,
"last_page": 1,
"data": [{
"id": 1, //记录ID
"score": 50, //变更积分
"before": 100, //变更前积分
"after": 150, //变更后积分
"memo": "测试", //备注
"createtime": "11/06/11:40", //记录时间
"order": { //订单信息
"id": 1, //订单ID
"market_name": "你好商城", //商城名称
"license_plate": "465" //车牌号
}
}]
}
})
*/
public function scoreList()
{
$page = $this->request->param('page', 1, 'intval');
$page_num = $this->request->param('page_num', 10, 'intval');
$data = UserScoreLog::with(['order'])
->where('user_id',$this->auth->id)
->order('createtime desc')
->paginate($page_num,false,['page'=>$page])
->each(function($v){
$v->createtime = date('m/d/H:i',$v->createtime); // 记录时间
$v->visible(['id','score','before','after','createtime','memo','order']);
if($v->getRelation('order')){
$v->getRelation('order')->visible(['id','market_name','license_plate']);
}
})->toArray();
$this->success('成功',$data);
}
/**
* @ApiWeigh (81)
* @ApiTitle (我的余额)
* @ApiSummary (我的余额)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1604282876",
"data": {
"money" : 50, //我的余额总数
}
})
*/
public function myMoney()
{
$this->success('成功',['money'=>$this->auth->money]);
}
/**
* @ApiWeigh (79)
* @ApiTitle (我的钱包列表)
* @ApiSummary (我的钱包列表)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="page", type="inter", required=false, description="当前页(默认1)")
* @ApiParams (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1604639360",
"data": {
"total": 6, //数据总数
"per_page": 15,
"current_page": 1,
"last_page": 1,
"data": [{
"id": 6, //记录ID
"money": "3.50", //金额
"before": "25.00", //变更前余额
"after": "28.50", //变更后余额
"memo": "订单完成", //备注
"order_id": 0, //订单ID
"withdraw_id": 0, //提现ID
"createtime": "11/06/13:06", //记录时间
"order": { //订单信息
"id": 1, //ID
"market_name": "你好商城", //商城名称
"license_plate": "465" //车牌号
}
}]
}
})
*/
public function moneyList()
{
$page = $this->request->param('page', 1, 'intval');
$page_num = $this->request->param('page_num', 10, 'intval');
$data = UserMoneyLog::with(['order'])
->where('user_id',$this->auth->id)
->order('createtime desc')
->paginate($page_num,false,['page'=>$page])
->each(function($v){
$v->createtime = date('m/d/H:i',$v->createtime); // 记录时间
$v->visible(['id','money','before','after','createtime','memo','order_id','withdraw_id','order']);
if($v->getRelation('order')){
$v->getRelation('order')->visible(['id','market_name','license_plate']);
}
})->toArray();
$this->success('成功',$data);
}
/**
* @ApiWeigh (79)
* @ApiTitle (我的钱包列表-详情)
* @ApiSummary (我的钱包列表-详情)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="user_money_log_id", type="inter", required=true, description="记录ID")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1605085126",
"data": {
"id": 1, //记录ID
"money": "35.00", //金额
"before": "0.00", //变更前余额
"after": "35.00", //变更后余额
"order": { //订单信息
"id": 2, //ID
"order_sn": "2020111148509997", //订单号
"pay_time": "2020/11/11/16:56" //付款时间
},
"withdraw": { //提现信息
"id": 2, //ID
"order_sn": "2020111148509997", //订单号
"payment_time": "2020/11/11/16:56" //提现时间
}
}
})
*/
public function moneyInfo()
{
$user_money_log_id = $this->request->param('user_money_log_id');
empty($user_money_log_id) && $this->error('缺少必需参数');
$info = UserMoneyLog::get($user_money_log_id,['order','withdraw']);
empty($info) && $this->error('记录信息不存在');
$info->visible(['id','money','before','after','order','withdraw']);
if($info->getRelation('order')){
$info->getRelation('order')->pay_time = date('Y/m/d/H:i',$info->getRelation('order')->pay_time);
$info->getRelation('order')->visible(['id','order_sn','pay_time']);
}
if($info->getRelation('withdraw')){
$payment_time = empty($info->getRelation('withdraw')->payment_time) ? $info->getRelation('withdraw')->createtime : $info->getRelation('withdraw')->payment_time;
$info->getRelation('withdraw')->payment_time = date('Y/m/d/H:i',$payment_time);
$info->getRelation('withdraw')->visible(['id','order_sn','payment_time']);
}
$this->success('成功',$info);
}
/**
* @ApiWeigh (77)
* @ApiTitle (提现)
* @ApiSummary (提现)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="money", type="string", required=true, description="提现金额")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1604282876",
"data": null
})
*/
public function withdraw()
{
$money = $this->request->param('money');
empty($money) && $this->error('请输入金额');
$money_reg = '/^[0-9]+(.[0-9]{1,2})?$/';
!preg_match($money_reg, $money) && $this->error('金额格式错误');
$money <= 0 && $this->error('提现金额不能小于0');
$money > $this->auth->money && $this->error('余额不足'.$money.'元');
// 禁止连点
!empty(cache('withdraw_token'.$this->auth->id)) && $this->error('提现频繁,请稍后再试');
cache('withdraw_token'.$this->auth->id,'123',5);
Db::startTrans();
try{
$withdraw = Withdraw::create([
'user_id' => $this->auth->id,
'order_sn' => get_order_sn(),
'money' => $money,
]);
// 变更会员余额
UserModel::money(-$money,$this->auth->id,'提现',['withdraw_id' => $withdraw['id']]);
// 发起企业付款
$Wechat = new Wechat;
$openid = Db::name('third')->where('user_id',$this->auth->id)->value('openid');
if(!$balance = $Wechat->toBalance($withdraw['order_sn'], $openid, $withdraw['money'] * 100)){
$this->error($Wechat->getError());
}
// 记录企业付款单号
$withdraw->payment_no = $balance['payment_no'];
$withdraw->payment_time = $balance['payment_time'];
$withdraw->status = '1';
$withdraw->save();
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success('成功');
}
/**
* @ApiWeigh (73)
* @ApiTitle (客服-问题分类)
* @ApiSummary (客服-问题分类)
* @ApiMethod (POST)
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1604631438",
"data": {
"category_list": [{ //分类列表
"id": 13, //ID
"name": "测试2" //名称
}],
"mobile_service": "0.331-5620" //电话客服
}
})
*/
public function problemCategory()
{
$category_list = ProblemCategory::order(['weigh'=>'desc','createtime'=>'desc'])->field('id,name')->select();
$mobile_service = config('site.mobile_service');
$this->success('成功',compact('category_list','mobile_service'));
}
/**
* @ApiWeigh (71)
* @ApiTitle (客服-问题)
* @ApiSummary (客服-问题)
* @ApiMethod (POST)
*
* @ApiParams (name="problem_category_id", type="inter", required=true, description="问题分类ID")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1604631597",
"data": [{
"id": 2, //问题ID
"title": "退货原因" //问题标题
}]
})
*/
public function problemList()
{
$problem_category_id = $this->request->param('problem_category_id');
empty($problem_category_id) && $this->error('缺少必需参数');
$list = Problem::where('problem_category_id',$problem_category_id)
->order(['createtime'=>'desc'])
->field('id,title')
->select();
$this->success('成功',$list);
}
/**
* @ApiWeigh (69)
* @ApiTitle (客服-问题详情)
* @ApiSummary (客服-问题详情)
* @ApiMethod (POST)
*
* @ApiParams (name="problem_id", type="inter", required=true, description="问题ID")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1604631762",
"data": {
"id": 2, //问题ID
"title": "退货原因", //问题标题
"content": "<p>六块腹肌的看法</p>" //问题内容
}
})
*/
public function problemInfo()
{
$problem_id = $this->request->param('problem_id');
empty($problem_id) && $this->error('缺少必需参数');
$info = Problem::get($problem_id);
empty($info) && $this->error('问题信息不存在');
$info->visible(['id','title','content']);
$this->success('成功',$info);
}
/**
* @ApiWeigh (67)
* @ApiTitle (投诉-客服)
* @ApiSummary (投诉-客服)
* @ApiMethod (POST)
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1604631827",
"data": {
"mobile_service": "0.331-5620", //电话客服
"wechat_service": "15133120361" //微信客服
}
})
*/
public function service()
{
$mobile_service = config('site.mobile_service');
$wechat_service = config('site.wechat_service');
$this->success('成功',compact('mobile_service','wechat_service'));
}
/**
* @ApiWeigh (65)
* @ApiTitle (投诉)
* @ApiSummary (投诉)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="content", type="string", required=true, description="反馈内容")
* @ApiParams (name="mobile", type="string", required=true, description="联系方式")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1604282876",
"data": null
})
*/
public function feedback()
{
$content = $this->request->param('content');
$mobile = $this->request->param('mobile');
empty($content) && $this->error('请输入反馈意见');
empty($mobile) && $this->error('请输入联系方式');
Feedback::create([
'user_id' => $this->auth->id,
'content' => $content,
'mobile' => $mobile,
]);
$this->success('反馈成功');
}
}