Ledger.php 8.1 KB

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