multi-store/app/api/controller/order/OrderController.php
2024-07-30 10:25:21 +08:00

511 lines
18 KiB
PHP

<?php
namespace app\api\controller\order;
use app\admin\logic\store_order\StoreOrderLogic;
use app\api\logic\order\OrderLogic;
use app\api\controller\BaseApiController;
use app\api\lists\order\OrderList;
use app\api\validate\OrderValidate;
use app\common\enum\PayEnum;
use app\common\enum\YesNoEnum;
use app\common\logic\PaymentLogic;
use app\common\logic\PayNotifyLogic;
use app\common\model\dict\DictData;
use app\common\model\store_order\StoreOrder;
use app\common\model\system_store\SystemStoreStaff;
use app\common\model\user\User;
use app\common\model\user\UserAddress;
use think\facade\Db;
use Webman\RedisQueue\Redis;
class OrderController extends BaseApiController
{
public $notNeedLogin = ['refund_reason'];
/**
* 订单列表
*/
public function order_list()
{
return $this->dataLists(new OrderList());
}
/**
* 核销码查数据
*/
public function write_code()
{
$code = $this->request->post('code');
if (empty($code)) {
return $this->fail('缺失参数');
}
$res = OrderLogic::getOne($code);
if ($res) {
$res = $res[0];
}
return $this->success('ok', $res);
}
/**
* 核销订单列表
*/
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);
$params = $this->request->post();
if (empty($page_no) || empty($page_size)) {
$params['page_no'] = 1;
$params['page_size'] = 15;
}
$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);
}
/**
* 核销数量
*/
public function write_count()
{
$info = $this->userInfo;
$params = $this->request->post();
$res = OrderLogic::write_count($info, $params);
return $this->success('ok', $res);
}
/**
* 订单校验
*/
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();
$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);
}
public function checkInventory()
{
$params = (new OrderValidate())->post()->goCheck('cart');
$res = OrderLogic::checkLeft($params, $this->userId);
if (!$res) {
$msg = OrderLogic::getError();
if ($msg == '购物车为空') {
return $this->data([]);
}
return $this->fail(OrderLogic::getError());
}
return $this->data($res);
}
/**
* 创建订单
*/
public function createOrder()
{
$cartId = (array)$this->request->post('cart_id', []);
$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();
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();
if ($pay_type == PayEnum::PURCHASE_FUNDS || $pay_type == PayEnum::BALANCE_PAY) {
if (!isset($params['password'])) {
return $this->fail('缺失参数');
}
if (empty($user['pay_password'])) {
return $this->fail('请设置密码');
}
if (!password_verify($params['password'], $user['pay_password'])) {
return $this->fail('密码错误');
}
}
$order = OrderLogic::createOrder($cartId, $addressId, $user, $params);
if ($order != false) {
if ($order['pay_price'] <= 0) {
$pay_type = 3;
}
switch ($pay_type) {
case PayEnum::PURCHASE_FUNDS:
//采购款支付
PayNotifyLogic::handle('purchase_funds', $order['order_id']);
return $this->success('采购款支付成功');
case PayEnum::BALANCE_PAY:
//余额支付
PayNotifyLogic::handle('balancePay', $order['order_id']);
return $this->success('余额支付成功');
case PayEnum::CASH_PAY:
//现金支付
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 {
Db::name('wechat_micropay')->insert(['order_id'=>$order['order_id'],'create_time'=>time(),'update_time'=>time()]);
Redis::send('send-code-pay',['order_id' => $order['order_id']]);
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());
}
}
public function order_count()
{
$userId = $this->request->userId;
$where = ['uid' => $userId, 'paid' => 0];
$no_pay = StoreOrder::where($where)->count(); //待付款
$where['paid'] = 1;
$where['status'] = 1;
$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]);
}
/**
* 订单支付
*/
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();
if (!$order) {
return $this->fail('订单不存在或已支付');
}
switch ($pay_type) {
case PayEnum::PURCHASE_FUNDS:
//采购款支付
PayNotifyLogic::handle('purchase_funds', $order['order_id']);
return $this->success('采购款支付成功');
case PayEnum::BALANCE_PAY:
//余额支付
PayNotifyLogic::handle('balancePay', $order['order_id']);
return $this->success('余额支付成功');
case PayEnum::CASH_PAY:
//现金支付
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', ['order_id' => $order['order_id']]);
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('支付失败');
}
// #[
// 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');
$lat = $this->request->get('lat', '');
$lng = $this->request->get('long', '');
$where = [
'id' => $order_id,
'uid' => $this->userId,
];
$url = 'https://' . $this->request->host(true);
$parm = [
'lat' => $lat,
'long' => $lng
];
$order = OrderLogic::detail($where, $url, $parm);
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 = ['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('添加成功');
}
}
/**
* 核销
*/
public function writeoff_order()
{
$params = (new OrderValidate())->post()->goCheck('check');
$count = StoreOrder::where('verify_code', $params['verify_code'])->count();
if (empty($count)) {
return $this->fail('无该核销码请检查');
}
$res = OrderLogic::writeOff($params);
if ($res) {
return $this->success('核销成功');
}
return $this->fail('核销失败' . OrderLogic::getError());
}
//收银端
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]);
}
/**
* 订单退款申请
*/
public function apply_refund()
{
$params = (new OrderValidate())->post()->goCheck('add');
$uid = $this->userId;
//拆单逻辑
$res=OrderLogic::dealRefund($uid, $params);
$detail = StoreOrder::where('id', $params['id'])->where('refund_type',1)->find();
if($detail && $res!=2){
$res=StoreOrderLogic::refund($detail,['order_id'=>$detail['order_id']]);
if($res!=false){
return $this->success($res);
}
}
return $this->success('申请成功');
}
// #[
// ApiDoc\Title('订单退款原因'),
// ApiDoc\url('/api/order/order/refund_reason'),
// ApiDoc\Method('GET'),
// ApiDoc\Param(),
// ApiDoc\NotHeaders(),
// ApiDoc\ResponseSuccess("data", type: "array"),
// ]
public function refund_reason()
{
$data = DictData::where('type_value', 'reason')->where('status', YesNoEnum::YES)
->select()->toArray();
return $this->success('ok', $data);
}
// #[
// 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"),
// ]
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('取消失败');
}
}
}