User.php 18 KB

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