Send_RedPack.php
4.12 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
<?php
namespace App\Console\Commands;
use App\Modules\Models\CouponOrder\CouponOrder;
use App\Modules\Models\RedPack\RedPack;
use Illuminate\Console\Command;
use EasyWeChat\Factory;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class Send_RedPack extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'Send_RedPack {--day=}';
/**
* The console command description.
*
* @var string
*/
protected $description = '发送红包';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//
Log:info('Send_RedPack----start');
$config = config('wechat.payment.redpack');
$app = Factory::payment($config);
$time = date('Y-m-d');
$day = date( "Y-m-d", strtotime( "$time -1 day" ) );
$start_time = $day.' 00:00:00';
$end_time = $day.' 23:59:59';
$datas = CouponOrder::select('customers.id as uid','customers.openid as oid', 'coupon_order.wx_guide_id', 'wx_guide.bonus',DB::raw('COUNT(coupon_order.wx_guide_id) as num'))
->leftJoin('wx_guide', 'coupon_order.wx_guide_id', '=', 'wx_guide.id')
->leftJoin('customers', 'wx_guide.user_id', '=', 'customers.id')
->where('coupon_order.status', '=', 2)
->where('coupon_order.updated_at','<=',$end_time)
->where('coupon_order.updated_at', '>=', $start_time)
->groupBy('coupon_order.wx_guide_id', 'customers.openid', 'customers.id', 'wx_guide.bonus')
->get()
->toArray();
foreach ($datas as $data){
if(!$data['uid'] || !$data['oid']){
continue;
}
$money = $data['bonus'] * $data['num'];
$order_id = 'RP' . date('Ymd') . str_pad(mt_rand(1, 9999999999), 10, '0', STR_PAD_LEFT);//R201807238820290612
$redpackData = [
'mch_billno' => $order_id,
'send_name' => '灰兔智能',
're_openid' => $data['oid'],
'total_num' => 1, //固定为1,可不传
'total_amount' => $money, //单位为分,不小于100//
'wishing' => '晓兔祝您万事如意, 心想事成',
//'client_ip' => '192.168.0.1', //可不传,不传则由 SDK 取当前客户端 IP
'act_name' => '优惠券',
'remark' => '景区导游',
// ...
];
$result = $app->redpack->sendNormal($redpackData);
$js_result = json_encode($result, JSON_UNESCAPED_UNICODE);
Log::info('[redpack params]: '.$js_result);
//{"return_code":"SUCCESS","return_msg":"发放成功","result_code":"SUCCESS","err_code":"SUCCESS","err_code_des":"发放成功","mch_billno":"RP201810169185965624","mch_id":"1487106062","wxappid":"wxc2696bc3c9241968","re_openid":"oVIM80iK-7631xOrWcuhOuXjnpoY","total_amount":"100","send_listid":"1000041701201810163000174641430"}
if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
$da = [
'send_list_id'=>$result['send_listid'],
'money'=>$result['total_amount'],
'user_id'=>$data['uid'],
'order_no'=>$result['mch_billno'],
'wx_guide_id'=>$data['wx_guide_id'],
'status'=>1,
'comment'=>$js_result
];
}else{
$da = [
//'send_list_id'=>$result['send_listid'],
'money'=>$money,
'user_id'=>$data['uid'],
'order_no'=>$order_id,
'wx_guide_id'=>$data['wx_guide_id'],
'status'=>2,
'comment'=>$js_result
];
}
RedPack::create($da);
}
}
}