You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
312 lines
12 KiB
312 lines
12 KiB
<?php
|
|
|
|
namespace App\AdminAgent\Controllers;
|
|
|
|
use App\AdminAgent\Renderable\SelectIndustryProductSpec;
|
|
use App\AdminAgent\Repositories\IndustryOrder;
|
|
use App\Common\OrderStatus;
|
|
use App\Common\PayType;
|
|
use App\Common\ProductStatus;
|
|
use App\Models\AdminSetting;
|
|
use App\Models\IndustryProduct;
|
|
use App\Models\IndustryProductSpec;
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Form;
|
|
use Dcat\Admin\Grid;
|
|
use Dcat\Admin\Show;
|
|
use Dcat\Admin\Http\Controllers\AdminController;
|
|
use EasyWeChat\Factory;
|
|
use EasyWeChat\Kernel\Http\StreamResponse;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class IndustryOrderController extends AdminController
|
|
{
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
return Grid::make(new IndustryOrder(['supplier:id,company_name,contact_phone']), function (Grid $grid) {
|
|
$grid->disableRowSelector();
|
|
$grid->disableCreateButton();
|
|
$grid->disableActions();
|
|
|
|
$grid->column('id')->sortable();
|
|
$grid->column('supplier.company_name', '供应商');
|
|
$grid->column('supplier.contact_phone', '供应商电话');
|
|
$grid->column('order_no')->limit(10);
|
|
$grid->column('num');
|
|
$grid->column('price');
|
|
$grid->column('name', '预留姓名');
|
|
$grid->column('mobile', '预留手机');
|
|
$grid->column('title');
|
|
$grid->column('picture')->image('', 80, 80);
|
|
$grid->column('status')
|
|
->using(OrderStatus::array())
|
|
->if(fn() => $this->status == OrderStatus::UNPAID)
|
|
->display(fn() => '<a class="btn btn-sm btn-primary" href="' . admin_url('industry_order/list', $this->id) . '">付款</a>')
|
|
->if(fn() => $this->status == OrderStatus::PAY_EARNEST)
|
|
->display(fn() => '<a class="btn btn-sm btn-primary" href="' . admin_url('industry_order/list', $this->id) . '">付尾款</a>');
|
|
$grid->column('paid_at', '订单确认时间');
|
|
$grid->column('verify_qrcode', '核销二维码')
|
|
->if(fn() => $this->verify_code)
|
|
->then(function ($column) {
|
|
$verify_code = $this->id . '-' . $this->verify_code;
|
|
$column->append(admin_url('industry_order/qrcode', $verify_code))->image('', 60, 60);
|
|
$column->append('<br>' . $verify_code);
|
|
})
|
|
->else()
|
|
->display('');
|
|
$grid->column('created_at');
|
|
|
|
$grid->filter(function (Grid\Filter $filter) {
|
|
$filter->equal('id')->width(2);
|
|
$filter->equal('order_no')->width(3);
|
|
});
|
|
});
|
|
}
|
|
|
|
//生成核销二维码,行业产品订单使用支付小程序核销
|
|
public function qrcode()
|
|
{
|
|
$verify_code = request()->route('verify_code');
|
|
|
|
$qrcode = storage_path("app/public/industry_verify_code/$verify_code.jpg");
|
|
if (file_exists($qrcode)) {
|
|
return redirect(Storage::disk('public')->url("industry_verify_code/$verify_code.jpg"));
|
|
}
|
|
|
|
$setting = AdminSetting::val(['payee_appid', 'payee_appsecret']);
|
|
$config = [
|
|
'app_id' => $setting['payee_appid'],
|
|
'secret' => $setting['payee_appsecret'],
|
|
];
|
|
$app = Factory::miniProgram($config);
|
|
|
|
//由于参数最多只能32个字符,故通过下面这种方式传参
|
|
//pt表示使用普通订单,使用api/verification/verify接口核销;
|
|
//hy表示行业产品订单,使用api/verification/industry_verify接口核销
|
|
$response = $app->app_code->getUnlimit('hy' . $verify_code, ['page' => 'pages/verification/index']);
|
|
|
|
if ($response instanceof StreamResponse) {
|
|
$filename = $response->saveAs(storage_path('app/public/industry_verify_code'), $verify_code); //保存二维码
|
|
// $qrcode = Storage::disk('public')->url('industry_verify_code/' . $filename); //获取前端路径
|
|
header("Content-Type: " . $response->getHeaderLine('Content-Type'));
|
|
exit($response); //输出图片
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
*
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
return Show::make($id, new IndustryOrder(['supplier:id,company_name', 'spec']), function (Show $show) {
|
|
$show->disableEditButton();
|
|
$show->disableDeleteButton();
|
|
|
|
$show->field('id');
|
|
$show->field('supplier.company_name', '供应商');
|
|
$show->field('order_no');
|
|
$show->field('status')->using(OrderStatus::array())->label();
|
|
$show->field('pay_type')->using(PayType::array());
|
|
$show->field('spec', '规格')->as(fn() => ($this->spec->name ?? '') . ' | ' . ($this->spec->date ?? ''));
|
|
$show->field('num');
|
|
$show->field('price');
|
|
$show->field('name', '姓名');
|
|
$show->field('mobile');
|
|
$show->field('title');
|
|
$show->field('picture')->image('', 80, 80);
|
|
$show->field('paid_at');
|
|
$show->field('created_at', '下单时间');
|
|
|
|
//付款对话框 weixin://wxpay/bizpayurl?pr=sk9zOCwzz
|
|
if (in_array($show->model()->status, [OrderStatus::UNPAID, OrderStatus::PAY_EARNEST])) {
|
|
$pay_config = $this->payConfig($show->model()->id);
|
|
if (empty($pay_config['code_url'])) {
|
|
if (isset($pay_config['result_code'], $pay_config['err_code_des']) && $pay_config['result_code'] != 'SUCCESS') {
|
|
$msg = $pay_config['err_code_des'];
|
|
} else {
|
|
$msg = $pay_config['return_msg'] ?? '获取支付信息失败';
|
|
}
|
|
Admin::script("Dcat.swal.info('支付失败:$msg', null);");
|
|
} else {
|
|
Admin::js('@qrcode');
|
|
Admin::script(<<<JS
|
|
Dcat.swal.info(null, null, {
|
|
type: null,
|
|
html: '<div id="qrcode" style="margin-top:1rem;"></div>',
|
|
imageWidth: 240,
|
|
imageHeight: 240,
|
|
animation: false,
|
|
confirmButtonText: '我已支付,刷新页面',
|
|
allowOutsideClick: false,
|
|
allowEscapeKey: false,
|
|
onOpen: function () {
|
|
$('#qrcode').qrcode({text:'{$pay_config['code_url']}', width:240, height:240});
|
|
},
|
|
onClose: function() {
|
|
window.location.reload();
|
|
}
|
|
});
|
|
JS
|
|
);
|
|
}
|
|
} else {
|
|
redirect(admin_url('industry_order/list'))->send();
|
|
}
|
|
});
|
|
}
|
|
|
|
//付款
|
|
private function payConfig($order_id)
|
|
{
|
|
$order = \App\Models\IndustryOrder::where('agent_id', Admin::user()->id)
|
|
->whereIn('status', [OrderStatus::UNPAID, OrderStatus::PAY_EARNEST])->find($order_id);
|
|
if (!$order) {
|
|
Admin::exit('订单不存在或已支付');
|
|
}
|
|
|
|
$config = AdminSetting::val(['payee_appid', 'payee_mchid', 'payee_mchkey']);
|
|
$config = [
|
|
'app_id' => $config['payee_appid'],
|
|
'mch_id' => $config['payee_mchid'],
|
|
'key' => $config['payee_mchkey'],
|
|
'notify_url' => route('industry_product_wxpay_notify'),
|
|
];
|
|
$app = Factory::payment($config);
|
|
|
|
//计算价格
|
|
if ($order->status == OrderStatus::PAY_EARNEST) {
|
|
$price = $order->price - $order->paid_money;
|
|
} else {
|
|
$price = $order->price;
|
|
}
|
|
|
|
return $app->order->unify([
|
|
'product_id' => $order->industry_product_id,
|
|
'body' => mb_strcut($order->title, 0, 127),
|
|
'out_trade_no' => $order->order_no . '-' . $order->status, //后面加status,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付
|
|
'total_fee' => round($price * 100), //支付金额单位为分
|
|
'trade_type' => 'NATIVE', // 请对应换成你的支付方式对应的值类型
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
$pid = request()->input('pid');
|
|
$industry = IndustryProduct::where([
|
|
['status', '=', ProductStatus::ON_SALE],
|
|
['stock', '>', 0],
|
|
])->find($pid);
|
|
|
|
return Form::make(new IndustryOrder(), function (Form $form) use ($industry) {
|
|
if (!$industry) {
|
|
Admin::exit('订单不允许编辑');
|
|
}
|
|
|
|
$form->selectTable('industry_product_spec_id', '选择产品规格')
|
|
->required()
|
|
->title('选择产品规格')
|
|
->dialogWidth('80%;min-width:825px;')
|
|
->from(SelectIndustryProductSpec::make(['industry_product_id' => request()->input('pid')]))
|
|
->model(IndustryProductSpec::class);
|
|
|
|
$form->hidden('pid')->value($industry->id); //pid要跟上面的request中的一样,否则提交出错
|
|
$form->number('num')
|
|
->min($industry->min_sale)->required()
|
|
->default($industry->min_sale);
|
|
$form->text('name')->default(Admin::user()->director)->required();
|
|
$form->mobile('mobile')->default(Admin::user()->contact_phone)->required();
|
|
|
|
$pay_type = [PayType::ONLINE, PayType::OFFLINE];
|
|
if ($industry->deposit) { //订金支付
|
|
$pay_type = [...$pay_type, PayType::DEPOSIT_PAY];
|
|
}
|
|
if ($industry->earnest) { //定金支付
|
|
$pay_type = [...$pay_type, PayType::EARNEST_PAY];
|
|
}
|
|
$options = array_filter(PayType::array(), fn($k) => in_array($k, $pay_type), ARRAY_FILTER_USE_KEY);
|
|
$form->select('pay_type')->options($options)->default(PayType::ONLINE)->required();
|
|
|
|
$form->divider();
|
|
$form->text('', '购买产品')->default($industry->title)->disable();
|
|
$form->text('', '单价')->default($industry->price)->disable();
|
|
$form->text('', '库存')->default($industry->stock)->disable();
|
|
$form->text('', '起购数量')->default($industry->min_sale)->disable();
|
|
$form->image('picture', '产品图')->default($industry->pictures)->disable();
|
|
$form->display('', '旅游须知')->default(fn() => preg_replace('/<script.*?>.*?<\/script>/is', '', $industry->know))->disable();
|
|
$form->display('', '产品详情')->default(fn() => preg_replace('/<script.*?>.*?<\/script>/is', '', $industry->content))->disable();
|
|
})->saving(function (Form $form) use ($industry) {
|
|
//禁止编辑
|
|
if ($form->isEditing()) {
|
|
return $form->response()->error('操作禁止');
|
|
}
|
|
|
|
//判断最小起购数
|
|
if ($form->num < $industry->min_sale) {
|
|
return $form->response()->error('购买数量不能小于最低起购数:' . $industry->min_sale);
|
|
}
|
|
|
|
//判断产品状态和库存
|
|
if (!$industry || $industry->status != ProductStatus::ON_SALE || $industry->stock < $form->num) {
|
|
return $form->response()->error('产品已下架或库存不足');
|
|
}
|
|
|
|
//生成订单号
|
|
list($micro, $sec) = explode(' ', microtime());
|
|
$micro = str_pad(floor($micro * 1000000), 6, 0, STR_PAD_LEFT);
|
|
$order_no = date('ymdHis', $sec) . $micro . mt_rand(1000, 9999);
|
|
|
|
//产品规格处理
|
|
$spec = IndustryProductSpec::where('industry_product_id', $form->pid)->find($form->industry_product_spec_id);
|
|
if (!$spec) {
|
|
return $form->response()->error('您选择的产品规格不存在');
|
|
}
|
|
|
|
$form->deleteInput(['pid', 'picture']);
|
|
|
|
$form->hidden(['industry_product_id', 'supplier_id', 'agent_id', 'order_no', 'price', 'title', 'picture', 'status', 'pay_type', 'paid_at', 'verify_code', 'trade_deposit', 'timeout']);
|
|
|
|
$form->name = $form->name ?? Admin::user()->director;
|
|
$form->mobile = $form->mobile ?? Admin::user()->contact_phone;
|
|
|
|
$form->industry_product_id = $industry->id;
|
|
$form->supplier_id = $industry->supplier_id;
|
|
$form->agent_id = Admin::user()->id;
|
|
$form->order_no = $order_no;
|
|
$form->price = $form->num * $spec->price;
|
|
$form->title = $industry->title;
|
|
$form->picture = $industry->pictures[0] ?? '' ;
|
|
$form->status = $form->pay_type == PayType::OFFLINE ? OrderStatus::OFFLINE_UNPAID : OrderStatus::UNPAID;
|
|
$form->paid_at = null;
|
|
$form->verify_code = '';
|
|
$form->trade_deposit = $form->num * $industry->single_deposit;
|
|
$form->single_price = $industry->single_deposit;
|
|
$form->timeout = null;
|
|
|
|
if ($form->pay_type == PayType::DEPOSIT_PAY) {
|
|
$form->prepay_price = $industry->deposit;
|
|
} else if ($form->pay_type == PayType::EARNEST_PAY) {
|
|
$form->prepay_price = $industry->earnest;
|
|
} else {
|
|
$form->prepay_price = 0;
|
|
}
|
|
})->saved(function (Form $form) {
|
|
return $form->response()->success('下单成功,请等待供应商审核订单')->redirect(admin_url('industry_order/list'));
|
|
})->deleting(function (Form $form) {
|
|
return $form->response()->error('操作禁止');
|
|
});
|
|
}
|
|
}
|