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