From a33ef7f1f8de7e85fcf0934497d94abc4267aa77 Mon Sep 17 00:00:00 2001 From: wanghousheng Date: Tue, 30 Jan 2024 13:15:55 +0800 Subject: [PATCH] 1 --- app/api/controller/Identity.php | 42 ++++++++++++++++++++-- app/api/model/user/Identity.php | 3 +- app/api/service/Identity.php | 42 +++++++++++++++++++--- app/api/service/Notify.php | 9 +++++ app/api/service/identity/PaySuccess.php | 47 ++++++++++++++++++++++--- app/api/service/identity/Payment.php | 3 ++ 6 files changed, 133 insertions(+), 13 deletions(-) diff --git a/app/api/controller/Identity.php b/app/api/controller/Identity.php index 9366158e..612ee75f 100644 --- a/app/api/controller/Identity.php +++ b/app/api/controller/Identity.php @@ -3,7 +3,6 @@ declare (strict_types=1); namespace app\api\controller; -use app\common\enum\user\IdentityEnum; use cores\exception\BaseException; use think\db\exception\DataNotFoundException; use think\db\exception\DbException; @@ -24,7 +23,7 @@ class Identity extends Controller { $client = $this->request->post('client'); $model = new \app\api\service\Identity(); - $list = $model->center($client, ['type' => IdentityEnum::MEMBER]); + $list = $model->userCenter($client); return $this->renderSuccess(compact('list')); } @@ -40,7 +39,44 @@ class Identity extends Controller { $client = $this->request->post('client'); $model = new \app\api\service\Identity(); - $list = $model->center($client, ['type' => IdentityEnum::MEMBER]); + $list = $model->dealerCenter($client); return $this->renderSuccess(compact('list')); } + + /** + * 确认充值订单 + * @return Json + * @throws BaseException + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException + */ + public function submit(): Json + { + $data = $this->postForm(); + $service = new \app\api\service\Identity(); + $data = $service->setMethod($data['method']) + ->setClient($data['client']) + ->orderPay($data['identity_id'], $data['extra']); + return $this->renderSuccess($data, $service->getMessage() ?: '下单成功'); + } + + /** + * 交易查询 + * @param string $outTradeNo 商户订单号 + * @param string $method 支付方式 + * @param string $client 指定的客户端 + * @return Json + * @throws BaseException + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException + */ + public function tradeQuery(string $outTradeNo, string $method, string $client): Json + { + $service = new \app\api\service\Identity(); + $result = $service->setMethod($method)->setClient($client)->tradeQuery($outTradeNo); + $message = $result ? '恭喜您,开通成功' : ($service->getError() ?: '很抱歉,订单未支付,请重新发起'); + return $this->renderSuccess(['isPay' => $result], $message); + } } \ No newline at end of file diff --git a/app/api/model/user/Identity.php b/app/api/model/user/Identity.php index e8e89afc..4c9ae3e0 100644 --- a/app/api/model/user/Identity.php +++ b/app/api/model/user/Identity.php @@ -12,10 +12,9 @@ class Identity extends BaseIdentity * @var array */ protected $hidden = [ - 'type', 'create_time', 'update_time', 'store_id', ]; - + } \ No newline at end of file diff --git a/app/api/service/Identity.php b/app/api/service/Identity.php index 8b545e30..e7b385cb 100644 --- a/app/api/service/Identity.php +++ b/app/api/service/Identity.php @@ -5,6 +5,7 @@ namespace app\api\service; use app\api\model\Payment as PaymentModel; use app\api\service\identity\Payment; use app\api\service\User as UserService; +use app\common\enum\user\IdentityEnum; use app\common\service\BaseService; use cores\exception\BaseException; use think\db\exception\DataNotFoundException; @@ -45,22 +46,55 @@ class Identity extends BaseService } /** - * 开通会员/分销页面数据 + * 开通会员页面数据 * @param string $client 当前客户端 - * @param array $where * @return array * @throws BaseException * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ - public function center(string $client, array $where): array + public function userCenter(string $client): array { // 当期用户信息 $userInfo = UserService::getCurrentLoginUser(true); + //是否分销商 + if (UserService::isDealerMember() || UserService::isStore()) { + throwError('非法操作'); + } // 获取充值方案列表 $model = new \app\api\model\user\Identity(); - $planList = $model->getList($where); + $planList = $model->getList(['type' => IdentityEnum::MEMBER]); + // 根据指定客户端获取可用的支付方式 + $PaymentModel = new PaymentModel; + $methods = $PaymentModel->getMethodsByClient($client); + // 返回数据 + return [ + 'personal' => $userInfo, + 'list' => $planList, + 'paymentMethods' => $methods + ]; + } + + /** + * 开通分销页面数据 + * @param string $client 当前客户端 + * @return array + * @throws BaseException + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException + */ + public function dealerCenter(string $client): array + { + // 当期用户信息 + $userInfo = UserService::getCurrentLoginUser(true); + if (UserService::isStore()) { + throwError('非法操作'); + } + // 获取充值方案列表 + $model = new \app\api\model\user\Identity(); + $planList = $model->getList(['type' => IdentityEnum::DEALER]); // 根据指定客户端获取可用的支付方式 $PaymentModel = new PaymentModel; $methods = $PaymentModel->getMethodsByClient($client); diff --git a/app/api/service/Notify.php b/app/api/service/Notify.php index 8a77e6fd..57553f14 100644 --- a/app/api/service/Notify.php +++ b/app/api/service/Notify.php @@ -164,6 +164,15 @@ class Notify ->setPaymentData($paymentData) ->handle(); } + // 订单支付成功业务处理 (开通会员、分销商) + if ($tradeInfo['order_type'] == OrderTypeEnum::MEMBER || $tradeInfo['order_type'] == OrderTypeEnum::DEALER) { + $service = new identity\PaySuccess(); + $service->setOrderNo($tradeInfo['order_no']) + ->setMethod($tradeInfo['pay_method']) + ->setTradeId($tradeInfo['trade_id']) + ->setPaymentData($paymentData) + ->handle(); + } Log::append('Notify-orderPaySucces', ['message' => '订单支付成功']); } catch (\Throwable $e) { // 记录错误日志 diff --git a/app/api/service/identity/PaySuccess.php b/app/api/service/identity/PaySuccess.php index 9f8d99b6..d5bcc53d 100644 --- a/app/api/service/identity/PaySuccess.php +++ b/app/api/service/identity/PaySuccess.php @@ -14,7 +14,6 @@ namespace app\api\service\identity; use app\api\model\PaymentTrade as PaymentTradeModel; use app\api\model\recharge\Order as OrderModel; -use app\api\model\Server\ServerOrder; use app\api\model\User as UserModel; use app\api\model\user\BalanceLog as BalanceLogModel; use app\api\model\user\IdentityOrder; @@ -22,9 +21,12 @@ use app\common\enum\order\PayStatus; use app\common\enum\payment\Method as PaymentMethodEnum; use app\common\enum\recharge\order\PayStatus as PayStatusEnum; use app\common\enum\user\balanceLog\Scene as SceneEnum; +use app\common\enum\user\IdentityEnum; +use app\common\enum\user\UserTypeEnum; use app\common\library\Lock; use app\common\library\Log; use app\common\service\BaseService; +use app\store\model\dealer\User; use cores\exception\BaseException; /** @@ -35,7 +37,7 @@ use cores\exception\BaseException; class PaySuccess extends BaseService { // 当前订单信息 - public ServerOrder $orderInfo; + public IdentityOrder $orderInfo; // 当前用户信息 private UserModel $userInfo; @@ -44,7 +46,7 @@ class PaySuccess extends BaseService private string $orderNo; // 当前订单ID - private string $orderId; + private int $orderId; // 订单支付方式 private string $method; @@ -59,7 +61,7 @@ class PaySuccess extends BaseService * 设置支付的订单ID * @param int $orderId 订单ID */ - public function setOrderId(int $orderId): \app\api\service\Server\PaySuccess + public function setOrderId(int $orderId): PaySuccess { $this->orderId = $orderId; return $this; @@ -182,9 +184,12 @@ class PaySuccess extends BaseService UserModel::setIncPayMoney($orderInfo['user_id'], (float)$orderInfo['pay_price']); // 记录订单支付信息 $this->updatePayInfo(); + //更改会员角色 + $this->activate(); }); } + /** * 记录订单支付的信息 * @throws BaseException @@ -333,4 +338,38 @@ class PaySuccess extends BaseService $orderInfo = $this->getOrderInfo(); Lock::unLock("OrderPaySuccess_{$orderInfo['order_id']}"); } + + private function activate(): void + { + $orderInfo = $this->orderInfo; + $userInfo = $this->userInfo; + //判断当前用户角色 + $orderType = $orderInfo['type']; + $userType = $userInfo['user_type']; + if ($userType == UserTypeEnum::STORE) { + return; + } + $userModel = new UserModel(); + $up = []; + $time = date('Y-m-d'); + if (!empty($userInfo['effective_time']) && strtotime($userInfo['effective_time']) > strtotime(date('Y-m-d'))) { + $time = $userInfo['effective_time']; + } + //已经是会员或者未开通会员 + if ($orderType == IdentityEnum::MEMBER && $userType != UserTypeEnum::DEALER) { + $up['user_type'] = UserTypeEnum::MEMBER; + } else { + //分销商 + $up['user_type'] = UserTypeEnum::DEALER; + if (!User::isDealerUser($userInfo['user_id'])) { + // 新增分销商用户 + User::add($userInfo['user_id'], [ + 'real_name' => $userInfo['mobile'], + 'mobile' => $userInfo['mobile'], + ]); + } + } + $up['effective_time'] = date("Y-m-d", strtotime("+{$orderInfo['month']} months", strtotime($time))); + $userModel->where(['user_id' => $userInfo['user_id']])->save($up); + } } \ No newline at end of file diff --git a/app/api/service/identity/Payment.php b/app/api/service/identity/Payment.php index 1173cb47..3bffaad7 100644 --- a/app/api/service/identity/Payment.php +++ b/app/api/service/identity/Payment.php @@ -146,6 +146,9 @@ class Payment extends BaseService if ($info->isEmpty()) { throwError('记录不存在'); } + if (UserService::isDealerMember() && $info['type'] == IdentityEnum::DEALER) { + throwError('非法操作'); + } if (!$model->createOrder($info->toArray())) { throwError($model->getError() ?: '创建订单失败'); }