Client.php
15.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
<?php
namespace app\api\controller\v1;
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 ;//测试用
$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')
->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']:'';
}
$this->success('活动获取成功', $activity);
}
/**
* 1.1参加活动用户信息
*/
public function participate_activitie(){
$activity_id=$this->postParam['id'];
$where['type']=1;
$where['verification_activity_id']=$activity_id;
$user=Db::name("verification_order")
->where($where)
->field('name,createtime')
->select();
if(!empty($user)){
for ($i=0;$i<count($user);$i++){
$sort=$i+1;
$user[$i]['sort']=$sort;
$user[$i]['createtime']=date("Y-m-d",$user[$i]['createtime']);
$user[$i]['name']=$this->substr_cut($user[$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'); //获取字符长度
$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'];
$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
];
$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,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'] = '0';//只查询代金券
$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;
foreach ($order['receive'] as $key => $value) {
if($order['receive'][$key]['image']){
$order['receive'][$key]['image']= request()->domain().$order['receive'][$key]['image'];
}
}
$this->success("查询成功",$order);
}else{
$this->error("查询失败");
}
}
/**
* 5、创建订单
*/
public function crateorder(){
$time=time();
$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("活动不存在!");
}
$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("添加订单失败!");
}
}
/**
* 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')->find();
$item['store_name'] = $store['name'];
//卡券名称
$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;
});
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("删除失败");
}
}
}