Ledger.php 8.0 KB

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