Store.php
40.7 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
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
<?php
namespace app\api\controller;
use addons\wechat\library\Config as ConfigService;
use app\admin\model\UserWithdraw;
use app\api\model\Deposit;
use app\api\model\DepositOrder;
use app\api\model\Industry;
use app\api\model\Report;
use app\api\model\StoreApply;
use app\api\model\StoreComment;
use app\api\model\StoreInform;
use app\api\model\StoreOrder;
use app\api\model\UserScoreLog;
use app\api\validate\StoreValidate;
use app\common\controller\Api;
use app\common\library\Sms as Smslib;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\Request;
/**
* 店铺接口
*/
class Store extends Api
{
protected $noNeedLogin = [];
protected $noNeedRight = ['*'];
protected $store_model;
protected $industry_model;
protected $deposit_model;
protected $deposit_order_model;
protected $favorite_model;
protected $comment_model;
protected $good_model;
protected $follow_model;
protected $user_id;
public function __construct(Request $request,\app\api\model\Store $store,Industry $industry,Deposit $deposit,DepositOrder $deposit_order)
{
parent::__construct($request);
$this->industry_model = $industry;
$this->deposit_model = $deposit;
$this->deposit_order_model = $deposit_order;
$this->store_model = $store;
$this->user_id = $this->auth->id;
}
/**
* 入驻协议
* @ApiWeigh (90)
*
* @ApiTitle (入驻协议)
* @ApiSummary (入驻协议)
* @ApiMethod (POST)
* @ApiRoute (/api/store/settled)
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
"settled": 入驻协议,
}
})
*/
public function settled()
{
if($this->request->isPost()){
$return = [
'settled' => config('site.settled')
];
$this->success('成功',$return);
}
}
/**
* 提现规则
* @ApiWeigh (89)
*
* @ApiTitle (提现规则)
* @ApiSummary (提现规则)
* @ApiMethod (POST)
* @ApiRoute (/api/store/withdraw_rule)
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
"withdraw_rule": 入驻协议,
}
})
*/
public function withdraw_rule()
{
if($this->request->isPost()){
$return = [
'withdraw_rule' => config('site.withdraw_rule')
];
$this->success('成功',$return);
}
}
/**
* 商家余额
* @ApiWeigh (88)
*
* @ApiTitle (商家余额)
* @ApiSummary (商家余额)
* @ApiMethod (POST)
* @ApiRoute (/api/store/money)
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
"money": 入驻协议,
}
})
*/
public function money()
{
if($this->request->isPost()){
$return = [
'money' => $this->auth->score / config('site.withdraw_percent')
];
$this->success('成功',$return);
}
}
/**
* 行业列表
* @ApiWeigh (80)
*
* @ApiTitle (行业列表)
* @ApiSummary (行业列表)
* @ApiMethod (POST)
* @ApiRoute (/api/store/industry)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="keyword", type="string", required=true, description="关键词")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
"list": [
{
"id": 1,
"name": "行业名称",
"weigh": 0,
}
]
}
})
*/
public function industry()
{
if($this->request->isPost()){
$keyword = $this->request->param('keyword','');
$where = [
'where' => []
];
if($keyword) {
$where['where'] = ['name'=>['like','%'.$keyword.'%']];
}
$indus = $this->industry_model->selectOrFail($where,false,'*','weigh');
$return = [
'list' => $indus,
];
$this->success('请求成功',$return);
}
}
/**
* 店铺申请
* @ApiWeigh (55)
*
* @ApiTitle (店铺申请)
* @ApiSummary (店铺申请)
* @ApiMethod (POST)
* @ApiRoute (/api/store/store_add)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="store_name", type="string", required=true, description="店铺名称")
* @ApiParams (name="house_ids", type="string", sample="店铺1id,店铺2id", required=true, description="入驻社区")
* @ApiParams (name="industry_id", type="string", required=true, description="行业/关键词")
* @ApiParams (name="name", type="string", required=true, description="联系人姓名")
* @ApiParams (name="mobile", type="string", required=true, description="联系人手机号")
* @ApiParams (name="code", type="string", required=true, description="验证码")
* @ApiParams (name="license", type="string", required=true, description="营业执照")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function store_add()
{
$param = (new StoreValidate())->goCheck('store_add');
$where_s = ['user_id'=>$this->user_id];
$where = [
'where' => $where_s
];
$store = $this->store_model->findOrFail($where,false,'*','createtime');
if($store) {
if($store['status'] == 1) {
$param['id'] = $store['id'];
}
if($store['status'] == 2) {
$this->error('商家申请仍在审核中');
}
if($store['status'] == 3) {
$this->error('商家申请已通过');
}
}
// 验证码验证
$mobile = $param['mobile'];
$captcha = $param['code'];
$event = 'test';
$ret = Smslib::check($mobile, $captcha, $event);
if (!$ret) {
$this->error(__('验证码不正确'));
}
unset($param['code']);
$order_sn = $param['order_sn'] = get_order_sn();
// 获取配置
$app = new Application(ConfigService::load());
$payment = $app->payment;
// 获取支付参数
$attributes = [
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
'body' => '发布招募合伙人',
'out_trade_no' => $order_sn,
'total_fee' => 1, // $param['money'] * 100
'spbill_create_ip' => request()->ip(), // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
'notify_url' => url('index/ajax/store_notify',[],true,true), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'openid' => Db::name('third')->where('user_id',$this->auth->id)->value('openid'),
];
$order = new Order($attributes);
$order_result = $payment->prepare($order);
if($order_result['return_code'] == 'SUCCESS' && $order_result['result_code'] == 'SUCCESS') {
$prepayId = $order_result->prepay_id;
$pay_data = $payment->configForJSSDKPayment($prepayId);
} else {
$this->error($order_result['return_msg']);
}
Db::startTrans();
$result = false;
$result_invite = true;
try{
$param['user_id'] = $this->auth->id;
if(!empty($param['id'])) {
$result = $this->store_model->edit($param);
} else {
$result = $this->store_model->add($param);
}
$id = $this->store_model->id;
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if(!$result || !$result_invite) {
$this->error('申请提交失败');
}
$this->success('申请提交成功',['id'=>$id,'pay_data'=>$pay_data]);
}
/**
* 开通社区提交
* @ApiWeigh (50)
*
* @ApiTitle (开通社区提交)
* @ApiSummary (开通社区提交)
* @ApiMethod (POST)
* @ApiRoute (/api/store/store_apply)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="store_name", type="string", required=true, description="店铺名称")
* @ApiParams (name="province", type="string", required=true, description="省")
* @ApiParams (name="city", type="string", required=true, description="市")
* @ApiParams (name="region", type="string", required=true, description="区")
* @ApiParams (name="name", type="string", required=true, description="联系人姓名")
* @ApiParams (name="mobile", type="string", required=true, description="联系人手机号")
* @ApiParams (name="code", type="string", required=true, description="验证码")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function store_apply()
{
$param = (new StoreValidate())->goCheck('store_apply');
// 验证码验证
$mobile = $param['mobile'];
$captcha = $param['code'];
$event = 'test';
$ret = Smslib::check($mobile, $captcha, $event);
if (!$ret) {
$this->error(__('验证码不正确'),$ret);
}
unset($param['code']);
Db::startTrans();
$result = false;
try{
$param['user_id'] = $this->auth->id;
$model = new StoreApply();
$result = $model->add($param);
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if(!$result) {
$this->error('提交失败');
}
$this->success('提交成功');
}
/**
* 举报建议提交
* @ApiWeigh (45)
*
* @ApiTitle (举报建议提交)
* @ApiSummary (举报建议提交)
* @ApiMethod (POST)
* @ApiRoute (/api/store/store_report)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="content", type="string", required=true, description="建议内容")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function store_report()
{
$param = (new StoreValidate())->goCheck('store_report');
Db::startTrans();
$result = false;
try{
$param['user_id'] = $this->auth->id;
$model = new Report();
$result = $model->add($param);
$id = $model->id;
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if(!$result) {
$this->error('提交失败');
}
$this->success('提交成功');
}
/**
* 商家首页
* @ApiWeigh (40)
*
* @ApiTitle (商家首页)
* @ApiSummary (商家首页)
* @ApiMethod (POST)
* @ApiRoute (/api/store/store)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
"id": 1,
"store_name": "123",
"image_arr": [
"http://cloud.caiyunpan.brotop.cn/assets/img/qrcode.png",
"http://cloud.caiyunpan.brotop.cn/assets/img/qrcode.png"
]
}
})
*/
public function store()
{
$store = $this->get_store();
$return = [
'id' => $store['id'],
'store_name' => $store['store_name'],
'image_arr' => $store['image_arr'],
];
$this->success('成功',$return);
}
/**
* 店铺信息
* @ApiWeigh (36)
*
* @ApiTitle (店铺信息)
* @ApiSummary (店铺信息)
* @ApiMethod (POST)
* @ApiRoute (/api/store/store_center)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
"store": {
"id": 1,
"user_id": 1,
"store_name": "123",
"mobile": "13911111111",
"store_icon": "http://cloud.caiyunpan.brotop.cn/assets/img/qrcode.png",
"province": "213",
"city": "2123",
"region": "123123",
"images": "/assets/img/qrcode.png,/assets/img/qrcode.png",
"content": "测试店铺详情",
"user_info": {
"id": 1,
"username": "admin"
},
"image_arr": [
"http://cloud.caiyunpan.brotop.cn/assets/img/qrcode.png",
"http://cloud.caiyunpan.brotop.cn/assets/img/qrcode.png"
]
}
}
})
*/
public function store_center()
{
$store = $this->get_store();
$return = [
'store' => $store
];
$this->success('成功',$return);
}
/**
* 店铺信息更新
* @ApiWeigh (35)
*
* @ApiTitle (店铺信息更新)
* @ApiSummary (店铺信息更新)
* @ApiMethod (POST)
* @ApiRoute (/api/store/store_edit)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="integer", required=true, description="店铺id")
* @ApiParams (name="store_icon", type="string", required=true, description="店铺图标")
* @ApiParams (name="store_name", type="string", required=true, description="店铺名称")
* @ApiParams (name="mobile", type="string", required=true, description="商家电话")
* @ApiParams (name="province", type="string", required=true, description="省")
* @ApiParams (name="city", type="string", required=true, description="市")
* @ApiParams (name="region", type="string", required=true, description="区")
* @ApiParams (name="images", type="string", required=true, description="宣传图/轮播图")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function store_edit()
{
$param = (new StoreValidate())->goCheck('store_edit');
$store = $this->get_store($param['id']);
Db::startTrans();
$result = false;
try{
$param['user_id'] = $this->auth->id;
$result = $this->store_model->edit($param);
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if(!$result) {
$this->error('保存失败');
}
$this->success('保存成功');
}
/**
* @ApiWeigh (34)
* @ApiTitle (已绑定社区列表)
* @ApiSummary (已绑定社区列表)
* @ApiMethod (POST)
* @ApiRoute (/api/store/house_list)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="area", type="string", required=false, description="市区地址例如:'天津市/西青区'")
* @ApiParams (name="keyword", type="string", required=false, description="关键字")
*
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
[
{
"id"://小区id
"name"://小区名称
"area"://所在区
}
]
}
})
*/
public function house_list()
{
$store = $this->get_store();
$where = [
'id' => ['in',$store['house_ids']]
];
$area = $this->request->param('area');
$keyword = $this->request->param('keyword');
if(!empty($area)){
$where['area'] = ['like',"%$area%"];
}
if(!empty($keyword)){
$where['name'] = ['like',"%$keyword%"];
}
$data = Db::name('house')
->where($where)
->field('id,name,area')
->order('createtime desc')
->select();
$this->success('success',$data);
}
/**
* 发布信息
* @ApiWeigh (30)
*
* @ApiTitle (发布信息)
* @ApiSummary (发布信息)
* @ApiMethod (POST)
* @ApiRoute (/api/store/store_inform_add)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="content", type="string", required=true, description="详细内容")
* @ApiParams (name="images", type="string", required=true, description="图片")
* @ApiParams (name="house_ids", type="string", required=true, description="推广社区")
* @ApiParams (name="type", type="integer", required=true, description="推广类型1=红包推送信息2=一般信息")
* @ApiParams (name="red_package", type="string", required=false, description="红包总金额")
* @ApiParams (name="number", type="string", required=false, description="红包数量")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function store_inform_add()
{
$param = (new StoreValidate())->goCheck('store_inform_add');
if($param['type'] == 1) {
$param = (new StoreValidate())->goCheck('red');
}
$store = $this->get_store();
if($param['type'] == 1) {
$param['single'] = $param['red_package'] / $param['number'];
$param['score'] = bcadd(config('site.send_score'),$param['red_package'] * config('site.withdraw_percent'),2);
if($this->auth->score < $param['score']) {
$this->error('板币不足,请充值');
}
}
Db::startTrans();
$result = false;
$res_user = $res_log = true;
try{
$param['user_id'] = $this->auth->id;
$param['house_ids'] = ','.$param['house_ids'].',';
$model = new StoreInform();
$result = $model->add($param);
if($param['type'] == 1) {
// 减少用户板币余额
$res_user = Db::name('user')->where('id',$param['user_id'])->setDec('score',$param['score']);
//记录钱包log
$insert_data = array(
'user_id' => $param['user_id'],
'score' => $param['score'],
'before' => $this->auth->score,
'after' => $this->auth->score - $param['score'],
'createtime' => time(),
'memo' => '发布信息',
);
$res_log = Db::name('user_score_log')->insert($insert_data);
}
if(!$result || !$res_user || !$res_log) {
Db::rollback();
} else {
Db::commit();
}
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if(!$result || !$res_user || !$res_log) {
$this->error('提交失败');
}
$this->success('提交成功');
}
/**
* 绑定新社区
* @ApiWeigh (25)
*
* @ApiTitle (绑定新社区)
* @ApiSummary (绑定新社区)
* @ApiMethod (POST)
* @ApiRoute (/api/store/store_new)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="house_ids", type="string", sample="店铺1id,店铺2id", required=true, description="绑定社区id")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function store_new()
{
$param = (new StoreValidate())->goCheck('store_new');
$store = $this->get_store();
$order_sn = $param['order_sn'] = get_order_sn();
// 获取配置
$app = new Application(ConfigService::load());
$payment = $app->payment;
// 获取支付参数
$attributes = [
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
'body' => '发布招募合伙人',
'out_trade_no' => $order_sn,
'total_fee' => 1, // $param['money'] * 100
'spbill_create_ip' => request()->ip(), // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
'notify_url' => url('index/ajax/store_order_notify',[],true,true), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'openid' => Db::name('third')->where('user_id',$this->auth->id)->value('openid'),
];
$order = new Order($attributes);
$order_result = $payment->prepare($order);
if($order_result['return_code'] == 'SUCCESS' && $order_result['result_code'] == 'SUCCESS') {
$prepayId = $order_result->prepay_id;
$pay_data = $payment->configForJSSDKPayment($prepayId);
} else {
$this->error($order_result['return_msg']);
}
$model = new StoreOrder();
Db::startTrans();
$result = false;
$result_invite = true;
try{
$param['store_id'] = $store['id'];
$param['user_id'] = $this->auth->id;
$result = $model->add($param);
$id = $model->id;
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if(!$result || !$result_invite) {
$this->error('提交失败');
}
$this->success('提交成功',['id'=>$id,'pay_data'=>$pay_data]);
}
/**
* 留言消息
* @ApiWeigh (24)
*
* @ApiTitle (留言消息)
* @ApiSummary (留言消息)
* @ApiMethod (POST)
* @ApiRoute (/api/store/comment_list)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="page", type="integer", required=true, description="页数")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
"list": [
{
"id": 1,
"name": "行业名称",
"weigh": 0,
}
]
}
})
*/
public function comment_list()
{
if($this->request->isPost()){
$param = (new StoreValidate())->goCheck('common');
$store = $this->get_store();
$page = $param['page'];
$where = [
'where' => ['store_id'=>$store['id'],'type'=>1],
'with' => ['user_info']
];
$order = ['createtime'=>'DESC'];
$model = new StoreComment();
$comment = $model->pageSelect($page,$where,'*',$order,config('option.num'));
$list = $comment->items();
$return = [
'list' => $list,
'this_page' => $comment->currentPage(),
'total_page' => $comment->lastPage()
];
$this->success('请求成功',$return);
}
}
/**
* 留言消息详情
* @ApiWeigh (23)
*
* @ApiTitle (留言消息详情)
* @ApiSummary (留言消息详情)
* @ApiMethod (POST)
* @ApiRoute (/api/store/comment_detail)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="comment_id", type="integer", required=true, description="留言id")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
"comment": [
{
"id": 1,
"name": "行业名称",
"weigh": 0,
}
]
}
})
*/
public function comment_detail()
{
if($this->request->isPost()){
$param = (new StoreValidate())->goCheck('comment_detail');
$store = $this->get_store();
$where = [
'where' => ['id'=>$param['comment_id']],
'with' => ['user_info']
];
$model = new StoreComment();
$comment = $model->findOrFail($where,'*');
$return = [
'comment' => $comment
];
$this->success('请求成功',$return);
}
}
/**
* 留言回复
* @ApiWeigh (22)
*
* @ApiTitle (留言回复)
* @ApiSummary (留言回复)
* @ApiMethod (POST)
* @ApiRoute (/api/store/comment_reply)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="comment_id", type="integer", required=true, description="留言id")
* @ApiParams (name="content", type="string", required=true, description="留言内容")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function comment_reply()
{
$param = (new StoreValidate())->goCheck('comment_reply');
Db::startTrans();
$result = false;
try{
$param['user_id'] = $this->auth->id;
$model = new StoreComment();
$result = $model->add($param);
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if(!$result) {
$this->error('提交失败');
}
$this->success('提交成功');
}
/**
* 板币充值列表
* @ApiWeigh (20)
*
* @ApiTitle (板币充值列表)
* @ApiSummary (板币充值列表)
* @ApiMethod (POST)
* @ApiRoute (/api/store/deposit)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
"list": [
{
"id": 1,
"money": "充值金额",
"score": "赠送板币",
"weigh": 1,
},
]
}
})
*/
public function deposit()
{
if($this->request->isPost()){
$where = [
'where' => []
];
$deposit = $this->deposit_model->selectOrFail($where,false,'*','weigh');
$return = [
'list' => $deposit,
];
$this->success('请求成功',$return);
}
}
/**
* 板币充值提交
* @ApiWeigh (15)
*
* @ApiTitle (板币充值提交)
* @ApiSummary (板币充值提交)
* @ApiMethod (POST)
* @ApiRoute (/api/store/deposit_order)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="deposit_id", type="integer", required=true, description="板币充值id")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function deposit_order()
{
$param = (new StoreValidate())->goCheck('deposit_order');
$store = $this->get_store();
$deposit = $this->deposit_model->findOrFail(['id'=>$param['depost_id']]);
$order_sn = $param['order_sn'] = get_order_sn();
$pay_data = [];
if($this->auth->end_time > time()) {
$param['status'] = 2;
} else {
$param['status'] = 1;
// 获取小程序配置
$options = \config('miniprogram.basic');
$app = new Application($options);
$payment = $app->payment;
// 获取支付参数
$attributes = [
'body' => '板币充值',
'out_trade_no' => $order_sn,
'total_fee' => 1, // $param['money'] * 100
'spbill_create_ip' => request()->ip(), // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
'notify_url' => url('index/ajax/notify',[],true,true), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
'openid' => Db::name('third')->where('user_id',$this->auth->id)->value('openid'),
];
$order = new Order($attributes);
$order_result = $payment->pay($order);
if($order_result['return_code'] == 'SUCCESS' && $order_result['result_code'] == 'SUCCESS') {
$prepayId = $order_result->prepay_id;
$pay_data = $payment->configForJSSDKPayment($prepayId); // 返回数组
} else {
$this->error($order_result['return_msg']);
}
}
$model = $this->deposit_order_model;
Db::startTrans();
$result = false;
$result_invite = true;
try{
$param['deposit_id'] = $store['id'];
$param['user_id'] = $this->auth->id;
$param['score'] = $deposit['score'];
$param['money'] = $deposit['money'];
$result = $model->add($param);
$id = $model->id;
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if(!$result || !$result_invite) {
$this->error('提交失败');
}
$this->success('提交成功',['id'=>$id,'status'=>$param['status'],'pay_data'=>$pay_data]);
}
/**
* 收支明细
* @ApiWeigh (10)
*
* @ApiTitle (收支明细)
* @ApiSummary (收支明细)
* @ApiMethod (POST)
* @ApiRoute (/api/store/score_log)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="page", type="integer", required=true, description="页数")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
"list": [
{
"id": 1,
"name": "行业名称",
"weigh": 0,
}
]
}
})
*/
public function score_log()
{
if($this->request->isPost()){
$param = (new StoreValidate())->goCheck('common');
$store = $this->get_store();
$page = $param['page'];
$where = [
'where' => ['store_id'=>$store['id']],
'with' => ['user_info']
];
$order = ['createtime'=>'DESC'];
$model = new UserScoreLog();
$score = $model->pageSelect($page,$where,'*',$order,config('option.num'));
$list = $score->items();
$return = [
'list' => $list,
'this_page' => $score->currentPage(),
'total_page' => $score->lastPage()
];
$this->success('请求成功',$return);
}
}
/**
* 提现
* @ApiWeigh (15)
*
* @ApiTitle (提现)
* @ApiSummary (提现)
* @ApiMethod (POST)
* @ApiRoute (/api/store/withdraw)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="money", type="integer", required=true, description="提现金额")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
})
*/
public function withdraw()
{
if($this->request->isPost()) {
$param = $this->request->param();
$validate = new \think\Validate([
'money' => 'require|number',
]);
$validate->message([
'money.require' => '请输入提现金额!',
'money.number' => '提现金额必须为数字!',
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
$withdraw_percent = config('site.withdraw_percent');
if ($param['money'] < 1) {
$this->error('提现金额不可小于1元');
}
if ($param['money'] > 1000) {
$this->error('提现金额不可大于1000元');
}
// 判断余额是否充足
if ($param['money'] * $withdraw_percent > $this->auth->money) {
$this->error('余额不足,无法提现');
}
// 提现记录
Db::startTrans();
$user_model = $this->auth->getUser();
$withdraw_model = new UserWithdraw();
$user = $user_model->where('id', $this->auth->id)->find();
$order_sn = date('YmdHis').rand(0000,9999);
$withdraw = [
'user_id' => $this->auth->id,
'before_money' => $this->auth->score,
'after_money' => $this->auth->score - $param['money'] * $withdraw_percent,
'order_sn' => $order_sn,
'money' => $param['money'],
];
$result = $withdraw_model->isUpdate(false)->save($withdraw);
// 记录用户余额
$result_user = $user_model->isUpdate(true)->save(['money' => $user['money'] - $param['money'] * $withdraw_percent]);
// 记录余额变更
$insert_data = array(
'user_id' => $this->auth->id,
'score' => $param['money'],
'before' => $this->auth->score,
'after' => $this->auth->score - $param['money'] * $withdraw_percent,
'createtime' => time(),
'memo' => '提现',
);
$res_log = Db::name('user_score_log')->insert($insert_data);
if (!$result || !$result_user || !$res_log) {
Db::rollback();
$this->error('提现申请失败', [$result, $result_user]);
}
// // 微信提现
// $withdraw_percent = config('site.withdraw_percent');
// $options = get_addon_config('epay');
// $wechat_option = [
// 'app_id' => $options['wechat']['appid'],
// 'app_secret' => $options['wechat']['app_secret'],
// 'payment' => [
// 'merchant_id' => $options['wechat']['mch_id'],
// 'key' => $options['wechat']['key'],
// 'cert_path' => ROOT_PATH . 'addons' . $options['wechat']['cert_client'],
// 'key_path' => ROOT_PATH . 'addons' . $options['wechat']['cert_key'],
// ]
// ];
// $app = new Application($wechat_option);
// $merchantPay = $app->merchant_pay;
// $merchantPayData = [
// 'partner_trade_no' => $order_sn, //随机字符串作为订单号,跟红包和支付一个概念。
// 'openid' => $user_openid, //收款人的openid
// 'check_name' => 'NO_CHECK', //文档中有三种校验实名的方法 NO_CHECK OPTION_CHECK FORCE_CHECK
// 'amount' => $param['money'] * (100 - $withdraw_percent), //单位为分
// 'desc' => '用户提现',
// 'spbill_create_ip' => request()->ip(), //发起交易的IP地址
// ];
// $result = $merchantPay->send($merchantPayData);
// if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
// // 记录用户余额
// $result_user = $user_model->isUpdate(true)->save(['money' => $this->auth->money - $param['money']]);
// // 余额变动记录
// $log = [
// 'user_id' => $this->auth->id,
// 'money' => $param['money'],
// 'before' => $this->auth->money,
// 'after' => $this->auth->money - $param['money'],
// 'memo' => '用户微信提现'
// ];
// $result_log = Db::name('user_money_log')->insertGetId($log);
// if (!$result_user || !$result_log) {
// Db::rollback();
// $this->error('提现申请失败');
// }
// } else {
// Db::rollback();
// $this->error($result['err_code_des']);
// }
Db::commit();
$this->success('提现申请成功');
}
}
/**
* 获取店铺信息
* @param string $id 店铺id
*/
private function get_store($id = null)
{
$where_s = ['user_id'=>$this->user_id];
if($id) {
$where_s['id'] = $id;
}
$where = [
'where' => $where_s,
'with' => ['user_info']
];
$store = $this->store_model->findOrFail($where,false);
if(!$store) {
$this->error('请先提交商家入驻申请');
}
if($store['status'] == 4) {
$this->error('请先提交商家入驻申请');
}
if($store['status'] == 1) {
$this->error('未提交入驻申请');
}
if($store['status'] == 2) {
$this->error('商家申请仍在审核中');
}
return $store;
}
}