Files
zzp-server/app/api/logic/FinanceLogic.php
2026-01-19 14:19:22 +08:00

605 lines
21 KiB
PHP

<?php
namespace app\api\logic;
use app\common\logic\BaseLogic;
use app\common\model\setting\Language;
use app\common\model\withdraw\WithdrawMethod;
use app\common\model\withdraw\WithdrawWallet;
use app\common\model\withdraw\WithdrawBank;
use app\common\model\finance\{WithdrawRecord,RechargeRecord,UserFinance,UserTransferRecord};
use app\common\enum\YesNoEnum;
use app\common\service\{FileService,UtilsService,ConfigService};
use app\common\model\user\{User,UserInfo};
use think\facade\Config;
use think\facade\{Db};
/**
* 资金逻辑层
* Class FinanceLogic
* @package app\shopapi\logic
*/
class FinanceLogic extends BaseLogic
{
/**
* @notes 首页数据
* @param $params
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbExceptionlimit
* @throws \think\db\exception\ModelNotFoundException
* @author BD
* @date 2024/3/21 19:15
*/
public static function getIndexData(array $params)
{
$user = User::where(['id' => $params['user_id']])->findOrEmpty();
$field = 'id,sn,change_type as type,change_amount as amount,remark,action,create_time';
$lists = UserFinance::field($field)
->where([
'user_id' => $params['user_id'],
])
->order(['create_time' => 'desc','id' => 'desc'])
->limit(5)
->select()
->toArray();
//查询语言
$timeFormat = 'Y-m-d H:i:s';
$language = Language::where(['symbol' => $params['lang']])->findOrEmpty();
if (!$language->isEmpty()) {
$timeFormat = $language['time_format'];
}
foreach ($lists as &$item) {
$item['create_time'] = date($timeFormat, strtotime($item['create_time']));
}
$report['user_money'] = $user['user_money'];
$report['user_point'] = $user['user_point'];
// $report['used_money'] = UtilsService::get_used_money($params['user_id']);
// if($report['used_money'] < 0) $report['used_money'] = 0;
// $report['noused_money'] = UserFinance::where(['user_id' => $params['user_id'],'frozen' => 1])->sum('change_amount');
// $report['exp_money'] = 0;
return [
'fund_list' => $lists,
'report' => $report
];
}
/**
* @notes 资金明细详情
* @param $params
* @return array
* @author BD
* @date 2024/9/20 17:09
*/
public static function detail(array $params)
{
$field = 'id,sn,change_type as type,change_amount as amount,remark,action,create_time';
$record = UserFinance::field($field)
->where(['id' => $params['id'],'user_id' => $params['user_id']])
->findOrEmpty()
->toArray();
//查询语言
$timeFormat = 'Y-m-d H:i:s';
$language = Language::where(['symbol' => $params['lang']])->findOrEmpty();
if (!$language->isEmpty()) {
$timeFormat = $language['time_format'];
}
$record['create_time'] = date($timeFormat, strtotime($record['create_time']));
return $record;
}
/**
* @notes 提现方式
* @param $lang
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author BD
* @date 2024/02/22 10:54
*/
public static function withdrawMethod($lang)
{
try {
//查询语言
$language = Language::where(['symbol' => $lang])->findOrEmpty();
if ($language->isEmpty()) {
throw new \Exception('network.parameterAbnormality');//参数异常
}
$field = 'id,type,name as text,is_qrcode,symbol,rate,precision,charge';
$methods = WithdrawMethod::field($field)
->where(['is_show' => YesNoEnum::YES])
->whereIn('lang_id', [0,$language['id']])
->order(['sort' => 'desc', 'id' => 'desc'])
->select()
->toArray();
return $methods;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 提现钱包
* @param $lang
* @param $userId
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author BD
* @date 2024/02/22 10:54
*/
public static function withdrawWallet($lang, $userId)
{
try {
//查询语言
$language = Language::where(['symbol' => $lang])->findOrEmpty();
if ($language->isEmpty()) {
throw new \Exception('network.parameterAbnormality');//参数异常
}
$field = 'ww.id,ww.method_id,ww.account,ww.type';
$field .= ',wm.name as method_name,wm.charge as method_charge,wm.rate as method_rate,wm.symbol as method_symbol,wm.precision as method_precision';
$wallets = WithdrawWallet::alias('ww')
->join('withdraw_method wm', 'wm.id = ww.method_id')
->field($field)
->where(['ww.user_id' => $userId])
->whereIn('ww.lang_id', [0,$language['id']])
->order(['wm.sort' => 'desc'])
->select()
->toArray();
foreach ($wallets as &$wallet) {
if($wallet['type'] == 2){
$wallet['account'] = substr($wallet['account'],0,3).'******'.substr($wallet['account'],strlen($wallet['account'])-3,strlen($wallet['account']));
$wallet['text'] = $wallet['method_name'].'('.$wallet['account'].')';
}else{
$wallet['account'] = substr($wallet['account'],0,4).'******'.substr($wallet['account'],strlen($wallet['account'])-4,strlen($wallet['account']));
$wallet['text'] = $wallet['method_name'].'('.$wallet['account'].')';
}
}
return $wallets;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 提现银行
* @param $lang
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author BD
* @date 2024/02/22 10:54
*/
public static function withdrawBanks($lang)
{
try {
//查询语言
$language = Language::where(['symbol' => $lang])->findOrEmpty();
if ($language->isEmpty()) {
throw new \Exception('network.parameterAbnormality');//参数异常
}
$field = ['id','name'];
$methods = WithdrawBank::field($field)
->where(['is_show' => YesNoEnum::YES])
->where(['lang_id' => $language['id']])
->order(['sort' => 'desc', 'id' => 'desc'])
->select()
->toArray();
return $methods;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 绑定提现钱包
* @param array $params
* @return bool
* @author BD
* @date 2024/02/22 10:54
*/
public static function withdrawWalletAdd(array $params)
{
try {
//查询语言
$language = Language::where(['symbol' => $params['lang']])->findOrEmpty();
if ($language->isEmpty()) {
throw new \Exception('network.parameterAbnormality');//参数异常
}
$method = WithdrawMethod::where(['id' => $params['method_id']])->findOrEmpty();
$bank_name = '';
$bank_id = '';
if($method['type'] == 3){
$bank = WithdrawBank::where(['id' => $params['bank_id']])->findOrEmpty();
$bank_name = $bank['name'];
$bank_id = $bank['id'];
}
$wallet = WithdrawWallet::create([
'lang_id' => $method['lang_id'],
'user_id' => $params['user_id'],
'method_id' => $params['method_id'],
'bank_id' => $bank_id,
'type' => $method['type'],
'name' => isset($params['name'])?$params['name']:'',
'account' => $params['account'],
'img' => isset($params['img'])?$params['img']:'',
'bank_name' => $bank_name,
]);
return [
'from' => 'walletAdd'
];
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 提现配置
* @param $params
* @return array
* @author BD
* @date 2024/02/22 10:54
*/
public static function withdrawConfig(array $params)
{
$config = ConfigService::get('website', 'trade');
$user = User::where(['id' => $params['user_id']]) -> findOrEmpty();
$is_withdraw = true;
// //判断提现次数(每24小时可提现n次)
// $time24Hours = time() - 24 * 60 * 60;
//判断提现次数(每天可提现n次)
$time24Hours = strtotime("today midnight");
$withdraw_num = WithdrawRecord::where(['user_id' => $params['user_id']])
->where('status in (0,1)')
->where("create_time > $time24Hours")
->count();
if($config['withdraw_num'] <= $withdraw_num){
$is_withdraw = false;
}
//查询实名认证状态
$need_realname = 0;
$userInfo = UserInfo::where(['user_id' => $params['user_id']]) -> findOrEmpty();
if (!$userInfo->isEmpty() && $config['need_realname'] == 1) {
if($userInfo['auth_card'] != 1) $need_realname = 1;
}
//查询初始交易密码
$need_set_pwd = 0;
$pwd_pay = ConfigService::get('login', 'password_pay');
$passwordSalt = Config::get('project.unique_identification');
if ($user['password_pay'] == create_password($pwd_pay, $passwordSalt)) {
$need_set_pwd = 1;
}
$balance = UtilsService::get_used_money($params['user_id']);
if($balance < 0) $balance = 0;
$noused_money = $user['user_money'] - $balance;
//预输入上一次提现方式和提现地址
$ahead['method_id'] = 0;
$ahead['address'] = '';
$withdrawRecord = WithdrawRecord::where(['user_id' => $params['user_id']])
->order(['create_time' => 'desc', 'id' => 'desc'])
->findOrEmpty();
if(!$withdrawRecord->isEmpty()){
$ahead['method_id'] = $withdrawRecord['method_id'];
$ahead['address'] = $withdrawRecord['account'];
}
return [
'min' => $config['withdraw_min'],
'max' => $config['withdraw_max'],
'num' => $config['withdraw_num'],
'is_withdraw' => $is_withdraw,
'balance' => $balance,
'noused_money' => $noused_money,
'need_realname' => $need_realname,
'need_set_pwd' => $need_set_pwd,
'ahead' => $ahead,
];
}
/**
* @notes 提现
* @param array $params
* @return array|false
* @author BD
* @date 2024/02/22 10:54
*/
public static function withdraw(array $params)
{
Db::startTrans();
try {
$method = WithdrawMethod::where(['id' => $params['method_id']])->findOrEmpty();
$order_amount_act = round($params['money'] * $method['rate'] , $method['precision']);
//手续费
$charge = 0;
if($method['charge'] > 0){
$charge = round($params['money'] * $method['charge']/100,2);
if($charge <= 0.01) $charge = 0;
}
$data = [
'sn' => generate_sn(WithdrawRecord::class, 'sn'),
'user_id' => $params['user_id'],
'method_id' => $method['id'],
'amount' => $params['money'],
'amount_act' => $order_amount_act,
'rate' => $method['rate'],
'symbol' => $method['symbol'],
'charge' => $charge,//手续费
'type' => $method['type'],
'account' => $params['address'],
];
$order = WithdrawRecord::create($data);
//USDT发起 优盾代付
if($method['type'] == 1){
$udun = ConfigService::get('website', 'udun');
if($udun['is_open_df'] == 1 && $method['is_open_df'] == 1 && $order['amount'] - $udun['pay_min'] >= 0 && $order['amount'] - $udun['pay_max'] <= 0){
$udunDispatch = UtilsService::get_udunDispatch();
$main_coin_type = $method['main_coin_type'];
$coin_type = $method['coin_type'];
//验证地址合法性
$result1 = $udunDispatch->checkAddress($main_coin_type,$order['account']);
if($result1['code'] == 200){
//申请提币
//提币金额 = (金额 - 手续费) x 汇率
$amount_df = round(($order['amount']- $order['charge']) * $method['rate'] , $method['precision']);
$result2 = $udunDispatch->withdraw($order['sn'],$main_coin_type,$coin_type,$order['account'],$amount_df);
if($result2['code'] == 200){
WithdrawRecord::update([
'id' => $order['id'],
'status2' => 1
]);
}
}
}
}
//记录日志
UtilsService::user_finance_add(
$data['user_id'],
2,
2,
$data['amount'],
$data['sn']
);
//用户资金修改
UtilsService::user_money_change($data['user_id'], 2, $data['amount'],'user_money');
//提现金额增加
UtilsService::user_money_change($data['user_id'], 1, $data['amount'],'total_withdraw');
Db::commit();
return [
'order_id' => (int)$order['id'],
'from' => 'withdraw'
];
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 提现记录详情
* @param $params
* @return array
* @author BD
* @date 2024/02/22 10:54
*/
public static function withdrawRecordDetail($params)
{
$field = 'id,method_id,sn,amount,amount_act,rate,charge,symbol,account,status,type,create_time,remark';
$record = WithdrawRecord::field($field)
->append(['method_name'])
->where(['id' => $params['id'],'user_id' => $params['user_id']])
->findOrEmpty()
->toArray();
//查询语言
$timeFormat = 'Y-m-d H:i:s';
$language = Language::where(['symbol' => $params['lang']])->findOrEmpty();
if (!$language->isEmpty()) {
$timeFormat = $language['time_format'];
}
$record['create_time'] = date($timeFormat, strtotime($record['create_time']));
if($record['type'] == 2){
$record['account'] = substr($record['account'],0,3).'******'.substr($record['account'],strlen($record['account'])-3,strlen($record['account']));
$record['account'] = $record['method_name'].'('.$record['account'].')';
}else{
$record['account'] = substr($record['account'],0,4).'******'.substr($record['account'],strlen($record['account'])-4,strlen($record['account']));
$record['account'] = $record['method_name'].'('.$record['account'].')';
}
return $record;
}
/**
* @notes 充值记录详情
* @param $params
* @return array
* @author BD
* @date 2024/02/22 10:54
*/
public static function rechargeRecordDetail($params)
{
$field = 'id,method_id,sn,amount,amount_act,rate,symbol,status,create_time';
$record = RechargeRecord::field($field)
->append(['method_name'])
->where(['id' => $params['id'],'user_id' => $params['user_id']])
->findOrEmpty()
->toArray();
//查询语言
$timeFormat = 'Y-m-d H:i:s';
$language = Language::where(['symbol' => $params['lang']])->findOrEmpty();
if (!$language->isEmpty()) {
$timeFormat = $language['time_format'];
}
$record['create_time'] = date($timeFormat, strtotime($record['create_time']));
return $record;
}
/**
* @notes 用户转账数据
* @param $params
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbExceptionlimit
* @throws \think\db\exception\ModelNotFoundException
* @author BD
* @date 2024/3/21 19:15
*/
public static function transferIndex(array $params)
{
$user = User::where(['id' => $params['user_id']])->findOrEmpty();
$resUser['balance'] = UtilsService::get_used_money($params['user_id']);
if($resUser['balance'] < 0) $resUser['balance'] = 0;
//查询初始交易密码
$need_set_pwd = 0;
$pwd_pay = ConfigService::get('login', 'password_pay');
$passwordSalt = Config::get('project.unique_identification');
if ($user['password_pay'] == create_password($pwd_pay, $passwordSalt)) {
$need_set_pwd = 1;
}
$config = ConfigService::get('website', 'trade');
$config['min'] = $config['transfer_min'];
$config['max'] = $config['transfer_max'];
$config['need_set_pwd'] = $need_set_pwd;
return [
'config' => $config,
'user' => $resUser
];
}
/**
* @notes 转账
* @param array $params
* @return array|false
* @author BD
* @date 2024/02/22 10:54
*/
public static function transfer(array $params)
{
Db::startTrans();
try {
//转出用户
$userFrom = User::where(['id' => $params['user_id']])->findOrEmpty();
//转入用户
$userTo = User::where(['account' => $params['account']])->findOrEmpty();
//转出用户记录
$data = [
'sn' => generate_sn(UserTransferRecord::class, 'sn'),
'user_id' => $userFrom['id'],
'user_id_from' => $userFrom['id'],
'user_id_to' => $userTo['id'],
'amount' => $params['money'],
'type' => 2,
];
$orderFrom = UserTransferRecord::create($data);
//记录日志
UtilsService::user_finance_add(
$data['user_id'],
19,
2,
$data['amount'],
$data['sn']
);
//用户资金修改
UtilsService::user_money_change($data['user_id'], 2, $data['amount'],'user_money');
//转入用户记录
$data = [
'sn' => generate_sn(UserTransferRecord::class, 'sn'),
'user_id' => $userTo['id'],
'user_id_from' => $userFrom['id'],
'user_id_to' => $userTo['id'],
'amount' => $params['money'],
'type' => 1,
];
$orderTo = UserTransferRecord::create($data);
//记录日志
UtilsService::user_finance_add(
$data['user_id'],
20,
1,
$data['amount'],
$data['sn']
);
//用户资金修改
UtilsService::user_money_change($data['user_id'], 1, $data['amount'],'user_money');
Db::commit();
return [
'from' => 'transfer'
];
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
}