Ledger.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\LedgerQubicChangeModel;
  5. use app\common\model\UserModel;
  6. use app\common\model\LedgerSmhChangeModel;
  7. use app\common\model\LedgerTeacChangeModel;
  8. use app\common\model\LedgerTokenChangeModel;
  9. use app\common\model\LedgerUsdtChangeModel;
  10. use app\common\model\LedgerWalletModel;
  11. use app\common\model\OfflineWithdrawRecordModel;
  12. use fast\Action;
  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['withdrawal_next_fee'] = $config['withdrawal_next_fee']; //600以下提现收费
  37. $res['withdrawal_up_fee'] = $config['withdrawal_up_fee']; //600以上提现收费
  38. $res['coin_list'] = [
  39. [
  40. 'coin_name' => '茶宝',
  41. 'coin_key' => 'token',
  42. 'amount' => $wallet->token,
  43. 'frozen_amount'=> 0 //冻结金额
  44. ],[
  45. 'coin_name' => 'Teac',
  46. 'coin_key' => 'teac',
  47. 'amount' => $wallet->teac,
  48. 'frozen_amount'=> 0 //冻结金额
  49. ],[
  50. 'coin_name' => '茶宝(标记账户)',
  51. 'coin_key' => 'frozen',
  52. 'amount' => $wallet->frozen,
  53. 'frozen_amount'=> 0 //冻结金额
  54. ]
  55. ];
  56. $this->success('', $res);
  57. }
  58. /**
  59. * 资产变动明细
  60. * @return void
  61. */
  62. public function coinList()
  63. {
  64. $type_id = $this->request->post('query.action'); // 账变类型
  65. $coin_type = $this->request->post('query.coin_type'); // 資金类型
  66. $where = ['user_id' => $this->auth->getTokenUserID()];
  67. if ($type_id > 0) $where['action'] = $type_id;
  68. switch ($coin_type){
  69. case 'usdt':
  70. $paginator = (new LedgerUsdtChangeModel());
  71. break;
  72. case 'teac':
  73. $paginator = Loader::model('LedgerTeacChangeModel');
  74. $res['data'] = $paginator->where($where)->order('id DESC')->paginate($this->pageSize);
  75. break;
  76. case 'token':
  77. $paginator = Loader::model('LedgerTokenChangeModel');
  78. $res['data'] = $paginator->alias('a')
  79. ->join('user u', 'a.from_id = u.id and a.action > 9 and a.action < 12', 'LEFT')
  80. ->field('a.*, u.address')
  81. ->where($where)
  82. ->order('a.id DESC')->paginate($this->pageSize);
  83. break;
  84. case 'smh':
  85. $paginator = (new LedgerSmhChangeModel());
  86. break;
  87. case 'qubic':
  88. $paginator = (new LedgerQubicChangeModel());
  89. break;
  90. default:
  91. $this->error(__('Invalid parameters'));
  92. break;
  93. }
  94. $res['statusList'] = $paginator::getStatusList();
  95. $this->success('',$res);
  96. }
  97. /**
  98. * Teac资产转账
  99. * @return void
  100. */
  101. public function freezeTransfer(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  102. {
  103. $amount = $this->request->post('amount'); // 茶宝
  104. $address = $this->request->post('address'); // 地址
  105. if(empty($amount) || empty($address)){
  106. $this->error(__('Parameter error'));
  107. }
  108. $real = bcsub($amount, bcmul(getConfig('frozen_transfer'), $amount, 2), 2) ; // 手续费
  109. // 启动事务
  110. Db::startTrans();
  111. try {
  112. $user = $userModel->getByAddress($address);
  113. if(empty($user)) throw new Exception(__("赠送用户不存在"));
  114. if($user['id'] == $this->auth->id) throw new Exception(__("赠送用户不能是自己"));
  115. $freeze = $ledgerWalletModel::getWalletFrozen($this->auth->id);
  116. //剩余冻结金额
  117. $available = bcsub($freeze, config('min_frozen'), 2);
  118. if(bccomp($amount, $available, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  119. // 更新USDT和账变
  120. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::FROZEN, -$amount, LedgerTokenChangeModel::FroTrans, $user['id']);
  121. $ledgerWalletModel->changeWalletAccount($user['id'], Asset::FROZEN, $real, LedgerTokenChangeModel::FroRecei, $this->auth->id);
  122. // 提交事务
  123. Db::commit();
  124. } catch (Exception $e) {
  125. // 回滚事务
  126. Db::rollback();
  127. $this->error($e->getMessage(), null, $e->getCode());
  128. }
  129. $this->success('ok');
  130. }
  131. /**
  132. * 赠送/转账明细
  133. * @return void
  134. */
  135. public function getGiftDesc()
  136. {
  137. $this->success('', ['chabao'=>['value' => getConfig('chabao_giveaway'), 'text' => getConfig('chabao_giveaway_txt')],
  138. 'frozen'=>['value' => getConfig('frozen_transfer'), 'text' => getConfig('frozen_transfer_txt')]]);
  139. }
  140. /**
  141. * 茶宝赠送
  142. * @return void
  143. */
  144. public function chabaoGift(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  145. {
  146. $amount = $this->request->post('amount'); // 茶宝
  147. $address = $this->request->post('address'); // 地址
  148. if(empty($amount) || empty($address)){
  149. $this->error(__('Parameter error'));
  150. }
  151. $real = bcsub($amount, bcmul(getConfig('chabao_giveaway'), $amount, 2), 2) ; // 手续费
  152. // 启动事务
  153. Db::startTrans();
  154. try {
  155. $user = $userModel->getByAddress($address);
  156. if(empty($user)) throw new Exception(__("赠送用户不存在"));
  157. if($user['id'] == $this->auth->id) throw new Exception(__("赠送用户不能是自己"));
  158. $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  159. if(bccomp($amount, $chabao, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  160. // 更新USDT和账变
  161. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$amount, LedgerTokenChangeModel::GiftPay, $user['id']);
  162. $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TOKEN, $real, LedgerTokenChangeModel::GiftReceipt, $this->auth->id);
  163. // 提交事务
  164. Db::commit();
  165. } catch (Exception $e) {
  166. // 回滚事务
  167. Db::rollback();
  168. $this->error($e->getMessage(), null, $e->getCode());
  169. }
  170. $this->success('ok');
  171. }
  172. /**
  173. * 提现自动打款回调
  174. * 接口回调信息格式:
  175. * @return void
  176. */
  177. public function withdrawCallback_my()
  178. {
  179. //post 获取过来的数据格式为:{"code":"1","data":{"orderNo":"1768","tx_hash":"xx4545"}}
  180. $parems = $this->request->post();
  181. Log::write('提现自动打款回调参数:','info');
  182. Log::info(json_encode($parems));
  183. if(empty($parems)){
  184. $this->error("回调参数为空");
  185. }
  186. if($parems['code'] != 1){
  187. $this->error("本次提现失败");
  188. }
  189. $rs_data = $parems['data'];
  190. $info = (new OfflineWithdrawRecordModel())
  191. ->where('id', $rs_data['orderNo'])
  192. ->find();
  193. if(empty($info)){
  194. $this->error("当前提现信息不存在");
  195. }
  196. if($info['status'] == 2){
  197. $this->success("更新成功");
  198. }
  199. if($info['status'] == 5){
  200. $is_update = (new OfflineWithdrawRecordModel())
  201. ->where('id', $info['id'])
  202. ->update([
  203. 'tx_hash' => $rs_data['tx_hash'],
  204. 'status' => 2,
  205. 'update_time' => time(),
  206. ]);
  207. if($is_update){
  208. $this->success("更新成功");
  209. }else{
  210. $this->error("更新失败");
  211. }
  212. }
  213. }
  214. // 获取充值地址
  215. public function getAddress()
  216. {
  217. return $this->success('', ['value'=> Env::get('rental.pay_address'), 'name'=>getConfig('recharge_txt')]);
  218. }
  219. }