Ledger.php 14 KB

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