erp_old/app/api/controller/order/RetailOrderController.php

293 lines
10 KiB
PHP
Raw Normal View History

2024-04-26 11:26:12 +08:00
<?php
namespace app\api\controller\order;
use app\admin\logic\order\CartLogic;
2024-04-30 14:14:30 +08:00
use app\admin\logic\retail\CashierclassLogic;
2024-04-26 11:26:12 +08:00
use app\api\logic\order\OrderLogic;
use app\api\controller\BaseApiController;
2024-04-29 16:36:12 +08:00
use app\api\lists\order\RetailOrderList;
2024-05-06 14:02:15 +08:00
use app\api\service\WechatUserService;
2024-04-26 11:26:12 +08:00
use app\common\logic\order\RetailOrderLogic;
use app\common\enum\PayEnum;
2024-05-06 14:02:15 +08:00
use app\common\logic\PaymentLogic;
2024-05-10 11:31:45 +08:00
use app\common\logic\PayNotifyLogic;
2024-04-26 11:26:12 +08:00
use app\common\model\order\Cart;
2024-04-29 16:36:12 +08:00
use app\common\model\retail\Cashierclass;
2024-04-26 11:26:12 +08:00
use app\common\model\user\User;
2024-05-08 11:48:07 +08:00
use app\common\model\user\UserAddress;
2024-05-06 14:02:15 +08:00
use app\common\service\wechat\WeChatConfigService;
2024-05-06 14:51:06 +08:00
use support\Log;
2024-04-26 11:26:12 +08:00
class RetailOrderController extends BaseApiController
{
2024-04-29 16:36:12 +08:00
/**
* 订单列表
*/
2024-05-09 17:44:27 +08:00
public function order_list()
{
2024-04-29 16:36:12 +08:00
return $this->dataLists(new RetailOrderList());
}
2024-04-30 18:01:52 +08:00
2024-05-09 17:44:27 +08:00
public function order_count()
{
$userId = $this->request->userId;
$where = ['uid' => $userId, 'paid' => 0];
$no_pay = Cashierclass::where($where)->count();
$where['paid'] = 1;
$where['status'] = 0;
$waiting = Cashierclass::where($where)->count();
$where['status'] = 1;
$receiving = Cashierclass::where($where)->count();
return $this->success('ok', ['no_pay' => $no_pay, 'waiting' => $waiting, 'receiving' => $receiving]);
2024-04-30 18:01:52 +08:00
}
2024-04-29 16:36:12 +08:00
/**
* @notes 检测零售订单
*/
2024-05-09 17:44:27 +08:00
public function checkOrder()
{
$cartId = (array)$this->request->post('cart_id', []);
2024-04-26 11:26:12 +08:00
$addressId = (int)$this->request->post('address_id');
2024-05-09 17:44:27 +08:00
// $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 == '购物车为空') {
2024-04-29 16:36:12 +08:00
return $this->data([]);
}
return $this->fail(OrderLogic::getError());
}
2024-04-26 11:26:12 +08:00
return $this->data($res);
}
/**
* @notes 创建零售订单
*/
2024-05-09 17:44:27 +08:00
public function createOrder()
{
2024-05-06 20:28:49 +08:00
2024-05-06 14:30:09 +08:00
// d(WeChatConfigService::getPayConfigByTerminal(1));
2024-05-09 17:44:27 +08:00
$user = User::where('id', $this->request->userId)->find();
$cartId = (array)$this->request->post('cart_id', []);
$mer_id = (array)$this->request->post('mer_id', 0);
2024-05-10 09:53:48 +08:00
$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();
if ($mer_id <= 0 && $pay_type!=9) {
2024-04-30 14:14:30 +08:00
return $this->fail('自提点不能为空');
}
2024-05-09 17:44:27 +08:00
if (count($cartId) > 100) {
2024-04-29 16:36:12 +08:00
return $this->fail('购物车商品不能超过100个');
}
2024-05-10 09:53:48 +08:00
if($pay_type==9){
2024-05-10 10:18:38 +08:00
if(empty($this->request->userInfo['merchant'])){
return $this->fail('请先绑定商户');
}
$mer_id=$this->request->userInfo['merchant']['mer_id'];
$params['mer_id']=$mer_id;
2024-05-10 09:53:48 +08:00
}
2024-05-09 17:44:27 +08:00
$order = OrderLogic::createOrder($cartId, $addressId, null, $params);
if ($order != false) {
switch ($pay_type) {
case PayEnum::WECHAT_PAY:
2024-05-10 09:53:48 +08:00
//余额支付
2024-05-09 17:44:27 +08:00
$user = User::where('id', $this->request->userId)->find();
$res = RetailOrderLogic::payBalance($user, $order);
if (RetailOrderLogic::hasError()) {
$res = RetailOrderLogic::paySuccess($order, ['money' => $order['actual']]);
if (RetailOrderLogic::hasError()) {
} else {
return $this->fail(RetailOrderLogic::getError());
}
} else {
2024-04-27 11:41:34 +08:00
return $this->fail(RetailOrderLogic::getError());
}
2024-05-09 17:44:27 +08:00
break;
case PayEnum::WECHAT_PAY:
2024-05-10 09:53:48 +08:00
//微信支付
2024-05-09 17:44:27 +08:00
$redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
$result = PaymentLogic::pay($pay_type, 'cashierclass', $order, $this->userInfo['terminal'], $redirectUrl);
if (PaymentLogic::hasError()) {
return $this->fail(PaymentLogic::getError(), $params);
}
return $this->success('', $result);
break;
case PayEnum::WECHAT_PAY_BARCODE:
2024-05-10 09:53:48 +08:00
//微信条码支付
2024-05-09 17:44:27 +08:00
$result = PaymentLogic::codepay($auth_code, $order);
if (PaymentLogic::hasError()) {
return $this->fail(PaymentLogic::getError(), $params);
}
return $this->success('', $result);
break;
default:
return $this->fail('支付方式错误');
2024-04-26 11:26:12 +08:00
}
2024-05-09 17:44:27 +08:00
return $this->data(['order_id' => $order->id]);
} else {
2024-04-26 11:26:12 +08:00
return $this->fail(OrderLogic::getError());
}
}
2024-04-29 16:36:12 +08:00
/**
* 重新支付
*/
2024-05-09 17:44:27 +08:00
public function Repayment()
{
2024-04-29 16:36:12 +08:00
$order_id = (int)$this->request->post('order_id');
2024-05-08 11:48:07 +08:00
$addressId = (int)$this->request->post('address_id');
2024-04-29 16:36:12 +08:00
$pay_type = (int)$this->request->post('pay_type');
2024-05-09 17:44:27 +08:00
$auth_code = $this->request->post('auth_code'); //微信支付条码
$params = $this->request->post();
$where = [
'id' => $order_id,
'uid' => $this->userId,
'paid' => 0,
2024-04-29 16:36:12 +08:00
];
2024-05-09 17:44:27 +08:00
$order = Cashierclass::where($where)->find();
2024-05-10 10:18:38 +08:00
if(!$order) return $this->fail('订单不存在或已支付');
2024-05-09 17:44:27 +08:00
switch ($pay_type) {
case PayEnum::BALANCE_PAY:
2024-05-10 10:18:38 +08:00
//余额支付
2024-05-09 17:44:27 +08:00
$user = User::where('id', $this->request->userId)->find();
$res = RetailOrderLogic::payBalance($user, $order);
if (!RetailOrderLogic::hasError()) {
$res = RetailOrderLogic::paySuccess($order, ['money' => $order['actual']]);
if(RetailOrderLogic::hasError()){
return $this->fail(RetailOrderLogic::getError());
}
return $this->success('余额支付成功');
2024-04-29 16:36:12 +08:00
}else{
return $this->fail(RetailOrderLogic::getError());
}
2024-05-09 17:44:27 +08:00
break;
case PayEnum::WECHAT_PAY:
2024-05-10 10:18:38 +08:00
//微信支付
2024-05-09 17:44:27 +08:00
$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;
Cashierclass::where(['id' => $order_id, 'uid' => Request()->userId])->update($_order);
}
2024-05-08 11:48:07 +08:00
}
2024-05-09 17:44:27 +08:00
$result = PaymentLogic::pay($pay_type, 'cashierclass', $order, $this->userInfo['terminal'], $redirectUrl);
if (PaymentLogic::hasError()) {
return $this->fail(PaymentLogic::getError());
}
return $this->success('', $result);
break;
case PayEnum::WECHAT_PAY_BARCODE:
2024-05-10 10:18:38 +08:00
//微信条码支付
2024-05-09 17:44:27 +08:00
$result = PaymentLogic::codepay($auth_code, $order);
if (PaymentLogic::hasError()) {
return $this->fail(PaymentLogic::getError(), $params);
}
2024-05-10 11:31:45 +08:00
if($result['trade_state_desc']=='支付成功'){
PayNotifyLogic::handle('cashierclass', $result['out_trade_no'], $result);
}
return $this->success('支付成功',['order_id'=>$order['order_id']]);
2024-05-09 17:44:27 +08:00
break;
default:
return $this->fail('支付方式错误');
2024-04-29 16:36:12 +08:00
}
return $this->fail('支付失败');
}
2024-05-09 17:44:27 +08:00
public function detail()
{
2024-04-30 14:14:30 +08:00
$order_id = (int)$this->request->get('order_id');
2024-05-09 17:44:27 +08:00
$where = [
'id' => $order_id,
'uid' => $this->userId,
2024-04-30 14:14:30 +08:00
];
2024-05-09 17:44:27 +08:00
$order = CashierclassLogic::detail($where);
if ($order) {
2024-04-30 14:14:30 +08:00
return $this->data($order);
2024-05-09 17:44:27 +08:00
} else {
2024-04-30 14:14:30 +08:00
return $this->fail('订单不存在');
}
}
2024-04-29 16:36:12 +08:00
/**
* 获取用户常用购买记录
*/
2024-05-09 17:44:27 +08:00
public function frequently_purchase()
{
$params = $this->request->get();
$res = RetailOrderLogic::frequentlyPurchase($params);
if (RetailOrderLogic::hasError()) {
2024-04-29 16:36:12 +08:00
return $this->fail(RetailOrderLogic::getError());
2024-05-09 17:44:27 +08:00
} else {
2024-04-29 16:36:12 +08:00
return $this->data($res);
}
}
2024-05-09 17:44:27 +08:00
2024-05-07 10:10:05 +08:00
/**
* 取消订单
*/
2024-05-09 17:44:27 +08:00
public function cancel_order()
{
2024-05-07 10:10:05 +08:00
$order_id = (int)$this->request->post('order_id');
$value = (int)$this->request->post('value');
2024-05-09 17:44:27 +08:00
$where = [
'id' => $order_id,
'uid' => $this->userId,
'paid' => 0,
2024-05-07 10:10:05 +08:00
];
2024-05-09 17:44:27 +08:00
$order = Cashierclass::where($where)->find();
if ($order) {
$data = ['data' => $value, 'delete_time' => time()];
2024-05-07 10:10:05 +08:00
Cashierclass::where($where)->update($data);
return $this->success('取消成功');
}
return $this->fail('取消失败');
}
2024-05-08 09:41:50 +08:00
/**
* 确认收货
*/
2024-05-09 17:44:27 +08:00
public function confirm_receipt()
{
2024-05-08 09:41:50 +08:00
$order_id = (int)$this->request->post('order_id');
2024-05-09 17:44:27 +08:00
$where = [
'id' => $order_id,
'uid' => $this->userId,
'paid' => 1,
'status' => 1,
2024-05-08 09:41:50 +08:00
];
2024-05-09 17:44:27 +08:00
$order = Cashierclass::where($where)->find();
if ($order) {
$data = ['status' => 2];
2024-05-08 09:41:50 +08:00
Cashierclass::where($where)->update($data);
return $this->success('确认成功');
}
return $this->fail('确认失败');
}
2024-05-08 17:29:32 +08:00
/**
* 再次购买
*/
2024-05-09 17:44:27 +08:00
public function purchase_again()
{
2024-05-08 17:29:32 +08:00
$order_id = (int)$this->request->get('order_id');
2024-05-09 17:44:27 +08:00
$res = RetailOrderLogic::purchaseAgain($order_id);
if ($res == false) {
2024-05-08 17:29:32 +08:00
return $this->fail('添加失败');
2024-05-09 17:44:27 +08:00
} else {
2024-05-08 17:29:32 +08:00
return $this->success('添加成功');
}
}
2024-05-09 17:44:27 +08:00
}