Browse Source

Merge branch 'develop' of ssh://8.134.10.79:222/Leadfyy.co/hainan into develop

develop
lemon 5 years ago
parent
commit
1aac8bb9ff
  1. 79
      app/Console/Commands/OrderTimeout.php
  2. 22
      app/Http/Controllers/Api/OrderController.php
  3. 34
      app/Http/Controllers/Api/VerificationController.php

79
app/Console/Commands/OrderTimeout.php

@ -0,0 +1,79 @@
<?php
namespace App\Console\Commands;
use App\Common\OrderStatus;
use App\Models\Order;
use App\Models\Product;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class OrderTimeout extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'order:timeout';
/**
* 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 int
*/
public function handle()
{
//记录最小ID,下次查询时按ID正序查询,只处理大于该ID的订单,避免重复扫描数据库
$min_id = Order::query()->where('status', OrderStatus::UNPAID)->min('id');
while (true) {
Order::query()
->where([
['status', '=', OrderStatus::UNPAID],
['id', '>=', $min_id],
])
->whereNotNull('timeout')
->orderBy('id')
->chunk(100, function ($order) use (&$min_id) {
foreach ($order as $v) {
if (!is_null($v->timeout) && strtotime($v->timeout) < time()) {
DB::beginTransaction();
try {
//取消订单
$v->status = OrderStatus::CANCEL;
$v->save();
//加回库存
Product::whereIn('id', explode(',', $v->product_ids))->increment('stock', $v->num);
DB::commit();
} catch (\Exception $exception) {
DB::rollBack();
}
}
$min_id = $v->id;
}
});
$this->line('[' . date('Y-m-d H:i:s') . "] ID游标:$min_id ,等待下一个任务");
sleep(3);
}
return 0;
}
}

22
app/Http/Controllers/Api/OrderController.php

@ -392,9 +392,9 @@ class OrderController extends Controller
{
$id = (int)request()->input('id');
$fields = ['id', 'order_no', 'agent_product_id', 'num', 'price', 'title', 'picture', 'status',
$fields = ['id', 'agent_id', 'order_no', 'agent_product_id', 'num', 'price', 'title', 'picture', 'status',
'pay_type', 'coupon_id', 'paid_money', 'paid_at', 'refund_info', 'verify_code', 'created_at'];
$order = Order::query()
$order = Order::with('agent:id,appid,appsecret')
->where('user_id', $this->user_id)
->find($id, $fields);
@ -405,6 +405,24 @@ class OrderController extends Controller
//订单ID和核销码拼接,查询时通过订单ID和核销码来查询,这样核销码不用建索引
$order->verify_code = $order->verify_code ? $order->id . '-' . $order->verify_code : '';
//如果有核销码,生成核销二维码
if ($order->verify_code) {
$config = [
'app_id' => $order->agent->appid,
'secret' => $order->agent->appsecret,
];
$app = Factory::miniProgram($config);
$response = $app->app_code->getUnlimit($order->verify_code, ['path' => 'pages/verification/index']);
if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
$filename = $response->saveAs(storage_path('app/public/verify_code'), $order->verify_code);
$order->verify_qrcode = Storage::disk('public')->url('verify_code/' . $filename);
}
}
unset($order->agent, $order->agent_id); //必须unset掉$order->agent,否则会造成appsecret泄漏
$order->coupon = Coupon::query()
->whereIn('id', $order->coupon_id)
->where(['agent_id' => $this->agent_id, 'agent_product_id' => $order->agent_product_id,])

34
app/Http/Controllers/Api/VerificationController.php

@ -47,38 +47,4 @@ class VerificationController extends Controller
return $this->success();
}
//生成核销二维码
public function qrcode()
{
$id = request()->input('id'); //订单ID
$order = Order::where(['agent_id' => $this->agent_id, 'user_id' => $this->user_id])->find($id);
if (!$order) {
return $this->error('订单不存在!');
} else if (!in_array($order->status, [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE, OrderStatus::OFFLINE_PAID, OrderStatus::REFUSED_REFUND])) {
return $this->error('当前订单状态不允许核销!');
} else if (!$order->verify_code) {
$order->verify_code = uniqid();
$order->save();
}
$verify_code = $order->id . '-' . $order->verify_code;
$agent = Agent::find($this->agent_id);
$config = [
'app_id' => $agent->appid,
'secret' => $agent->appsecret,
];
$app = Factory::miniProgram($config);
$response = $app->app_code->getUnlimit($verify_code, ['path' => 'pages/verification/index']);
if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
$filename = $response->saveAs(storage_path('app/public/verify_code'), $verify_code);
}
$prefix = Storage::disk('public')->url('verify_code/');
return $this->success(['qrcode' => $prefix . $filename]);
}
}
Loading…
Cancel
Save