| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\model\ProductOrder;
- use app\common\model\LedgerSmhChangeModel;
- use app\common\model\LedgerTokenChangeModel;
- use app\common\model\LedgerWalletModel;
- use app\common\model\OfflineWithdrawRecordModel;
- use app\common\model\UserBalanceLog;
- use app\common\model\TeamLevelModel;
- use app\common\model\UserModel;
- use app\common\model\ParametersModel;
- use fast\Action;
- use fast\Asset;
- use fast\Random;
- use think\Config;
- use think\Db;
- use think\Exception;
- /**
- * 会员接口
- */
- class User extends Api
- {
- protected string $lan = '';
- public function _initialize()
- {
- parent::_initialize();
- $this->lan = $this->request->getLan();
-
- }
- /**
- * 获取用户信息
- * @return void
- */
- public function userInfo()
- {
- $user = $this->auth->getUser();
- $teamLevelName = '';
- $teamLevelInfo = (new TeamLevelModel())->get($user['team_level_id']);
- if (!empty($teamLevelInfo)) {
- $teamLevelName = $teamLevelInfo->toArray()['name'];
- }
- $resp = [
- 'id' => $user['id'],
- 'nickname' => $user['nickname'],
- 'address' => $user['address'],// 地址
- 'usdt' => '0', //USDT余额
- 'token' => '0', // 平台币余额
- 'name' => $user['name'], // 姓名
- 'phone' => $user['phone'], // 手机号
- 'rental_power' => '0', // 自己购买的算力
- 'team_power' => '0', // 团队总算里
- 'balance' => $user['balance'], // 余额
- 'team_level_id' => $user['team_level_id'], // 团队等级ID
- 'team_level_name' => $teamLevelName, // 团队等级名称
- 'parent_id' => $user['parent_id'], // 上级ID
- 'parent_address' => '', // 上级的地址
- 'invite_link' => Config::get('rental.invite_domain') . '/?inviteCode=' . $user['address'],
- ];
- $this->success('', $resp);
- }
- /**
- * 获取Nft列表
- * @return void
- */
- public function getNftList(ProductOrder $productOrder)
- {
-
- $list = $productOrder->alias('a')
- ->join("product_list b", "b.id = a.product_id", "left")
- ->join("products c", "c.id = b.type_id", "left")
- ->field('a.id as order_id,a.product_id,'.'b.'.$this->lan.'_name,b.images as img_url,a.price,a.status,a.type_id,c.'.$this->lan.'_title')
- ->where('a.user_id', $this->auth->id)
- ->order('a.id DESC')
- ->paginate($this->pageSize);
-
- $this->success('', $list);
- }
- /**
- * 余额记录信息
- * @return void
- */
- public function getUserBalanceLog(UserBalanceLog $userBalanceLog)
- {
- // 启动事务
- Db::startTrans();
- try {
- $list = $userBalanceLog::where('user_id', $this->auth->id)
- ->where('type_id', $userBalanceLog::Share)
- ->order('id desc')
- ->paginate($this->pageSize);
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error('提交失败:' . $e->getMessage());
- }
- $this->success('', $list);
- }
- /**
- * 我的茶友
- * @return void
- */
- public function getChaList(UserModel $userModel)
- {
-
- // 总推荐数
- $list['total'] = $userModel::where('parent_id', $this->auth->id)->count();
- // 直推列表
- $list['data'] = $userModel::where('parent_id', $this->auth->id)
- ->field("address,create_time,nickname, REPLACE(phone, SUBSTRING(phone, 4, 4), '****') as phone")
- ->order('id desc')
- ->paginate($this->pageSize);
- $this->success('', $list);
- }
- /**
- * 报单算力互转信息
- * @return void
- */
- public function declarationInfo()
- {
- $user = $this->auth->getUser();
- if (empty($user)) {
- $this->error('用户信息不存在');
- }
- $resp = [
- 'declaration_min_amount' => 10, // 最低互转数量
- 'tips' => "", //提现提示信息
- ];
- $wallet = (new LedgerWalletModel())->getWallet($user['id']);
- if (!empty($wallet)) {
- $resp['declaration'] = $wallet['declaration'];
- }
- $config = (new ParametersModel)
- ->where('name', '=', 'declarationMinAmount')
- ->find();
- if(empty($config)){
- $this->error('报单算力互转最低数量参数');
- }
- $resp['declaration_min_amount'] = $config['value'];
- $resp['tips'] = $config['tip'];
- $this->success('', $resp);
- }
- /**
- * 报单算力互转
- * @return void
- */
- public function declarationTransfer()
- {
- $amount = $this->request->post('amount'); // 金额
- $sign = $this->request->post('sign'); // 签名信
- $address = $this->request->post('address'); // 提现地址
- // $sign = 'test';
- if(empty($sign)){
- $this->error('参数错误');
- }
- $real = $amount; // 实际到账
- $min = (new ParametersModel)->getValue('declarationMinAmount') ?? '0';
- if ($amount <= 0) {
- $this->error('互转金额必须大于0');
- } else if ($amount < $min) {
- $this->error('互转金额不能小于' . $min);
- }
- $uid = $this->auth->getTokenUserID();
- // 用户信息
- $from_user = (new UserModel())->getById($uid);
- if (empty($from_user)) {
- $this->error('用户不存在');
- }
- $to_user = (new UserModel())
- ->where('address', $address)
- ->find();
- if (empty($to_user)) {
- $this->error('接收用户不存在');
- }
- $wallet = (new LedgerWalletModel())->getWallet($from_user['id']);
- if (empty($wallet)) {
- $this->error('用户不存在');
- }
- if($amount > $wallet['declaration']){
- $this->error('余额不足');
- }
- // 验签
- $signMsg = "DeclarationWithdraw"; // 与前端约定的固定值
- if (!checkSign($signMsg, $sign, $from_user['address'])) {
- $this->error('签名校验失败');
- }
- // 启动事务
- Db::startTrans();
- try {
- // 更新USDT和账变
- (new LedgerWalletModel())->changeWalletAccount($from_user['id'], Asset::DECLARATION, -$amount, Action::TransferOut);
- (new LedgerWalletModel())->changeWalletAccount($to_user['id'], Asset::DECLARATION, $amount, Action::TransferIn);
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error('提交失败:' . $e->getMessage());
- }
- $this->success('提现申请已提交');
- }
- /**
- * 获取smh出款信息
- * @return void
- */
- public function smhInfo()
- {
- $user = $this->auth->getUser();
- if (empty($user)) {
- $this->error('用户信息不存在');
- }
- $resp = [
- 'smh' => '0', // 平台币余额
- 'smh_min_amount' => 0, // 最低提现U数量
- 'tips' => "", //提现提示信息
- 'address' => "", // 出款地址,此地址有值时,前台不允许重新输入
- 'smh_price' => (new SmhWithdrawRecordModel())->getEtcPrice(), //smh价格
- 'smh_exchange_ratio' => (new ParametersModel())->getValue('smhExchangeRatio'), // smh兑换USDT手续费
- 'smh_fee_rate' => (new ParametersModel())->getValue('smhFeeRate'), // smh提现手续费
- ];
- $wallet = (new LedgerWalletModel())->getWallet($user['id']);
- if (!empty($wallet)) {
- $resp['smh'] = $wallet['smh'];
- }
- $config = (new ParametersModel)
- ->where('name', '=', 'smhMinAmount')
- ->find();
- if(empty($config)){
- $this->error('未配置SMH出款参数');
- }
- $resp['smh_min_amount'] = $config['value'];
- $resp['tips'] = $config['tip'];
- $wallet = (new OfflineWithdrawRecordModel())
- ->where('user_id', $user['id'])
- ->where('symbol', 'smh')
- ->where('status', OfflineWithdrawRecordModel::StatusSuccessHand)
- ->find();
- if (!empty($wallet)) {
- $resp['address'] = $wallet['to_address'];
- }
- $this->success('', $resp);
- }
- /**
- * 提交出款信息
- * @return void
- */
- public function smhSubmit()
- {
- $amount = $this->request->post('amount'); // 金额
- $sign = $this->request->post('sign'); // 签名信
- $address = $this->request->post('address'); // 提现地址
- // $sign = 'test';
- if(empty($sign)){
- $this->error('参数错误');
- }
- $real = $amount; // 实际到账
- $min = (new ParametersModel)->getValue('smhMinAmount') ?? '0';
- if ($amount <= 0) {
- $this->error('提现金额必须大于0');
- } else if ($amount < $min) {
- $this->error('提现金额不能小于' . $min);
- }
- $uid = $this->auth->getTokenUserID();
- // 用户信息
- $user = (new UserModel())->getById($uid);
- if (empty($user)) {
- $this->error('用户不存在');
- }
- $wallet = (new LedgerWalletModel())->getWallet($user['id']);
- if (empty($wallet)) {
- $this->error('用户不存在');
- }
- if($amount > $wallet['smh']){
- $this->error('SMH余额不足');
- }
- $rate = (new ParametersModel)->getValue('smhFeeRate') ?? '0';
- // 扣除手续费后
- if ($rate >= 0 && $rate < 1) { // 比例范围只在0-1之间
- $real = bcmul($amount, bcsub(1, $rate, 6), 6);
- }else{
- $this->error('手续费设置错误:' . $rate);
- }
- // 验签
- $signMsg = "EtcWithdraw"; // 与前端约定的固定值
- if (!checkSign($signMsg, $sign, $user['address'])) {
- $this->error('签名校验失败');
- }
- //有过出款记录,则不能修改出款钱包地址
- $wallet = (new OfflineWithdrawRecordModel())
- ->where('user_id', $user['id'])
- ->where('symbol', 'smh')
- ->where('status', OfflineWithdrawRecordModel::StatusSuccessHand)
- ->find();
- if (!empty($wallet)) {
- $address = $wallet['to_address'];
- }
- // 启动事务
- Db::startTrans();
- try {
- // 更新USDT和账变
- (new LedgerWalletModel())->changeWalletAccount($uid, Asset::SMH, -$amount, LedgerSmhChangeModel::WithdrawCash);
- // 创建提现记录
- $txHash = Random::uuid();
- (new OfflineWithdrawRecordModel())->createWithdraw($txHash, $uid, $amount, $real, $address, 'smh');
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error('提交失败:' . $e->getMessage());
- }
- $this->success('提现申请已提交');
- }
- /**
- * 提交出款信息
- * @return void
- */
- public function smhExchange()
- {
- $amount = $this->request->post('amount'); // 金额
- if($amount < 0.01){
- $this->error('兑换数量太少');
- }
- $uid = $this->auth->getTokenUserID();
- // 用户信息
- $user = (new UserModel())->getById($uid);
- if (empty($user)) {
- $this->error('用户不存在');
- }
- $wallet = (new LedgerWalletModel())->getWallet($user['id']);
- if (empty($wallet)) {
- $this->error('用户不存在');
- }
- if($amount > $wallet['smh']){
- $this->error('SMH余额不足');
- }
- $etc_ratio = (new ParametersModel())->getValue('smhExchangeRatio');//兑换手续费
- $etc_price = (new SmhWithdrawRecordModel())->getEtcPrice(); //ETC价格
- if($etc_ratio > 0.9){
- $this->error('兑换手续费异常');
- }
- $usdt_amount = $amount * $etc_price * (1 - $etc_ratio);
- // 启动事务
- Db::startTrans();
- try {
- // 更新ETC和账变
- (new LedgerWalletModel())->changeWalletAccount($uid, Asset::SMH, -$amount, LedgerSmhChangeModel::Exchange);
- // 更新USDT和账变
- (new LedgerWalletModel())->changeWalletAccount($uid, Asset::USDT, $usdt_amount, Action::EtcExchange);
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error('提交失败:' . $e->getMessage());
- }
- $this->success('提现申请已提交');
- }
- /**
- * 获取aleo出款信息
- * @return void
- */
- public function aleoInfo()
- {
- $user = $this->auth->getUser();
- if (empty($user)) {
- $this->error('用户信息不存在');
- }
- $resp = [
- 'aleo' => '0', // 平台币余额
- 'aleo_min_amount' => 0, // 最低提现U数量
- 'tips' => "", //提现提示信息
- 'address' => "", // 出款地址,此地址有值时,前台不允许重新输入
- //'smh_price' => (new SmhWithdrawRecordModel())->getEtcPrice(), //smh价格
- //'smh_exchange_ratio' => (new ParametersModel())->getValue('smhExchangeRatio'), // smh兑换USDT手续费
- 'aleo_fee_rate' => (new ParametersModel())->getValue('aleoFeeRate'), // smh提现手续费
- ];
- $wallet = (new LedgerWalletModel())->getWallet($user['id']);
- if (!empty($wallet)) {
- $resp['aleo'] = $wallet['token'];
- }
- $config = (new ParametersModel)
- ->where('name', '=', 'aleoMinAmount')
- ->find();
- if(empty($config)){
- $this->error('未配置Aleo出款参数');
- }
- $resp['aleo_min_amount'] = $config['value'];
- $resp['tips'] = $config['tip'];
- $wallet = (new OfflineWithdrawRecordModel())
- ->where('user_id', $user['id'])
- ->where('symbol', 'aleo')
- ->where('status', OfflineWithdrawRecordModel::StatusSuccessHand)
- ->find();
- if (!empty($wallet)) {
- $resp['address'] = $wallet['to_address'];
- }
- $this->success('', $resp);
- }
- /**
- * 提交出款信息
- * @return void
- */
- public function aleoSubmit()
- {
- $amount = $this->request->post('amount'); // 金额
- $sign = $this->request->post('sign'); // 签名信
- $address = $this->request->post('address'); // 提现地址
- // $sign = 'test';
- if(empty($sign)){
- $this->error('参数错误');
- }
- $real = $amount; // 实际到账
- $min = (new ParametersModel)->getValue('aleoMinAmount') ?? '0';
- if ($amount <= 0) {
- $this->error('提现金额必须大于0');
- } else if ($amount < $min) {
- $this->error('提现金额不能小于' . $min);
- }
- $uid = $this->auth->getTokenUserID();
- // 用户信息
- $user = (new UserModel())->getById($uid);
- if (empty($user)) {
- $this->error('用户不存在');
- }
- $wallet = (new LedgerWalletModel())->getWallet($user['id']);
- if (empty($wallet)) {
- $this->error('用户不存在');
- }
- if($amount > $wallet['token']){
- $this->error('Aleo余额不足');
- }
- $rate = (new ParametersModel)->getValue('aleoFeeRate') ?? '0';
- // 扣除手续费后
- if ($rate >= 0 && $rate < 1) { // 比例范围只在0-1之间
- $real = bcmul($amount, bcsub(1, $rate, 6), 6);
- }else{
- $this->error('手续费设置错误:' . $rate);
- }
- // 验签
- $signMsg = "EtcWithdraw"; // 与前端约定的固定值
- if (!checkSign($signMsg, $sign, $user['address'])) {
- $this->error('签名校验失败');
- }
- //有过出款记录,则不能修改出款钱包地址
- $wallet = (new OfflineWithdrawRecordModel())
- ->where('user_id', $user['id'])
- ->where('symbol', 'aleo')
- ->where('status', OfflineWithdrawRecordModel::StatusSuccessHand)
- ->find();
- if (!empty($wallet)) {
- $address = $wallet['to_address'];
- }
- // 启动事务
- Db::startTrans();
- try {
- // 更新USDT和账变
- (new LedgerWalletModel())->changeWalletAccount($uid, Asset::TOKEN, -$amount, LedgerTokenChangeModel::WithdrawCash);
- // 创建提现记录
- $txHash = Random::uuid();
- (new OfflineWithdrawRecordModel())->createWithdraw($txHash, $uid, $amount, $real, $address, 'aleo');
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error('提交失败:' . $e->getMessage());
- }
- $this->success('提现申请已提交');
- }
- }
|