Ledger.php 14 KB

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