修改订单校验、下单、支付

This commit is contained in:
luofei 2024-06-04 16:51:26 +08:00
parent 9680835324
commit cc0e9a3a24
18 changed files with 150 additions and 109 deletions

View File

@ -5,6 +5,7 @@ namespace app;
use hg\apidoc\exception\ErrorException;
use Next\VarDumper\Dumper;
use Next\VarDumper\DumperHandler;
use support\exception\BusinessException;
use support\exception\Handler;
use Throwable;
use Webman\Http\Request;
@ -21,6 +22,8 @@ class ExceptionHandler extends Handler
return \response(self::convertToHtml($exception));
} elseif ($exception instanceof ErrorException) {
return response($exception->getMessage(), 401);
} elseif ($exception instanceof BusinessException) {
return response($exception->getMessage());
} elseif ($exception instanceof \Exception) {
$isDebug = config('app.debug');
$error = [

View File

@ -2,6 +2,8 @@
namespace app\api\controller;
use app\common\controller\BaseLikeController;
use hg\apidoc\annotation as ApiDoc;
#[ApiDoc\NotParse()]
class BaseApiController extends BaseLikeController
{
@ -15,4 +17,4 @@ class BaseApiController extends BaseLikeController
$this->userId = $this->request->userInfo['user_id'];
}
}
}
}

View File

@ -7,6 +7,8 @@ use app\admin\logic\tools\GeneratorLogic;
use app\common\logic\store_order\StoreOrderLogic;
use think\facade\Db;
use Webman\Config;
use hg\apidoc\annotation as ApiDoc;
#[ApiDoc\NotParse()]
class IndexController extends BaseApiController
{
@ -14,7 +16,7 @@ class IndexController extends BaseApiController
public function index()
{
d(2);
$params=['store_id'=>2,'pay_type'=>17];
@ -44,7 +46,7 @@ class IndexController extends BaseApiController
return $this->success('ok',$find);
}
/**
* @notes 获取省列表
*/

View File

@ -5,6 +5,8 @@ namespace app\api\controller;
use app\api\logic\LoginLogic;
use app\api\validate\{LoginAccountValidate, WechatLoginValidate};
use app\common\model\user\UserAuth;
use hg\apidoc\annotation as ApiDoc;
#[ApiDoc\NotParse()]
class LoginController extends BaseApiController
{
@ -32,7 +34,7 @@ class LoginController extends BaseApiController
return $this->success();
}
/**
* @notes 小程序-登录接口
* @return

View File

@ -7,6 +7,8 @@ use app\common\logic\PayNotifyLogic;
use app\common\model\retail\Cashierclass;
use app\common\service\pay\PayService;
use support\Log;
use hg\apidoc\annotation as ApiDoc;
#[ApiDoc\NotParse()]
/**
* 支付

View File

@ -1,10 +1,12 @@
<?php
namespace app\api\controller;
use app\common\service\UploadService;
use Exception;
use hg\apidoc\annotation as ApiDoc;
#[ApiDoc\NotParse()]
class UploadController extends BaseApiController
{
/**
@ -22,7 +24,7 @@
return $this->fail($e->getMessage());
}
}
/**
* @notes 上传视频
* @author 乔峰
@ -38,4 +40,4 @@
return $this->fail($e->getMessage());
}
}
}
}

View File

@ -5,6 +5,8 @@ namespace app\api\controller\cate;
use app\api\controller\BaseApiController;
use app\api\lists\cate\CateLists;
use hg\apidoc\annotation as ApiDoc;
#[ApiDoc\NotParse()]
/**
* 商品分类控制器
@ -26,4 +28,4 @@ class CateController extends BaseApiController
}
}
}

View File

@ -7,6 +7,8 @@ use app\api\validate\CartValidate;
use app\api\controller\BaseApiController;
use app\api\lists\order\CartList;
use app\common\model\order\Cart;
use hg\apidoc\annotation as ApiDoc;
#[ApiDoc\NotParse()]
class CartController extends BaseApiController
{
@ -64,6 +66,6 @@ class CartController extends BaseApiController
}
}
}
}

View File

@ -19,7 +19,9 @@ use app\common\model\user\UserAddress;
use app\common\service\wechat\WeChatConfigService;
use support\Log;
use Webman\RedisQueue\Redis;
use hg\apidoc\annotation as ApiDoc;
#[ApiDoc\title('订单')]
class OrderController extends BaseApiController
{
@ -32,65 +34,42 @@ class OrderController extends BaseApiController
return $this->dataLists(new OrderList());
}
/**
* 摊贩订单列表
*/
/* public function merchant_order_list()
{
$number = $this->request->get('number');
$page_no = $this->request->get('page_no', 1);
$date = $this->request->get('date', date('Y-m-d'));
if (!$this->userInfo['merchant']['mer_id']) {
return $this->fail('没有权限');
}
$where = [];
if ($number) {
$where[] = ['number', 'like', '%' . $number . '%'];
}
$where[] = ['merchant', '=', $this->userInfo['merchant']['mer_id']];
$where[] = ['paid', '=', 1];
$where[] = ['pay_type', '<>', 9];
$where[] = ['is_opurchase', '=', 0];
$res = StoreOrder::where($where)->page($page_no, 25)->whereDay('create_time', $date)
->order('address_id asc,id desc')
->select()->each(function ($item) {
$item['goods_list'] = Cashierinfo::where('pid', $item['id'])->with('goodsName')->field('goods,nums,price sell')->limit(5)->select();
$item['goods_count'] = count(explode(',', $item['cart_id']));
});
$data['count'] = StoreOrder::where($where)->whereDay('create_time', $date)->count();
$data['lists'] = $res?->toArray();
$data['page_no'] = $page_no;
$data['page_siz'] = 15;
return $this->success('ok', $data);
}*/
/**
* 摊贩订单统计
*/
/* public function merchant_order_count()
{
$date = $this->request->get('date', date('Y-m-d'));
$where[] = ['merchant', '=', $this->userInfo['merchant']['mer_id']];
$where[] = ['paid', '=', 1];
$where[] = ['pay_type', '<>', 9];
$where[] = ['is_opurchase', '=', 0];
$res = StoreOrder::where($where)->whereDay('create_time', $date)->count();
return $this->success('ok', ['order_count' => $res]);
}
public function order_count()
{
$userId = $this->request->userId;
$where = ['uid' => $userId, 'paid' => 0];
$no_pay = StoreOrder::where($where)->count();
$where['paid'] = 1;
$where['status'] = 0;
$waiting = StoreOrder::where($where)->count();
$where['status'] = 1;
$receiving = StoreOrder::where($where)->count();
return $this->success('ok', ['no_pay' => $no_pay, 'waiting' => $waiting, 'receiving' => $receiving]);
}*/
/**
* @notes 检测零售订单
*/
#[
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' => 'add_time', 'desc' => '添加时间', 'type' => 'int'],
['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', []);
@ -109,9 +88,19 @@ class OrderController extends BaseApiController
return $this->data($res);
}
/**
* @notes 创建零售订单
*/
#[
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', []);
@ -195,10 +184,19 @@ class OrderController extends BaseApiController
}
}
/**
* 重新支付
*/
public function Repayment()
#[
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');
@ -211,7 +209,9 @@ class OrderController extends BaseApiController
'paid' => 0,
];
$order = StoreOrder::where($where)->find();
if (!$order) return $this->fail('订单不存在或已支付');
if (!$order) {
return $this->fail('订单不存在或已支付');
}
switch ($pay_type) {
case PayEnum::BALANCE_PAY:
@ -368,7 +368,7 @@ class OrderController extends BaseApiController
return $this->success('添加成功');
}
}
//核销
public function writeoff_order()
{
@ -380,6 +380,6 @@ class OrderController extends BaseApiController
}
return $this->fail('核销失败');
}
}

View File

@ -3,6 +3,8 @@
namespace app\api\controller\product;
use app\api\controller\BaseApiController;
use app\api\lists\product\ProductLists;
use hg\apidoc\annotation as ApiDoc;
#[ApiDoc\NotParse()]
class ProductController extends BaseApiController{
public $notNeedLogin = ['lists'];
@ -22,4 +24,4 @@ class ProductController extends BaseApiController{
return $this->dataLists(new ProductLists());
}
}
}

View File

@ -9,6 +9,8 @@ use app\common\model\dict\DictData;
use app\common\model\merchant\Merchant;
use app\common\model\supplier\Supplier;
use think\facade\Db;
use hg\apidoc\annotation as ApiDoc;
#[ApiDoc\NotParse()]
class IndexController extends BaseApiController{
public $notNeedLogin = ['apply','get_token','merchat_type','record','apply_detail'];
@ -62,7 +64,7 @@ class IndexController extends BaseApiController{
$post['city_id'] = '';
$post['area_id'] = '';
}
if($post['is_merchant_type']==1){
if($find){
$post['mer_id'] = $find['pid'];

View File

@ -6,6 +6,8 @@ use app\api\lists\store\SystemStoreLists;
use app\api\controller\BaseApiController;
use app\common\service\pay\PayService;
use Webman\Config;
use hg\apidoc\annotation as ApiDoc;
#[ApiDoc\NotParse()]
class StoreController extends BaseApiController
{
@ -16,4 +18,4 @@ class StoreController extends BaseApiController
}
}
}

View File

@ -6,6 +6,8 @@ use app\api\controller\BaseApiController;
use app\api\lists\user\UserAddressList;
use app\api\logic\user\AddressLogic;
use app\api\validate\UserAddressValidate;
use hg\apidoc\annotation as ApiDoc;
#[ApiDoc\NotParse()]
class AddressController extends BaseApiController
{

View File

@ -7,6 +7,8 @@ use app\api\logic\user\UserLogic;
use app\api\validate\PasswordValidate;
use app\api\validate\SetUserInfoValidate;
use app\api\validate\UserValidate;
use hg\apidoc\annotation as ApiDoc;
#[ApiDoc\NotParse()]
/**
* 用户控制器
@ -42,4 +44,4 @@ class UserController extends BaseApiController
}
}
}

View File

@ -1,15 +1,17 @@
<?php
namespace app\api\controller\user;
use app\api\lists\user\UserFeedbackLists;
use app\api\logic\user\UserFeedbackLogic;
use app\admin\validate\user\UserFeedbackValidate;
use app\api\controller\BaseApiController;
use hg\apidoc\annotation as ApiDoc;
#[ApiDoc\NotParse()]
class UserFeedbackController extends BaseApiController
{
/**
* @notes 获取用户反馈表列表
* @return \support\Response
@ -20,8 +22,8 @@
{
return $this->dataLists(new UserFeedbackLists());
}
public function add()
{
$params = (new UserFeedbackValidate())->post()->goCheck('add');
@ -31,4 +33,4 @@
}
return $this->fail(UserFeedbackLogic::getError());
}
}
}

View File

@ -43,7 +43,7 @@ class OrderLogic extends BaseLogic
static public function cartIdByOrderInfo($cartId, $addressId, $user = null, $params = [])
{
$where = ['is_pay' => 0, 'is_del' => 0];
$where = ['is_pay' => 0];
$cart_select = Cart::whereIn('id', $cartId)->where($where)->field('product_id as goods,cart_num')->select()->toArray();
if (empty($cart_select)) {
self::setError('购物车为空');
@ -59,7 +59,7 @@ class OrderLogic extends BaseLogic
}
$cart_select[$k]['total'] = bcmul($v['cart_num'], $find['price'], 2);//钱
$cart_select[$k]['price'] = $find['price'];
$cart_select[$k]['product_id'] = $find['goods'];
$cart_select[$k]['product_id'] = $v['goods'];
$cart_select[$k]['old_cart_id'] = implode(',', $cartId);
$cart_select[$k]['cart_num'] = $v['cart_num'];
$cart_select[$k]['verify_code'] = $params['verify_code'];
@ -107,20 +107,16 @@ class OrderLogic extends BaseLogic
// `delivery_type` '发货类型',
// `delivery_id'快递单号/手机号',
$_order = $orderInfo['order'];
if ($orderInfo['order']['shipping_type'] == 1) {
$_order['delivery_name'] = $params['delivery_name'];
$_order['delivery_id'] = $params['delivery_id'];
}
$_order['deduction_price'] = 0;
$_order['uid'] = request()->userId;
$user = User::where('id', \request()->userId)->find();
$_order['real_name'] = $user['real_name'];
$_order['mobile'] = $user['mobile'];
$_order['pay_type'] = $user['pay_type'];
$_order['pay_type'] = $orderInfo['order']['pay_type'];
$_order['verify_code'] = $verify_code;
if ($addressId > 0) {
$address = UserAddress::where(['address_id' => $addressId, 'uid' => Request()->userId])->find();
$address = UserAddress::where(['id' => $addressId, 'uid' => Request()->userId])->find();
if ($address) {
$_order['real_name'] = $address['real_name'];
$_order['user_phone'] = $address['phone'];
@ -142,11 +138,19 @@ class OrderLogic extends BaseLogic
$goods_list[$k]['uid'] = request()->userId;
$goods_list[$k]['cart_id'] = implode(',', $cartId);
$goods_list[$k]['delivery_id'] = $params['store_id'];//商家id
$goods_list[$k]['cart_info'] = json_encode([
'goods' => $v['product_id'],
'cart_num' => $v['cart_num'],
'total' => $v['total'],
'price' => $v['price'],
'product_id' => $v['product_id'],
'old_cart_id' => $v['old_cart_id'],
'verify_code' => $v['verify_code']
], JSON_UNESCAPED_UNICODE);
}
(new StoreOrderCartInfo())->saveAll($goods_list);
$where = ['is_pay' => 0, 'is_del' => 0];
Cart::whereIn('cart_id', $cartId)->where($where)->update(['is_pay' => 1]);
$where = ['is_pay' => 0];
Cart::whereIn('id', $cartId)->where($where)->update(['is_pay' => 1]);
Db::commit();
return $order;
} catch (\Exception $e) {
@ -287,19 +291,19 @@ class OrderLogic extends BaseLogic
*/
static public function payBalance(User $user, $order)
{
if ($user['user_money'] < $order['actual']) {
if ($user['now_money'] < $order['pay_price']) {
self::setError('余额不足,请更换支付方式');
return false;
}
Db::startTrans();
try {
$user->user_money = bcsub($user->user_money, $order['actual'], 2);
$user->now_money = bcsub($user->now_money, $order['pay_price'], 2);
$user->save();
$capitalFlowDao = new CapitalFlowLogic($user);
$capitalFlowDao->userExpense('user_order_pay', 'order', $order['id'], $order['actual']);
$capitalFlowDao->userExpense('user_order_pay', 'order', $order['id'], $order['pay_price']);
Db::commit();
return true;
} catch (Exception $e) {
} catch (\Exception $e) {
Db::rollback();
Log::error('余额支付失败' . $e->getMessage() . '。line:' . $e->getLine() . '。file:' . $e->getFile());
self::setError('余额支付失败' . $e->getMessage());

View File

@ -58,7 +58,7 @@ class CapitalFlowLogic extends BaseLogic
public function userExpense($category, $linkType, $linkId, $amount, $mark = '')
{
$model = new CapitalFlow();
$model->uid = $this->user['uid'];
$model->uid = $this->user['id'];
$model->category = $category;
$model->link_type = $linkType;
$model->link_id = $linkId;

View File

@ -15,6 +15,14 @@ return [
'path' => 'app\store\controller',
// 必须唯一的key
'key' => 'storeApi',
],
[
// (必须)标题
'title' => 'api接口',
// (必须)控制器目录地址
'path' => 'app\api\controller',
// 必须唯一的key
'key' => 'apiApi',
]
],
// (必须)指定通用注释定义的文件地址