Ledger.php 7.9 KB

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