Client.php
19.6 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
<?php
namespace app\api\controller\v1;
use app\admin\model\User;
use app\common\controller\Api;
use think\Db;
use think\Exception;
/**
* 用户端接口
*/
class Client extends Base
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
protected $postParam;
protected $store_id;
protected $store;
public function _initialize()
{
$postParam = $this->request->param();
$this->postParam = $postParam;
$res=$this->request->header();
parent::_initialize();
//$this->auth->id=19;//测试用
$postParam = $this->request->param();
$this->postParam = $postParam;
if (empty($postParam['store_id'])) {
$this->error("门店store_id异常");
}
$this->store_id = $postParam['store_id'];
$store = Db::name("verification_store")->where("id", $this->store_id)->find();
if (empty($store)) {
$this->error("门店不存在,请退出登录后重试!");
}
$this->store = $store;
}
/**
* 1、获取活动
*/
public function getactivity(){
//根据门店查询活动
$time=time();
$where["closetime"] = [">", $time];
$where["verification_store_id"] = ["=", $this->store_id];
$activity=Db::name('verification_activity')
->alias('a')
->join('verification_store b','a.verification_store_id=b.id')
->field('a.*,b.address,b.name,b.phone as store_phone')
->where($where)->find();
if(!empty($activity)){
$store = Db::name('verification_store')->where(['id'=>$activity['verification_store_id']])->field('lat,lng')->find();
$activity['lat'] = $store['lat'];
$activity['lng'] = $store['lng'];
$activity['image'] = !empty($activity['poster'])?request()->domain().$activity['image']:'';
$activity['poster'] = !empty($activity['poster'])?request()->domain().$activity['poster']:'';
$activity['share_image'] = !empty($activity['share_image'])?request()->domain().$activity['share_image']:'';
}
$this->success('活动获取成功', $activity);
}
/**
* 1.1参加活动用户信息
*/
public function participate_activitie(){
$activity_id=$this->postParam['id'];
$where['type']=1;
$where['verification_activity_id']=$activity_id;
$page = $this->request->post('page', 1);
$total = $this->request->post('total', 10);
$user=Db::name("verification_order")
->where($where)
->field('name,createtime')
->order("id",'DESC')
->paginate($total, false, ['page' => $page])
->toArray();
if(!empty($user['data'])){
for ($i=0;$i<count($user['data']);$i++){
$sort=$i+1;
$user['data'][$i]['sort']=$sort;
$user['data'][$i]['createtime']=date("Y-m-d",$user['data'][$i]['createtime']);
$user['data'][$i]['name']=$this->substr_cut($user['data'][$i]['name']);
}
}
$this->success('查询成功',$user);
}
/**
* 1.1.1名字加*
* @param $user_name
* @return string
*/
function substr_cut($user_name){
$strlen = mb_strlen($user_name, 'utf-8'); //获取字符长度
if($strlen <= 1){
return $user_name;
}
$firstStr = mb_substr($user_name, 0, 1, 'utf-8'); //查找字符第一个
$str=$firstStr . str_repeat('*', $strlen - 1); //拼接第一个+把字符串 "* " 重复 $strlen - 1 次:
return $str;
}
/**
* 2、举办活动
*/
public function holdactivity(){
$verification_store_id=$this->store_id;
$name=$this->postParam['name'];
$phone=$this->postParam['phone'];
$industry=$this->postParam['industry'];
$address=$this->postParam['address'];
$verification_activity_id=$this->postParam['id'];
//半个小时提交一次
$time=time();
$res=Db::name('verification_hold_activity')->where(["user_id"=>$this->auth->id])->order('id','desc')->find();
$newtime=$time-1800;
if($res['createtime']>$newtime){
$this->error("请勿重复提交");
}
$data=[
'verification_store_id'=>$verification_store_id,
'name'=>$name,
'phone'=>$phone,
'industry'=>$industry,
'address'=>$address,
'verification_activity_id'=>$verification_activity_id,
'user_id'=>$this->auth->id,
'createtime'=>$time
];
$res=Db::name('verification_hold_activity')->insertGetId($data);
if($res){
$this->success("提交成功");
}else{
$this->error("提交失败");
}
}
/**
* 3、查询个人订单
*/
public function myorder(){
$id=$this->auth->id;
$type=$this->postParam['type'];
$verification_store_id=$this->store_id;
$where['a.verification_store_id']=$verification_store_id;
$page = $this->request->post('page', 1);
$total = $this->request->post('total', 10);
if($type){
$where['a.type']=$type;
}
$where['a.user_id'] = $id;
$row=Db::name('verification_order')
->alias('a')
->join('verification_store b','a.verification_store_id=b.id')
->join('verification_activity c','a.verification_activity_id=c.id')
->field('a.id,c.id as activity_id,a.type,a.name as user_name,b.name,a.phone,a.status,c.image,a.order_no,c.title,b.address,b.lng,b.lat,a.price,a.payMode,DATE_FORMAT(FROM_UNIXTIME(a.paytime), "%Y-%m-%d %H:%i:%S") AS paytime')
->where($where)
->order('a.id','desc')
->paginate($total, false, ['page' => $page])
->toArray();
foreach ($row['data'] as $key => $value) {
if ($row['data'][$key]['image']) {
$row['data'][$key]['image'] = request()->domain() . $row['data'][$key]['image'];
}
}
return $this->success("查询成功",$row);
}
/**
* 4、订单详情
*/
public function orderinfo(){
$order_id=$this->postParam['id'];
$where['a.id']=$order_id;
$where['a.user_id']=$this->auth->id;
$verification_store_id=$this->store_id;
$where['a.verification_store_id']=$verification_store_id;
$order=Db::name('verification_order')
->alias('a')
->where($where)
->join('verification_store b','a.verification_store_id=b.id')
->join('verification_activity c','a.verification_activity_id=c.id')
->field('a.id,a.type,a.status,c.image,a.order_no,c.title,b.address,b.lng,b.lat,a.price,a.payMode,DATE_FORMAT(FROM_UNIXTIME(a.paytime), "%Y-%m-%d %H:%i:%S") AS paytime')
->find();
if($order){
$order['image'] = !empty($order['image'])?request()->domain().$order['image']:'';
$receivewhere['a.verification_order_id']=$order_id;
$receivewhere['a.coupon_type'] = '2';//只查询消费券
$receivewhere['a.user_id'] = $this->auth->id;
$receive=Db::name('verification_receive')
->alias('a')
->join('verification_coupon b','a.verification_coupon_id=b.id')
->field("a.id,b.name,a.type,
b.type as verification_coupon_type,
b.closetime,b.voucher_amount,b.gift,b.consumption,b.reduction,b.consumption_name,b.image,DATE_FORMAT(FROM_UNIXTIME(b.closetime), '%Y-%m-%d') AS closetime")
->where($receivewhere)->select();
$time = time();
if(!empty($receive)){
//过期检测
foreach($receive as $k=>&$v){
$v['expire'] = false;//快过期 false不过期
//已过期
if(strtotime($v['closetime']." 23:59:59") < $time){
Db::name('verification_receive')->where(['id'=>$v['id']])->update(['type'=>'2']);
$v['type'] = '2';
}else{
//是否快过期 接近一天时间
if((strtotime($v['closetime']) - $time) <= 3600 * 24){
$v['expire'] = true;
}
}
}
}
$order['receive']=$receive;
$isuser=0;
$onuser=0;
$overuser=0;
foreach ($order['receive'] as $key => $value) {
if($order['receive'][$key]['image']){
$order['receive'][$key]['image']= request()->domain().$order['receive'][$key]['image'];
}
if($order['receive'][$key]['type']==1){
$isuser++;
}
if($order['receive'][$key]['type']==0){
$onuser++;
}
if($order['receive'][$key]['type']==2){
$overuser++;
}
}
$order['onuser']=$onuser;
$order['isuser']=$isuser;
$order['overuser']=$overuser;
$this->success("查询成功",$order);
}else{
$this->error("查询失败");
}
}
/**
* 5、创建订单
*/
public function crateorder(){
$time=time();
if(empty($this->auth->id)){
$this->error('登录失败');
}
$verification_activity_id=$this->postParam['verification_activity_id'];
if(empty($verification_activity_id)){return $this->error('活动信息错误');}
//查询到活动
$where["id"] = ["=", $verification_activity_id];
$activity=Db::name('verification_activity')->where($where)->find();
//判断活动是否到期
if(!empty($activity)){
if($activity['closetime']<=$time){
$this->error("活动已过期!");
}
}else{
$this->error("活动不存在!");
}
//每个微信用户只能购买一次一个活动
$count = Db::name('verification_order')->where(['user_id'=>$this->auth->id,'type'=>['<>','0']])->count();
if($count >=1){
$this->error("该活动仅限购买1次!");
}
$phone=$this->postParam['phone'];
$name=$this->postParam['name'];
$verification_coupon_ids=$activity['verification_coupon_ids'];
$order_no=$time.rand(10000,99999);
$verification_store_id=$this->store_id;
$user_id=$this->auth->id;
$createtime=$time;
$type=0;
//整理数组
$data=[
'order_no'=>$order_no,
'verification_store_id'=>$verification_store_id,
'user_id'=>$user_id,
'createtime'=>$createtime,
'type'=>$type,
'price'=>$activity['price'],
'verification_coupon_ids'=>$verification_coupon_ids,
'verification_activity_id'=>$verification_activity_id,
'name'=>$name,
'phone'=>$phone
];
//插入订单表
$res=Db::name('verification_order')->insertGetId($data);
if($res){
$res = (new \app\api\controller\v1\Wechat())->prepare_order($order_no);
$this->success('添加订单成功',$res);
}else{
$this->error("添加订单失败!");
}
}
/**
* 5.1立即支付
*/
public function immediate_payment(){
$order_id=$this->postParam['id'];
Db::startTrans();
try{
$new_order_no=time().rand(10000,99999);
//获取到订单信息
$res=Db::name('verification_order')->where(['id'=>$order_id])->find();
if($res){
$res=Db::name('verification_order')->where(['id'=>$res['id']])->update([
'order_no'=>$new_order_no
]);
}
$res = (new \app\api\controller\v1\Wechat())->prepare_order($new_order_no);
Db::commit();
}catch (Exception $e){
Db::rollback();
$this->error($e->getMessage());
}
if($res){
$this->success('添加订单成功',$res);
}else{
$this->success('添加订单失败');
}
}
/**
* 6、卡卷详情
*/
public function receiveinfo(){
$receive_id=$this->postParam['id'];
$where['a.id']=$receive_id;
$where['a.user_id']=$this->auth->id;
$field="a.*,b.image,b.name as store_name,c.name as coupon_name";
$receive=Db::name('verification_receive')
->alias('a')
->join('verification_store b','a.verification_store_id=b.id')
->join('verification_coupon c','a.verification_coupon_id = c.id','left')
->where($where)
->field($field)
->find();
if(!empty($receive)){
$receive['qr_code'] = !empty($receive['qr_code'])?request()->domain().$receive['qr_code']:'';
$receive['image'] = !empty($receive['image'])?request()->domain().$receive['image']:'';
$receive['closetime'] = date("Y-m-d",$receive['closetime']);
}
$this->success("查询成功",$receive);
}
/*
7.生成二维码 及 规则 https://coupon.xp.yn.cn?store_id=1&activity_id=3&share_id=2
二维码 没有则创新
参数 用户id 门店id 活动id
share_id store_id activiy_id
请求需要加 header token
由前端自行合成分销图片
*/
public function synthesis(){
//参数
$param = request()->param();
$user_id = $this->auth->id;
//验证
if(empty($user_id)){return $this->error('用户信息错误');}
if(empty($param['store_id'])){return $this->error('请确认门店');}
if(empty($param['activity_id'])){return $this->error('请确认活动');}
$w['user_id'] = $user_id;
$w['store_id'] = $param['store_id'];
$w['activity_id'] = $param['activity_id'];
$r = Db::name('verification_user_synthesis')->where($w)->find();
//有 则 返回 无 则新增
if(empty($r)){
$param['share_id']=$user_id;
$url = (new \app\api\controller\v1\Base())->url_h5.'?'.http_build_query($param);
$res = (new \app\api\controller\v1\Index())->build($url);
$w['create_at'] = time();
$w['url'] = request()->domain().$res;
Db::name('verification_user_synthesis')->insert($w);
$res = $w['url'];
}else{
$res = $r['url'];
}
//获取 当前活动当前门店的背景图
$st_a['verification_store_id'] = $param['store_id'];
$st_a['id'] = $param['activity_id'];
$storeActivity = Db::name('verification_activity')->where($st_a)->find();
$data['ercode_url'] = $res;//二维码地址
$data['bg_url'] = !empty($storeActivity['poster'])?request()->domain().$storeActivity['poster']:'';//背景图
return $this->success('',$data);
}
/*
我的礼品券
store_id
分页参数page
coupon_type 券类型 卡卷类型:0=代金卷,1=礼品卷,2=消费卷,3=满减卷
type 卡卷状态:0=未使用,1=已使用,2=已过期
*/
public function my_receive_coupons_recode(){
//参数
$param = request()->param();
$user_id = $this->auth->id;
//验证
if(empty($user_id)){return $this->error('用户信息错误');}
if(empty($param['store_id'])){return $this->error('请确认门店');}
if(!is_numeric($param['type'])){return $this->error('请确认卡券类型');}
$w['user_id'] = $user_id;
$w['verification_store_id'] = $param['store_id'];
$w['coupon_type'] = 1;
$w['type'] = $param['type'];
$time = time();
$data = Db::name('verification_receive')->where($w)->field("*")->paginate(10)->each(function($item) use($time){
$item['qr_code'] = !empty($item['qr_code'])?request()->domain().$item['qr_code']:'';
//门店名称
$store = Db::name('verification_store')->where(['id'=>$item['verification_store_id']])->field('name,lat,lng,address')->find();
$item['store_name'] = $store['name'];
$item['lat'] = $store['lat'];
$item['address'] = $store['address'];
$item['lng'] = $store['lng'];
//卡券名称
$coupon = Db::name('verification_coupon')->where(['id'=>$item['verification_coupon_id']])->field('name')->find();
$item['coupon_name'] = $coupon['name'];
if($item['closetime'] < $time){
//过期
Db::name('verification_receive')->where(['id'=>$item['id']])->update(['type'=>'2']);
$item['type'] = '2';
}
$item['closetime'] = date("Y-m-d",$item['closetime']);
return $item;
});
$where['user_id'] = $user_id;
$where['verification_store_id'] = $param['store_id'];
$where['coupon_type'] = 1;
$where['type'] = 0;
$time = time();
$isuser=0;
$onuser=0;
$overuser=0;
$onuser = Db::name('verification_receive')->where($where)->count();
$where['type'] = 1;
$isuser=Db::name('verification_receive')->where($where)->count();
$where['type'] = 2;
$overuser=Db::name('verification_receive')->where($where)->count();
if ($data){
$data=$data->toArray();
}
$data['overuser']=$overuser;
$data['onuser']=$onuser;
$data['isuser']=$isuser;
return $this->success('查询成功',$data);
}
/**
* 11、代理人邀请
*/
public function agentinvite(){
$name=$this->postParam['name'];
$phone=$this->postParam['phone'];
$group=$this->postParam['group'];
$user_id=$this->auth->id;
$verification_store_id=$this->store_id;
$type=1;
//先查询是否存在
$where['user_id']=['=',$user_id];
$where['verification_store_id']=['=',$verification_store_id];
$isexist=Db::name('verification_staff')->where($where)->find();
if (!empty($isexist)){
return $this->error("请勿重复提交");
}
$data=[
'name'=>$name,
'phone'=>$phone,
'group'=>$group,
'user_id'=>$user_id,
'verification_store_id'=>$verification_store_id
];
$res=Db::name('verification_staff')->insertGetId($data);
if ($res){
return $this->success("提交成功");
}else{
return $this->error("提交失败");
}
}
/**
* 12、删除订单
*/
public function deleteorder(){
$order_id=$this->postParam['id'];
//查询到订单信息
$order=Db::name('verification_order')->find($order_id);
if($order['type']==0){
Db::startTrans();
try{
$res=Db::name('verification_order')->where(['id'=>$order_id])->delete();
Db::commit();
}catch (Exception $e){
Db::rollback();
$this->error($e->getMessage());
}
return $this->success("删除成功");
}else{
return $this->error("删除失败");
}
}
public function SendMessage(){
$wxxcxpush=new WxxcxPush();
$res=$wxxcxpush->Message('o5OQw6NqWE8Xc34S9kQIzYeFPNUM');
if ($res !== false) {
$this->success('请求成功',$res);
} else {
$this->error(__('No rows were inserted'));
}
}
}