TurnPrize.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
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
<?php
namespace app\products\controller;
use app\serviceapi\controller\TurnPrize as TurnPrizeModel;
use app\web\controller\BaseController;
use think\Log;
use think\Request;
use util\MCurl;
use think\Cookie;
use think\Session;
header("Content-type: text/html; charset=utf-8");
header('Access-Control-Allow-Origin:*');
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
/**
* 功能描述 抽奖管理
* Class TurnTable
* @package app\products\controller
* Date: 2020/8/25
* Time: 10:00
* @author nyq
*/
class TurnPrize extends BaseController
{
private $appid = 'wx3e41adc018004fd1';
private $appsecrt = 'f072ba0a6dbb24a2f6b504259367ecfc';
//请求地址
//private $api_url = "http://demo8.xiaochakeji.com/serviceapi/prize/";
//查询接口
//本地
//private $api_url = "www.aisigit.com/products/Turn_prize/";
//测试
//private $api_url = "http://demo6.xiaochakeji.com/products/Turn_prize/";
//生产
// private $api_url = "http://hzy.sosyun.com/products/Turn_prize/";
/**
* 函数功能描述 微信分享获取AccessToken
* Date: 2020/8/31
* Time: 10:00
* @author nyq
*/
public function getAccessToken() {
//查询数据表里面的值
$data['appid']= $this->appid;
$data['appsecrt']= $this->appsecrt;
$prize_win_model = new TurnPrizeModel();
$info = $prize_win_model->getAccessToken();
//$info = MCurl::instance($this->api_url."getAccessToken")->post($data);
$info = json_decode($info,true);
if(empty($info) || $info['expires_in'] < time()){
//获取token的值
$url_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->appsecrt;
$tmp = $this->curl_get($url_token); //json格式
$obj = json_decode($tmp,true);
if($obj['access_token'] != null){
$data['access_token'] = $obj['access_token'];
$data['expires_in'] = time() + $obj['expires_in'];
if($info){
$data['id'] = $info['id'];
$arr = $prize_win_model->updateAccessToken($data);
//$arr = MCurl::instance($this->api_url."updateAccessToken")->post($data);
}else{
$arr = $prize_win_model->addAccessToken($data);
// $arr = MCurl::instance($this->api_url."addAccessToken")->post($data);
}
return $data;
}else return 'error';
}else return $info;
}
/**
* 函数功能描述 微信分享获取Ticket
* Date: 2020/8/31
* Time: 10:00
* @author nyq
*/
public function getTicket(){
$token = $this->getAccessToken();
if(empty($token['jsapi_ticket']) || $token['expires_in'] < time()){
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$token['access_token']."&type=jsapi";
$tmp = $this->curl_get($url); //json格式
$obj = json_decode($tmp,true);
$data['id'] = $token['id'];
$data['jsapi_ticket'] = $obj['jsapi_ticket'];
$prize_win_model = new TurnPrizeModel();
$arr = $prize_win_model->updateAccessToken($data);
//$arr = MCurl::instance($this->api_url."updateAccessToken")->post($data);
return $obj['jsapi_ticket'];
}else{
return $token['jsapi_ticket'];
}
}
/**
* 函数功能描述 微信分享获取sdk
* Date: 2020/8/31
* Time: 10:00
* @author nyq
*/
public function generateSign(Request $request){
$dataurl = $request->param('url');
//echo $dataurl;die;
$url=str_replace('amp;','',$dataurl);
$noncestr = uniqid();
$timestamp = time();
//$url = 'http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
$ticket = $this->getTicket();
if ($ticket) {
$str = 'jsapi_ticket='.$ticket.'&noncestr='.$noncestr.'×tamp='.$timestamp.'&url='.$url;
//return $str;die;
$signature = sha1($str);
$data['ticket'] = $ticket;
$data['noncestr'] = $noncestr;
$data['timestamp'] = $timestamp;
$data['signature'] = $signature;
$data['appId'] = $this->appid;
$data['link'] = $url;
return $data;
}
}
/**
* 函数功能描述 抽奖用户模拟微信授权 返回 openid
* Date: 2020/8/26
* Time: 10:00
* @author nyq
*/
public function getWxCode(Request $request)
{
$data = $request->param();
$code=$data['code'];
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->appid . "&secret=" . $this->appsecrt . "&code=$code&grant_type=authorization_code";
$r = $this->curl_get($url);
$openid=json_decode($r,true);
return $openid;
}
/**
* 函数功能描述 获取订单信息 / 获取奖品列表接口
* Date: 2020/8/27
* Time: 10:00
* @author nyq
*/
public function prize_list(Request $request)
{
$data = $request->param();
//分页暂未开启,保留.
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 25;
$offset=($page-1)*$rows;
$data['offset']=$offset;
$data['rows']=$rows;
$prize_win_model = new TurnPrizeModel();
$res = $prize_win_model->prizeList($data);
//$res = MCurl::instance($this->api_url."prizeList")->post($data);
$json=json_decode($res,true);
foreach($json as $key=>$val ){
$json[$key]['addTime']=date('Y-m-d H:i:s',$val['addTime']);
if($val['display'] ==0){
$json[$key]['display']="已发布";
}else{
$json[$key]['display']="未发布";
}
}
$result["total"] =count($json);
$result['rows']=$json;
echo json_encode($result);
//return $json;
}
/**
* 函数功能描述 文件上传
* Date: 2020/8/07
* Time: 10:00
* @author nyq
*/
public function uploadFileImgs(){
$files = request()->file('files');
$path = input('path');
if($files){
$imgarr = [];
foreach($files as $file){
$info = $file->validate(['size'=>204800000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public/web/' . DS . 'prize');
if($info){
$newurl = '/web/prize/'.$info->getSaveName();
$img= str_replace('\\','/',$newurl);
$imgarr[]=$img;
}
}
return json_encode($imgarr);
}else{
echo '没有图片上传';
}
}
/**
* 函数功能描述 删除中奖用户列表
* Date: 2020/8/07
* Time: 10:00
* @author nyq
*/
public function del_user()
{
$data = $this->request->post();
$prize_win_model = new TurnPrizeModel();
$res = $prize_win_model->delUser($data);
//$res = MCurl::instance($this->api_url."delUser")->post($data);
$json=json_decode($res,true);
if(is_numeric($json)){
$data=[
'code'=>200,
'msg'=>'删除成功'
];
}else{
$data=[
'code'=>100,
'msg'=>'删除失败'
];
}
return $data;
}
/**
* 函数功能描述 添加奖品
* Date: 2020/8/07
* Time: 10:00
* @author nyq
*/
public function prize_add(Request $request)
{
$data = $request->param();
$data['addTime']=time();
$prize_win_model = new TurnPrizeModel();
$res = $prize_win_model->prizeAdd($data);
//return $res;
// $res = MCurl::instance($this->api_url."prizeAdd")->post($data);
$json=json_decode($res,true);
if(is_numeric($json)){
$data=[
'code'=>200,
'msg'=>'修改成功'
];
}else{
$data=[
'code'=>100,
'msg'=>'修改失败'
];
}
return $data;
}
/**
* 函数功能描述 中奖用户列表
* Date: 2020/8/26
* Time: 10:00
* @author nyq
*/
public function prize_user_list(Request $request)
{
$data = $request->param();
$page = $data['page'];
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
$offset=($page-1)*$rows;
$data['offset']=$offset;
$data['rows']=$rows;
$prize_win_model = new TurnPrizeModel();
$res = $prize_win_model->prizeUserList($data);
//var_dump($res); die;
//$res = MCurl::instance($this->api_url."prizeUserList")->post($data);
//$res=json_decode($res,true);
$json=$res['rows'];
unset($data['id']);
Cookie::set('data', $data);
foreach($json as $key=>$val ){
$json[$key]['addTime']=date('Y-m-d H:i:s',$val['addTime']);
if(isset($val['num']) && $val['num'] !=1 ){
$json[$key]['prizeName']="双击查看";
}
if($val['receive'] ==0){
$json[$key]['receive']="未领取";
}else{
$json[$key]['receive']="已领取";
}
}
$last_names = array_column($json,'addTime');
array_multisort($last_names,SORT_DESC,$json);
//获取当前的条数
// if(isset($data['phone'])&&!empty($data['phone'])) {
// $count = count($json);
// }else{
// $count = MCurl::instance($this->api_url . "prizeUserCount")->post($data);
// $count = json_decode($count, true);
// }
$result["total"] =$res['total'];
$result['rows']=$json;
echo json_encode($result);
//return $json;
}
/**
* 函数功能描述 导出数据到表格
* Date: 2020/8/26
* Time: 10:00
* @author nyq
*/
public function to_excel()
{
$data = $this->request->Post();
$data= Cookie::get('data');
$data['to_excel']=1;
$prize_win_model = new TurnPrizeModel();
$json = $prize_win_model->prizeUserList($data);
//$res = MCurl::instance($this->api_url."prizeUserList")->post($data);
// $json=json_decode($res,true);
foreach($json as $key=>$val ){
$json[$key]['addTime']=date('Y-m-d H:i:s',$val['addTime']);
}
ob_end_clean();//清除缓冲区,避免乱码
Vendor('PHPExcel.PHPExcel');
$objPHPExcel=new \PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '用户名')
->setCellValue('B1', '电话')
->setCellValue('C1', '奖品名称')
->setCellValue('D1', '部门')
->setCellValue('E1', '时间');
$rowIndex = 2;
foreach ($json as $k => $v) {
$objPHPExcel->getActiveSheet()
->setCellValue('A' . $rowIndex, $v['uname'])
->setCellValue('B' . $rowIndex, $v['phone'])
->setCellValue('C' . $rowIndex, $v['prizeName'])
->setCellValue('D' . $rowIndex, $v['source'])
->setCellValue('E' . $rowIndex, $v['addTime']);
$rowIndex++;
}
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(25);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(25);
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(25);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(25);
$filename = '抽奖用户数据表';
header('Content-Type: application/vnd.ms-excel');
$ua = $_SERVER["HTTP_USER_AGENT"];
header('Content-type: text/html; charset=gbk');
header("Content-type:application/vnd.ms-excel;charset=gbk");
//兼容IE11
if (preg_match("/MSIE/", $ua) || preg_match("/Trident\/7.0/", $ua)) {
header('Content-Disposition: attachment;filename="' . urlencode($filename) . '.xls"');
} else if (preg_match("/Firefox/", $ua)) {
header('Content-Disposition: attachment;filename*="' . $filename . '.xls"');
} else {
header('Content-Disposition: attachment; filename="' . $filename . '.xls"');
}
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: cache, must-revalidate');
header('Pragma: public');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
}
/**
* 函数功能描述 重置抽奖次数
* Date: 2020/8/26
* Time: 10:00
* @author nyq
*/
public function prize_reset(Request $request)
{
//$data = $request->param();
$prize_win_model = new TurnPrizeModel();
$res = $prize_win_model->prizeReset();
//$res = MCurl::instance($this->api_url."prizeReset")->post($data);
$json=json_decode($res,true);
if(is_numeric($json)){
$data=[
'code'=>200,
'msg'=>'重置成功'
];
}else{
$data=[
'code'=>100,
'msg'=>'重置失败'
];
}
return $data;
}
/**
* 函数功能描述 规则设置 // 获取抽奖规则接口
* Date: 2020/8/26
* Time: 10:00
* @author nyq
*/
public function prize_rule(Request $request)
{
//$data = $request->param();
$prize_win_model = new TurnPrizeModel();
$res = $prize_win_model->prizeRule();
//$res = MCurl::instance($this->api_url."prizeRule")->post($data);
$json=json_decode($res,true);
$code="fg5Fu7Is9aHJ5ug5".time();
$json['openid']=$code;
return $json;
// $json=json_encode($res);
// $aa= html_entity_decode($json);
// //数据特殊符号转义
// $aa = htmlspecialchars_decode($aa);
// return $aa;
}
/**
* 函数功能描述 修改规则设置
* Date: 2020/8/26
* Time: 10:00
* @author nyq
*/
public function prize_rule_edit(Request $request)
{
$data = $request->param();
//file_put_contents('aaqq.txt',$data);
//$data['title']=json_encode($data['title'],true);
$data['addTime']=time();
//return $data;
if(isset($data['open']) && $data['open']=="on"){
$data['open']=1;
}else{
$data['open']=0;
}
$prize_win_model = new TurnPrizeModel();
$res = $prize_win_model->prizeRuleEdit($data);
//$res = MCurl::instance($this->api_url."prizeRuleEdit")->post($data);
$json=json_decode($res,true);
if(is_numeric($json)){
$data=[
'code'=>200,
'msg'=>'修改成功'
];
}else{
$data=[
'code'=>100,
'msg'=>'修改失败'
];
}
return $data;
}
/**
* 函数功能描述 获取抽奖规则接口
* Date: 2020/8/26
* Time: 10:00
* @author nyq
*/
public function prize_rule_api(Request $request)
{
$data = $request->param();
$prize_win_model = new TurnPrizeModel();
$res = $prize_win_model->prizeRuleEdit($data);
// $res = MCurl::instance($this->api_url."prizeRuleEdit")->post($data);
$json=json_decode($res,true);
if(is_numeric($json)){
$data=[
'code'=>200,
'msg'=>'重置成功'
];
}else{
$data=[
'code'=>100,
'msg'=>'重置失败'
];
}
return $data;
}
/**
* 函数功能描述 首次进入 合适身份信息
* Date: 2020/8/26
* Time: 10:00
* @author nyq
*/
public function sel_prize_member(Request $request)
{
$data = $request->param();
//给我 openID number
//查看当前用户是否存在
$prize_win_model = new TurnPrizeModel();
$res = $prize_win_model->selPrizeMember($data);
return $res;
// //$res = MCurl::instance($this->api_url."selPrizeMember")->post($data);
// $json=json_decode($res,true);
// return $json;
}
/**
* 函数功能描述 抽奖接口
* Date: 2020/8/26
* Time: 10:00
* @author nyq
*/
public function luck_draw(Request $request)
{
//传递 openID prize_id
$data = $request->param();
//查看当前用户是否存在
$prize_win_model = new TurnPrizeModel();
$json = $prize_win_model->luckDraw($data);
//$res = MCurl::instance($this->api_url."luckDraw")->post($data);
//$json=json_decode($res,true);
//传递 openID prize_id
return $json;
}
/**
* 函数功能描述 获取最新的20条中奖列表 / 个人的奖品列表
* Date: 2020/8/26
* Time: 10:00
* @author nyq
*/
public function win_list(Request $request)
{
//传递 openID
$data = $request->param();
//查看当前用户是否存在
$prize_win_model = new TurnPrizeModel();
$res = $prize_win_model->prizeWinList($data);
//$res = MCurl::instance($this->api_url."prizeWinList")->post($data);
$json=json_decode($res,true);
foreach($json as $key=>$val ){
$json[$key]['addTime']=date('Y-m-d H:i:s',$val['addTime']);
}
//传递 openID prize_id
return $json;
}
/**
* 函数功能描述 兑奖接口
* Date: 2020/8/26
* Time: 10:00
* @author nyq
*/
public function cash_prize(Request $request)
{
//传递 openID
$data = $request->param();
//查看当前用户是否存在
$prize_win_model = new TurnPrizeModel();
$res = $prize_win_model->cashPrize($data);
//$res = MCurl::instance($this->api_url."cashPrize")->post($data);
$json=json_decode($res,true);
if($json==1){
$data=[
'code'=>200,
'msg'=>'成功'
];
}else{
$data=[
'code'=>100,
'msg'=>'失败'
];
}
//传递 openID prize_id
return $data;
}
/**
* 发起GET请求
* @param string $url 请求地址
* @param array $data 请求参数
* @param string $cookiePath 请求所保存的cookie路径
*/
function curl_get($url, $data=[], $cookiePath = '')
{
if(count($data)!=0) {
$url = get_query_url($url, $data);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiePath);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiePath);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
}