Ledger.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\LedgerDeclarationChange;
  4. use app\common\controller\Api;
  5. use app\common\model\LedgerPowerChangeModel;
  6. use app\common\model\LedgerQubicChangeModel;
  7. use app\common\model\UserModel;
  8. use app\common\model\LedgerSmhChangeModel;
  9. use app\common\model\LedgerTokenChangeModel;
  10. use app\common\model\LedgerUsdtChangeModel;
  11. use app\common\model\LedgerWalletModel;
  12. use app\common\model\OfflineWithdrawRecordModel;
  13. use fast\Action;
  14. use think\Log;
  15. use think\Env;
  16. use think\Db;
  17. use fast\Asset;
  18. use think\Exception;
  19. /**
  20. * 首页接口
  21. */
  22. class Ledger extends Api
  23. {
  24. protected array $noNeedLogin = ['withdrawCallback'];
  25. /**
  26. * 资产首页
  27. */
  28. public function assets()
  29. {
  30. $config = getAllConfig();
  31. $wallet = LedgerWalletModel::getWallet($this->auth->id);
  32. $res['assets'] = $wallet->token;
  33. $res['transfes_fee'] = $config['transfer_fee']; //转让手续费比例
  34. $res['transfes_txt'] = $config['transfes_txt']; //转让文字表述
  35. $res['chabao_rate'] = $config['chabao_rate']; //茶宝汇率
  36. $res['withdrawal_next_fee'] = $config['withdrawal_next_fee']; //600以下提现收费
  37. $res['withdrawal_up_fee'] = $config['withdrawal_up_fee']; //600以上提现收费
  38. $res['coin_list'] = [
  39. [
  40. 'coin_name' => '茶宝',
  41. 'coin_key' => 'token',
  42. 'amount' => $wallet->token,
  43. 'frozen_amount' => $wallet->frozen //冻结金额
  44. ]
  45. ];
  46. $this->success('', $res);
  47. }
  48. /**
  49. * 资产变动明细
  50. * @return void
  51. */
  52. public function coinList(LedgerWalletModel $ledgerWalletModel)
  53. {
  54. $type_id = $this->request->post('query.action'); // 账变类型
  55. $coin_type = $this->request->post('query.coin_type'); // 資金类型
  56. $where = [
  57. 'user_id' => $this->auth->getTokenUserID(),
  58. ];
  59. if ($type_id > 0) {
  60. $where['action'] = $type_id;
  61. }
  62. switch ($coin_type){
  63. case 'usdt':
  64. $paginator = (new LedgerUsdtChangeModel());
  65. break;
  66. case 'power':
  67. $paginator = (new LedgerPowerChangeModel());
  68. break;
  69. case 'declaration':
  70. $paginator = (new LedgerDeclarationChange());
  71. break;
  72. case 'etc':
  73. $paginator = (new LedgerTokenChangeModel());
  74. break;
  75. case 'token':
  76. $paginator = (new LedgerTokenChangeModel());
  77. break;
  78. case 'smh':
  79. $paginator = (new LedgerSmhChangeModel());
  80. break;
  81. case 'qubic':
  82. $paginator = (new LedgerQubicChangeModel());
  83. break;
  84. default:
  85. $this->error(__('Invalid parameters'));
  86. break;
  87. }
  88. $res['data'] = $paginator->where($where)->order('id DESC')->paginate($this->pageSize);
  89. $res['statusList'] = LedgerWalletModel::getStatusList();
  90. $this->success('',$res);
  91. }
  92. /**
  93. * 资产变动类型
  94. * @return void
  95. */
  96. public function coinAction()
  97. {
  98. $coin_type = $this->request->post('coin_type'); // 資金类型
  99. switch ($coin_type){
  100. case 'smh':
  101. $res = (new LedgerSmhChangeModel())->pay_status;//资金变动类型列表
  102. break;
  103. case 'qubic':
  104. $res = (new LedgerQubicChangeModel())->aciton_name;//资金变动类型列表
  105. break;
  106. case 'aleo':
  107. $res = (new LedgerTokenChangeModel())->aciton_name;//资金变动类型列表
  108. break;
  109. default:
  110. $res = Action::getAll($coin_type);//资金变动类型列表
  111. break;
  112. }
  113. $this->success('',$res);
  114. }
  115. public function actionGet()
  116. {
  117. $asset = $this->request->post('type'); // 资产类型
  118. $resp = [];
  119. switch ($asset) {
  120. case 1:
  121. $resp = [
  122. $this->getActionValueText(Action::All),
  123. $this->getActionValueText(Action::PowerRentalPower),
  124. $this->getActionValueText(Action::PowerDirectAward),
  125. $this->getActionValueText(Action::PowerTeamAward),
  126. $this->getActionValueText(Action::PowerEqualAward),
  127. $this->getActionValueText(Action::PowerBonusAward),
  128. $this->getActionValueText(Action::PowerCommunityBonusAward),
  129. ];
  130. break;
  131. case 2:
  132. $resp = [
  133. $this->getActionValueText(Action::All),
  134. $this->getActionValueText(Action::UsdtRentalPower),
  135. $this->getActionValueText(Action::UsdtGenerateProfit),
  136. $this->getActionValueText(Action::UsdtWeightDividend),
  137. $this->getActionValueText(Action::UsdtWithdrawCash),
  138. $this->getActionValueText(Action::UsdtWithdrawReturn),
  139. $this->getActionValueText(Action::UsdtRegBonus),
  140. $this->getActionValueText(Action::UsdtCmmunityBonus),
  141. ];
  142. break;
  143. case 3:
  144. $resp = [
  145. $this->getActionValueText(Action::All),
  146. $this->getActionValueText(Action::TokenAllocateEtc),
  147. ];
  148. break;
  149. default:
  150. $this->error(__('Invalid parameters'));
  151. break;
  152. }
  153. $this->success('', $resp);
  154. }
  155. private function getActionValueText(int $action): array
  156. {
  157. return ['value' => $action, 'text' => Action::getText($action)];
  158. }
  159. /**
  160. * 茶宝赠送明细
  161. * @return void
  162. */
  163. public function chabao()
  164. {
  165. $this->success('', ['value' => getConfig('chabao_giveaway'), 'text' => getConfig('chabao_giveaway_txt')]);
  166. }
  167. /**
  168. * 茶宝赠送
  169. * @return void
  170. */
  171. public function chabaoGift(UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  172. {
  173. $amount = $this->request->post('amount'); // 茶宝
  174. $address = $this->request->post('address'); // 地址
  175. if(empty($amount) || empty($address)){
  176. $this->error(__('Parameter error'));
  177. }
  178. $real = bcsub($amount, bcmul(getConfig('chabao_giveaway'), $amount, 2), 2) ; // 手续费
  179. // 启动事务
  180. Db::startTrans();
  181. try {
  182. $user = $userModel->getByAddress($address);
  183. if(empty($user)) throw new Exception(__("赠送用户不存在"));
  184. if($user['id'] == $this->auth->id) throw new Exception(__("赠送用户不能是自己"));
  185. $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  186. if(bccomp($amount, $chabao, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  187. // 更新USDT和账变
  188. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$amount, $ledgerWalletModel::GiftPay);
  189. $ledgerWalletModel->changeWalletAccount($user['id'], Asset::TOKEN, $real, $ledgerWalletModel::GiftReceipt);
  190. // 提交事务
  191. Db::commit();
  192. } catch (Exception $e) {
  193. // 回滚事务
  194. Db::rollback();
  195. $this->error($e->getMessage(), null, $e->getCode());
  196. }
  197. $this->success('ok');
  198. }
  199. /**
  200. * 虚拟币明细
  201. * @return void
  202. */
  203. public function tokenList()
  204. {
  205. $type = $this->request->post('query.action'); // 账变类型
  206. $where = [
  207. 'user_id' => $this->auth->getTokenUserID(),
  208. ];
  209. if ($type != Action::All) {
  210. $where['action'] = $type;
  211. }
  212. $paginator = (new LedgerTokenChangeModel())->where($where)->order('id DESC')->paginate($this->pageSize);
  213. $this->success('', $this->buildResp($paginator->total(), $paginator->currentPage(), $paginator->items()));
  214. }
  215. /**
  216. * 提现自动打款回调
  217. * 接口回调信息格式:
  218. *
  219. * @return void
  220. */
  221. public function withdrawCallback_my()
  222. {
  223. //post 获取过来的数据格式为:{"code":"1","data":{"orderNo":"1768","tx_hash":"xx4545"}}
  224. $parems = $this->request->post();
  225. Log::write('提现自动打款回调参数:','info');
  226. Log::info(json_encode($parems));
  227. if(empty($parems)){
  228. $this->error("回调参数为空");
  229. }
  230. if($parems['code'] != 1){
  231. $this->error("本次提现失败");
  232. }
  233. $rs_data = $parems['data'];
  234. $info = (new OfflineWithdrawRecordModel())
  235. ->where('id', $rs_data['orderNo'])
  236. ->find();
  237. if(empty($info)){
  238. $this->error("当前提现信息不存在");
  239. }
  240. if($info['status'] == 2){
  241. $this->success("更新成功");
  242. }
  243. if($info['status'] == 5){
  244. $is_update = (new OfflineWithdrawRecordModel())
  245. ->where('id', $info['id'])
  246. ->update([
  247. 'tx_hash' => $rs_data['tx_hash'],
  248. 'status' => 2,
  249. 'update_time' => time(),
  250. ]);
  251. if($is_update){
  252. $this->success("更新成功");
  253. }else{
  254. $this->error("更新失败");
  255. }
  256. }
  257. }
  258. /**
  259. * 提现自动打款回调
  260. * 接口回调信息格式:
  261. * companyWithdrawId=126&sign=8e3c6aee53e3ea4ff974c1d80f4e8beb&status=1&txId=0x39ce05a0698ff2b7459ca707703fd48937dd958422d98ebade6b4f5188b70995
  262. * @return void
  263. */
  264. public function withdrawCallback()
  265. {
  266. // $body1 = file_get_contents("php://input");
  267. // Log::write('提现自动打款回调1:' . $body1, 'info');
  268. //
  269. // $body = $this->request->post();
  270. $body = file_get_contents("php://input");
  271. //$body = 'companyWithdrawId=126&sign=8e3c6aee53e3ea4ff974c1d80f4e8beb&status=1&txId=0x39ce05a0698ff2b7459ca707703fd48937dd958422d98ebade6b4f5188b70995';
  272. Log::write('提现自动打款回调:' . $body, 'info');
  273. if(empty($body)){
  274. return;
  275. }
  276. $parems = explode('&', $body);
  277. $req_arr = [];
  278. foreach ($parems as $item){
  279. $temp = explode('=', $item);
  280. $req_arr[$temp[0]] = $temp[1];
  281. }
  282. Log::write('提现自动打款回调参数:','info');
  283. Log::info(json_encode($req_arr));
  284. $info = (new OfflineWithdrawRecordModel())
  285. ->where('id', $req_arr['companyWithdrawId'])
  286. ->find();
  287. if(empty($info)){
  288. $this->error("当前提现信息不存在");
  289. }
  290. if($info['status'] == OfflineWithdrawRecordModel::StatusSuccessAuto){
  291. $this->success("更新成功");
  292. }
  293. if($info['status'] == OfflineWithdrawRecordModel::StatusConfirm){
  294. $is_update = (new OfflineWithdrawRecordModel())
  295. ->where('id', $req_arr['companyWithdrawId'])
  296. ->update([
  297. 'tx_hash' => $req_arr['txId'],
  298. 'status' => OfflineWithdrawRecordModel::StatusSuccessAuto,
  299. 'update_time' => time(),
  300. ]);
  301. if($is_update){
  302. $this->success("更新成功");
  303. }else{
  304. $this->error("更新失败");
  305. }
  306. }
  307. }
  308. // 获取充值地址
  309. public function getAddress()
  310. {
  311. return $this->success('', ['value'=> Env::get('rental.pay_address'), 'name'=>getConfig('recharge_txt')]);
  312. }
  313. }