User.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\ProductOrder;
  5. use app\common\model\LedgerSmhChangeModel;
  6. use app\common\model\LedgerTokenChangeModel;
  7. use app\common\model\LedgerWalletModel;
  8. use app\common\model\OfflineWithdrawRecordModel;
  9. use app\common\model\UserBalanceLog;
  10. use app\common\model\TeamLevelModel;
  11. use app\common\model\UserModel;
  12. use app\common\model\ParametersModel;
  13. use fast\Action;
  14. use fast\Asset;
  15. use fast\Random;
  16. use think\Config;
  17. use think\Db;
  18. use think\Exception;
  19. /**
  20. * 会员接口
  21. */
  22. class User extends Api
  23. {
  24. protected string $lan = '';
  25. public function _initialize()
  26. {
  27. parent::_initialize();
  28. $this->lan = $this->request->getLan();
  29. }
  30. /**
  31. * 获取用户信息
  32. * @return void
  33. */
  34. public function userInfo()
  35. {
  36. $user = $this->auth->getUser();
  37. $teamLevelName = '';
  38. $teamLevelInfo = (new TeamLevelModel())->get($user['team_level_id']);
  39. if (!empty($teamLevelInfo)) {
  40. $teamLevelName = $teamLevelInfo->toArray()['name'];
  41. }
  42. $resp = [
  43. 'id' => $user['id'],
  44. 'nickname' => $user['nickname'],
  45. 'address' => $user['address'],// 地址
  46. 'usdt' => '0', //USDT余额
  47. 'token' => '0', // 平台币余额
  48. 'name' => $user['name'], // 姓名
  49. 'phone' => $user['phone'], // 手机号
  50. 'rental_power' => '0', // 自己购买的算力
  51. 'team_power' => '0', // 团队总算里
  52. 'balance' => $user['balance'], // 余额
  53. 'team_level_id' => $user['team_level_id'], // 团队等级ID
  54. 'team_level_name' => $teamLevelName, // 团队等级名称
  55. 'parent_id' => $user['parent_id'], // 上级ID
  56. 'parent_address' => '', // 上级的地址
  57. 'invite_link' => Config::get('rental.invite_domain') . '/?inviteCode=' . $user['address'],
  58. ];
  59. $this->success('', $resp);
  60. }
  61. /**
  62. * 获取Nft列表
  63. * @return void
  64. */
  65. public function getNftList(ProductOrder $productOrder)
  66. {
  67. $list = $productOrder->alias('a')
  68. ->join("product_list b", "b.id = a.product_id", "left")
  69. ->join("products c", "c.id = b.type_id", "left")
  70. ->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')
  71. ->where('a.user_id', $this->auth->id)
  72. ->order('a.id DESC')
  73. ->paginate($this->pageSize);
  74. $this->success('', $list);
  75. }
  76. /**
  77. * 余额记录信息
  78. * @return void
  79. */
  80. public function getUserBalanceLog(UserBalanceLog $userBalanceLog)
  81. {
  82. // 启动事务
  83. Db::startTrans();
  84. try {
  85. $list = $userBalanceLog::where('user_id', $this->auth->id)
  86. ->where('type_id', $userBalanceLog::Share)
  87. ->order('id desc')
  88. ->paginate($this->pageSize);
  89. // 提交事务
  90. Db::commit();
  91. } catch (Exception $e) {
  92. // 回滚事务
  93. Db::rollback();
  94. $this->error('提交失败:' . $e->getMessage());
  95. }
  96. $this->success('', $list);
  97. }
  98. /**
  99. * 我的茶友
  100. * @return void
  101. */
  102. public function getChaList(UserModel $userModel)
  103. {
  104. // 总推荐数
  105. $list['total'] = $userModel::where('parent_id', $this->auth->id)->count();
  106. // 直推列表
  107. $list['data'] = $userModel::where('parent_id', $this->auth->id)
  108. ->field("address,create_time,nickname, REPLACE(phone, SUBSTRING(phone, 4, 4), '****') as phone")
  109. ->order('id desc')
  110. ->paginate($this->pageSize);
  111. $this->success('', $list);
  112. }
  113. /**
  114. * 报单算力互转信息
  115. * @return void
  116. */
  117. public function declarationInfo()
  118. {
  119. $user = $this->auth->getUser();
  120. if (empty($user)) {
  121. $this->error('用户信息不存在');
  122. }
  123. $resp = [
  124. 'declaration_min_amount' => 10, // 最低互转数量
  125. 'tips' => "", //提现提示信息
  126. ];
  127. $wallet = (new LedgerWalletModel())->getWallet($user['id']);
  128. if (!empty($wallet)) {
  129. $resp['declaration'] = $wallet['declaration'];
  130. }
  131. $config = (new ParametersModel)
  132. ->where('name', '=', 'declarationMinAmount')
  133. ->find();
  134. if(empty($config)){
  135. $this->error('报单算力互转最低数量参数');
  136. }
  137. $resp['declaration_min_amount'] = $config['value'];
  138. $resp['tips'] = $config['tip'];
  139. $this->success('', $resp);
  140. }
  141. /**
  142. * 报单算力互转
  143. * @return void
  144. */
  145. public function declarationTransfer()
  146. {
  147. $amount = $this->request->post('amount'); // 金额
  148. $sign = $this->request->post('sign'); // 签名信
  149. $address = $this->request->post('address'); // 提现地址
  150. // $sign = 'test';
  151. if(empty($sign)){
  152. $this->error('参数错误');
  153. }
  154. $real = $amount; // 实际到账
  155. $min = (new ParametersModel)->getValue('declarationMinAmount') ?? '0';
  156. if ($amount <= 0) {
  157. $this->error('互转金额必须大于0');
  158. } else if ($amount < $min) {
  159. $this->error('互转金额不能小于' . $min);
  160. }
  161. $uid = $this->auth->getTokenUserID();
  162. // 用户信息
  163. $from_user = (new UserModel())->getById($uid);
  164. if (empty($from_user)) {
  165. $this->error('用户不存在');
  166. }
  167. $to_user = (new UserModel())
  168. ->where('address', $address)
  169. ->find();
  170. if (empty($to_user)) {
  171. $this->error('接收用户不存在');
  172. }
  173. $wallet = (new LedgerWalletModel())->getWallet($from_user['id']);
  174. if (empty($wallet)) {
  175. $this->error('用户不存在');
  176. }
  177. if($amount > $wallet['declaration']){
  178. $this->error('余额不足');
  179. }
  180. // 验签
  181. $signMsg = "DeclarationWithdraw"; // 与前端约定的固定值
  182. if (!checkSign($signMsg, $sign, $from_user['address'])) {
  183. $this->error('签名校验失败');
  184. }
  185. // 启动事务
  186. Db::startTrans();
  187. try {
  188. // 更新USDT和账变
  189. (new LedgerWalletModel())->changeWalletAccount($from_user['id'], Asset::DECLARATION, -$amount, Action::TransferOut);
  190. (new LedgerWalletModel())->changeWalletAccount($to_user['id'], Asset::DECLARATION, $amount, Action::TransferIn);
  191. // 提交事务
  192. Db::commit();
  193. } catch (Exception $e) {
  194. // 回滚事务
  195. Db::rollback();
  196. $this->error('提交失败:' . $e->getMessage());
  197. }
  198. $this->success('提现申请已提交');
  199. }
  200. /**
  201. * 获取smh出款信息
  202. * @return void
  203. */
  204. public function smhInfo()
  205. {
  206. $user = $this->auth->getUser();
  207. if (empty($user)) {
  208. $this->error('用户信息不存在');
  209. }
  210. $resp = [
  211. 'smh' => '0', // 平台币余额
  212. 'smh_min_amount' => 0, // 最低提现U数量
  213. 'tips' => "", //提现提示信息
  214. 'address' => "", // 出款地址,此地址有值时,前台不允许重新输入
  215. 'smh_price' => (new SmhWithdrawRecordModel())->getEtcPrice(), //smh价格
  216. 'smh_exchange_ratio' => (new ParametersModel())->getValue('smhExchangeRatio'), // smh兑换USDT手续费
  217. 'smh_fee_rate' => (new ParametersModel())->getValue('smhFeeRate'), // smh提现手续费
  218. ];
  219. $wallet = (new LedgerWalletModel())->getWallet($user['id']);
  220. if (!empty($wallet)) {
  221. $resp['smh'] = $wallet['smh'];
  222. }
  223. $config = (new ParametersModel)
  224. ->where('name', '=', 'smhMinAmount')
  225. ->find();
  226. if(empty($config)){
  227. $this->error('未配置SMH出款参数');
  228. }
  229. $resp['smh_min_amount'] = $config['value'];
  230. $resp['tips'] = $config['tip'];
  231. $wallet = (new OfflineWithdrawRecordModel())
  232. ->where('user_id', $user['id'])
  233. ->where('symbol', 'smh')
  234. ->where('status', OfflineWithdrawRecordModel::StatusSuccessHand)
  235. ->find();
  236. if (!empty($wallet)) {
  237. $resp['address'] = $wallet['to_address'];
  238. }
  239. $this->success('', $resp);
  240. }
  241. /**
  242. * 提交出款信息
  243. * @return void
  244. */
  245. public function smhSubmit()
  246. {
  247. $amount = $this->request->post('amount'); // 金额
  248. $sign = $this->request->post('sign'); // 签名信
  249. $address = $this->request->post('address'); // 提现地址
  250. // $sign = 'test';
  251. if(empty($sign)){
  252. $this->error('参数错误');
  253. }
  254. $real = $amount; // 实际到账
  255. $min = (new ParametersModel)->getValue('smhMinAmount') ?? '0';
  256. if ($amount <= 0) {
  257. $this->error('提现金额必须大于0');
  258. } else if ($amount < $min) {
  259. $this->error('提现金额不能小于' . $min);
  260. }
  261. $uid = $this->auth->getTokenUserID();
  262. // 用户信息
  263. $user = (new UserModel())->getById($uid);
  264. if (empty($user)) {
  265. $this->error('用户不存在');
  266. }
  267. $wallet = (new LedgerWalletModel())->getWallet($user['id']);
  268. if (empty($wallet)) {
  269. $this->error('用户不存在');
  270. }
  271. if($amount > $wallet['smh']){
  272. $this->error('SMH余额不足');
  273. }
  274. $rate = (new ParametersModel)->getValue('smhFeeRate') ?? '0';
  275. // 扣除手续费后
  276. if ($rate >= 0 && $rate < 1) { // 比例范围只在0-1之间
  277. $real = bcmul($amount, bcsub(1, $rate, 6), 6);
  278. }else{
  279. $this->error('手续费设置错误:' . $rate);
  280. }
  281. // 验签
  282. $signMsg = "EtcWithdraw"; // 与前端约定的固定值
  283. if (!checkSign($signMsg, $sign, $user['address'])) {
  284. $this->error('签名校验失败');
  285. }
  286. //有过出款记录,则不能修改出款钱包地址
  287. $wallet = (new OfflineWithdrawRecordModel())
  288. ->where('user_id', $user['id'])
  289. ->where('symbol', 'smh')
  290. ->where('status', OfflineWithdrawRecordModel::StatusSuccessHand)
  291. ->find();
  292. if (!empty($wallet)) {
  293. $address = $wallet['to_address'];
  294. }
  295. // 启动事务
  296. Db::startTrans();
  297. try {
  298. // 更新USDT和账变
  299. (new LedgerWalletModel())->changeWalletAccount($uid, Asset::SMH, -$amount, LedgerSmhChangeModel::WithdrawCash);
  300. // 创建提现记录
  301. $txHash = Random::uuid();
  302. (new OfflineWithdrawRecordModel())->createWithdraw($txHash, $uid, $amount, $real, $address, 'smh');
  303. // 提交事务
  304. Db::commit();
  305. } catch (Exception $e) {
  306. // 回滚事务
  307. Db::rollback();
  308. $this->error('提交失败:' . $e->getMessage());
  309. }
  310. $this->success('提现申请已提交');
  311. }
  312. /**
  313. * 提交出款信息
  314. * @return void
  315. */
  316. public function smhExchange()
  317. {
  318. $amount = $this->request->post('amount'); // 金额
  319. if($amount < 0.01){
  320. $this->error('兑换数量太少');
  321. }
  322. $uid = $this->auth->getTokenUserID();
  323. // 用户信息
  324. $user = (new UserModel())->getById($uid);
  325. if (empty($user)) {
  326. $this->error('用户不存在');
  327. }
  328. $wallet = (new LedgerWalletModel())->getWallet($user['id']);
  329. if (empty($wallet)) {
  330. $this->error('用户不存在');
  331. }
  332. if($amount > $wallet['smh']){
  333. $this->error('SMH余额不足');
  334. }
  335. $etc_ratio = (new ParametersModel())->getValue('smhExchangeRatio');//兑换手续费
  336. $etc_price = (new SmhWithdrawRecordModel())->getEtcPrice(); //ETC价格
  337. if($etc_ratio > 0.9){
  338. $this->error('兑换手续费异常');
  339. }
  340. $usdt_amount = $amount * $etc_price * (1 - $etc_ratio);
  341. // 启动事务
  342. Db::startTrans();
  343. try {
  344. // 更新ETC和账变
  345. (new LedgerWalletModel())->changeWalletAccount($uid, Asset::SMH, -$amount, LedgerSmhChangeModel::Exchange);
  346. // 更新USDT和账变
  347. (new LedgerWalletModel())->changeWalletAccount($uid, Asset::USDT, $usdt_amount, Action::EtcExchange);
  348. // 提交事务
  349. Db::commit();
  350. } catch (Exception $e) {
  351. // 回滚事务
  352. Db::rollback();
  353. $this->error('提交失败:' . $e->getMessage());
  354. }
  355. $this->success('提现申请已提交');
  356. }
  357. /**
  358. * 获取aleo出款信息
  359. * @return void
  360. */
  361. public function aleoInfo()
  362. {
  363. $user = $this->auth->getUser();
  364. if (empty($user)) {
  365. $this->error('用户信息不存在');
  366. }
  367. $resp = [
  368. 'aleo' => '0', // 平台币余额
  369. 'aleo_min_amount' => 0, // 最低提现U数量
  370. 'tips' => "", //提现提示信息
  371. 'address' => "", // 出款地址,此地址有值时,前台不允许重新输入
  372. //'smh_price' => (new SmhWithdrawRecordModel())->getEtcPrice(), //smh价格
  373. //'smh_exchange_ratio' => (new ParametersModel())->getValue('smhExchangeRatio'), // smh兑换USDT手续费
  374. 'aleo_fee_rate' => (new ParametersModel())->getValue('aleoFeeRate'), // smh提现手续费
  375. ];
  376. $wallet = (new LedgerWalletModel())->getWallet($user['id']);
  377. if (!empty($wallet)) {
  378. $resp['aleo'] = $wallet['token'];
  379. }
  380. $config = (new ParametersModel)
  381. ->where('name', '=', 'aleoMinAmount')
  382. ->find();
  383. if(empty($config)){
  384. $this->error('未配置Aleo出款参数');
  385. }
  386. $resp['aleo_min_amount'] = $config['value'];
  387. $resp['tips'] = $config['tip'];
  388. $wallet = (new OfflineWithdrawRecordModel())
  389. ->where('user_id', $user['id'])
  390. ->where('symbol', 'aleo')
  391. ->where('status', OfflineWithdrawRecordModel::StatusSuccessHand)
  392. ->find();
  393. if (!empty($wallet)) {
  394. $resp['address'] = $wallet['to_address'];
  395. }
  396. $this->success('', $resp);
  397. }
  398. /**
  399. * 提交出款信息
  400. * @return void
  401. */
  402. public function aleoSubmit()
  403. {
  404. $amount = $this->request->post('amount'); // 金额
  405. $sign = $this->request->post('sign'); // 签名信
  406. $address = $this->request->post('address'); // 提现地址
  407. // $sign = 'test';
  408. if(empty($sign)){
  409. $this->error('参数错误');
  410. }
  411. $real = $amount; // 实际到账
  412. $min = (new ParametersModel)->getValue('aleoMinAmount') ?? '0';
  413. if ($amount <= 0) {
  414. $this->error('提现金额必须大于0');
  415. } else if ($amount < $min) {
  416. $this->error('提现金额不能小于' . $min);
  417. }
  418. $uid = $this->auth->getTokenUserID();
  419. // 用户信息
  420. $user = (new UserModel())->getById($uid);
  421. if (empty($user)) {
  422. $this->error('用户不存在');
  423. }
  424. $wallet = (new LedgerWalletModel())->getWallet($user['id']);
  425. if (empty($wallet)) {
  426. $this->error('用户不存在');
  427. }
  428. if($amount > $wallet['token']){
  429. $this->error('Aleo余额不足');
  430. }
  431. $rate = (new ParametersModel)->getValue('aleoFeeRate') ?? '0';
  432. // 扣除手续费后
  433. if ($rate >= 0 && $rate < 1) { // 比例范围只在0-1之间
  434. $real = bcmul($amount, bcsub(1, $rate, 6), 6);
  435. }else{
  436. $this->error('手续费设置错误:' . $rate);
  437. }
  438. // 验签
  439. $signMsg = "EtcWithdraw"; // 与前端约定的固定值
  440. if (!checkSign($signMsg, $sign, $user['address'])) {
  441. $this->error('签名校验失败');
  442. }
  443. //有过出款记录,则不能修改出款钱包地址
  444. $wallet = (new OfflineWithdrawRecordModel())
  445. ->where('user_id', $user['id'])
  446. ->where('symbol', 'aleo')
  447. ->where('status', OfflineWithdrawRecordModel::StatusSuccessHand)
  448. ->find();
  449. if (!empty($wallet)) {
  450. $address = $wallet['to_address'];
  451. }
  452. // 启动事务
  453. Db::startTrans();
  454. try {
  455. // 更新USDT和账变
  456. (new LedgerWalletModel())->changeWalletAccount($uid, Asset::TOKEN, -$amount, LedgerTokenChangeModel::WithdrawCash);
  457. // 创建提现记录
  458. $txHash = Random::uuid();
  459. (new OfflineWithdrawRecordModel())->createWithdraw($txHash, $uid, $amount, $real, $address, 'aleo');
  460. // 提交事务
  461. Db::commit();
  462. } catch (Exception $e) {
  463. // 回滚事务
  464. Db::rollback();
  465. $this->error('提交失败:' . $e->getMessage());
  466. }
  467. $this->success('提现申请已提交');
  468. }
  469. }