Ledger.php 11 KB

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