Ledger.php 12 KB

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