TeacLogin.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace app\api\logic;
  3. use app\common\model\ProductTeac;
  4. use fast\Http;
  5. use think\Loader;
  6. use Exception;
  7. use finfo;
  8. use think\Cache;
  9. use fast\Asset;
  10. use think\Log;
  11. use think\Lang;
  12. use app\common\model\LedgerTeacChangeModel;
  13. use app\common\model\LedgerTokenChangeModel;
  14. /**
  15. * Teac交易
  16. */
  17. class TeacLogin
  18. {
  19. protected static $feeList = [
  20. 1 => 'sell_serve_fee' , //出售服务费
  21. 2 => 'buy_serve_fee' , //求购服务费
  22. ];
  23. /**
  24. * 发布求购出售信息
  25. */
  26. public static function setCreateTrade(int $uid, float $price, int $stock, int $typeId, float $frozen):object
  27. {
  28. $rows = ProductTeac::where('user_id', $uid)->where('type_id', $typeId)->where('status', ProductTeac::Normal)->find();
  29. if($rows) throw new Exception(__("你有未完成的求购订单、不能重复发布"));
  30. //添加订单信息
  31. return ProductTeac::setUserCreateOrder($uid, $typeId, $price, $stock, config('teac_trade.'. self::$feeList[$typeId]), $frozen);
  32. }
  33. /**
  34. * 购买出售
  35. * @$uid 用户人id
  36. * @$fromUid 来源订单id
  37. */
  38. public static function setCreateSellOrder(int $uid, int $fromUid, float $chabao, int $num, float $fee)
  39. {
  40. $ledgerWalletModel = Loader::model('LedgerWalletModel');
  41. $total_price = bcmul($chabao, $num, 2); //总价
  42. $fee = bcmul($total_price, $fee, 2); //手续费
  43. //扣除用户茶宝
  44. $ledgerWalletModel->changeWalletAccount($uid, Asset::TOKEN, -$total_price, LedgerTokenChangeModel::BuySellg, $fromUid);
  45. //添加用户Teac
  46. $ledgerWalletModel->changeWalletAccount($uid, Asset::TEAC, $num, LedgerTeacChangeModel::SellBuy, $fromUid);
  47. //添加来源茶宝
  48. $ledgerWalletModel->changeWalletAccount($fromUid, Asset::TOKEN, bcsub($total_price, $fee, 2), LedgerTokenChangeModel::BuySellg, $uid);
  49. return true;
  50. }
  51. /**
  52. * 求购出售
  53. * @$uid 用户人id
  54. * @$fromUid 来源订单id
  55. */
  56. public static function setCreateBuyingOrder(int $uid, int $fromUid, float $chabao, int $num, float $fee)
  57. {
  58. $ledgerWalletModel = Loader::model('LedgerWalletModel');
  59. $total_price = bcmul($chabao, $num, 2); //总价
  60. $fee = bcmul($total_price, $fee, 2); //手续费
  61. //添加用户茶宝
  62. $ledgerWalletModel->changeWalletAccount($uid, Asset::TOKEN, $total_price, LedgerTokenChangeModel::BuySellg, $fromUid);
  63. //扣除用户Teac
  64. $ledgerWalletModel->changeWalletAccount($uid, Asset::TEAC, $num, LedgerTeacChangeModel::BuySell, $fromUid);
  65. //扣除来源茶宝
  66. $ledgerWalletModel->changeWalletAccount($fromUid, Asset::TOKEN, bcsub($total_price, $fee, 2), LedgerTokenChangeModel::BuySellg, $uid);
  67. return true;
  68. }
  69. /**
  70. * 根据时间戳获取最近的区块高度
  71. * api接口返回数据格式
  72. * @param $orderInfo
  73. * @return array|string
  74. */
  75. public function getBlockNoByTime($time):array
  76. {
  77. if(!Cache::has('block_' . $time)){
  78. if (empty($time)) {
  79. return _error('时间戳不能为空');
  80. }
  81. //https://api.bscscan.com/api?module=block&action=getblocknobytime&timestamp=1601510400&closest=before&apikey=YourApiKeyToken
  82. $url = "https://api.bscscan.com/api?module=block&action=getblocknobytime&";
  83. $url .= "&closest=before";//closest 值还有个参数 after 控制返回接近时间戳之前还是之后的区块高度
  84. $url .= "&apikey=" . $this->bsc_api_key;
  85. $url .= "&timestamp=" . $time;
  86. dump($url);
  87. $body = Http::get($url);
  88. if (empty($body)) {
  89. return _error('获取区块高度api返回内容为空');
  90. }
  91. // 转成数组
  92. $rsArr = json_decode($body, true);
  93. if (empty($rsArr) || !is_array($rsArr)) {
  94. return _error('获取区块高度api返回数据异常,json转换失败');
  95. }
  96. if ($rsArr['status'] != '1') {
  97. return _error($rsArr['message'] . ' -- ' . $rsArr['result']);
  98. }
  99. Cache::set('block_' . $time, _success($rsArr['result']), 3600);
  100. }
  101. //获取开始预约前的区块高度
  102. return Cache::get('block_' . $time);
  103. }
  104. public function getBlcokNoByCache($time){
  105. if(!Cache::has('block')){
  106. $get_block = (new BscApi())->getBlockNoByTime($start_time);
  107. if($get_block['code'] == 0){
  108. Log::info($get_block['msg'] . date('Y-m-d H:i:s'));
  109. dump('获取区块高度有误');
  110. dump($get_block);
  111. return;
  112. }
  113. Cache::set('block', $get_block['data'], 3600);
  114. }
  115. //获取开始预约前的区块高度
  116. $start_block = Cache::get('block');
  117. }
  118. }