User.php 17 KB

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