Ledger.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\logic\WelfareLoginc;
  4. use app\common\controller\Api;
  5. use app\common\model\LedgerFrozenChangeModel;
  6. use app\common\model\LedgerTeacEcolyChangeModel;
  7. use app\common\model\LedgerTeacChangeModel;
  8. use app\common\model\LedgerTeacAngelChangeModel;
  9. use app\common\model\UserModel;
  10. use app\common\model\LedgerTokenChangeModel;
  11. use app\common\model\LedgerWalletModel;
  12. use app\common\model\OfflineWithdrawRecordModel;
  13. use think\Log;
  14. use think\Env;
  15. use think\Db;
  16. use fast\Asset;
  17. use think\Loader ;
  18. use think\Exception;
  19. /**
  20. * 首页接口
  21. */
  22. class Ledger extends Api
  23. {
  24. protected array $noNeedLogin = ['withdrawCallback'];
  25. /**
  26. * 资产首页
  27. */
  28. public function assets()
  29. {
  30. $config = getAllConfig();
  31. $wallet = LedgerWalletModel::getWallet($this->auth->id);
  32. $res['assets'] = $wallet->token;
  33. $res['transfes_fee'] = $config['transfer_fee']; //转让手续费比例
  34. $res['transfes_txt'] = $config['transfes_txt']; //转让文字表述
  35. $res['chabao_rate'] = $config['chabao_rate']; //茶宝汇率
  36. $res['giveaway_txt'] = $config['giveaway_txt']; //赠送说明
  37. $res['withdrawal_next_fee'] = $config['withdrawal_next_fee']; //600以下提现收费
  38. $res['withdrawal_up_fee'] = $config['withdrawal_up_fee']; //600以上提现收费
  39. $res['coin_list'] = [
  40. [
  41. 'coin_name' => '茶宝',
  42. 'coin_key' => 'token',
  43. 'amount' => $wallet->token,
  44. 'frozen_amount'=> $wallet->buying
  45. ],[
  46. 'coin_name' => 'TeaC',
  47. 'coin_key' => 'teac',
  48. 'amount' => $wallet->teac,
  49. 'frozen_amount'=> 0
  50. ],[
  51. 'coin_name' => '茶宝(手续费账户)',
  52. 'coin_key' => 'frozen',
  53. 'amount' => $wallet->frozen,
  54. 'frozen_amount'=> 0 //冻结金额
  55. ],[
  56. 'coin_name' => 'TeaC·天使',
  57. 'coin_key' => 'teac_angel',
  58. 'amount' => $wallet->teac_angel,
  59. 'frozen_amount'=> 0
  60. ],[
  61. 'coin_name' => 'TeaC·生态发展',
  62. 'coin_key' => 'teac_ecology',
  63. 'amount' => $wallet->teac_ecology,
  64. 'frozen_amount'=> 0
  65. ]
  66. ];
  67. $this->success('', $res);
  68. }
  69. /**
  70. * 资产变动明细
  71. * @return void
  72. */
  73. public function coinList()
  74. {
  75. $type_id = $this->request->post('query.action'); // 账变类型
  76. $coin_type = $this->request->post('query.coin_type'); // 資金类型
  77. $where = ['user_id' => $this->auth->id];
  78. if ($type_id > 0) $where['action'] = $type_id;
  79. switch ($coin_type){
  80. case 'token':
  81. $paginator = Loader::model('LedgerTokenChangeModel');
  82. $res['data']= $paginator->alias('a')
  83. ->join('user u', 'a.from_id = u.id and a.action > 9 and a.action < 12', 'LEFT')
  84. ->field('a.*, u.address')
  85. ->where($where)
  86. ->order('a.id DESC')->paginate($this->pageSize);
  87. break;
  88. case 'frozen':
  89. $paginator = (new LedgerFrozenChangeModel());
  90. $res['data'] = $paginator->alias('a')
  91. ->join('user u', 'a.from_id = u.id and a.action > 2', 'LEFT')
  92. ->field('a.*, u.address')
  93. ->where($where)
  94. ->order('id DESC')->paginate($this->pageSize);
  95. break;
  96. case 'teac':
  97. $paginator = Loader::model('LedgerTeacChangeModel');
  98. $res['data'] = $paginator->where($where)->order('id DESC')->paginate($this->pageSize);
  99. break;
  100. case 'teac_angel':
  101. $paginator = (new LedgerTeacAngelChangeModel());
  102. $res['data'] = $paginator->where($where)->order('id DESC')->paginate($this->pageSize);
  103. break;
  104. case 'teac_ecology':
  105. $paginator = (new LedgerTeacEcolyChangeModel());
  106. $res['data'] = $paginator->where($where)->order('id DESC')->paginate($this->pageSize);
  107. break;
  108. default:
  109. $this->error(__('Invalid parameters'));
  110. break;
  111. }
  112. $res['statusList'] = $paginator::getStatusList();
  113. $this->success('',$res);
  114. }
  115. public function getCoinList($type_id,$coin_type)
  116. {
  117. $where = ['user_id' => $this->auth->id];
  118. if ($type_id > 0) $where['action'] = $type_id;
  119. switch ($coin_type){
  120. case 'token':
  121. $paginator = Loader::model('LedgerTokenChangeModel');
  122. $res['data']= $paginator->alias('a')
  123. ->join('user u', 'a.from_id = u.id and a.action > 9 and a.action < 12', 'LEFT')
  124. ->field('a.*, u.address')
  125. ->where($where)
  126. ->order('a.id DESC')->paginate($this->pageSize);
  127. break;
  128. case 'frozen':
  129. $paginator = (new LedgerFrozenChangeModel());
  130. $res['data'] = $paginator->alias('a')
  131. ->join('user u', 'a.from_id = u.id and a.action > 2', 'LEFT')
  132. ->field('a.*, u.address')
  133. ->where($where)
  134. ->order('id DESC')->paginate($this->pageSize);
  135. break;
  136. case 'teac':
  137. $paginator = Loader::model('LedgerTeacChangeModel');
  138. $res['data'] = $paginator->where($where)->order('id DESC')->paginate($this->pageSize);
  139. break;
  140. case 'teac_angel':
  141. $paginator = (new LedgerTeacAngelChangeModel());
  142. $res['data'] = $paginator->where($where)->order('id DESC')->paginate($this->pageSize);
  143. break;
  144. case 'teac_ecology':
  145. $paginator = (new LedgerTeacEcolyChangeModel());
  146. $res['data'] = $paginator->where($where)->order('id DESC')->paginate($this->pageSize);
  147. break;
  148. default:
  149. $this->error(__('Invalid parameters'));
  150. break;
  151. }
  152. $res['statusList'] = $paginator::getStatusList();
  153. $this->success('',$res);
  154. }
  155. /**
  156. * 标记茶宝资产转账
  157. * @return void
  158. */
  159. public function frozenTransfer(UserModel $userModel, LedgerWalletModel $ledgerWalletModel, LedgerFrozenChangeModel $ledgerFrozenChangeModel)
  160. {
  161. $amount = $this->request->post('amount'); // 茶宝
  162. $account= $this->request->post('account', ''); // 账号
  163. if(empty($amount) || empty($account)){
  164. $this->error(__('Parameter error'));
  165. }
  166. $fee = bcmul(getConfig('frozen_transfer'), $amount, 2);
  167. $real = bcsub($amount, $fee, 2) ; // 手续费
  168. // 启动事务
  169. Db::startTrans();
  170. try {
  171. $user = (substr($account, 0, 2) == '0x')? $userModel->getByAddress($account): $userModel->getByUid($account);
  172. if(empty($user)) throw new Exception(__("赠送用户不存在"));
  173. if($user['id'] == $this->auth->id) throw new Exception(__("赠送用户不能是自己"));
  174. $freeze = $ledgerWalletModel::getWalletFrozen($this->auth->id);
  175. //剩余冻结金额
  176. $available = bcsub($freeze, config('min_frozen'), 2);
  177. if(bccomp($amount, $available, 2) > 0) throw new Exception(__("转账后余额不能低于9.9"), 15001);
  178. // 更新USDT和账变
  179. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::FROZEN, -$amount, $ledgerFrozenChangeModel::Payment, $user['id']);
  180. $newFrozen = $ledgerWalletModel->changeWalletAccount($user['id'], Asset::FROZEN, $real, $ledgerFrozenChangeModel::Receive, $this->auth->id);
  181. //添加手续费
  182. $ledgerFrozenChangeModel::createChangeFees($user['id'], $this->auth->id, -$fee, $newFrozen);
  183. // 提交事务
  184. Db::commit();
  185. } catch (Exception $e) {
  186. // 回滚事务
  187. Db::rollback();
  188. $this->error($e->getMessage(), null, $e->getCode());
  189. }
  190. $this->success('ok');
  191. }
  192. /**
  193. * 赠送/转账明细
  194. * @return void
  195. */
  196. public function getGiftDesc()
  197. {
  198. $this->success('', [
  199. 'chabao' => ['value' => getConfig('chabao_giveaway'), 'text' => getConfig('chabao_giveaway_txt')],
  200. 'frozen' => ['value' => getConfig('frozen_transfer'), 'text' => getConfig('frozen_transfer_txt')],
  201. 'teac' => ['value' => config('teac_giveaway'), 'text' => config('teac_giveaway_txt')],
  202. 'teac_angel' => ['value' => getConfig('teac_angel'), 'text' => getConfig('teac_angel_txt')],
  203. 'teac_ecology' => ['value' => getConfig('teac_ecology'), 'text' => getConfig('teac_ecology_txt')]
  204. ]);
  205. }
  206. /**
  207. * 茶宝赠送 0x
  208. * @return void
  209. */
  210. public function chabaoGift(UserModel $userModel, LedgerWalletModel $ledgerWalletModel, LedgerTokenChangeModel $ledgerTokenChangeModel)
  211. {
  212. $amount = $this->request->post('amount'); // 茶宝
  213. $account= $this->request->post('account', ''); // 账号
  214. if(empty($amount) || empty($account)){
  215. $this->error(__('Parameter error'));
  216. }
  217. $fee = bcmul(getConfig('chabao_giveaway'), $amount, 2);
  218. $real = bcsub($amount, $fee, 2) ; // 手续费
  219. // 启动事务
  220. Db::startTrans();
  221. try {
  222. $user = (substr($account, 0, 2) == '0x')? $userModel->getByAddress($account): $userModel->getByUid($account);
  223. if(empty($user)) throw new Exception(__("赠送用户不存在"));
  224. if($user['id'] == $this->auth->id) throw new Exception(__("赠送用户不能是自己"));
  225. $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  226. if(bccomp($amount, $chabao, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  227. // 更新USDT和账变
  228. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$amount, $ledgerTokenChangeModel::GiftPay, $user['id']);
  229. $newChabao = $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TOKEN, $real, $ledgerTokenChangeModel::GiftReceipt, $this->auth->id);
  230. //添加手续费
  231. $ledgerTokenChangeModel::createChangeFees($user['id'], $this->auth->id, -$fee, $newChabao);
  232. // 提交事务
  233. Db::commit();
  234. } catch (Exception $e) {
  235. // 回滚事务
  236. Db::rollback();
  237. $this->error($e->getMessage(), null, $e->getCode());
  238. }
  239. $this->success('ok');
  240. }
  241. /**
  242. * Teac赠送 0x
  243. * @return void
  244. */
  245. public function teacGift(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  246. {
  247. $amount = $this->request->post('amount'); // 茶宝
  248. $account= $this->request->post('account', ''); // 账号
  249. if(empty($amount) || empty($account)){
  250. $this->error(__('Parameter error'));
  251. }
  252. $real = bcsub($amount, bcmul(config('teac_giveaway'), $amount, 2), 2) ; // 手续费
  253. // 启动事务
  254. Db::startTrans();
  255. try {
  256. $user = (substr($account, 0, 2) == '0x')? $userModel->getByAddress($account): $userModel->getByUid($account);
  257. if(empty($user)) throw new Exception(__("赠送用户不存在"));
  258. if($user['id'] == $this->auth->id) throw new Exception(__("赠送用户不能是自己"));
  259. $teac = $ledgerWalletModel::getWalletTeac($this->auth->id);
  260. if(bccomp($amount, $teac, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  261. // 更新USDT和账变
  262. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TEAC, -$amount, LedgerTeacChangeModel::GiftPay, $user['id']);
  263. $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TEAC, $real, LedgerTeacChangeModel::GiftReceipt, $this->auth->id);
  264. // 提交事务
  265. Db::commit();
  266. } catch (Exception $e) {
  267. // 回滚事务
  268. Db::rollback();
  269. $this->error($e->getMessage(), null, $e->getCode());
  270. }
  271. $this->success('ok');
  272. }
  273. /**
  274. * teac天使的转账
  275. * @return void
  276. */
  277. public function teacAngelTransfer(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  278. {
  279. $amount = $this->request->post('amount'); // 茶宝
  280. $account= $this->request->post('account', ''); // 账号
  281. if(empty($amount) || empty($account)){
  282. $this->error(__('Parameter error'));
  283. }
  284. $real = bcsub($amount, bcmul(getConfig('teac_angel'), $amount, 2), 2) ; // 手续费
  285. $data=[];
  286. // 启动事务
  287. Db::startTrans();
  288. try {
  289. $user = (substr($account, 0, 2) == '0x')? $userModel->getByAddress($account): $userModel->getByUid($account);
  290. if(empty($user)) throw new Exception(__("赠送用户不存在"));
  291. if($user['id'] == $this->auth->id) throw new Exception(__("赠送用户不能是自己"));
  292. $teac = $ledgerWalletModel::getTeacAngel($this->auth->id);
  293. if(bccomp($amount, $teac, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  294. // 更新USDT和账变
  295. $re1=$ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TEAC_ANGEL, -$amount, LedgerTeacAngelChangeModel::GiftPay, $user['id']);
  296. $re2=$ledgerWalletModel->changeWalletAccount($user['id'], Asset::TEAC_ANGEL, $real, LedgerTeacAngelChangeModel::GiftReceipt, $this->auth->id);
  297. $data[]=$re1;
  298. $data[]=$re2;
  299. // 提交事务
  300. Db::commit();
  301. } catch (Exception $e) {
  302. // 回滚事务
  303. Db::rollback();
  304. $this->error($e->getMessage(), null, $e->getCode());
  305. }
  306. $this->success('ok');
  307. }
  308. /**
  309. * teac生态的转账
  310. * @return void
  311. */
  312. public function teacEcologyTransfer(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  313. {
  314. $amount = $this->request->post('amount'); // 茶宝
  315. $account= $this->request->post('account', ''); // 账号
  316. if(empty($amount) || empty($account)){
  317. $this->error(__('Parameter error'));
  318. }
  319. $real = bcsub($amount, bcmul(getConfig('teac_ecology'), $amount, 2), 2) ; // 手续费
  320. // 启动事务
  321. Db::startTrans();
  322. try {
  323. $user = (substr($account, 0, 2) == '0x')? $userModel->getByAddress($account): $userModel->getByUid($account);
  324. if(empty($user)) throw new Exception(__("赠送用户不存在"));
  325. if($user['id'] == $this->auth->id) throw new Exception(__("赠送用户不能是自己"));
  326. $teac = $ledgerWalletModel::getTeacAngel($this->auth->id);
  327. if(bccomp($amount, $teac, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  328. // 更新USDT和账变
  329. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TEAC_ECOLY, -$amount, LedgerTeacEcolyChangeModel::GiftPay, $user['id']);
  330. $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TEAC_ECOLY, $real, LedgerTeacEcolyChangeModel::GiftReceipt, $this->auth->id);
  331. // 提交事务
  332. Db::commit();
  333. } catch (Exception $e) {
  334. // 回滚事务
  335. Db::rollback();
  336. $this->error($e->getMessage(), null, $e->getCode());
  337. }
  338. $this->success('ok');
  339. }
  340. /**
  341. * 提现自动打款回调
  342. * 接口回调信息格式:
  343. * @return void
  344. */
  345. public function withdrawCallback_my()
  346. {
  347. //post 获取过来的数据格式为:{"code":"1","data":{"orderNo":"1768","tx_hash":"xx4545"}}
  348. $parems = $this->request->post();
  349. Log::write('提现自动打款回调参数:','info');
  350. Log::info(json_encode($parems));
  351. if(empty($parems)){
  352. $this->error("回调参数为空");
  353. }
  354. if($parems['code'] != 1){
  355. $this->error("本次提现失败");
  356. }
  357. $rs_data = $parems['data'];
  358. $info = (new OfflineWithdrawRecordModel())
  359. ->where('id', $rs_data['orderNo'])
  360. ->find();
  361. if(empty($info)){
  362. $this->error("当前提现信息不存在");
  363. }
  364. if($info['status'] == 2){
  365. $this->success("更新成功");
  366. }
  367. if($info['status'] == 5){
  368. $is_update = (new OfflineWithdrawRecordModel())
  369. ->where('id', $info['id'])
  370. ->update([
  371. 'tx_hash' => $rs_data['tx_hash'],
  372. 'status' => 2,
  373. 'update_time' => time(),
  374. ]);
  375. if($is_update){
  376. $this->success("更新成功");
  377. }else{
  378. $this->error("更新失败");
  379. }
  380. }
  381. }
  382. // 获取充值地址
  383. public function getAddress()
  384. {
  385. return $this->success('', ['value'=> Env::get('rental.pay_address'), 'name'=>getConfig('recharge_txt')]);
  386. }
  387. }