User.php 15 KB

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