// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\api\controller; use app\api\model\recharge\Order as OrderModel; use app\api\model\Setting as SettingModel; use app\api\service\Payment as PaymentService; use app\api\service\User as UserService; use app\common\enum\OrderType as OrderTypeEnum; use app\common\library\helper; use cores\exception\BaseException; use app\api\model\User as UserModel; /** * 用户充值管理 * Class Recharge * @package app\api\controller */ class Recharge extends Controller { /** * 确认充值 * @param int|null $planId 方案ID * @param float|string|null $customMoney 自定义金额 * @return array|\think\response\Json * @throws BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function submit(int $planId = null, $customMoney = null) { if (getPlatform() !== 'MP-WEIXIN') { return $this->renderError('很抱歉,余额充值暂时仅支持微信小程序端'); } // 生成充值订单 $model = new OrderModel; if (!$model->createOrder($planId, (float)$customMoney)) { return $this->renderError($model->getError() ?: '充值失败'); } // 构建微信支付 $payment = PaymentService::wechat( $model['order_id'], $model['order_no'], $model['pay_price'], OrderTypeEnum::RECHARGE ); // 充值状态提醒 $message = ['success' => '充值成功', 'error' => '订单未支付']; return $this->renderSuccess(compact('payment', 'message')); } /** * 确认充值 * @param float|string|null $customMoney 自定义金额 * @param string|null $tel 手机号 * @return array|\think\response\Json * @throws BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function share($customMoney = null, $tel = null) { if ($customMoney === '' || $customMoney < 0) { return $this->renderError('请输入正确的分销金额'); } if (!preg_match('/^\d+$/', $customMoney)) { return $this->renderError('金额只能输入正整数'); } // 充值设置 $setting = SettingModel::getItem('recharge'); // 验证最低充值金额 if (helper::bccomp($customMoney, $setting['lowest_money']) === -1) { return $this->renderError("很抱歉,当前最低充值金额不能低于{$setting['lowest_money']}元"); } // 当前用户信息 $userInfo = UserService::getCurrentLoginUser(true); if($customMoney > $userInfo['balance']){ return $this->renderError('分销金额不能大于余额'); } // 分销对象 $obj = UserModel::detail([['mobile','=',$tel]]); if(!$obj){ return $this->renderError('系统中不存在此用户,请提醒用户注册'); } if($userInfo['user_id'] == $obj['user_id']){ return $this->renderError('不能自己分给自己'); } // 减少当前用的余额 $dec = $userInfo->rechargeToBalance($obj['mobile'],[ 'money'=>$customMoney, 'mode'=>'dec', 'remark'=>'转出余额', ]); if(!$dec){ return $this->renderError('转出失败'); } // 增加分销对象的余额 $inc = $obj->rechargeToBalance($userInfo['mobile'],[ 'money'=>$customMoney, 'mode'=>'inc', 'remark'=>'转入余额', ]); if(!$inc){ return $this->renderError('转入失败'); } return $this->renderSuccess('操作成功'); } }