multi-store/app/api/controller/order/OrderController.php

553 lines
23 KiB
PHP
Raw Normal View History

<?php
namespace app\api\controller\order;
use app\api\logic\order\OrderLogic;
use app\api\controller\BaseApiController;
use app\api\lists\order\OrderList;
2024-06-03 22:45:25 +08:00
use app\api\validate\OrderValidate;
use app\common\enum\PayEnum;
2024-06-08 09:43:24 +08:00
use app\common\enum\YesNoEnum;
use app\common\logic\PaymentLogic;
use app\common\logic\PayNotifyLogic;
2024-06-08 09:43:24 +08:00
use app\common\model\dict\DictData;
use app\common\model\store_order\StoreOrder;
2024-06-06 14:58:20 +08:00
use app\common\model\system_store\SystemStoreStaff;
2024-06-13 16:00:19 +08:00
use app\common\model\user\User;
use app\common\model\user\UserAddress;
use Webman\RedisQueue\Redis;
class OrderController extends BaseApiController
{
2024-06-08 09:43:24 +08:00
public $notNeedLogin = ['refund_reason'];
/**
* 订单列表
*/
public function order_list()
{
return $this->dataLists(new OrderList());
}
2024-06-14 15:53:47 +08:00
// #[
// ApiDoc\Title('核销码查数据'),
// ApiDoc\url('/api/order/order/write_code'),
// ApiDoc\Method('POST'),
// ApiDoc\Param(name: "code", type: "string", require: false, desc: "核销码"),
// ApiDoc\NotHeaders(),
// ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"),
// ApiDoc\ResponseSuccess("data", type: "array"),
// ]
2024-06-05 22:47:49 +08:00
public function write_code()
{
$code = $this->request->post('code');
if (empty($code)) {
2024-06-05 22:47:49 +08:00
return $this->fail('缺失参数');
}
$res = OrderLogic::getOne($code);
if ($res) {
2024-06-06 14:58:20 +08:00
$res = $res[0];
}
return $this->success('ok', $res);
2024-06-05 22:47:49 +08:00
}
2024-06-14 15:53:47 +08:00
// #[
// ApiDoc\Title('核销订单列表'),
// ApiDoc\url('/api/order/order/write_list'),
// ApiDoc\Method('POST'),
// ApiDoc\Param(name: "status", type: "int", require: true, desc: "1待核销2已核销"),
// ApiDoc\Param(name: "name", type: "string", require: false, desc: "搜商品或者订单id"),
// ApiDoc\Param(name: "page_no", type: "int", require: true, desc: "默认1页数"),
// ApiDoc\Param(name: "page_size", type: "int", require: false, desc: "条数默认15"),
// ApiDoc\NotHeaders(),
// ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"),
// ApiDoc\ResponseSuccess("data", type: "array"),
// ]
2024-06-05 13:27:17 +08:00
public function write_list()
{
$status = (int)$this->request->post('status', 1);
$page_no = (int)$this->request->post('page_no', 1);
$page_size = (int)$this->request->post('page_size', 15);
2024-06-05 13:27:17 +08:00
$params = $this->request->post();
if (empty($page_no) || empty($page_size)) {
2024-06-05 14:19:11 +08:00
$params['page_no'] = 1;
$params['page_size'] = 15;
}
2024-06-05 13:27:17 +08:00
$info = $this->userInfo;
$res = OrderLogic::write_list($info, $status, $params);
$res['page_no'] = $params['page_no'];
$res['page_size'] = $params['page_size'];
return $this->success('ok', $res);
2024-06-05 13:27:17 +08:00
}
2024-06-05 15:11:42 +08:00
2024-06-14 15:53:47 +08:00
// #[
// ApiDoc\Title('核销数量'),
// ApiDoc\url('/api/order/order/write_count'),
// ApiDoc\Method('POST'),
// ApiDoc\Param(name: "name", type: "string", require: false, desc: "搜商品或者订单id"),
// ApiDoc\NotHeaders(),
// ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"),
// ApiDoc\ResponseSuccess("data", type: "array"),
// ]
2024-06-05 15:11:42 +08:00
public function write_count()
{
$info = $this->userInfo;
$params = $this->request->post();
$res = OrderLogic::write_count($info, $params);
return $this->success('ok', $res);
2024-06-05 15:11:42 +08:00
}
2024-06-14 15:53:47 +08:00
// #[
// ApiDoc\Title('订单校验'),
// ApiDoc\url('/api/order/order/checkOrder'),
// ApiDoc\Method('POST'),
// ApiDoc\Param(name: "cart_id", type: "int", require: true, desc: "购物车id"),
// ApiDoc\Param(name: "address_id", type: "int", require: true, desc: "地址id"),
// ApiDoc\Param(name: "store_id", type: "int", require: true, desc: "店铺id"),
// ApiDoc\Param(name: "verify_code", type: "int", require: true, desc: "校验码"),
// ApiDoc\Param(name: "shipping_type", type: "int", require: true, desc: "配送方式"),
// ApiDoc\Param(name: "pay_type", type: "int", require: true, desc: "支付类型"),
// ApiDoc\NotHeaders(),
// ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"),
// ApiDoc\ResponseSuccess("data", type: "array", children: [
// ['name' => 'order', 'desc' => '订单信息', 'type' => 'array', 'children' => [
// ['name' => 'create_time', 'desc' => '订单创建时间', 'type' => 'int'],
// ['name' => 'order_id', 'desc' => '订单id', 'type' => 'int'],
// ['name' => 'total_price', 'desc' => '订单总金额', 'type' => 'float'],
// ['name' => 'pay_price', 'desc' => '实际支付金额', 'type' => 'float'],
// ['name' => 'total_num', 'desc' => '订单总数量', 'type' => 'int'],
// ['name' => 'pay_type', 'desc' => '支付方式', 'type' => 'int'],
// ['name' => 'cart_id', 'desc' => '购物车id', 'type' => 'string'],
// ['name' => 'store_id', 'desc' => '店铺id', 'type' => 'int'],
// ['name' => 'shipping_type', 'desc' => '配送方式', 'type' => 'int'],
// ]],
// ['name' => 'cart_list', 'desc' => '购物车商品列表', 'type' => 'array', 'children' => [
// ['name' => 'goods', 'desc' => '商品id', 'type' => 'int'],
// ['name' => 'cart_num', 'desc' => '购买数量', 'type' => 'int'],
// ['name' => 'total', 'desc' => '商品总价', 'type' => 'float'],
// ['name' => 'price', 'desc' => '商品单价', 'type' => 'float'],
// ['name' => 'product_id', 'desc' => '商品id', 'type' => 'int'],
// ['name' => 'old_cart_id', 'desc' => '原购物车id', 'type' => 'string'],
// ['name' => 'verify_code', 'desc' => '校验码', 'type' => 'string'],
// ]],
// ]),
// ]
public function checkOrder()
{
$cartId = (array)$this->request->post('cart_id', []);
$addressId = (int)$this->request->post('address_id');
// $pay_type = (int)$this->request->post('pay_type');
// $auth_code = $this->request->post('auth_code'); //微信支付条码
$params = $this->request->post();
2024-06-13 16:00:19 +08:00
$user=User::where('id',$this->userId)->find();
$res = OrderLogic::cartIdByOrderInfo($cartId, $addressId, $user, $params);
if ($res == false) {
$msg = OrderLogic::getError();
if ($msg == '购物车为空') {
return $this->data([]);
}
return $this->fail(OrderLogic::getError());
}
return $this->data($res);
}
2024-06-14 15:53:47 +08:00
// #[
// ApiDoc\Title('创建订单'),
// ApiDoc\url('/api/order/order/createOrder'),
// ApiDoc\Method('POST'),
// ApiDoc\Param(name: "cart_id", type: "int", require: true, desc: "id"),
// ApiDoc\Param(name: "store_id", type: "int", require: true, desc: "店铺id"),
// ApiDoc\Param(name: "address_id", type: "int", require: true, desc: "地址id"),
// ApiDoc\Param(name: "auth_code", type: "string", require: true, desc: "付款码"),
// ApiDoc\Param(name: "pay_type", type: "int", require: true, desc: "支付类型"),
// ApiDoc\NotHeaders(),
// ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"),
// ApiDoc\ResponseSuccess("data", type: "array"),
// ]
public function createOrder()
{
$cartId = (array)$this->request->post('cart_id', []);
2024-06-03 21:57:23 +08:00
$store_id = (array)$this->request->post('store_id', 0);
$pay_type = (int)$this->request->post('pay_type');
$addressId = (int)$this->request->post('address_id');
$auth_code = $this->request->post('auth_code'); //微信支付条码
$params = $this->request->post();
2024-06-03 21:57:23 +08:00
if ($store_id <= 0 && $pay_type != 9) {
return $this->fail('自提点不能为空');
}
if (count($cartId) > 100) {
return $this->fail('购物车商品不能超过100个');
}
$user=User::where('id',$this->userId)->find();
$order = OrderLogic::createOrder($cartId, $addressId, $user, $params);
2024-06-13 17:05:43 +08:00
if($order['pay_price'] <= 0){
$pay_type = 3;
}
if ($order != false) {
switch ($pay_type) {
case PayEnum::PURCHASE_FUNDS:
//采购款支付
PayNotifyLogic::handle('purchase_funds', $order['order_id']);
return $this->success('采购款支付成功');
case PayEnum::BALANCE_PAY:
//余额支付
2024-06-04 17:33:11 +08:00
PayNotifyLogic::handle('balancePay', $order['order_id']);
2024-06-04 17:21:57 +08:00
return $this->success('余额支付成功');
case PayEnum::CASH_PAY:
//现金支付
2024-06-04 17:33:11 +08:00
PayNotifyLogic::handle('cash_pay', $order['order_id']);
return $this->success('现金支付成功');
case PayEnum::WECHAT_PAY_MINI:
//微信小程序支付
$redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
$result = PaymentLogic::pay($pay_type, 'wechat_common', $order, $this->userInfo['terminal'] ?? 1, $redirectUrl);
if (PaymentLogic::hasError()) {
return $this->fail(PaymentLogic::getError(), $params);
}
return $this->success('', $result);
case PayEnum::WECHAT_PAY_BARCODE:
//微信条码支付
$result = PaymentLogic::codepay($auth_code, $order);
if (PaymentLogic::hasError()) {
return $this->fail(PaymentLogic::getError(), $params);
}
if (isset($result['trade_state_desc']) && $result['trade_state_desc'] == '支付成功') {
PayNotifyLogic::handle('StoreOrder', $result['out_trade_no'], $result);
} else {
Redis::send('send-code-pay', ['number' => $order['number']]);
return $this->success('用户支付中');
}
return $this->success('支付成功', ['out_trade_no' => $result['out_trade_no'], 'pay_type' => PayEnum::WECHAT_PAY_BARCODE, 'transaction_id' => $result['transaction_id']]);
case PayEnum::ALIPAY_BARCODE:
//支付宝条码支付
$result = PaymentLogic::ali_auth_code($auth_code, $order);
if (PaymentLogic::hasError()) {
return $this->fail(PaymentLogic::getError(), $params);
}
if ($result['msg'] !== 'Success') {
return $this->success('用户支付中');
}
$result['create_time'] = $order['create_time'];
return $this->success('支付成功', ['out_trade_no' => $result['out_trade_no'], 'pay_type' => PayEnum::ALIPAY_BARCODE, 'transaction_id' => $result['trade_no']]);
default:
return $this->fail('支付方式错误');
}
// return $this->data(['order_id' => $order->id]);
} else {
return $this->fail(OrderLogic::getError());
}
}
2024-06-06 13:43:23 +08:00
public function order_count()
{
$userId = $this->request->userId;
$where = ['uid' => $userId, 'paid' => 0];
$no_pay = StoreOrder::where($where)->count(); //待付款
2024-06-06 16:42:35 +08:00
2024-06-06 13:43:23 +08:00
$where['paid'] = 1;
$where['status'] = 1;
$waiting = StoreOrder::where($where)->count(); //待核销
2024-06-06 16:42:35 +08:00
$where['status'] = 2;
$receiving = StoreOrder::where($where)->count(); //已核销
2024-06-06 16:42:35 +08:00
$where['status'] = -1;
$applyRefund = StoreOrder::where($where)->count(); //申请退款
2024-06-06 16:42:35 +08:00
$where['status'] = 4;
$refund = StoreOrder::where($where)->count(); //已退款
2024-06-06 16:42:35 +08:00
$all = StoreOrder::where(['uid' => $userId])->count(); //全部
return $this->success('ok', ['no_pay' => $no_pay, 'waiting' => $waiting, 'receiving' => $receiving, 'all' => $all, 'applyRefund' => $applyRefund, 'refund' => $refund]);
2024-06-06 13:43:23 +08:00
}
2024-06-14 15:53:47 +08:00
// #[
// ApiDoc\Title('订单支付'),
// ApiDoc\url('/api/order/order/pay'),
// ApiDoc\Method('POST'),
// ApiDoc\Param(name: "order_id", type: "int", require: true, desc: "订单id"),
// ApiDoc\Param(name: "address_id", type: "int", require: true, desc: "地址id"),
// ApiDoc\Param(name: "auth_code", type: "string", require: true, desc: "付款码"),
// ApiDoc\Param(name: "pay_type", type: "int", require: true, desc: "支付类型"),
// ApiDoc\NotHeaders(),
// ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"),
// ApiDoc\ResponseSuccess("data", type: "array"),
// ]
2024-06-04 16:51:26 +08:00
public function pay()
{
$order_id = (int)$this->request->post('order_id');
$addressId = (int)$this->request->post('address_id');
$pay_type = (int)$this->request->post('pay_type');
$auth_code = $this->request->post('auth_code'); //微信支付条码
$params = $this->request->post();
$where = [
'id' => $order_id,
'uid' => $this->userId,
'paid' => 0,
];
$order = StoreOrder::where($where)->find();
2024-06-04 16:51:26 +08:00
if (!$order) {
return $this->fail('订单不存在或已支付');
}
switch ($pay_type) {
case PayEnum::BALANCE_PAY:
//余额支付
2024-06-04 17:21:57 +08:00
PayNotifyLogic::handle('balancePay', $order['order_id']);
return $this->success('余额支付成功');
case PayEnum::CASH_PAY:
//现金支付
2024-06-04 17:33:11 +08:00
PayNotifyLogic::handle('cash_pay', $order['order_id']);
return $this->success('现金支付成功');
break;
case PayEnum::WECHAT_PAY_MINI:
//微信小程序支付
$redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
if ($addressId != $order['address_id']) {
$address = UserAddress::where(['id' => $addressId, 'uid' => Request()->userId])->find();
if ($address) {
$_order['real_name'] = $address['real_name'];
$_order['user_phone'] = $address['phone'];
$_order['user_address'] = $address['detail'];
StoreOrder::where(['id' => $order_id, 'uid' => Request()->userId])->update($_order);
}
}
$result = PaymentLogic::pay($pay_type, 'wechat_common', $order, $this->userInfo['terminal'] ?? 1, $redirectUrl);
if (PaymentLogic::hasError()) {
return $this->fail(PaymentLogic::getError());
}
return $this->success('', $result);
break;
case PayEnum::WECHAT_PAY_BARCODE:
//微信条码支付
$result = PaymentLogic::codepay($auth_code, $order);
if (PaymentLogic::hasError()) {
return $this->fail(PaymentLogic::getError(), $params);
}
if (isset($result['trade_state_desc']) && $result['trade_state_desc'] == '支付成功') {
PayNotifyLogic::handle('StoreOrder', $result['out_trade_no'], $result);
} else {
Redis::send('send-code-pay', ['number' => $order['number']]);
return $this->success('用户支付中');
}
return $this->success('支付成功', ['out_trade_no' => $result['out_trade_no'], 'pay_type' => PayEnum::WECHAT_PAY_BARCODE, 'transaction_id' => $result['transaction_id']]);
break;
case PayEnum::ALIPAY_BARCODE:
//支付宝条码支付
$result = PaymentLogic::ali_auth_code($auth_code, $order);
if (PaymentLogic::hasError()) {
return $this->fail(PaymentLogic::getError(), $params);
}
if ($result['msg'] !== 'Success') {
return $this->success('用户支付中');
}
return $this->success('支付成功', ['out_trade_no' => $result['out_trade_no'], 'pay_type' => PayEnum::ALIPAY_BARCODE, 'transaction_id' => $result['trade_no']]);
break;
default:
return $this->fail('支付方式错误');
}
return $this->fail('支付失败');
}
2024-06-08 09:43:24 +08:00
2024-06-14 15:53:47 +08:00
// #[
// ApiDoc\Title('订单详情'),
// ApiDoc\url('/api/order/order/detail'),
// ApiDoc\Method('GET'),
// ApiDoc\Param(name: "order_id", type: "int", require: true, desc: "订单id"),
// ApiDoc\NotHeaders(),
// ApiDoc\ResponseSuccess("data", type: "array"),
// ]
public function detail()
{
$order_id = (int)$this->request->get('order_id');
$where = [
'id' => $order_id,
'uid' => $this->userId,
];
2024-06-12 16:59:33 +08:00
$url = 'https://'.$this->request->host(true);
$order = OrderLogic::detail($where,$url);
if ($order) {
return $this->data($order);
} else {
return $this->fail('订单不存在');
}
}
/**
* 获取用户常用购买记录
*/
public function frequently_purchase()
{
$params = $this->request->get();
$res = OrderLogic::frequentlyPurchase($params);
if (OrderLogic::hasError()) {
return $this->fail(OrderLogic::getError());
} else {
return $this->data($res);
}
}
/**
* 取消订单
*/
public function cancel_order()
{
$order_id = (int)$this->request->post('order_id');
$value = (int)$this->request->post('value');
$where = [
'id' => $order_id,
'uid' => $this->userId,
'paid' => 0,
];
$order = StoreOrder::where($where)->find();
if ($order) {
2024-06-08 17:21:55 +08:00
$data = ['cancle_reason' => $value, 'delete_time' => time()];
StoreOrder::where($where)->update($data);
return $this->success('取消成功');
}
return $this->fail('取消失败');
}
/**
* 确认收货
*/
public function confirm_receipt()
{
$order_id = (int)$this->request->post('order_id');
$where = [
'id' => $order_id,
'uid' => $this->userId,
'paid' => 1,
'status' => 1,
];
$order = StoreOrder::where($where)->find();
if ($order) {
$data = ['status' => 2];
StoreOrder::where($where)->update($data);
return $this->success('确认成功');
}
return $this->fail('确认失败');
}
/**
* 再次购买
*/
public function purchase_again()
{
$order_id = (int)$this->request->get('order_id');
$res = OrderLogic::purchaseAgain($order_id);
if ($res == false) {
return $this->fail('添加失败');
} else {
return $this->success('添加成功');
}
}
2024-06-04 16:51:26 +08:00
2024-06-14 15:53:47 +08:00
// #[
// ApiDoc\Title('核销'),
// ApiDoc\url('/api/order/order/writeoff_order'),
// ApiDoc\Method('POST'),
// ApiDoc\Param(name: "verify_code", type: "string", require: true, desc: "验证码"),
// ApiDoc\NotHeaders(),
// ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"),
// ApiDoc\ResponseSuccess("data", type: "array"),
// ]
2024-06-03 22:45:25 +08:00
public function writeoff_order()
{
$params = (new OrderValidate())->post()->goCheck('check');
$count = StoreOrder::where('verify_code', $params['verify_code'])->count();
if (empty($count)) {
2024-06-06 14:15:42 +08:00
return $this->fail('无该核销码请检查');
}
2024-06-05 22:47:49 +08:00
$res = OrderLogic::writeOff($params);
2024-06-03 22:45:25 +08:00
if ($res) {
return $this->success('核销成功');
}
return $this->fail('核销失败' . OrderLogic::getError());
2024-06-03 22:45:25 +08:00
}
2024-06-04 16:51:26 +08:00
2024-06-06 14:58:20 +08:00
//收银端
public function merchant_order_count()
{
$date = $this->request->get('date', date('Y-m-d'));
$store_id = SystemStoreStaff::where('phone', $this->userInfo['mobile'])->value('store_id');
if (empty($store_id)) {
2024-06-06 14:58:20 +08:00
throw new \Exception('该用户未绑定店铺');
}
$where[] = ['store_id', '=', $store_id];
$where[] = ['paid', '=', 1];
$where[] = ['pay_type', '<>', 9];
$res = StoreOrder::where($where)->whereDay('create_time', $date)->count();
return $this->success('ok', ['order_count' => $res]);
}
2024-06-07 15:38:52 +08:00
2024-06-14 15:53:47 +08:00
// #[
// ApiDoc\Title('订单退款申请'),
// ApiDoc\url('/api/order/order/apply_refund'),
// ApiDoc\Method('POST'),
// ApiDoc\Param(name: "refund_message", type: "string", require: true, desc: "退款原因"),
// ApiDoc\Param(name: "refund_num", type: "int", require: true, desc: "退款数量"),
// ApiDoc\Param(name: "id", type: "int", require: true, desc: "订单id"),
// ApiDoc\Param(name: "old_cart_id", type: "int", require: true, desc: "购物车id"),
// ApiDoc\Param(name: "refund_type", type: "int", require: true, desc: "退款申请类型"),
// ApiDoc\NotHeaders(),
// ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"),
// ApiDoc\ResponseSuccess("data", type: "array"),
// ]
2024-06-07 09:31:43 +08:00
public function apply_refund()
{
$params = (new OrderValidate())->post()->goCheck('add');
$uid = $this->userId;
2024-06-07 15:38:52 +08:00
//拆单逻辑
OrderLogic::dealRefund($uid, $params);
2024-06-07 15:38:52 +08:00
return $this->success('申请成功');
2024-06-07 09:31:43 +08:00
}
2024-06-08 09:43:24 +08:00
2024-06-14 15:53:47 +08:00
// #[
// ApiDoc\Title('订单退款原因'),
// ApiDoc\url('/api/order/order/refund_reason'),
// ApiDoc\Method('GET'),
// ApiDoc\Param(),
// ApiDoc\NotHeaders(),
// ApiDoc\ResponseSuccess("data", type: "array"),
// ]
2024-06-08 09:43:24 +08:00
public function refund_reason()
{
$data = DictData::where('type_value', 'reason')->where('status', YesNoEnum::YES)
2024-06-08 09:43:24 +08:00
->select()->toArray();
return $this->success('ok', $data);
2024-06-08 09:43:24 +08:00
}
2024-06-14 15:53:47 +08:00
// #[
// ApiDoc\Title('取消售后'),
// ApiDoc\url('/api/order/order/cancel_sale'),
// ApiDoc\Method('GET'),
// ApiDoc\Param(name: "order_id", type: "int", require: true, desc: "订单id"),
// ApiDoc\NotHeaders(),
// ApiDoc\ResponseSuccess("data", type: "array"),
// ]
2024-06-08 09:43:24 +08:00
public function cancel_sale()
{
$order_id = (int)$this->request->get('order_id');
$where = [
'id' => $order_id,
'uid' => $this->userId,
];
$order = OrderLogic::cancelSell($where);
if ($order) {
return $this->success();
} else {
return $this->fail('取消失败');
}
}
}