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

504 lines
21 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;
use app\common\logic\PaymentLogic;
use app\common\logic\PayNotifyLogic;
use app\common\model\store_order\StoreOrder;
2024-06-06 14:58:20 +08:00
use app\common\model\system_store\SystemStoreStaff;
use app\common\model\user\UserAddress;
use Webman\RedisQueue\Redis;
2024-06-04 16:51:26 +08:00
use hg\apidoc\annotation as ApiDoc;
2024-06-04 16:51:26 +08:00
#[ApiDoc\title('订单')]
class OrderController extends BaseApiController
{
/**
* 订单列表
*/
public function order_list()
{
return $this->dataLists(new OrderList());
}
2024-06-05 22:47:49 +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"),
]
public function write_code()
{
$code = $this->request->post('code');
if(empty($code)){
return $this->fail('缺失参数');
}
$res = OrderLogic::getOne($code);
2024-06-06 14:58:20 +08:00
if($res){
$res = $res[0];
}
2024-06-05 22:47:49 +08:00
return $this->success('ok',$res);
}
2024-06-05 13:27:17 +08:00
#[
ApiDoc\Title('核销订单列表'),
ApiDoc\url('/api/order/order/write_list'),
ApiDoc\Method('POST'),
2024-06-05 13:50:00 +08:00
ApiDoc\Param(name: "status", type: "int", require: true, desc: "1待核销2已核销"),
2024-06-05 15:11:42 +08:00
ApiDoc\Param(name: "name", type: "string", require: false, desc: "搜商品或者订单id"),
2024-06-05 13:50:00 +08:00
ApiDoc\Param(name: "page_no", type: "int", require: true, desc: "默认1页数"),
ApiDoc\Param(name: "page_size", type: "int", require: false, desc: "条数默认15"),
2024-06-05 13:27:17 +08:00
ApiDoc\NotHeaders(),
ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"),
ApiDoc\ResponseSuccess("data", type: "array"),
]
public function write_list()
{
$status = (int)$this->request->post('status',1);
2024-06-05 14:19:11 +08:00
$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();
2024-06-05 14:19:11 +08:00
if(empty($page_no) || empty($page_size)){
$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 15:11:42 +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"),
]
public function write_count()
{
$info = $this->userInfo;
$params = $this->request->post();
$res = OrderLogic::write_count($info,$params);
return $this->success('ok',$res);
}
2024-06-04 16:51:26 +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();
$res = OrderLogic::cartIdByOrderInfo($cartId, $addressId, null, $params);
if ($res == false) {
$msg = OrderLogic::getError();
if ($msg == '购物车为空') {
return $this->data([]);
}
return $this->fail(OrderLogic::getError());
}
return $this->data($res);
}
2024-06-04 16:51:26 +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个');
}
2024-06-03 21:57:23 +08:00
// if ($pay_type == 9 || $pay_type == 17 ||$pay_type==13) {
// if (empty($this->request->userInfo['merchant'])) {
// return $this->fail('请先绑定商户');
// }
// $mer_id = $this->request->userInfo['merchant']['mer_id'];
// $params['mer_id'] = $mer_id;
// }
$order = OrderLogic::createOrder($cartId, $addressId, null, $params);
if ($order != false) {
switch ($pay_type) {
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:
//微信支付
$redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
2024-06-07 10:07:03 +08:00
$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('支付方式错误');
}
2024-06-03 21:57:23 +08:00
// 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];
2024-06-06 16:42:35 +08:00
$no_pay = StoreOrder::where($where)->count();//待付款
2024-06-06 13:43:23 +08:00
$where['paid'] = 1;
$where['status'] = 1;
2024-06-06 16:42:35 +08:00
$waiting = StoreOrder::where($where)->count();//待核销
$where['status'] = 2;
$receiving = StoreOrder::where($where)->count();//已核销
$where['status'] = -1;
$applyRefund= StoreOrder::where($where)->count();//申请退款
$where['status'] = 4;
$refund= StoreOrder::where($where)->count();//已退款
$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-04 16:51:26 +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"),
]
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:
//微信支付
$redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
if ($addressId != $order['address_id']) {
$address = UserAddress::where(['address_id' => $addressId, 'uid' => Request()->userId])->find();
if ($address) {
$_order['real_name'] = $address['real_name'];
$_order['user_phone'] = $address['phone'];
$_order['user_address'] = $address['detail'];
$_order['address_id'] = $addressId;
StoreOrder::where(['id' => $order_id, 'uid' => Request()->userId])->update($_order);
}
}
2024-06-06 15:34:57 +08:00
$result = PaymentLogic::pay($pay_type, 'wechat_common', $order, $this->userInfo['terminal'], $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('支付失败');
}
public function detail()
{
$order_id = (int)$this->request->get('order_id');
$where = [
'id' => $order_id,
'uid' => $this->userId,
];
$order = OrderLogic::detail($where);
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) {
$data = ['data' => $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-04 23:15:30 +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');
2024-06-06 14:15:42 +08:00
$count = StoreOrder::where('verify_code',$params['verify_code'])->count();
if(empty($count)){
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('核销成功');
}
2024-06-04 22:20:47 +08:00
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)){
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 09:31:43 +08:00
2024-06-07 15:38:52 +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\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);
return $this->success('申请成功');
2024-06-07 09:31:43 +08:00
}
2024-06-06 14:58:20 +08:00
}