User.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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列表 transfer_price
  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.thum 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', $this->auth->id)
  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. * 获取smh出款信息
  141. * @return void
  142. */
  143. public function smhInfo()
  144. {
  145. $user = $this->auth->getUser();
  146. if (empty($user)) {
  147. $this->error('用户信息不存在');
  148. }
  149. $resp = [
  150. 'smh' => '0', // 平台币余额
  151. 'smh_min_amount' => 0, // 最低提现U数量
  152. 'tips' => "", //提现提示信息
  153. 'address' => "", // 出款地址,此地址有值时,前台不允许重新输入
  154. 'smh_price' => (new SmhWithdrawRecordModel())->getEtcPrice(), //smh价格
  155. 'smh_exchange_ratio' => (new ParametersModel())->getValue('smhExchangeRatio'), // smh兑换USDT手续费
  156. 'smh_fee_rate' => (new ParametersModel())->getValue('smhFeeRate'), // smh提现手续费
  157. ];
  158. $wallet = (new LedgerWalletModel())->getWallet($user['id']);
  159. if (!empty($wallet)) {
  160. $resp['smh'] = $wallet['smh'];
  161. }
  162. $config = (new ParametersModel)
  163. ->where('name', '=', 'smhMinAmount')
  164. ->find();
  165. if(empty($config)){
  166. $this->error('未配置SMH出款参数');
  167. }
  168. $resp['smh_min_amount'] = $config['value'];
  169. $resp['tips'] = $config['tip'];
  170. $wallet = (new OfflineWithdrawRecordModel())
  171. ->where('user_id', $user['id'])
  172. ->where('symbol', 'smh')
  173. ->where('status', OfflineWithdrawRecordModel::StatusSuccess)
  174. ->find();
  175. if (!empty($wallet)) {
  176. $resp['address'] = $wallet['to_address'];
  177. }
  178. $this->success('', $resp);
  179. }
  180. /**
  181. * 提交出款信息
  182. * @return void
  183. */
  184. public function smhSubmit()
  185. {
  186. $amount = $this->request->post('amount'); // 金额
  187. $sign = $this->request->post('sign'); // 签名信
  188. $address = $this->request->post('address'); // 提现地址
  189. // $sign = 'test';
  190. if(empty($sign)){
  191. $this->error('参数错误');
  192. }
  193. $real = $amount; // 实际到账
  194. $min = (new ParametersModel)->getValue('smhMinAmount') ?? '0';
  195. if ($amount <= 0) {
  196. $this->error('提现金额必须大于0');
  197. } else if ($amount < $min) {
  198. $this->error('提现金额不能小于' . $min);
  199. }
  200. $uid = $this->auth->getTokenUserID();
  201. // 用户信息
  202. $user = (new UserModel())->getById($uid);
  203. if (empty($user)) {
  204. $this->error('用户不存在');
  205. }
  206. $wallet = (new LedgerWalletModel())->getWallet($user['id']);
  207. if (empty($wallet)) {
  208. $this->error('用户不存在');
  209. }
  210. if($amount > $wallet['smh']){
  211. $this->error('SMH余额不足');
  212. }
  213. $rate = (new ParametersModel)->getValue('smhFeeRate') ?? '0';
  214. // 扣除手续费后
  215. if ($rate >= 0 && $rate < 1) { // 比例范围只在0-1之间
  216. $real = bcmul($amount, bcsub(1, $rate, 6), 6);
  217. }else{
  218. $this->error('手续费设置错误:' . $rate);
  219. }
  220. // 验签
  221. $signMsg = "EtcWithdraw"; // 与前端约定的固定值
  222. if (!checkSign($signMsg, $sign, $user['address'])) {
  223. $this->error('签名校验失败');
  224. }
  225. //有过出款记录,则不能修改出款钱包地址
  226. $wallet = (new OfflineWithdrawRecordModel())
  227. ->where('user_id', $user['id'])
  228. ->where('symbol', 'smh')
  229. ->where('status', OfflineWithdrawRecordModel::StatusSuccess)
  230. ->find();
  231. if (!empty($wallet)) {
  232. $address = $wallet['to_address'];
  233. }
  234. // 启动事务
  235. Db::startTrans();
  236. try {
  237. // 更新USDT和账变
  238. (new LedgerWalletModel())->changeWalletAccount($uid, Asset::SMH, -$amount, LedgerSmhChangeModel::WithdrawCash);
  239. // 创建提现记录
  240. $txHash = Random::uuid();
  241. (new OfflineWithdrawRecordModel())->createWithdraw($txHash, $uid, $amount, $real, $address, 'smh');
  242. // 提交事务
  243. Db::commit();
  244. } catch (Exception $e) {
  245. // 回滚事务
  246. Db::rollback();
  247. $this->error('提交失败:' . $e->getMessage());
  248. }
  249. $this->success('提现申请已提交');
  250. }
  251. /**
  252. * 提交出款信息
  253. * @return void
  254. */
  255. public function smhExchange()
  256. {
  257. $amount = $this->request->post('amount'); // 金额
  258. if($amount < 0.01){
  259. $this->error('兑换数量太少');
  260. }
  261. $uid = $this->auth->getTokenUserID();
  262. // 用户信息
  263. $user = (new UserModel())->getById($uid);
  264. if (empty($user)) {
  265. $this->error('用户不存在');
  266. }
  267. $wallet = (new LedgerWalletModel())->getWallet($user['id']);
  268. if (empty($wallet)) {
  269. $this->error('用户不存在');
  270. }
  271. if($amount > $wallet['smh']){
  272. $this->error('SMH余额不足');
  273. }
  274. $etc_ratio = (new ParametersModel())->getValue('smhExchangeRatio');//兑换手续费
  275. $etc_price = (new SmhWithdrawRecordModel())->getEtcPrice(); //ETC价格
  276. if($etc_ratio > 0.9){
  277. $this->error('兑换手续费异常');
  278. }
  279. $usdt_amount = $amount * $etc_price * (1 - $etc_ratio);
  280. // 启动事务
  281. Db::startTrans();
  282. try {
  283. // 更新ETC和账变
  284. (new LedgerWalletModel())->changeWalletAccount($uid, Asset::SMH, -$amount, LedgerSmhChangeModel::Exchange);
  285. // 更新USDT和账变
  286. (new LedgerWalletModel())->changeWalletAccount($uid, Asset::USDT, $usdt_amount, Action::EtcExchange);
  287. // 提交事务
  288. Db::commit();
  289. } catch (Exception $e) {
  290. // 回滚事务
  291. Db::rollback();
  292. $this->error('提交失败:' . $e->getMessage());
  293. }
  294. $this->success('提现申请已提交');
  295. }
  296. /**
  297. * 获取aleo出款信息
  298. * @return void
  299. */
  300. public function aleoInfo()
  301. {
  302. $user = $this->auth->getUser();
  303. if (empty($user)) {
  304. $this->error('用户信息不存在');
  305. }
  306. $resp = [
  307. 'aleo' => '0', // 平台币余额
  308. 'aleo_min_amount' => 0, // 最低提现U数量
  309. 'tips' => "", //提现提示信息
  310. 'address' => "", // 出款地址,此地址有值时,前台不允许重新输入
  311. //'smh_price' => (new SmhWithdrawRecordModel())->getEtcPrice(), //smh价格
  312. //'smh_exchange_ratio' => (new ParametersModel())->getValue('smhExchangeRatio'), // smh兑换USDT手续费
  313. 'aleo_fee_rate' => (new ParametersModel())->getValue('aleoFeeRate'), // smh提现手续费
  314. ];
  315. $wallet = (new LedgerWalletModel())->getWallet($user['id']);
  316. if (!empty($wallet)) {
  317. $resp['aleo'] = $wallet['token'];
  318. }
  319. $config = (new ParametersModel)
  320. ->where('name', '=', 'aleoMinAmount')
  321. ->find();
  322. if(empty($config)){
  323. $this->error('未配置Aleo出款参数');
  324. }
  325. $resp['aleo_min_amount'] = $config['value'];
  326. $resp['tips'] = $config['tip'];
  327. $wallet = (new OfflineWithdrawRecordModel())
  328. ->where('user_id', $user['id'])
  329. ->where('symbol', 'aleo')
  330. ->where('status', OfflineWithdrawRecordModel::StatusSuccess)
  331. ->find();
  332. if (!empty($wallet)) {
  333. $resp['address'] = $wallet['to_address'];
  334. }
  335. $this->success('', $resp);
  336. }
  337. /**
  338. * 提交出款信息
  339. * @return void
  340. */
  341. public function aleoSubmit()
  342. {
  343. $amount = $this->request->post('amount'); // 金额
  344. $sign = $this->request->post('sign'); // 签名信
  345. $address = $this->request->post('address'); // 提现地址
  346. // $sign = 'test';
  347. if(empty($sign)){
  348. $this->error('参数错误');
  349. }
  350. $real = $amount; // 实际到账
  351. $min = (new ParametersModel)->getValue('aleoMinAmount') ?? '0';
  352. if ($amount <= 0) {
  353. $this->error('提现金额必须大于0');
  354. } else if ($amount < $min) {
  355. $this->error('提现金额不能小于' . $min);
  356. }
  357. $uid = $this->auth->getTokenUserID();
  358. // 用户信息
  359. $user = (new UserModel())->getById($uid);
  360. if (empty($user)) {
  361. $this->error('用户不存在');
  362. }
  363. $wallet = (new LedgerWalletModel())->getWallet($user['id']);
  364. if (empty($wallet)) {
  365. $this->error('用户不存在');
  366. }
  367. if($amount > $wallet['token']){
  368. $this->error('Aleo余额不足');
  369. }
  370. $rate = (new ParametersModel)->getValue('aleoFeeRate') ?? '0';
  371. // 扣除手续费后
  372. if ($rate >= 0 && $rate < 1) { // 比例范围只在0-1之间
  373. $real = bcmul($amount, bcsub(1, $rate, 6), 6);
  374. }else{
  375. $this->error('手续费设置错误:' . $rate);
  376. }
  377. // 验签
  378. $signMsg = "EtcWithdraw"; // 与前端约定的固定值
  379. if (!checkSign($signMsg, $sign, $user['address'])) {
  380. $this->error('签名校验失败');
  381. }
  382. //有过出款记录,则不能修改出款钱包地址
  383. $wallet = (new OfflineWithdrawRecordModel())
  384. ->where('user_id', $user['id'])
  385. ->where('symbol', 'aleo')
  386. ->where('status', OfflineWithdrawRecordModel::StatusSuccess)
  387. ->find();
  388. if (!empty($wallet)) {
  389. $address = $wallet['to_address'];
  390. }
  391. // 启动事务
  392. Db::startTrans();
  393. try {
  394. // 更新USDT和账变
  395. (new LedgerWalletModel())->changeWalletAccount($uid, Asset::TOKEN, -$amount, LedgerTokenChangeModel::WithdrawCash);
  396. // 创建提现记录
  397. $txHash = Random::uuid();
  398. (new OfflineWithdrawRecordModel())->createWithdraw($txHash, $uid, $amount, $real, $address, 'aleo');
  399. // 提交事务
  400. Db::commit();
  401. } catch (Exception $e) {
  402. // 回滚事务
  403. Db::rollback();
  404. $this->error('提交失败:' . $e->getMessage());
  405. }
  406. $this->success('提现申请已提交');
  407. }
  408. }