Ledger.php 8.8 KB

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