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

393 lines
13 KiB
PHP

<?php
namespace app\api\logic;
use app\common\enum\YesNoEnum;
use app\common\logic\BaseLogic;
use app\common\model\finance\RechargeRecord;
use app\common\model\user\{User,UserUdun,UserTron,UserMember};
use app\common\model\setting\Language;
use app\common\model\setting\RechargeMethod;
use app\common\model\withdraw\WithdrawWallet;
use app\common\service\{ConfigService,UtilsService};
use think\facade\{Db};
/**
* 充值逻辑层
* Class RechargeLogic
* @package app\shopapi\logic
*/
class RechargeLogic extends BaseLogic
{
/**
* @notes 充值
* @param array $params
* @return array|false
* @author BD
* @date 2024/2/24 10:43
*/
public static function recharge(array $params)
{
try {
$method = RechargeMethod::where(['id' => $params['id']])->findOrEmpty();
$order_amount_act = round($params['money'] * $method['rate'] , $method['precision']);
$account = $method['account'];
//充值类型为 自定义地址
if($method['type'] == 7){
$address = $params['address'];
$addressArr = explode(PHP_EOL, $method['address']);
if (!in_array($address, $addressArr)) {
throw new \Exception('network.parameterAbnormality');//参数异常
}
//查询会员等级
$member_id = UtilsService::get_user_member_id($params['user_id']);
if ($member_id < $method['member_id']) {
throw new \Exception('network.parameterAbnormality');//参数异常
}
$account = $address;
}
$data = [
'sn' => generate_sn(RechargeRecord::class, 'sn'),
'user_id' => $params['user_id'],
'method_id' => $params['id'],
'amount' => $params['money'],
'amount_act' => $order_amount_act,
'account' => $account,
'voucher' => $params['voucher'],
'rate' => $method['rate'],
'symbol' => $method['symbol'],
];
$order = RechargeRecord::create($data);
return [
'order_id' => (int)$order['id'],
'from' => 'recharge'
];
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 充值配置
* @param $userId
* @return array
* @author BD
* @date 2024/2/24 16:56
*/
public static function config($userId)
{
$config = ConfigService::get('website', 'trade');
//查询需要绑定提现账户才可提现
$need_bing_wallet = 0;
if ($config['need_bing_wallet'] == 1) {
$count = WithdrawWallet::where(['user_id' => $userId])->count();
if($count == 0){
$need_bing_wallet = 1;
}
}
return [
'min' => $config['recharge_min'],
'need_bing_wallet' => $need_bing_wallet
];
}
/**
* @notes 充值方式
* @param $params
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author BD
* @date 2024/10/13 10:53
*/
public static function getAllMethod(array $params)
{
try {
//查询语言
$language = Language::where(['symbol' => $params['lang']])->findOrEmpty();
if ($language->isEmpty()) {
throw new \Exception('network.parameterAbnormality');//参数异常
}
//查询会员等级
$member_id = UtilsService::get_user_member_id($params['user_id']);
$field = ['id','type','name','logo'];
$methods = RechargeMethod::field($field)
->where(['is_show' => YesNoEnum::YES])
->whereIn('lang_id', [0,$language['id']])
->where(" member_id = 0 OR ( member_id !=0 AND member_id <= $member_id ) ")
->order(['sort' => 'desc', 'id' => 'desc'])
->select()
->toArray();
return $methods;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 充值方式详情
* @param $id
* @param $lang
* @return array
* @author BD
* @date 2024/9/20 17:09
*/
public static function methodDetail($id,$lang)
{
try {
//查询语言
$language = Language::where(['symbol' => $lang])->findOrEmpty();
if ($language->isEmpty()) {
throw new \Exception('network.parameterAbnormality');//参数异常
}
$field = ['id','type','name','logo','account','is_voucher','img as qrcode','bank_name','bank_username','symbol','rate','precision'];
$method = RechargeMethod::field($field)
->where(['is_show' => YesNoEnum::YES])
->where(['id' => $id])
->whereIn('lang_id', [0,$language['id']])
->findOrEmpty()
->toArray();
if (empty($method)) {
throw new \Exception('network.parameterAbnormality');//参数异常
}
return $method;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 充值方式详情-优盾
* @param array $params
* @return array
* @author BD
* @date 2024/9/20 17:09
*/
public static function methodDetailUdun(array $params)
{
try {
//查询语言
$language = Language::where(['symbol' => $params['lang']])->findOrEmpty();
if ($language->isEmpty()) {
throw new \Exception('network.parameterAbnormality');//参数异常
}
$method = RechargeMethod::where(['is_show' => YesNoEnum::YES])
->where(['id' => $params['id']])
->findOrEmpty()
->toArray();
if (empty($method)) {
throw new \Exception('network.parameterAbnormality');//参数异常
}
//查看是否创建地址
$userUdun = UserUdun::where(['user_id' => $params['user_id'],'method_id' => $method['id']])
->findOrEmpty()
->toArray();
$address = "";
$qrcode = "";
$data = [];
if (empty($userUdun)) {
//查看是否创建主钱包相同地址
$userUdun2 = UserUdun::where(['user_id' => $params['user_id'],'main_coin_type' => $method['main_coin_type']])
->findOrEmpty()
->toArray();
if (empty($userUdun2)) {
//没有创建则创建地址
$udunDispatch = UtilsService::get_udunDispatch();
$result = $udunDispatch->createAddress($method['main_coin_type']);
$address = $result['data']['address'];
$qrcode = UtilsService::get_qrcode($address);
}else{
$address = $userUdun2['address'];
$qrcode = $userUdun2['qrcode'];
}
//保存地址
$data = [
'user_id' => $params['user_id'],
'method_id' => $method['id'],
'address' => $address,
'qrcode' => $qrcode,
'main_coin_type' => $method['main_coin_type'],
'coin_type' => $method['coin_type'],
];
UserUdun::create($data);
}else{
$data['address'] = $userUdun['address'];
$data['qrcode'] = $userUdun['qrcode'];
}
return [
'address' => $data['address'],
'qrcode' => $data['qrcode'],
'protocol' => $method['protocol'],
'logo' => $method['logo'],
'name' => $method['name'],
];
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 充值方式详情-波场
* @param array $params
* @return array
* @author BD
* @date 2024/9/20 17:09
*/
public static function methodDetailTron(array $params)
{
try {
//查询语言
$language = Language::where(['symbol' => $params['lang']])->findOrEmpty();
if ($language->isEmpty()) {
throw new \Exception('network.parameterAbnormality');//参数异常
}
$method = RechargeMethod::where(['is_show' => YesNoEnum::YES])
->where(['id' => $params['id']])
->findOrEmpty()
->toArray();
if (empty($method)) {
throw new \Exception('network.parameterAbnormality');//参数异常
}
//查看是否创建地址
$userTron = UserTron::where(['user_id' => $params['user_id'],'method_id' => $method['id']])
->findOrEmpty()
->toArray();
$data = [
'user_id' => $params['user_id'],
'method_id' => $method['id'],
];
if (empty($userTron)) {
//创建地址
$tronData = [
'action' => 'reg'
];
$response = UtilsService::usdt_request($tronData, 'POST');
$response = json_decode($response, true);
if($response['code'] == 200){
$data['address'] = $response['data']['addr'];
$data['qrcode'] = UtilsService::get_qrcode($data['address']);
$data['key'] = $response['data']['key'];
$data['last_time'] = time();
UserTron::create($data);
}else{
throw new \Exception('network.parameterAbnormality');//参数异常
}
}else{
$data['address'] = $userTron['address'];
$data['qrcode'] = $userTron['qrcode'];
// 更新最后使用时间
UserTron::where(['id' => $userTron['id']])->update(['last_time' => time()]);
}
return [
'address' => $data['address'],
'qrcode' => $data['qrcode'],
'protocol' => $method['protocol'],
'logo' => $method['logo'],
'name' => $method['name'],
];
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 充值方式详情-自定义地址
* @param array $params
* @return array
* @author BD
* @date 2024/9/20 17:09
*/
public static function methodDetailAddress(array $params)
{
try {
//查询语言
$language = Language::where(['symbol' => $params['lang']])->findOrEmpty();
if ($language->isEmpty()) {
throw new \Exception('network.parameterAbnormality');//参数异常
}
$method = RechargeMethod::where(['is_show' => YesNoEnum::YES])
->where(['id' => $params['id']])
->findOrEmpty()
->toArray();
if (empty($method)) {
throw new \Exception('network.parameterAbnormality');//参数异常
}
//随机获取地址
$addressArr = explode(PHP_EOL, $method['address']);
return [
'avail_num' => $method['avail_num'],
'address' => $addressArr[array_rand($addressArr)],
'protocol' => $method['protocol'],
'logo' => $method['logo'],
'name' => $method['name'],
'symbol' => $method['symbol'],
'rate' => $method['rate'],
'precision' => $method['precision'],
'is_voucher' => $method['is_voucher'],
'date' => date('Y-m-d'),
];
} 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/10/13 10:53
*/
public static function getCommonMoney($lang)
{
$config = ConfigService::get('website', 'trade');
return explode("\n", $config['common_money']);
}
}