Ledger.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\logic\WelfareLoginc;
  4. use app\common\controller\Api;
  5. use app\common\model\LedgerFrozenChangeModel;
  6. use app\common\model\ProductLists;
  7. use app\common\model\LedgerTeacChangeModel;
  8. use app\common\model\LedgerTeacAngelChangeModel;
  9. use app\common\model\UserModel;
  10. use app\common\model\LedgerTokenChangeModel;
  11. use app\common\model\LedgerWalletModel;
  12. use app\common\model\OfflineWithdrawRecordModel;
  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['giveaway_txt'] = $config['giveaway_txt']; //赠送说明
  37. $res['withdrawal_next_fee'] = $config['withdrawal_next_fee']; //600以下提现收费
  38. $res['withdrawal_up_fee'] = $config['withdrawal_up_fee']; //600以上提现收费
  39. $res['coin_list'] = [
  40. [
  41. 'coin_name' => '茶宝',
  42. 'coin_key' => 'token',
  43. 'amount' => $wallet->token,
  44. 'frozen_amount'=> $wallet->buying //冻结金额
  45. ],[
  46. 'coin_name' => 'TeaC',
  47. 'coin_key' => 'teac',
  48. 'amount' => $wallet->teac,
  49. 'frozen_amount'=> 0 //冻结金额
  50. ],[
  51. 'coin_name' => '茶宝(手续费账户)',
  52. 'coin_key' => 'frozen',
  53. 'amount' => $wallet->frozen,
  54. 'frozen_amount'=> 0 //冻结金额
  55. ],[
  56. 'coin_name' => 'TeaC·天使',
  57. 'coin_key' => 'teac_angel',
  58. 'amount' => $wallet->teac_angel,
  59. 'frozen_amount'=> 0 //冻结金额
  60. ]
  61. ];
  62. $this->success('', $res);
  63. }
  64. /**
  65. * 资产变动明细
  66. * @return void
  67. */
  68. public function coinList()
  69. {
  70. $type_id = $this->request->post('query.action'); // 账变类型
  71. $coin_type = $this->request->post('query.coin_type'); // 資金类型
  72. $where = ['user_id' => $this->auth->id];
  73. if ($type_id > 0) $where['action'] = $type_id;
  74. switch ($coin_type){
  75. case 'token':
  76. $paginator = Loader::model('LedgerTokenChangeModel');
  77. $res['data']= $paginator->alias('a')
  78. ->join('user u', 'a.from_id = u.id and a.action > 9 and a.action < 12', 'LEFT')
  79. ->field('a.*, u.address')
  80. ->where($where)
  81. ->order('a.id DESC')->paginate($this->pageSize);
  82. break;
  83. case 'frozen':
  84. $paginator = (new LedgerFrozenChangeModel());
  85. $res['data'] = $paginator->alias('a')
  86. ->join('user u', 'a.from_id = u.id and a.action > 2', 'LEFT')
  87. ->field('a.*, u.address')
  88. ->where($where)
  89. ->order('id DESC')->paginate($this->pageSize);
  90. break;
  91. case 'teac':
  92. $paginator = Loader::model('LedgerTeacChangeModel');
  93. $res['data'] = $paginator->where($where)->order('id DESC')->paginate($this->pageSize);
  94. break;
  95. case 'teac_angel':
  96. $paginator = (new LedgerTeacAngelChangeModel());
  97. $res['data'] = $paginator->where($where)->order('id DESC')->paginate($this->pageSize);
  98. break;
  99. default:
  100. $this->error(__('Invalid parameters'));
  101. break;
  102. }
  103. $res['statusList'] = $paginator::getStatusList();
  104. $this->success('',$res);
  105. }
  106. /**
  107. * 标记茶宝资产转账
  108. * @return void
  109. */
  110. public function frozenTransfer(UserModel $userModel, LedgerWalletModel $ledgerWalletModel, LedgerFrozenChangeModel $ledgerFrozenChangeModel)
  111. {
  112. $amount = $this->request->post('amount'); // 茶宝
  113. $account= $this->request->post('account', ''); // 账号
  114. if(empty($amount) || empty($account)){
  115. $this->error(__('Parameter error'));
  116. }
  117. $fee = bcmul(getConfig('frozen_transfer'), $amount, 2);
  118. $real = bcsub($amount, $fee, 2) ; // 手续费
  119. // 启动事务
  120. Db::startTrans();
  121. try {
  122. $user = (substr($account, 0, 2) == '0x')? $userModel->getByAddress($account): $userModel->getByUid($account);
  123. if(empty($user)) throw new Exception(__("赠送用户不存在"));
  124. if($user['id'] == $this->auth->id) throw new Exception(__("赠送用户不能是自己"));
  125. $freeze = $ledgerWalletModel::getWalletFrozen($this->auth->id);
  126. //剩余冻结金额
  127. $available = bcsub($freeze, config('min_frozen'), 2);
  128. if(bccomp($amount, $available, 2) > 0) throw new Exception(__("转账后余额不能低于9.9"), 15001);
  129. // 更新USDT和账变
  130. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::FROZEN, -$amount, $ledgerFrozenChangeModel::Payment, $user['id']);
  131. $newFrozen = $ledgerWalletModel->changeWalletAccount($user['id'], Asset::FROZEN, $real, $ledgerFrozenChangeModel::Receive, $this->auth->id);
  132. //添加手续费
  133. $ledgerFrozenChangeModel::createChangeFees($user['id'], $this->auth->id, -$fee, $newFrozen);
  134. // 提交事务
  135. Db::commit();
  136. } catch (Exception $e) {
  137. // 回滚事务
  138. Db::rollback();
  139. $this->error($e->getMessage(), null, $e->getCode());
  140. }
  141. $this->success('ok');
  142. }
  143. /**
  144. * 赠送/转账明细
  145. * @return void
  146. */
  147. public function getGiftDesc()
  148. {
  149. $this->success('', ['chabao'=>['value' => getConfig('chabao_giveaway'), 'text' => getConfig('chabao_giveaway_txt')],
  150. 'frozen'=>['value' => getConfig('frozen_transfer'), 'text' => getConfig('frozen_transfer_txt')],
  151. 'teac'=>['value' => config('teac_giveaway'), 'text' => config('teac_giveaway_txt')]]);
  152. }
  153. /**
  154. * 茶宝赠送 0x
  155. * @return void
  156. */
  157. public function chabaoGift(UserModel $userModel, LedgerWalletModel $ledgerWalletModel, LedgerTokenChangeModel $ledgerTokenChangeModel)
  158. {
  159. $amount = $this->request->post('amount'); // 茶宝
  160. $account= $this->request->post('account', ''); // 账号
  161. if(empty($amount) || empty($account)){
  162. $this->error(__('Parameter error'));
  163. }
  164. $fee = bcmul(getConfig('chabao_giveaway'), $amount, 2);
  165. $real = bcsub($amount, $fee, 2) ; // 手续费
  166. // 启动事务
  167. Db::startTrans();
  168. try {
  169. $user = (substr($account, 0, 2) == '0x')? $userModel->getByAddress($account): $userModel->getByUid($account);
  170. if(empty($user)) throw new Exception(__("赠送用户不存在"));
  171. if($user['id'] == $this->auth->id) throw new Exception(__("赠送用户不能是自己"));
  172. $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  173. if(bccomp($amount, $chabao, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  174. // 更新USDT和账变
  175. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$amount, $ledgerTokenChangeModel::GiftPay, $user['id']);
  176. $newChabao = $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TOKEN, $real, $ledgerTokenChangeModel::GiftReceipt, $this->auth->id);
  177. //添加手续费
  178. $ledgerTokenChangeModel::createChangeFees($user['id'], $this->auth->id, -$fee, $newChabao);
  179. // 提交事务
  180. Db::commit();
  181. } catch (Exception $e) {
  182. // 回滚事务
  183. Db::rollback();
  184. $this->error($e->getMessage(), null, $e->getCode());
  185. }
  186. $this->success('ok');
  187. }
  188. /**
  189. * Teac赠送 0x
  190. * @return void
  191. */
  192. public function teacGift(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  193. {
  194. $amount = $this->request->post('amount'); // 茶宝
  195. $account= $this->request->post('account', ''); // 账号
  196. if(empty($amount) || empty($account)){
  197. $this->error(__('Parameter error'));
  198. }
  199. $real = bcsub($amount, bcmul(config('teac_giveaway'), $amount, 2), 2) ; // 手续费
  200. // 启动事务
  201. Db::startTrans();
  202. try {
  203. $user = (substr($account, 0, 2) == '0x')? $userModel->getByAddress($account): $userModel->getByUid($account);
  204. if(empty($user)) throw new Exception(__("赠送用户不存在"));
  205. if($user['id'] == $this->auth->id) throw new Exception(__("赠送用户不能是自己"));
  206. $teac = $ledgerWalletModel::getWalletTeac($this->auth->id);
  207. if(bccomp($amount, $teac, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  208. // 更新USDT和账变
  209. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TEAC, -$amount, LedgerTeacChangeModel::GiftPay, $user['id']);
  210. $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TEAC, $real, LedgerTeacChangeModel::GiftReceipt, $this->auth->id);
  211. // 提交事务
  212. Db::commit();
  213. } catch (Exception $e) {
  214. // 回滚事务
  215. Db::rollback();
  216. $this->error($e->getMessage(), null, $e->getCode());
  217. }
  218. $this->success('ok');
  219. }
  220. /**
  221. * 提现自动打款回调
  222. * 接口回调信息格式:
  223. * @return void
  224. */
  225. public function withdrawCallback_my()
  226. {
  227. //post 获取过来的数据格式为:{"code":"1","data":{"orderNo":"1768","tx_hash":"xx4545"}}
  228. $parems = $this->request->post();
  229. Log::write('提现自动打款回调参数:','info');
  230. Log::info(json_encode($parems));
  231. if(empty($parems)){
  232. $this->error("回调参数为空");
  233. }
  234. if($parems['code'] != 1){
  235. $this->error("本次提现失败");
  236. }
  237. $rs_data = $parems['data'];
  238. $info = (new OfflineWithdrawRecordModel())
  239. ->where('id', $rs_data['orderNo'])
  240. ->find();
  241. if(empty($info)){
  242. $this->error("当前提现信息不存在");
  243. }
  244. if($info['status'] == 2){
  245. $this->success("更新成功");
  246. }
  247. if($info['status'] == 5){
  248. $is_update = (new OfflineWithdrawRecordModel())
  249. ->where('id', $info['id'])
  250. ->update([
  251. 'tx_hash' => $rs_data['tx_hash'],
  252. 'status' => 2,
  253. 'update_time' => time(),
  254. ]);
  255. if($is_update){
  256. $this->success("更新成功");
  257. }else{
  258. $this->error("更新失败");
  259. }
  260. }
  261. }
  262. // 获取充值地址
  263. public function getAddress()
  264. {
  265. return $this->success('', ['value'=> Env::get('rental.pay_address'), 'name'=>getConfig('recharge_txt')]);
  266. }
  267. }