298 lines
11 KiB
PHP
298 lines
11 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||
// +----------------------------------------------------------------------
|
||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||
// | 开源版本可自由商用,可去除界面版权logo
|
||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||
// | 访问官网:https://www.likeadmin.cn
|
||
// | likeadmin团队 版权所有 拥有最终解释权
|
||
// +----------------------------------------------------------------------
|
||
// | author: likeadminTeam
|
||
// +----------------------------------------------------------------------
|
||
|
||
namespace app\api\validate;
|
||
|
||
use app\common\enum\PayEnum;
|
||
use app\common\service\{ConfigService,UtilsService};
|
||
use app\common\validate\BaseValidate;
|
||
use app\common\model\setting\RechargeMethod;
|
||
use app\common\model\withdraw\WithdrawWallet;
|
||
use app\common\model\withdraw\WithdrawMethod;
|
||
use app\common\model\withdraw\WithdrawBank;
|
||
use app\common\model\finance\{WithdrawRecord};
|
||
use app\common\model\setting\Language;
|
||
use app\common\model\user\User;
|
||
use app\common\model\member\UserMember;
|
||
use app\common\model\lh\{LhRecord};
|
||
use app\common\cache\UserAccountSafeCache;
|
||
use think\facade\Config;
|
||
|
||
/**
|
||
* 资金验证器
|
||
* Class FinanceValidate
|
||
* @package app\api\validate
|
||
*/
|
||
class FinanceValidate extends BaseValidate
|
||
{
|
||
|
||
protected $rule = [
|
||
'account' => 'require|checkWalletAdd',
|
||
'money' => 'require|gt:0|checkWithdraw',
|
||
'transfer' => 'checkTransfer',
|
||
];
|
||
|
||
|
||
protected $message = [
|
||
'account.require' => 'network.parameterAbnormality',
|
||
'money.require' => 'network.parameterAbnormality',//请输入金额
|
||
'money.gt' => 'network.parameterAbnormality',//请输入正确的金额
|
||
];
|
||
|
||
|
||
public function sceneWalletAdd()
|
||
{
|
||
return $this->only(['account']);
|
||
}
|
||
|
||
public function sceneWithdraw()
|
||
{
|
||
return $this->only(['money']);
|
||
}
|
||
|
||
public function sceneTransfer()
|
||
{
|
||
return $this->only(['transfer']);
|
||
}
|
||
|
||
/**
|
||
* @notes 校验绑定钱包
|
||
* @param $money
|
||
* @param $rule
|
||
* @param $data
|
||
* @return bool|string
|
||
* @author 段誉
|
||
* @date 2023/2/24 10:42
|
||
*/
|
||
protected function checkWalletAdd($money, $rule, $data)
|
||
{
|
||
|
||
$bindTips = '';//已绑定提示语
|
||
|
||
//查询语言
|
||
$language = Language::where(['symbol' => $data['lang']])->findOrEmpty();
|
||
if ($language->isEmpty()) {
|
||
throw new \Exception('network.parameterAbnormality');//参数异常
|
||
}
|
||
|
||
//判断提现方式
|
||
$method = WithdrawMethod::where(['id' => $data['method_id']])->findOrEmpty();
|
||
if ($method->isEmpty()) {
|
||
return 'network.parameterAbnormality';//参数异常
|
||
}
|
||
if ($method['type'] != $data['type']) {
|
||
return 'network.parameterAbnormality';//参数异常
|
||
}
|
||
|
||
//判断钱包是否绑定
|
||
$userWallet = WithdrawWallet::where(['method_id' => $data['method_id'] ,'user_id' => $data['user_id'] ])->findOrEmpty();
|
||
if (!$userWallet->isEmpty()) {
|
||
return 'network.parameterAbnormality';//该钱包已绑定,请勿重复绑定
|
||
}
|
||
|
||
//类型1USDT2扫码3银行卡
|
||
switch ($data['type']){
|
||
case 1:
|
||
//USDT方式:account、img
|
||
if (!isset($data['account']) || strlen($data['account']) < 8 || strlen($data['account']) > 128) {
|
||
return 'network.parameterAbnormality';
|
||
}
|
||
if($method['is_qrcode']){
|
||
if (!isset($data['img']) || strlen($data['account']) < 8 || strlen($data['account']) > 128
|
||
|| strlen($data['img']) < 8 || strlen($data['img']) > 128) {
|
||
return 'network.parameterAbnormality';
|
||
}
|
||
}
|
||
$bindTips = 'withdraw.addressExist';//该地址已使用
|
||
break;
|
||
case 2:
|
||
//扫码方式:account、img
|
||
if (!isset($data['account']) || !isset($data['img'])) {
|
||
return 'network.parameterAbnormality';
|
||
}
|
||
if (strlen($data['account']) < 6 || strlen($data['account']) > 30
|
||
|| strlen($data['img']) < 6 || strlen($data['img']) > 128) {
|
||
return 'network.parameterAbnormality';
|
||
}
|
||
|
||
$bindTips = 'withdraw.qrcodeAccountExist';//该账号已使用
|
||
break;
|
||
case 3:
|
||
//银行卡方式:name、account、bank_id
|
||
if (!isset($data['account']) || !isset($data['name']) || !isset($data['bank_id'])) {
|
||
return 'network.parameterAbnormality';
|
||
}
|
||
if (strlen($data['account']) < 6 || strlen($data['account']) > 30
|
||
|| strlen($data['name']) < 2 || strlen($data['name']) > 30) {
|
||
return 'network.parameterAbnormality';
|
||
}
|
||
|
||
$bank = WithdrawBank::where(['id' => $data['bank_id']])->findOrEmpty();
|
||
if ($bank->isEmpty()) {
|
||
return 'network.parameterAbnormality';//提现银行不存在
|
||
}
|
||
|
||
$bindTips = 'withdraw.bankAccountExist';//该卡号已使用
|
||
break;
|
||
default:
|
||
$bindTips = 'network.parameterAbnormality';//不支持的类型
|
||
break;
|
||
}
|
||
|
||
//判断账号是否绑定
|
||
$wallet = WithdrawWallet::where(['account' => $data['account']])->findOrEmpty();
|
||
|
||
if (!$wallet->isEmpty()) {
|
||
return $bindTips;//该账号已使用
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* @notes 校验提现
|
||
* @param $money
|
||
* @param $rule
|
||
* @param $data
|
||
* @return bool|string
|
||
* @author 段誉
|
||
* @date 2023/2/24 10:42
|
||
*/
|
||
protected function checkWithdraw($money, $rule, $data)
|
||
{
|
||
//账号安全机制,连续输错后锁定,防止账号密码暴力破解
|
||
$userAccountSafeCache = new UserAccountSafeCache();
|
||
if (!$userAccountSafeCache->isSafe()) {
|
||
return 'network.frequentOperation';
|
||
//密码连续' . $userAccountSafeCache->count . '次输入错误,请' . $userAccountSafeCache->minute . '分钟后重试
|
||
}
|
||
|
||
//判断提现方式
|
||
$method = WithdrawMethod::where(['id' => $data['method_id']])->findOrEmpty();
|
||
if ($method->isEmpty()) {
|
||
return 'network.parameterAbnormality';//提现方式不存在
|
||
}
|
||
|
||
//判断提现金额
|
||
$config = ConfigService::get('website', 'trade');
|
||
$withdraw_min = $config['withdraw_min'];
|
||
$withdraw_max = $config['withdraw_max'];
|
||
|
||
if($data['money'] < $withdraw_min || $data['money'] > $withdraw_max ) return 'network.parameterAbnormality';//请输入正确的金额
|
||
|
||
//判断余额
|
||
$user = User::where(['id' => $data['user_id']])->findOrEmpty();
|
||
|
||
$used_money = UtilsService::get_used_money($data['user_id']);
|
||
if($data['money'] - $used_money > 0) return 'network.parameterAbnormality';//余额不足
|
||
|
||
//判断提现次数(每天可提现n次)
|
||
$todayStart = strtotime("today midnight");
|
||
$withdraw_num = WithdrawRecord::where(['user_id' => $data['user_id']])
|
||
->where('status in (0,1)')
|
||
->where("create_time > $todayStart")
|
||
->count();
|
||
if($config['withdraw_num'] <= $withdraw_num){
|
||
return 'network.parameterAbnormality';//每日提现次数限制
|
||
}
|
||
|
||
//交易密码
|
||
if (empty($data['pay_pwd'])) {
|
||
$userAccountSafeCache->record();
|
||
return 'network.parameterAbnormality';//请输入密码
|
||
}
|
||
if (strlen($data['pay_pwd']) != 6) {
|
||
$userAccountSafeCache->record();
|
||
return 'network.parameterAbnormality';//请输入正确的密码
|
||
}
|
||
|
||
$passwordSalt = Config::get('project.unique_identification');
|
||
if ($user['password_pay'] !== create_password($data['pay_pwd'], $passwordSalt)) {
|
||
$userAccountSafeCache->record();
|
||
return 'common.payPwdError';//密码错误
|
||
}
|
||
|
||
|
||
$userAccountSafeCache->relieve();
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* @notes 校验转账
|
||
* @param $money
|
||
* @param $rule
|
||
* @param $data
|
||
* @return bool|string
|
||
* @author 段誉
|
||
* @date 2023/2/24 10:42
|
||
*/
|
||
protected function checkTransfer($transfer, $rule, $data)
|
||
{
|
||
//账号安全机制,连续输错后锁定,防止账号密码暴力破解
|
||
$userAccountSafeCache = new UserAccountSafeCache();
|
||
if (!$userAccountSafeCache->isSafe()) {
|
||
return 'network.frequentOperation';
|
||
//密码连续' . $userAccountSafeCache->count . '次输入错误,请' . $userAccountSafeCache->minute . '分钟后重试
|
||
}
|
||
|
||
|
||
//判断转账金额
|
||
$config = ConfigService::get('website', 'trade');
|
||
$transfer_min = $config['transfer_min'];
|
||
$transfer_max = $config['transfer_max'];
|
||
|
||
if($data['money'] < $transfer_min || $data['money'] > $transfer_max ) return 'network.parameterAbnormality';//请输入正确的金额
|
||
|
||
//判断余额
|
||
$user = User::where(['id' => $data['user_id']])->findOrEmpty();
|
||
|
||
$used_money = UtilsService::get_used_money($data['user_id']);
|
||
if($data['money'] - $used_money > 0) return 'network.parameterAbnormality';//余额不足
|
||
|
||
//判断是否开启转账
|
||
if($user['is_transfer'] != 1) return 'transfer.transferDisableTips';
|
||
|
||
//判断是否自己
|
||
if($data['account'] == $user['account']) return 'transfer.limitMyself';//禁止给自己转账
|
||
|
||
//判断用户是否存在
|
||
$transferUser = User::where(['account' => $data['account']])->findOrEmpty();
|
||
if ($transferUser->isEmpty()){
|
||
$userAccountSafeCache->record();
|
||
return 'transfer.accountNoExist';//用户不存在
|
||
}
|
||
|
||
//交易密码
|
||
if (empty($data['pay_pwd'])) {
|
||
$userAccountSafeCache->record();
|
||
return 'network.parameterAbnormality';//请输入密码
|
||
}
|
||
if (strlen($data['pay_pwd']) != 6) {
|
||
$userAccountSafeCache->record();
|
||
return 'network.parameterAbnormality';//请输入正确的密码
|
||
}
|
||
|
||
$passwordSalt = Config::get('project.unique_identification');
|
||
if ($user['password_pay'] !== create_password($data['pay_pwd'], $passwordSalt)) {
|
||
$userAccountSafeCache->record();
|
||
return 'common.payPwdError';//密码错误
|
||
}
|
||
|
||
$userAccountSafeCache->relieve();
|
||
|
||
return true;
|
||
}
|
||
|
||
} |