User.php 17 KB

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