User.php 16 KB

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