Ledger.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\logic\WelfareLoginc;
  4. use app\common\controller\Api;
  5. use app\common\logic\BscApi;
  6. use app\common\model\LedgerFrozenChangeModel;
  7. use app\common\model\ProductLists;
  8. use app\common\model\ProductOrder;
  9. use app\common\model\RwaExchangeRecordModel;
  10. use app\common\model\UserModel;
  11. use app\common\model\LedgerTokenChangeModel;
  12. use app\common\model\LedgerWalletModel;
  13. use app\common\model\OfflineWithdrawRecordModel;
  14. use think\Log;
  15. use think\Env;
  16. use think\Db;
  17. use fast\Asset;
  18. use think\Loader ;
  19. use think\Exception;
  20. /**
  21. * 首页接口
  22. */
  23. class Ledger extends Api
  24. {
  25. protected array $noNeedLogin = ['withdrawCallback'];
  26. /**
  27. * 资产首页
  28. */
  29. public function assets()
  30. {
  31. $config = getAllConfig();
  32. $wallet = LedgerWalletModel::getWallet($this->auth->id);
  33. $res['assets'] = $wallet->token;
  34. $res['transfes_fee'] = $config['transfer_fee']; //转让手续费比例
  35. $res['transfes_txt'] = $config['transfes_txt']; //转让文字表述
  36. $res['chabao_rate'] = $config['chabao_rate']; //茶宝汇率
  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. ];
  57. $this->success('', $res);
  58. }
  59. /**
  60. * 资产变动明细
  61. * @return void
  62. */
  63. public function coinList()
  64. {
  65. $type_id = $this->request->post('query.action'); // 账变类型
  66. $coin_type = $this->request->post('query.coin_type'); // 資金类型
  67. $where = ['user_id' => $this->auth->id];
  68. if ($type_id > 0) $where['action'] = $type_id;
  69. switch ($coin_type){
  70. case 'token':
  71. $paginator = Loader::model('LedgerTokenChangeModel');
  72. $res['data']= $paginator->alias('a')
  73. ->join('user u', 'a.from_id = u.id and a.action > 9 and a.action < 12', 'LEFT')
  74. ->field('a.*, u.address')
  75. ->where($where)
  76. ->order('a.id DESC')->paginate($this->pageSize);
  77. break;
  78. case 'frozen':
  79. $paginator = (new LedgerFrozenChangeModel());
  80. $res['data'] = $paginator->alias('a')
  81. ->join('user u', 'a.from_id = u.id and a.action > 2', 'LEFT')
  82. ->field('a.*, u.address')
  83. ->where($where)
  84. ->order('id DESC')->paginate($this->pageSize);
  85. break;
  86. case 'teac':
  87. $paginator = Loader::model('LedgerTeacChangeModel');
  88. $res['data'] = $paginator->where($where)->order('id DESC')->paginate($this->pageSize);
  89. break;
  90. case 'usdt':
  91. $paginator = (new LedgerUsdtChangeModel());
  92. break;
  93. case 'smh':
  94. $paginator = (new LedgerSmhChangeModel());
  95. break;
  96. default:
  97. $this->error(__('Invalid parameters'));
  98. break;
  99. }
  100. $res['statusList'] = $paginator::getStatusList();
  101. $this->success('',$res);
  102. }
  103. /**
  104. * Teac资产转账
  105. * @return void
  106. */
  107. public function frozenTransfer(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  108. {
  109. $amount = $this->request->post('amount'); // 茶宝
  110. $address = $this->request->post('address'); // 地址
  111. if(empty($amount) || empty($address)){
  112. $this->error(__('Parameter error'));
  113. }
  114. $real = bcsub($amount, bcmul(getConfig('frozen_transfer'), $amount, 2), 2) ; // 手续费
  115. // 启动事务
  116. Db::startTrans();
  117. try {
  118. $user = $userModel->getByAddress($address);
  119. if(empty($user)) throw new Exception(__("赠送用户不存在"));
  120. if($user['id'] == $this->auth->id) throw new Exception(__("赠送用户不能是自己"));
  121. $freeze = $ledgerWalletModel::getWalletFrozen($this->auth->id);
  122. //剩余冻结金额
  123. $available = bcsub($freeze, config('min_frozen'), 2);
  124. if(bccomp($amount, $available, 2) > 0) throw new Exception(__("转账后余额不能低于9.9"), 15001);
  125. // 更新USDT和账变
  126. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::FROZEN, -$amount, LedgerFrozenChangeModel::Payment, $user['id']);
  127. $ledgerWalletModel->changeWalletAccount($user['id'], Asset::FROZEN, $real, LedgerFrozenChangeModel::Receive, $this->auth->id);
  128. // 提交事务
  129. Db::commit();
  130. } catch (Exception $e) {
  131. // 回滚事务
  132. Db::rollback();
  133. $this->error($e->getMessage(), null, $e->getCode());
  134. }
  135. $this->success('ok');
  136. }
  137. /**
  138. * 赠送/转账明细
  139. * @return void
  140. */
  141. public function getGiftDesc()
  142. {
  143. $this->success('', ['chabao'=>['value' => getConfig('chabao_giveaway'), 'text' => getConfig('chabao_giveaway_txt')],
  144. 'frozen'=>['value' => getConfig('frozen_transfer'), 'text' => getConfig('frozen_transfer_txt')]]);
  145. }
  146. /**
  147. * 茶宝赠送
  148. * @return void
  149. */
  150. public function chabaoGift(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  151. {
  152. $amount = $this->request->post('amount'); // 茶宝
  153. $address = $this->request->post('address'); // 地址
  154. if(empty($amount) || empty($address)){
  155. $this->error(__('Parameter error'));
  156. }
  157. $real = bcsub($amount, bcmul(getConfig('chabao_giveaway'), $amount, 2), 2) ; // 手续费
  158. // 启动事务
  159. Db::startTrans();
  160. try {
  161. $user = $userModel->getByAddress($address);
  162. if(empty($user)) throw new Exception(__("赠送用户不存在"));
  163. if($user['id'] == $this->auth->id) throw new Exception(__("赠送用户不能是自己"));
  164. $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  165. if(bccomp($amount, $chabao, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  166. // 更新USDT和账变
  167. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$amount, LedgerTokenChangeModel::GiftPay, $user['id']);
  168. $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TOKEN, $real, LedgerTokenChangeModel::GiftReceipt, $this->auth->id);
  169. // 提交事务
  170. Db::commit();
  171. } catch (Exception $e) {
  172. // 回滚事务
  173. Db::rollback();
  174. $this->error($e->getMessage(), null, $e->getCode());
  175. }
  176. $this->success('ok');
  177. }
  178. //福利兑换配置
  179. public function getWelfareRedeConfig(){
  180. $this->success('ok', config('welfare_rede'));
  181. }
  182. public function submitWelfare(LedgerWalletModel $ledgerWalletModel)
  183. {
  184. $coin = $this->request->post('coin'); // 代币
  185. $coin_from_address = $this->request->post('coin_from_address'); // 代码转入地址
  186. $usdt_from_address = $this->request->post('usdt_from_address'); // U转入地址
  187. if (empty($coin) || empty($coin_from_address) || empty($usdt_from_address)) {
  188. $this->error(__('Parameter error'));
  189. }
  190. //检查区块链地址是否合法
  191. if(!(isErc20AddressValid($coin_from_address) && isErc20AddressValid($usdt_from_address))){
  192. $this->error(__('Invalid parameters'));
  193. }
  194. $welfare_config = config('welfare_rede');
  195. if (!in_array($coin, $welfare_config['currency'])) {
  196. $this->error(__('Invalid parameters'));
  197. }
  198. $BscApi = new BscApi($welfare_config['contract_address'][$coin]);
  199. $result_coin = $BscApi->getTransactionRecordsByAddress($coin_from_address, $welfare_config['transfer_address'], 49990000);
  200. if ($result_coin['code'] == 0) {
  201. $this->error($result_coin['msg']);
  202. }
  203. Log::info($result_coin, '代币所有转入记录');
  204. $coin_list = [];
  205. foreach ($result_coin['data'] as $value) {
  206. if ($value['amount'] == $welfare_config['currency_price'][$coin]) {
  207. $coin_list[] = $value;
  208. }
  209. }
  210. if (empty($coin_list)) {
  211. $this->error('未识别到代币转入记录');
  212. }
  213. Log::info($coin_list, '代币精准转入记录');
  214. $coin_data = [];
  215. foreach ($coin_list as $item) {
  216. $check_info = (new RwaExchangeRecordModel())->where('tx_hash', $item['hash'])->find();
  217. if (empty($check_info)) {
  218. $coin_data = $item;
  219. break;
  220. }
  221. }
  222. if (empty($coin_data)) {
  223. $this->error('未识别到新代币转入记录');
  224. }
  225. Log::info($coin_list, '代币可用转入记录');
  226. $BscApi = new BscApi($welfare_config['contract_address']['USDT']);
  227. $result_usdt = $BscApi->getTransactionRecordsByAddress($usdt_from_address, $welfare_config['transfer_address'], 49990000);
  228. if ($result_usdt['code'] == 0) {
  229. $this->error($result_usdt['msg']);
  230. }
  231. Log::info($result_usdt, 'USDT所有转入记录');
  232. $usdt_list = [];
  233. foreach ($result_usdt['data'] as $value) {
  234. if ($value['amount'] == $welfare_config['currency_price']['USDT']) {
  235. $usdt_list[] = $value;
  236. }
  237. }
  238. if (empty($usdt_list)) {
  239. $this->error('未识别到USDT转入记录');
  240. }
  241. Log::info($usdt_list, 'USDT精准转入记录');
  242. $usdt_data = [];
  243. foreach ($usdt_list as $item) {
  244. $check_info = (new RwaExchangeRecordModel())->where('tx_hash', $item['hash'])->find();
  245. if (empty($check_info)) {
  246. $usdt_data = $item;
  247. break;
  248. }
  249. }
  250. if (empty($usdt_data)) {
  251. $this->error('未识别到新USDT转入记录');
  252. }
  253. Log::info($usdt_list, 'USDT可用入记录');
  254. $order_no = date('YmdHis') . rand(1000, 9999);
  255. $inster_data = [
  256. [
  257. 'order_no' => $order_no,
  258. 'tx_hash' => $coin_data['hash'],
  259. 'user_id' => $this->auth->id,
  260. 'symbol' => $coin,
  261. 'amount' => $coin_data['amount'],
  262. 'product_id' => $welfare_config['product_id'],
  263. 'from_address' => $coin_data['from'],
  264. 'to_address' => $coin_data['to'],
  265. 'status' => 200,
  266. 'create_time' => time(),
  267. ],
  268. [
  269. 'order_no' => $order_no,
  270. 'tx_hash' => $usdt_data['hash'],
  271. 'user_id' => $this->auth->id,
  272. 'symbol' => 'USDT',
  273. 'amount' => $usdt_data['amount'],
  274. 'product_id' => $welfare_config['product_id'],
  275. 'from_address' => $usdt_data['from'],
  276. 'to_address' => $usdt_data['to'],
  277. 'status' => 200,
  278. 'create_time' => time(),
  279. ]
  280. ];
  281. Log::info($inster_data, '插入数据');
  282. $product_id = $welfare_config['product_id'];
  283. $product_info = (new ProductLists())->where('id', $product_id)->find();
  284. if (empty($product_info)) {
  285. $this->error('产品不存在');
  286. }
  287. try {
  288. Db::startTrans();
  289. $rs = Db::name('rwa_exchange_record')->fetchSql(false)->insertAll($inster_data);
  290. //添加标记茶宝记录
  291. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::FROZEN, $usdt_data['amount'], LedgerFrozenChangeModel::RwaExchangeRecord, 0);
  292. //发放产品
  293. $rs = WelfareLoginc::setUserProductOrder(1, false, $order_no, 0, $product_info['id'], $this->auth->id, ProductOrder::Exchange);
  294. Db::commit();
  295. $this->success('ok', $order_no);
  296. } catch (Exception $e) {
  297. Db::rollback();
  298. $this->error($e->getMessage(), null, $e->getCode());
  299. }
  300. }
  301. /**
  302. * 提现自动打款回调
  303. * 接口回调信息格式:
  304. * @return void
  305. */
  306. public function withdrawCallback_my()
  307. {
  308. //post 获取过来的数据格式为:{"code":"1","data":{"orderNo":"1768","tx_hash":"xx4545"}}
  309. $parems = $this->request->post();
  310. Log::write('提现自动打款回调参数:','info');
  311. Log::info(json_encode($parems));
  312. if(empty($parems)){
  313. $this->error("回调参数为空");
  314. }
  315. if($parems['code'] != 1){
  316. $this->error("本次提现失败");
  317. }
  318. $rs_data = $parems['data'];
  319. $info = (new OfflineWithdrawRecordModel())
  320. ->where('id', $rs_data['orderNo'])
  321. ->find();
  322. if(empty($info)){
  323. $this->error("当前提现信息不存在");
  324. }
  325. if($info['status'] == 2){
  326. $this->success("更新成功");
  327. }
  328. if($info['status'] == 5){
  329. $is_update = (new OfflineWithdrawRecordModel())
  330. ->where('id', $info['id'])
  331. ->update([
  332. 'tx_hash' => $rs_data['tx_hash'],
  333. 'status' => 2,
  334. 'update_time' => time(),
  335. ]);
  336. if($is_update){
  337. $this->success("更新成功");
  338. }else{
  339. $this->error("更新失败");
  340. }
  341. }
  342. }
  343. // 获取充值地址
  344. public function getAddress()
  345. {
  346. return $this->success('', ['value'=> Env::get('rental.pay_address'), 'name'=>getConfig('recharge_txt')]);
  347. }
  348. }