feat: 修改了订单相关的API,优化了支付逻辑;

fix: 修复了用户地址、商品库存等错误;
refactor: 重构了登录逻辑,提高了代码可读性;
style: 调整了代码格式,使其更加规范;
test: 增加了订单支付的测试用例;
docs: 更新了相关文档;
build: 更新了依赖;
ops: 优化了服务器性能;
chore: 更新了.gitignore文件;
This commit is contained in:
mkm 2024-08-27 11:56:48 +08:00
parent 42b19ff900
commit 40ec3e5ee0
29 changed files with 156 additions and 301 deletions

View File

@ -46,18 +46,13 @@ class StoreBranchProductController extends BaseAdminController
{ {
$params = (new StoreProductValidate())->post()->goCheck('add'); $params = (new StoreProductValidate())->post()->goCheck('add');
$result = StoreProductLogic::add($params); $result = StoreProductLogic::add($params);
if (true === $result) { return $this->success('添加成功', [], 1, 1);
return $this->success('添加成功', [], 1, 1);
}
return $this->fail(StoreProductLogic::getError());
} }
public function update() public function update()
{ {
$params = $this->request->post(); $params = $this->request->post();
StoreBranchProductLogic::edit($params); StoreBranchProductLogic::edit($params);
if (StoreBranchProductLogic::hasError()) {
return $this->fail(StoreBranchProductLogic::getError());
}
return $this->success('更新成功', [], 1, 1); return $this->success('更新成功', [], 1, 1);
} }
@ -110,9 +105,6 @@ class StoreBranchProductController extends BaseAdminController
{ {
$params = (new StoreProductValidate())->post()->goCheck('delete'); $params = (new StoreProductValidate())->post()->goCheck('delete');
StoreBranchProductLogic::delete($params); StoreBranchProductLogic::delete($params);
if(StoreBranchProductLogic::hasError()){
return $this->fail(StoreBranchProductLogic::getError());
}
return $this->success('删除成功', [], 1, 1); return $this->success('删除成功', [], 1, 1);
} }

View File

@ -44,10 +44,7 @@ class StoreProductController extends BaseAdminController
{ {
$params = (new StoreProductValidate())->post()->goCheck('add'); $params = (new StoreProductValidate())->post()->goCheck('add');
$result = StoreProductLogic::add($params); $result = StoreProductLogic::add($params);
if (true === $result) { return $this->success('添加成功', [], 1, 1);
return $this->success('添加成功', [], 1, 1);
}
return $this->fail(StoreProductLogic::getError());
} }
@ -61,10 +58,8 @@ class StoreProductController extends BaseAdminController
{ {
$params = (new StoreProductValidate())->post()->goCheck('edit'); $params = (new StoreProductValidate())->post()->goCheck('edit');
$result = StoreProductLogic::edit($params); $result = StoreProductLogic::edit($params);
if (true === $result) { return $this->success('编辑成功', [], 1, 1);
return $this->success('编辑成功', [], 1, 1);
}
return $this->fail(StoreProductLogic::getError());
} }

View File

@ -58,7 +58,6 @@ class WarehouseOrderController extends BaseAdminController
if (true === $result) { if (true === $result) {
return $this->success('添加成功', [], 1, 1); return $this->success('添加成功', [], 1, 1);
} }
return $this->fail(WarehouseOrderLogic::getError());
} }
/** /**
* @notes 添加出库单 * @notes 添加出库单
@ -145,7 +144,6 @@ class WarehouseOrderController extends BaseAdminController
if (true === $result) { if (true === $result) {
return $this->success('编辑成功', [], 1, 1); return $this->success('编辑成功', [], 1, 1);
} }
return $this->fail(WarehouseOrderLogic::getError());
} }
/** /**
@ -173,9 +171,6 @@ class WarehouseOrderController extends BaseAdminController
{ {
$params = (new WarehouseOrderValidate())->post()->goCheck('delete'); $params = (new WarehouseOrderValidate())->post()->goCheck('delete');
WarehouseOrderLogic::delete($params); WarehouseOrderLogic::delete($params);
if(WarehouseOrderLogic::hasError()){
return $this->fail(WarehouseOrderLogic::getError());
}
return $this->success('删除成功', [], 1, 1); return $this->success('删除成功', [], 1, 1);
} }

View File

@ -61,12 +61,8 @@ class WarehouseProductController extends BaseAdminController
} }
WarehouseProductLogic::add($data); WarehouseProductLogic::add($data);
} }
return $this->success('添加成功', [], 1, 1);
if (WarehouseProductLogic::hasError()) {
return $this->fail(WarehouseProductLogic::getError());
} else {
return $this->success('添加成功', [], 1, 1);
}
} }
@ -95,11 +91,8 @@ class WarehouseProductController extends BaseAdminController
{ {
$params = (new WarehouseProductValidate())->post()->goCheck('delete'); $params = (new WarehouseProductValidate())->post()->goCheck('delete');
WarehouseProductLogic::delete($params); WarehouseProductLogic::delete($params);
if (WarehouseProductLogic::hasError()) { return $this->success('删除成功', [], 1, 1);
return $this->fail(WarehouseProductLogic::getError());
} else {
return $this->success('删除成功', [], 1, 1);
}
} }

View File

@ -52,8 +52,7 @@ class StoreBranchProductLogic extends BaseLogic
{ {
$StoreProduct = StoreBranchProduct::where('id', $params['id'])->find(); $StoreProduct = StoreBranchProduct::where('id', $params['id'])->find();
if ($params['status'] == 1 && $StoreProduct['price'] == 0) { if ($params['status'] == 1 && $StoreProduct['price'] == 0) {
self::setError('商品价格不能为0,无法上架'); throw new BusinessException('商品价格不能为0,无法上架');
return false;
} }
Db::startTrans(); Db::startTrans();
try { try {
@ -67,10 +66,9 @@ class StoreBranchProductLogic extends BaseLogic
StoreBranchProduct::where('id', $params['id'])->update($data); StoreBranchProduct::where('id', $params['id'])->update($data);
Db::commit(); Db::commit();
return true; return true;
} catch (\Exception $e) { } catch (\Throwable $e) {
Db::rollback(); Db::rollback();
self::setError($e->getMessage()); throw new BusinessException('商品编辑失败:',$e->getMessage());
return false;
} }
} }
/** /**
@ -116,8 +114,7 @@ class StoreBranchProductLogic extends BaseLogic
{ {
$stock = StoreBranchProduct::where('id', $params['id'])->value('stock'); $stock = StoreBranchProduct::where('id', $params['id'])->value('stock');
if ($stock > 0) { if ($stock > 0) {
self::setError('商品库存不为0,无法删除'); throw new BusinessException('商品库存不为0,无法删除');
return false;
} }
StoreBranchProduct::destroy($params['id']); StoreBranchProduct::destroy($params['id']);
return true; return true;

View File

@ -218,7 +218,7 @@ class StoreProductLogic extends BaseLogic
Db::commit(); Db::commit();
return true; return true;
} catch (\Exception $e) { } catch (\Throwable $e) {
Db::rollback(); Db::rollback();
throw new BusinessException('编辑商品失败'.$e->getMessage()); throw new BusinessException('编辑商品失败'.$e->getMessage());

View File

@ -31,6 +31,7 @@ use app\common\model\user_sign\UserSign;
use app\common\model\vip_flow\VipFlow; use app\common\model\vip_flow\VipFlow;
use think\facade\Db; use think\facade\Db;
use app\common\service\FileService; use app\common\service\FileService;
use support\exception\BusinessException;
use Webman\Config; use Webman\Config;
/** /**
@ -69,10 +70,9 @@ class UserLogic extends BaseLogic
Db::commit(); Db::commit();
return $res; return $res;
} catch (\Exception $e) { } catch (\Throwable $e) {
Db::rollback(); Db::rollback();
self::setError($e->getMessage()); throw new BusinessException($e->getMessage());
return false;
} }
} }
@ -81,23 +81,19 @@ class UserLogic extends BaseLogic
$user_ship=$params['user_ship']??0; $user_ship=$params['user_ship']??0;
if($user_ship==2){ if($user_ship==2){
if(!isset($params['village'])){ if(!isset($params['village'])){
self::setError('请设置村参数'); throw new BusinessException('请设置村参数');
return false;
} }
$arr=User::where('user_ship',$user_ship)->alias('user')->join('user_address address','user.id=address.uid and village='.$params['village'])->find(); $arr=User::where('user_ship',$user_ship)->alias('user')->join('user_address address','user.id=address.uid and village='.$params['village'])->find();
if ($arr) { if ($arr) {
self::setError('该区域已有村长请重新选择'); throw new BusinessException('该区域已有村长请重新选择');
return false;
} }
}elseif($user_ship==3){ }elseif($user_ship==3){
if(!isset($params['brigade'])){ if(!isset($params['brigade'])){
self::setError('请设置队参数'); throw new BusinessException('请设置队参数');
return false;
} }
$arr=User::where('user_ship',$user_ship)->alias('user')->join('user_address address','user.id=address.uid and village='.$params['village'] .' and brigade='.$params['brigade'])->find(); $arr=User::where('user_ship',$user_ship)->alias('user')->join('user_address address','user.id=address.uid and village='.$params['village'] .' and brigade='.$params['brigade'])->find();
if($arr){ if($arr){
self::setError('该区域已有队长请重新选择'); throw new BusinessException('该区域已有队长请重新选择');
return false;
} }
} }
return true; return true;
@ -148,8 +144,7 @@ class UserLogic extends BaseLogic
return $res; return $res;
} catch (\Exception $e) { } catch (\Exception $e) {
Db::rollback(); Db::rollback();
self::setError($e->getMessage()); throw new BusinessException($e->getMessage());
return false;
} }
} }
/** /**
@ -179,8 +174,7 @@ class UserLogic extends BaseLogic
return true; return true;
} catch (\Exception $e) { } catch (\Exception $e) {
Db::rollback(); Db::rollback();
self::setError($e->getMessage()); throw new BusinessException($e->getMessage());
return false;
} }
} }

View File

@ -7,6 +7,7 @@ use app\common\model\user_ship\UserShip;
use app\common\logic\BaseLogic; use app\common\logic\BaseLogic;
use app\common\model\user\User; use app\common\model\user\User;
use app\common\model\user\UserAddress; use app\common\model\user\UserAddress;
use support\exception\BusinessException;
use think\facade\Db; use think\facade\Db;
@ -106,8 +107,7 @@ class UserShipLogic extends BaseLogic
$user_ship=$params['user_ship']??0; $user_ship=$params['user_ship']??0;
if($user_ship==2){ if($user_ship==2){
if(!isset($params['village'])){ if(!isset($params['village'])){
self::setError('请设置村参数'); throw new BusinessException('请设置村参数');
return false;
} }
$arr=User::where('user_ship',$user_ship)->column('id'); $arr=User::where('user_ship',$user_ship)->column('id');
if($arr){ if($arr){
@ -116,14 +116,12 @@ class UserShipLogic extends BaseLogic
if($params['uid']==$find['uid']){ if($params['uid']==$find['uid']){
return true; return true;
} }
self::setError('该区域已有村长请重新选择'); throw new BusinessException('该区域已有村长请重新选择');
return false;
} }
} }
}elseif($user_ship==3){ }elseif($user_ship==3){
if(!isset($params['brigade'])){ if(!isset($params['brigade'])){
self::setError('请设置队参数'); throw new BusinessException('请设置队参数');
return false;
} }
$arr=User::where('user_ship',$user_ship)->column('id'); $arr=User::where('user_ship',$user_ship)->column('id');
if($arr){ if($arr){
@ -132,8 +130,7 @@ class UserShipLogic extends BaseLogic
if($params['uid']==$find['uid']){ if($params['uid']==$find['uid']){
return true; return true;
} }
self::setError('该区域已有队长请重新选择'); throw new BusinessException('该区域已有队长请重新选择');
return false;
} }
} }
} }

View File

@ -6,6 +6,7 @@ use app\admin\logic\warehouse_product\WarehouseProductLogic;
use app\common\model\warehouse_order\WarehouseOrder; use app\common\model\warehouse_order\WarehouseOrder;
use app\common\logic\BaseLogic; use app\common\logic\BaseLogic;
use app\common\model\warehouse_product\WarehouseProduct; use app\common\model\warehouse_product\WarehouseProduct;
use support\exception\BusinessException;
use think\facade\Db; use think\facade\Db;
/** /**
@ -69,10 +70,9 @@ class WarehouseOrderLogic extends BaseLogic
} }
Db::commit(); Db::commit();
return true; return true;
} catch (\Exception $e) { } catch (\Throwable $e) {
Db::rollback(); Db::rollback();
self::setError($e->getMessage()); throw new BusinessException($e->getMessage());
return false;
} }
} }
@ -128,8 +128,7 @@ class WarehouseOrderLogic extends BaseLogic
return true; return true;
} catch (\Throwable $e) { } catch (\Throwable $e) {
Db::rollback(); Db::rollback();
self::setError($e->getMessage()); throw new BusinessException($e->getMessage());
return false;
} }
} }
@ -145,8 +144,7 @@ class WarehouseOrderLogic extends BaseLogic
{ {
$count = WarehouseProduct::where('oid', $params['id'])->count(); $count = WarehouseProduct::where('oid', $params['id'])->count();
if ($count >= 1) { if ($count >= 1) {
self::setError('该订单下还有商品没有删除,请先删除商品'); throw new BusinessException('该订单下还有商品没有删除,请先删除商品');
return false;
} }
WarehouseOrder::destroy($params['id']); WarehouseOrder::destroy($params['id']);
$find = WarehouseProduct::where('oid', $params['id'])->field('sum(nums) as nums,sum(total_price) as total_price')->find(); $find = WarehouseProduct::where('oid', $params['id'])->field('sum(nums) as nums,sum(total_price) as total_price')->find();

View File

@ -208,8 +208,7 @@ class WarehouseProductLogic extends BaseLogic
} elseif ($res['financial_pm'] == 0) { } elseif ($res['financial_pm'] == 0) {
$stock = StoreBranchProduct::where(['store_id' => $res['store_id'], 'product_id' => $res['product_id']])->value('stock'); $stock = StoreBranchProduct::where(['store_id' => $res['store_id'], 'product_id' => $res['product_id']])->value('stock');
if ($stock < $res['nums']) { if ($stock < $res['nums']) {
self::setError('商品库存不足,无法退回'); throw new BusinessException('商品库存不足,无法退回');
return false;
} }
StoreBranchProduct::where(['store_id' => $res['store_id'], 'product_id' => $res['product_id']])->dec('stock', $res['nums'])->update(); StoreBranchProduct::where(['store_id' => $res['store_id'], 'product_id' => $res['product_id']])->dec('stock', $res['nums'])->update();
WarehouseProductStorege::where('warehouse_id', $res['warehouse_id'])->where('product_id', $res['product_id'])->inc('nums', $res['nums'])->update(); WarehouseProductStorege::where('warehouse_id', $res['warehouse_id'])->where('product_id', $res['product_id'])->inc('nums', $res['nums'])->update();
@ -223,7 +222,7 @@ class WarehouseProductLogic extends BaseLogic
} }
return true; return true;
} }
return false; throw new BusinessException('没有查到出入库信息');
} }
/** /**

View File

@ -19,9 +19,6 @@ class LoginController extends BaseApiController
{ {
$params = (new LoginAccountValidate())->post()->goCheck(); $params = (new LoginAccountValidate())->post()->goCheck();
$result = LoginLogic::login($params); $result = LoginLogic::login($params);
if (false === $result) {
return $this->fail(LoginLogic::getError());
}
return $this->success('', $result); return $this->success('', $result);
} }
/** /**
@ -44,9 +41,6 @@ class LoginController extends BaseApiController
{ {
$params = (new WechatLoginValidate())->post()->goCheck('mnpLogin'); $params = (new WechatLoginValidate())->post()->goCheck('mnpLogin');
$res = LoginLogic::mnpLogin($params); $res = LoginLogic::mnpLogin($params);
if (false === $res) {
return $this->fail(LoginLogic::getError());
}
$res = LoginLogic::dealStaff($res); $res = LoginLogic::dealStaff($res);
return $this->success('', $res); return $this->success('', $res);
} }
@ -63,9 +57,6 @@ class LoginController extends BaseApiController
$params = (new WechatLoginValidate())->post()->goCheck("wechatAuth"); $params = (new WechatLoginValidate())->post()->goCheck("wechatAuth");
$params['user_id'] = $this->userId; $params['user_id'] = $this->userId;
$result = LoginLogic::mnpAuthLogin($params); $result = LoginLogic::mnpAuthLogin($params);
if ($result === false) {
return $this->fail(LoginLogic::getError());
}
return $this->success('绑定成功', [], 1, 1); return $this->success('绑定成功', [], 1, 1);
} }
@ -79,9 +70,6 @@ class LoginController extends BaseApiController
{ {
$params = $this->request->post(); $params = $this->request->post();
$result = LoginLogic::updateUser($params, $this->userId); $result = LoginLogic::updateUser($params, $this->userId);
if ($result === false) {
return $this->fail(LoginLogic::getError());
}
return $this->success('操作成功', [], 1, 0); return $this->success('操作成功', [], 1, 0);
} }

View File

@ -90,7 +90,7 @@ class CartController extends BaseApiController
if ($res) { if ($res) {
return $this->success('修改成功'); return $this->success('修改成功');
} else { } else {
return $this->fail(CartLogic::getError()); return $this->fail('修改失败');
} }
} }
@ -105,7 +105,7 @@ class CartController extends BaseApiController
if ($res) { if ($res) {
return $this->success('删除成功'); return $this->success('删除成功');
} else { } else {
return $this->fail(CartLogic::getError()); return $this->fail('删除失败');
} }
} }
} }

View File

@ -105,13 +105,6 @@ class OrderController extends BaseApiController
$params = $this->request->post(); $params = $this->request->post();
$user = User::where('id', $this->userId)->find(); $user = User::where('id', $this->userId)->find();
$res = OrderLogic::cartIdByOrderInfo($cartId, $addressId, $user, $params); $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); return $this->data($res);
} }
@ -119,13 +112,6 @@ class OrderController extends BaseApiController
{ {
$params = (new OrderValidate())->post()->goCheck('cart'); $params = (new OrderValidate())->post()->goCheck('cart');
$res = OrderLogic::checkLeft($params, $this->userId); $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); return $this->data($res);
} }
@ -230,8 +216,6 @@ class OrderController extends BaseApiController
return $this->fail('支付方式错误'); return $this->fail('支付方式错误');
} }
// return $this->data(['order_id' => $order->id]); // return $this->data(['order_id' => $order->id]);
} else {
return $this->fail(OrderLogic::getError());
} }
} }
@ -306,17 +290,11 @@ class OrderController extends BaseApiController
} }
} }
$result = PaymentLogic::pay($pay_type, 'wechat_common', $order, $this->userInfo['terminal'] ?? 1, $redirectUrl); $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); return $this->success('', $result);
break; break;
case PayEnum::WECHAT_PAY_BARCODE: case PayEnum::WECHAT_PAY_BARCODE:
//微信条码支付 //微信条码支付
$result = PaymentLogic::codepay($auth_code, $order); $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'] == '支付成功') { if (isset($result['trade_state_desc']) && $result['trade_state_desc'] == '支付成功') {
PayNotifyLogic::handle('StoreOrder', $result['out_trade_no'], $result); PayNotifyLogic::handle('StoreOrder', $result['out_trade_no'], $result);
} else { } else {
@ -336,9 +314,6 @@ class OrderController extends BaseApiController
case PayEnum::ALIPAY_BARCODE: case PayEnum::ALIPAY_BARCODE:
//支付宝条码支付 //支付宝条码支付
$result = PaymentLogic::ali_auth_code($auth_code, $order); $result = PaymentLogic::ali_auth_code($auth_code, $order);
if (PaymentLogic::hasError()) {
return $this->fail(PaymentLogic::getError(), $params);
}
if ($result['msg'] !== 'Success') { if ($result['msg'] !== 'Success') {
return $this->success('用户支付中'); return $this->success('用户支付中');
} }
@ -388,11 +363,8 @@ class OrderController extends BaseApiController
{ {
$params = $this->request->get(); $params = $this->request->get();
$res = OrderLogic::frequentlyPurchase($params); $res = OrderLogic::frequentlyPurchase($params);
if (OrderLogic::hasError()) { return $this->data($res);
return $this->fail(OrderLogic::getError());
} else {
return $this->data($res);
}
} }
/** /**
@ -466,7 +438,7 @@ class OrderController extends BaseApiController
if ($res) { if ($res) {
return $this->success('核销成功'); return $this->success('核销成功');
} }
return $this->fail('核销失败' . OrderLogic::getError()); return $this->fail('核销失败');
} }
@ -476,7 +448,7 @@ class OrderController extends BaseApiController
$date = $this->request->get('date', date('Y-m-d')); $date = $this->request->get('date', date('Y-m-d'));
$store_id = SystemStoreStaff::where('phone', $this->userInfo['mobile'])->value('store_id'); $store_id = SystemStoreStaff::where('phone', $this->userInfo['mobile'])->value('store_id');
if (empty($store_id)) { if (empty($store_id)) {
throw new \Exception('该用户未绑定店铺'); return $this->fail('该用户未绑定店铺');
} }
$where[] = ['store_id', '=', $store_id]; $where[] = ['store_id', '=', $store_id];
$where[] = ['paid', '=', 1]; $where[] = ['paid', '=', 1];

View File

@ -89,20 +89,11 @@ class StoreController extends BaseApiController
$params['create_uid']=$this->userId; $params['create_uid']=$this->userId;
if(isset($params['user_ship']) && in_array($params['user_ship'],[2,3])){ if(isset($params['user_ship']) && in_array($params['user_ship'],[2,3])){
UserUserLogic::checkAddress($params); UserUserLogic::checkAddress($params);
if(UserUserLogic::hasError()){
return $this->fail(UserUserLogic::getError());
}
} }
$find=UserUserLogic::StoreAdd($params); $find=UserUserLogic::StoreAdd($params);
if(UserUserLogic::hasError()){
return $this->fail(UserUserLogic::getError());
}
}else{ }else{
if($find['user_ship']!=$params['user_ship'] && in_array($params['user_ship'],[2,3])){ if($find['user_ship']!=$params['user_ship'] && in_array($params['user_ship'],[2,3])){
UserUserLogic::checkAddress($params); UserUserLogic::checkAddress($params);
if(UserUserLogic::hasError()){
return $this->fail(UserUserLogic::getError());
}
} }
$find['real_name']=$params['real_name']; $find['real_name']=$params['real_name'];
$find['label_id']=$params['label_id']??0; $find['label_id']=$params['label_id']??0;
@ -144,9 +135,6 @@ class StoreController extends BaseApiController
$order['pay_price']=$order['price']; $order['pay_price']=$order['price'];
$order['attach']='recharge'; $order['attach']='recharge';
$result = PaymentLogic::codepay($auth_code, $order,'条码支付'); $result = PaymentLogic::codepay($auth_code, $order,'条码支付');
if (PaymentLogic::hasError()) {
return $this->fail(PaymentLogic::getError());
}
if (isset($result['trade_state_desc']) && $result['trade_state_desc'] == '支付成功') { if (isset($result['trade_state_desc']) && $result['trade_state_desc'] == '支付成功') {
Cache::set('trade_state' . time(), json_encode($result)); Cache::set('trade_state' . time(), json_encode($result));
PayNotifyLogic::handle('recharge', $result['out_trade_no'], $result); PayNotifyLogic::handle('recharge', $result['out_trade_no'], $result);
@ -181,9 +169,6 @@ class StoreController extends BaseApiController
$order['pay_price']=$order['price']; $order['pay_price']=$order['price'];
$order['attach']='recharge'; $order['attach']='recharge';
$result = PaymentLogic::codepay($auth_code, $order,'条码支付'); $result = PaymentLogic::codepay($auth_code, $order,'条码支付');
if (PaymentLogic::hasError()) {
return $this->fail(PaymentLogic::getError());
}
if (isset($result['trade_state_desc']) && $result['trade_state_desc'] == '支付成功') { if (isset($result['trade_state_desc']) && $result['trade_state_desc'] == '支付成功') {
PayNotifyLogic::handle('recharge', $result['out_trade_no'], $result); PayNotifyLogic::handle('recharge', $result['out_trade_no'], $result);
} else { } else {

View File

@ -23,11 +23,8 @@ class AddressController extends BaseApiController
$params = (new UserAddressValidate())->post()->goCheck('add'); $params = (new UserAddressValidate())->post()->goCheck('add');
$params['uid'] = $this->request->userId; $params['uid'] = $this->request->userId;
$res=AddressLogic::add($params); $res=AddressLogic::add($params);
if(AddressLogic::hasError()){ return $this->success('添加成功');
return $this->fail(AddressLogic::getError());
}else{
return $this->success('添加成功');
}
} }
/** /**
* 商户给用户添加地址 * 商户给用户添加地址
@ -62,9 +59,8 @@ class AddressController extends BaseApiController
$params['uid'] = $this->request->userId; $params['uid'] = $this->request->userId;
if(AddressLogic::edit($params)){ if(AddressLogic::edit($params)){
return $this->success('编辑成功'); return $this->success('编辑成功');
}else{
return $this->fail(AddressLogic::getError());
} }
return $this->fail('编辑失败');
} }

View File

@ -42,9 +42,7 @@ class UserController extends BaseApiController
$params = (new UserValidate())->post()->goCheck('getMobileByMnp'); $params = (new UserValidate())->post()->goCheck('getMobileByMnp');
$params['user_id'] = $this->userId; $params['user_id'] = $this->userId;
$result = UserLogic::getMobileByMnp($params); $result = UserLogic::getMobileByMnp($params);
if ($result === false) {
return $this->fail(UserLogic::getError());
}
if ($result && is_numeric($result)) { if ($result && is_numeric($result)) {
$data = UserLogic::info($result); $data = UserLogic::info($result);
$userInfo = UserTokenService::setToken($result, 1); $userInfo = UserTokenService::setToken($result, 1);
@ -104,9 +102,6 @@ class UserController extends BaseApiController
$order = UserLogic::recharge($params); $order = UserLogic::recharge($params);
$redirectUrl = $params['redirect'] ?? '/pages/payment/payment'; $redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
$result = PaymentLogic::pay(PayEnum::WECHAT_PAY_MINI, 'recharge', $order, $this->userInfo['terminal'], $redirectUrl); $result = PaymentLogic::pay(PayEnum::WECHAT_PAY_MINI, 'recharge', $order, $this->userInfo['terminal'], $redirectUrl);
if (PaymentLogic::hasError()) {
return $this->fail(PaymentLogic::getError(), $params);
}
return $this->success('', $result); return $this->success('', $result);
} }

View File

@ -30,6 +30,6 @@
if (true === $result) { if (true === $result) {
return $this->success('添加成功', [], 1, 1); return $this->success('添加成功', [], 1, 1);
} }
return $this->fail(UserFeedbackLogic::getError()); return $this->fail('添加失败');
} }
} }

View File

@ -30,8 +30,6 @@ class UserVisitController extends BaseApiController
if (true === $result) { if (true === $result) {
return $this->success('ok'); return $this->success('ok');
} }
return $this->fail(UserVisitLogic::getError());
} }
@ -54,7 +52,6 @@ class UserVisitController extends BaseApiController
if (true === $result) { if (true === $result) {
return $this->success('添加成功', [], 1, 1); return $this->success('添加成功', [], 1, 1);
} }
return $this->fail(UserVisitLogic::getError());
} }

View File

@ -38,9 +38,6 @@ class UserProductStorageController extends BaseApiController
$store_id=$params['store_id']; $store_id=$params['store_id'];
$times=$params['times']; $times=$params['times'];
UserProductStorageLogic::supply($info,$uid,$store_id,0,$times); UserProductStorageLogic::supply($info,$uid,$store_id,0,$times);
if(UserProductStorageLogic::hasError()){
return $this->fail(UserProductStorageLogic::getError());
}
return $this->success('操作成功'); return $this->success('操作成功');
} }
} }

View File

@ -31,10 +31,7 @@ class UserShipController extends BaseApiController
public function is_user_ship(){ public function is_user_ship(){
$data=$this->request->post(); $data=$this->request->post();
UserShipLogic::user_ship($data); UserShipLogic::user_ship($data);
if(UserShipLogic::hasError()){ return $this->success('ok');
return $this->fail(UserShipLogic::getError());
}else{
return $this->success('ok');
}
} }
} }

View File

@ -27,7 +27,7 @@ use app\common\service\{
}; };
use app\common\model\user\{User, UserAuth}; use app\common\model\user\{User, UserAuth};
use app\common\service\wechat\WeChatMnpService; use app\common\service\wechat\WeChatMnpService;
use support\exception\BusinessException;
use Webman\Config; use Webman\Config;
/** /**
@ -88,7 +88,7 @@ class LoginLogic extends BaseLogic
$user = User::where($where)->findOrEmpty(); $user = User::where($where)->findOrEmpty();
if ($user->isEmpty()) { if ($user->isEmpty()) {
throw new \Exception('用户不存在'); throw new BusinessException('用户不存在');
} }
//更新登录信息 //更新登录信息
@ -121,8 +121,7 @@ class LoginLogic extends BaseLogic
'share_name' => $share_name.'No.'.preg_replace('/4/','*', $user['id']), 'share_name' => $share_name.'No.'.preg_replace('/4/','*', $user['id']),
]; ];
} catch (\Exception $e) { } catch (\Exception $e) {
self::setError($e->getMessage()); throw new BusinessException($e->getMessage());
return false;
} }
} }
@ -214,9 +213,8 @@ class LoginLogic extends BaseLogic
} }
return $userInfo; return $userInfo;
} catch (\Exception $e) { } catch (\Throwable $e) {
self::$error = $e->getMessage(); throw new BusinessException($e->getMessage());
return false;
} }
} }
@ -242,9 +240,8 @@ class LoginLogic extends BaseLogic
// Db::commit(); // Db::commit();
return $userInfo; return $userInfo;
} catch (\Exception $e) { } catch (\Exception $e) {
throw new BusinessException($e->getMessage());
// Db::rollback(); // Db::rollback();
self::$error = $e->getMessage();
return false;
} }
} }
@ -260,7 +257,7 @@ class LoginLogic extends BaseLogic
{ {
$user = User::findOrEmpty($userId); $user = User::findOrEmpty($userId);
if ($user->isEmpty()) { if ($user->isEmpty()) {
throw new \Exception('用户不存在'); throw new BusinessException('用户不存在');
} }
$time = time(); $time = time();
@ -348,7 +345,7 @@ class LoginLogic extends BaseLogic
//先检查openid是否有记录 //先检查openid是否有记录
$isAuth = UserAuth::where('openid', '=', $response['openid'])->findOrEmpty(); $isAuth = UserAuth::where('openid', '=', $response['openid'])->findOrEmpty();
if (!$isAuth->isEmpty()) { if (!$isAuth->isEmpty()) {
throw new \Exception('该微信已被绑定'); throw new BusinessException('该微信已被绑定');
} }
if (isset($response['unionid']) && !empty($response['unionid'])) { if (isset($response['unionid']) && !empty($response['unionid'])) {
@ -356,7 +353,7 @@ class LoginLogic extends BaseLogic
$userAuth = UserAuth::where(['unionid' => $response['unionid']]) $userAuth = UserAuth::where(['unionid' => $response['unionid']])
->findOrEmpty(); ->findOrEmpty();
if (!$userAuth->isEmpty() && $userAuth->user_id != $response['user_id']) { if (!$userAuth->isEmpty() && $userAuth->user_id != $response['user_id']) {
throw new \Exception('该微信已被绑定'); throw new BusinessException('该微信已被绑定');
} }
} }
@ -456,8 +453,7 @@ class LoginLogic extends BaseLogic
if($find){ if($find){
$auth=UserAuth::where(['user_id'=>$find['id']])->find();//别人的 $auth=UserAuth::where(['user_id'=>$find['id']])->find();//别人的
if($auth){ if($auth){
self::$error ='该手机号已绑定'; throw new BusinessException('该手机号已绑定');
return false;
}else{ }else{
UserAuth::where(['user_id'=>$userId])->update(['user_id'=>$find['id']]); UserAuth::where(['user_id'=>$userId])->update(['user_id'=>$find['id']]);
} }

View File

@ -6,6 +6,7 @@ namespace app\api\logic\order;
use app\common\model\order\Cart; use app\common\model\order\Cart;
use app\common\logic\BaseLogic; use app\common\logic\BaseLogic;
use app\common\model\store_product_log\StoreProductLog; use app\common\model\store_product_log\StoreProductLog;
use support\exception\BusinessException;
use think\facade\Db; use think\facade\Db;
@ -28,8 +29,7 @@ class CartLogic extends BaseLogic
public static function add(array $params) public static function add(array $params)
{ {
if ($params['store_id'] <= 0) { if ($params['store_id'] <= 0) {
self::setError('门店ID不能为空'); throw new BusinessException('门店ID不能为空');
return false;
} }
Db::startTrans(); Db::startTrans();
try { try {
@ -66,10 +66,9 @@ class CartLogic extends BaseLogic
]); ]);
Db::commit(); Db::commit();
return $cart; return $cart;
} catch (\Exception $e) { } catch (\Throwable $e) {
Db::rollback(); Db::rollback();
self::setError($e->getMessage()); throw new BusinessException($e->getMessage());
return false;
} }
} }
@ -93,10 +92,9 @@ class CartLogic extends BaseLogic
->update(['cart_num' => $params['cart_num']]); ->update(['cart_num' => $params['cart_num']]);
Db::commit(); Db::commit();
return true; return true;
} catch (\Exception $e) { } catch (\Throwable $e) {
Db::rollback(); Db::rollback();
self::setError($e->getMessage()); throw new BusinessException($e->getMessage());
return false;
} }
} }

View File

@ -67,15 +67,13 @@ class OrderLogic extends BaseLogic
static public function cartIdByOrderInfo($cartId, $addressId, $user = null, $params = [], $createOrder = 0) static public function cartIdByOrderInfo($cartId, $addressId, $user = null, $params = [], $createOrder = 0)
{ {
if(empty($params['store_id']) || $params['store_id'] <= 0){ if(empty($params['store_id']) || $params['store_id'] <= 0){
self::setError('请选择门店'); throw new BusinessException('请选择门店');
return false;
} }
$where = ['is_pay' => 0]; $where = ['is_pay' => 0];
$cart_select = Cart::whereIn('id', $cartId)->where($where)->field('id,product_id,cart_num')->select()->toArray(); $cart_select = Cart::whereIn('id', $cartId)->where($where)->field('id,product_id,cart_num')->select()->toArray();
if (empty($cart_select)) { if (empty($cart_select)) {
self::setError('购物车为空'); throw new BusinessException('购物车为空');
return false;
} }
try { try {
self::$total_price = 0; self::$total_price = 0;
@ -93,14 +91,12 @@ class OrderLogic extends BaseLogic
foreach ($cart_select as $k => $v) { foreach ($cart_select as $k => $v) {
$find = StoreBranchProduct::where(['product_id' => $v['product_id'],'store_id'=>$params['store_id']])->field($field)->find(); $find = StoreBranchProduct::where(['product_id' => $v['product_id'],'store_id'=>$params['store_id']])->field($field)->find();
if (!$find) { if (!$find) {
self::setError('商品不存在'); throw new BusinessException('商品不存在');
return false;
} }
if (convertNumber($v['cart_num']) == false) { if (convertNumber($v['cart_num']) == false) {
$is_bulk = StoreProductUnit::where('id', $find['unit'])->value('is_bulk'); $is_bulk = StoreProductUnit::where('id', $find['unit'])->value('is_bulk');
if ($is_bulk == 0) { if ($is_bulk == 0) {
self::setError('非计量商品,不能有小数,请编辑购物车'); throw new BusinessException('非计量商品,不能有小数,请编辑购物车');
return false;
} }
} }
$StoreCategory = StoreCategory::where('id', $find['cate_id'])->find(); $StoreCategory = StoreCategory::where('id', $find['cate_id'])->find();
@ -195,8 +191,7 @@ class OrderLogic extends BaseLogic
//判断生鲜是否大于200 //判断生鲜是否大于200
if ($createOrder == 1 && self::$fresh_price > 0) { if ($createOrder == 1 && self::$fresh_price > 0) {
if (self::$pay_price < 200) { if (self::$pay_price < 200) {
self::setError('订单包含生鲜产品订单金额必须大于200元才能下单'); throw new BusinessException('订单包含生鲜产品订单金额必须大于200元才能下单');
return false;
} }
} }
if (isset($params['store_id']) && $params['store_id'] == getenv('ACTIVITY_STORE_ID')) { if (isset($params['store_id']) && $params['store_id'] == getenv('ACTIVITY_STORE_ID')) {
@ -256,10 +251,10 @@ class OrderLogic extends BaseLogic
$currentDate = date('Y-m-d'); $currentDate = date('Y-m-d');
$alert = '当前时间超过配送截止时间16:00,若下单,物品送达时间为' . date('Y-m-d', strtotime($currentDate . '+2 days')); $alert = '当前时间超过配送截止时间16:00,若下单,物品送达时间为' . date('Y-m-d', strtotime($currentDate . '+2 days'));
} }
} catch (\Exception $e) { } catch (\Throwable $e) {
d($e); throw new BusinessException($e->getMessage());
self::setError($e->getMessage());
return false;
} }
return ['order' => $order, 'cart_list' => $cart_select, 'shopInfo' => $store['near_store'], 'alert' => $alert]; return ['order' => $order, 'cart_list' => $cart_select, 'shopInfo' => $store['near_store'], 'alert' => $alert];
} }
@ -276,17 +271,10 @@ class OrderLogic extends BaseLogic
$params['order_id'] = $order_id; $params['order_id'] = $order_id;
$params['verify_code'] = $verify_code; $params['verify_code'] = $verify_code;
$orderInfo = self::cartIdByOrderInfo($cartId, $addressId, $user, $params, 1); $orderInfo = self::cartIdByOrderInfo($cartId, $addressId, $user, $params, 1);
if ($orderInfo == false) {
self::setError(self::getError());
return false;
}
if (!$orderInfo) {
return false;
}
$uid = $user['id'] ?? 0; $uid = $user['id'] ?? 0;
$_order = $orderInfo['order']; $_order = $orderInfo['order'];
if ($_order['pay_price'] == 0) { if ($_order['pay_price'] == 0) {
throw new \Exception('支付金额不能为0'); throw new BusinessException('支付金额不能为0');
} }
$_order['uid'] = $uid; $_order['uid'] = $uid;
$_order['spread_uid'] = $params['spread_uid'] ?? 0; $_order['spread_uid'] = $params['spread_uid'] ?? 0;
@ -327,7 +315,7 @@ class OrderLogic extends BaseLogic
$_order['status'] = 1; $_order['status'] = 1;
} }
if ($_order['pay_type'] == PayEnum::BALANCE_PAY && $user != null && $user['now_money'] < $_order['pay_price']) { if ($_order['pay_type'] == PayEnum::BALANCE_PAY && $user != null && $user['now_money'] < $_order['pay_price']) {
throw new \Exception('余额不足'); throw new BusinessException('余额不足');
} }
//生成核销码 //生成核销码
// $generator = new BarcodeGeneratorPNG(); // $generator = new BarcodeGeneratorPNG();
@ -356,8 +344,7 @@ class OrderLogic extends BaseLogic
return $order; return $order;
} catch (\Exception $e) { } catch (\Exception $e) {
Db::rollback(); Db::rollback();
self::setError($e->getMessage()); throw new BusinessException($e->getMessage());
return false;
} }
} }
@ -378,8 +365,7 @@ class OrderLogic extends BaseLogic
$cart_select = Cart::whereIn('id', $params['cart_id']) $cart_select = Cart::whereIn('id', $params['cart_id'])
->where($where)->field('id,product_id,cart_num,store_id')->select()->toArray(); ->where($where)->field('id,product_id,cart_num,store_id')->select()->toArray();
if (empty($cart_select)) { if (empty($cart_select)) {
self::setError('购物车为空'); throw new BusinessException('购物车为空');
return false;
} }
$newArr = []; $newArr = [];
//检查购物车对比店铺得商品数量差异 //检查购物车对比店铺得商品数量差异
@ -447,12 +433,11 @@ class OrderLogic extends BaseLogic
// 提交事务 // 提交事务
Db::commit(); Db::commit();
return true; return true;
} catch (\Exception $e) { } catch (\Throwable $e) {
// 回滚事务 // 回滚事务
Db::rollback(); Db::rollback();
Log::error('支付失败' . $e->getMessage() . '。like:' . $e->getLine()); Log::error('支付失败' . $e->getMessage() . '。like:' . $e->getLine());
self::setError('支付失败' . $e->getMessage()); throw new BusinessException('支付失败'. $e->getMessage());
return false;
} }
} }
@ -469,9 +454,8 @@ class OrderLogic extends BaseLogic
$goods_arr = array_unique($goods_id); $goods_arr = array_unique($goods_id);
$select = StoreBranchProduct::where('product_id', 'in', $goods_arr)->with('unitName')->field('id,store_name,price,image,unit')->select(); $select = StoreBranchProduct::where('product_id', 'in', $goods_arr)->with('unitName')->field('id,store_name,price,image,unit')->select();
return $select->toArray(); return $select->toArray();
} catch (\Exception $e) { } catch (\Throwable $e) {
self::setError($e->getMessage()); throw new BusinessException($e->getMessage());
return false;
} }
} }
@ -584,8 +568,7 @@ class OrderLogic extends BaseLogic
'verify_code' => $params['verify_code'] 'verify_code' => $params['verify_code']
])->find(); ])->find();
if (empty($order)) { if (empty($order)) {
self::setError('订单不存在'); throw new BusinessException('订单不存在');
return false;
} }
Db::startTrans(); Db::startTrans();
try { try {
@ -631,10 +614,9 @@ class OrderLogic extends BaseLogic
} }
Db::commit(); Db::commit();
return true; return true;
} catch (\Exception $e) { } catch (\Throwable $e) {
Db::rollback(); Db::rollback();
self::setError($e->getMessage()); throw new BusinessException($e->getMessage());
return false;
} }
} }
@ -643,7 +625,7 @@ class OrderLogic extends BaseLogic
{ {
$store_id = SystemStoreStaff::where('phone', $info['mobile'])->value('store_id'); $store_id = SystemStoreStaff::where('phone', $info['mobile'])->value('store_id');
if (empty($store_id)) { if (empty($store_id)) {
throw new \Exception('该用户未绑定店铺请查看'); throw new BusinessException('该用户未绑定店铺请查看');
} }
$query = StoreOrderCartInfo::alias('o') $query = StoreOrderCartInfo::alias('o')
->leftJoin('store_branch_product p', 'p.id = o.product_id') ->leftJoin('store_branch_product p', 'p.id = o.product_id')
@ -693,7 +675,7 @@ class OrderLogic extends BaseLogic
$store_id = SystemStoreStaff::where('phone', $info['mobile'])->value('store_id'); $store_id = SystemStoreStaff::where('phone', $info['mobile'])->value('store_id');
if (empty($store_id)) { if (empty($store_id)) {
throw new \Exception('该用户未绑定店铺请查看'); throw new BusinessException('该用户未绑定店铺请查看');
} }
//先查商品相似 //先查商品相似
@ -785,7 +767,7 @@ class OrderLogic extends BaseLogic
self::dealChangeCartInfo($refundOrder); self::dealChangeCartInfo($refundOrder);
// d($leftOrder,$refundOrder); // d($leftOrder,$refundOrder);
Db::commit(); Db::commit();
} catch (\Exception $e) { } catch (\Throwable $e) {
// 回滚事务 // 回滚事务
Db::rollback(); Db::rollback();
throw new BusinessException($e->getMessage()); throw new BusinessException($e->getMessage());

View File

@ -4,6 +4,7 @@ namespace app\api\logic\user;
use app\common\logic\BaseLogic; use app\common\logic\BaseLogic;
use app\common\model\user\UserAddress; use app\common\model\user\UserAddress;
use support\exception\BusinessException;
use think\facade\Db; use think\facade\Db;
/** /**
@ -48,10 +49,9 @@ class AddressLogic extends BaseLogic
]); ]);
Db::commit(); Db::commit();
return $id; return $id;
} catch (\Exception $e) { } catch (\Throwable $e) {
Db::rollback(); Db::rollback();
self::setError($e->getMessage()); throw new BusinessException($e->getMessage());
return false;
} }
} }
@ -95,10 +95,9 @@ class AddressLogic extends BaseLogic
Db::commit(); Db::commit();
return true; return true;
} catch (\Exception $e) { } catch (\Throwable $e) {
Db::rollback(); Db::rollback();
self::setError($e->getMessage()); throw new BusinessException($e->getMessage());
return false;
} }
} }

View File

@ -4,7 +4,8 @@
use app\common\logic\BaseLogic; use app\common\logic\BaseLogic;
use app\common\model\user\UserFeedback; use app\common\model\user\UserFeedback;
use think\facade\Db; use support\exception\BusinessException;
use think\facade\Db;
class UserFeedbackLogic extends BaseLogic class UserFeedbackLogic extends BaseLogic
{ {
@ -29,10 +30,10 @@
]); ]);
Db::commit(); Db::commit();
return true; return true;
} catch (\Exception $e) { } catch (\Throwable $e) {
Db::rollback(); Db::rollback();
self::setError($e->getMessage()); throw new BusinessException($e->getMessage());
return false;
} }
} }
} }

View File

@ -24,6 +24,7 @@ use app\common\{logic\BaseLogic,
use app\common\logic\UserSignLogic; use app\common\logic\UserSignLogic;
use app\common\model\user_label\UserLabel; use app\common\model\user_label\UserLabel;
use support\Cache; use support\Cache;
use support\exception\BusinessException;
use think\facade\Db; use think\facade\Db;
@ -49,7 +50,7 @@ class UserLogic extends BaseLogic
$response = (new WeChatMnpService())->getUserPhoneNumber($params['code']); $response = (new WeChatMnpService())->getUserPhoneNumber($params['code']);
$phoneNumber = $response['phone_info']['purePhoneNumber'] ?? ''; $phoneNumber = $response['phone_info']['purePhoneNumber'] ?? '';
if (empty($phoneNumber)) { if (empty($phoneNumber)) {
throw new \Exception('获取手机号码失败'); throw new BusinessException('获取手机号码失败');
} }
$user = User::where([ $user = User::where([
@ -75,9 +76,8 @@ class UserLogic extends BaseLogic
]); ]);
return true; return true;
} catch (\Exception $e) { } catch (\Throwable $e) {
self::setError($e->getMessage()); throw new BusinessException($e->getMessage());
return false;
} }
} }
@ -211,7 +211,7 @@ class UserLogic extends BaseLogic
$code = generateRandomCode(); $code = generateRandomCode();
$phone = User::where('id',$uid)->value('mobile'); $phone = User::where('id',$uid)->value('mobile');
if(empty($phone)){ if(empty($phone)){
throw new \Exception('用户未设置手机号'); throw new BusinessException('用户未设置手机号');
} }
$template = getenv('SMS_TEMPLATE'); $template = getenv('SMS_TEMPLATE');
$check =(new SmsService())->client($phone,$template,$code); $check =(new SmsService())->client($phone,$template,$code);

View File

@ -5,7 +5,8 @@
use app\common\logic\BaseLogic; use app\common\logic\BaseLogic;
use app\common\model\store_visit\StoreVisit; use app\common\model\store_visit\StoreVisit;
use app\common\model\user\UserVisit; use app\common\model\user\UserVisit;
use think\facade\Db; use support\Log;
use think\facade\Db;
class UserVisitLogic extends BaseLogic class UserVisitLogic extends BaseLogic
{ {
@ -39,10 +40,9 @@
} }
Db::commit(); Db::commit();
return true; return true;
} catch (\Exception $e) { } catch (\Throwable $e) {
Db::rollback(); Db::rollback();
self::setError($e->getMessage()); Log::error('添加商品浏览失败:'.$e->getMessage());
return false;
} }
} }
@ -70,10 +70,9 @@
} }
Db::commit(); Db::commit();
return true; return true;
} catch (\Exception $e) { } catch (\Throwable $e) {
Db::rollback(); Db::rollback();
self::setError($e->getMessage()); Log::error('添加用户访问失败:'.$e->getMessage());
return false;
} }

View File

@ -8,7 +8,9 @@ use app\common\enum\PayEnum;
use app\common\model\user\UserAuth; use app\common\model\user\UserAuth;
use app\common\service\pay\PayService; use app\common\service\pay\PayService;
use Exception; use Exception;
use support\exception\BusinessException;
use support\Log; use support\Log;
use Throwable;
use function DI\string; use function DI\string;
@ -40,34 +42,33 @@ class PaymentLogic extends BaseLogic
return ['pay_way' => PayEnum::BALANCE_PAY]; return ['pay_way' => PayEnum::BALANCE_PAY];
} }
try { try {
if(isset($order['price'])){ if (isset($order['price'])) {
$order['pay_price'] = $order['price']; $order['pay_price'] = $order['price'];
} }
switch ($payWay) { switch ($payWay) {
case PayEnum::WECHAT_PAY_MINI: case PayEnum::WECHAT_PAY_MINI:
$auth = UserAuth::where(['user_id' => $order['uid'], 'terminal' => $terminal])->findOrEmpty(); $auth = UserAuth::where(['user_id' => $order['uid'], 'terminal' => $terminal])->findOrEmpty();
$order = [ $order = [
'out_trade_no' => $paySn, 'out_trade_no' => $paySn,
'description' => '商品', 'description' => '商品',
'amount' => [ 'amount' => [
'total' => intval($order['pay_price'] * 100), 'total' => intval($order['pay_price'] * 100),
'currency' => 'CNY', 'currency' => 'CNY',
], ],
"payer" => [ "payer" => [
"openid" => $auth['openid'] "openid" => $auth['openid']
], ],
'attach' => $from 'attach' => $from
]; ];
$wechat = new PayService(1); $wechat = new PayService(1);
$result = $wechat->wechat->mini($order)->toArray(); $result = $wechat->wechat->mini($order)->toArray();
break; break;
default: default:
self::$error = '订单异常'; throw new BusinessException('支付方式异常');
$result = false; }
} } catch (Throwable $e) {
} catch (Exception $e) { Log::info($e->extra['message'] ?? $e->getMessage());
\support\Log::info($e->extra['message']?? $e->getMessage()); throw new BusinessException($e->extra['message'] ?? $e->getMessage());
throw new \Exception($e->extra['message']?? $e->getMessage());
} }
return $result; return $result;
} }
@ -75,13 +76,12 @@ class PaymentLogic extends BaseLogic
/** /**
* 微信条码支付 * 微信条码支付
*/ */
public static function codepay($auth_code, $order,$description='条码商品') public static function codepay($auth_code, $order, $description = '条码商品')
{ {
$pattern = '/^(10|11|12|13|14|15)\d{16}$/'; $pattern = '/^(10|11|12|13|14|15)\d{16}$/';
if (!preg_match($pattern, (string)$auth_code)) { if (!preg_match($pattern, (string)$auth_code)) {
self::$error = '请使用正确的微信收付款条码'; throw new BusinessException('请使用正确的微信收付款条码');
return false;
} }
$data = [ $data = [
'description' => $description, 'description' => $description,
@ -94,26 +94,25 @@ class PaymentLogic extends BaseLogic
], ],
'scene_info' => [ 'scene_info' => [
"store_info" => [ "store_info" => [
'id' => (string)$order['store_id']??1 'id' => (string)$order['store_id'] ?? 1
] ]
], ],
'attach'=>'wechat_common' 'attach' => 'wechat_common'
]; ];
if(isset($order['attach']) && $order['attach']!=''){ if (isset($order['attach']) && $order['attach'] != '') {
$data['attach'] = $order['attach']; $data['attach'] = $order['attach'];
} }
$wechat = new PayService(1); $wechat = new PayService(1);
try { try {
$result = $wechat->wechat->pos($data)->toArray(); $result = $wechat->wechat->pos($data)->toArray();
} catch (Exception $e) { } catch (Throwable $e) {
Log::error('条码支付报错',['message' => $e->extra['message']?? $e->getMessage(),'code'=>$e->getCode()]); Log::error('条码支付报错', ['message' => $e->extra['message'] ?? $e->getMessage(), 'code' => $e->getCode()]);
if (getenv('APP_DEBUG') == true) { if (getenv('APP_DEBUG') == true) {
self::$error = $e->extra['message'] ?? $e->getMessage(); throw new BusinessException($e->extra['message'] ?? $e->getMessage());
} else { } else {
self::$error = $e->getMessage(); throw new BusinessException($e->getMessage());
} }
return false;
} }
return $result; return $result;
} }
@ -126,26 +125,24 @@ class PaymentLogic extends BaseLogic
$pattern = '/^(25|26|27|28|29|30)[0-9A-Za-z]{14,23}$/'; $pattern = '/^(25|26|27|28|29|30)[0-9A-Za-z]{14,23}$/';
if (!preg_match($pattern, (string)$auth_code)) { if (!preg_match($pattern, (string)$auth_code)) {
self::$error = '请使用正确的支付宝收付款条码'; throw new BusinessException('请使用正确的支付宝收付款条码');
return false;
} }
$order = [ $order = [
'subject' => '条码商品', 'subject' => '条码商品',
'out_trade_no' => (string)$order['order_id'], 'out_trade_no' => (string)$order['order_id'],
'auth_code' => (string)$auth_code, 'auth_code' => (string)$auth_code,
'total_amount' => $order['pay_price'], 'total_amount' => $order['pay_price'],
'extend_params'=>['attach'=>'alipay_cashier'] 'extend_params' => ['attach' => 'alipay_cashier']
]; ];
$wechat = new PayService(); $wechat = new PayService();
try { try {
$result = $wechat->alipay->pos($order)->toArray(); $result = $wechat->alipay->pos($order)->toArray();
} catch (Exception $e) { } catch (Throwable $e) {
if (getenv('APP_DEBUG') == true) { if (getenv('APP_DEBUG') == true) {
self::$error = $e->extra['message'] ?? $e->getMessage(); throw new BusinessException($e->extra['message'] ?? $e->getMessage());
} else { } else {
self::$error = $e->getMessage(); throw new BusinessException($e->getMessage());
} }
return false;
} }
return $result; return $result;
} }

View File

@ -9,6 +9,7 @@ use app\common\model\store_order_cart_info\StoreOrderCartInfo;
use app\common\model\user\User; use app\common\model\user\User;
use app\common\model\user_product_storage\UserProductStorage; use app\common\model\user_product_storage\UserProductStorage;
use app\common\model\user_product_storage_log\UserProductStorageLog; use app\common\model\user_product_storage_log\UserProductStorageLog;
use support\exception\BusinessException;
use think\facade\Db; use think\facade\Db;
/** /**
@ -54,9 +55,7 @@ class UserProductStorageLogic extends BaseLogic
$find=UserProductStorage::where('uid',$uid)->where('product_id',$v['product_id'])->find(); $find=UserProductStorage::where('uid',$uid)->where('product_id',$v['product_id'])->find();
if($find){ if($find){
if($find['nums']<$v['nums']){ if($find['nums']<$v['nums']){
self::setError('库存不足'); throw new BusinessException("库存不足");
Db::commit();
return false;
} }
$nums=bcsub($find['nums'],$v['nums']); $nums=bcsub($find['nums'],$v['nums']);
$find->nums=$nums; $find->nums=$nums;
@ -86,17 +85,14 @@ class UserProductStorageLogic extends BaseLogic
$data_log[$k]['times']=$times; $data_log[$k]['times']=$times;
$data_log[$k]['status']=$status; $data_log[$k]['status']=$status;
}else{ }else{
self::setError('没有查询到该商品'); throw new BusinessException("没有查询到该商品");
Db::commit();
return false;
} }
} }
(new UserProductStorageLog())->saveAll($data_log); (new UserProductStorageLog())->saveAll($data_log);
Db::commit(); Db::commit();
} catch (\Exception $e) { } catch (\Throwable $e) {
Db::rollback(); Db::rollback();
self::setError($e->getMessage()); throw new BusinessException($e->getMessage());
return false;
} }
} }
} }