User.php 16 KB

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