Ledger.php 8.6 KB

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