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