TeacLogin.php 4.7 KB

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