User.php 17 KB

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