RechargeController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\dao\Pay\AccountPayInfo;
  4. use App\Facade\TableName;
  5. use App\Http\helper\NumConfig;
  6. use App\Http\logic\api\CashPayLogic;
  7. use App\Http\logic\api\RechargeLogic;
  8. use App\Models\AccountsInfo;
  9. use App\Models\Order;
  10. use App\Models\Platform\MonthCard;
  11. use App\Models\PrivateMail;
  12. use App\Models\RecordUserDataStatistics;
  13. use App\Services\ApkService;
  14. use App\Util;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\Cache;
  17. use Illuminate\Support\Facades\DB;
  18. use Illuminate\Support\Facades\Log;
  19. use Illuminate\Support\Facades\Redis;
  20. use function GuzzleHttp\Psr7\rewind_body;
  21. class RechargeController
  22. {
  23. protected $JumpOut = [2, 4, 9, 13, 12, 14];
  24. protected $Sdk = [];
  25. // 返回支付档位
  26. public function gear()
  27. {
  28. $names = DB::table('agent.dbo.admin_configs')
  29. ->where([
  30. 'type' => 'pay_method',
  31. 'status' => 1,
  32. ])
  33. ->selectRaw('id, name, status, new_pay_type as type')
  34. ->orderByDesc('sort')->get()->toArray();
  35. $list = DB::table('agent.dbo.recharge_gear')
  36. ->where('first_pay', 0)
  37. ->orderBy('money', 'asc')->where('status', 1)->get();
  38. foreach ($list as $val) {
  39. $val->favorable_price = $val->favorable_price + $val->give;
  40. $gear = $names;
  41. $val->gear = \GuzzleHttp\json_encode($gear);
  42. }
  43. return apiReturnSuc($list);
  44. }
  45. // 返回支付档位
  46. public function gearAct(Request $request)
  47. {
  48. $UserID=$request->UserID??"";
  49. if(empty($UserID)){
  50. return apiReturnFail(__('messages.api.pay.payment_error'));
  51. }
  52. $RechargeTimes=DB::table(TableName::QPRecordDB() . 'RecordUserTotalStatistics')->where('UserID', $UserID)->value('RechargeTimes')??0;
  53. if($RechargeTimes>=4) {
  54. $Status = 1;
  55. }else{
  56. $Status=$RechargeTimes+3;
  57. }
  58. $names = DB::table('agent.dbo.admin_configs')
  59. ->where([
  60. 'type' => 'pay_method',
  61. 'status' => 1,
  62. ])
  63. ->selectRaw('id, name, status, new_pay_type as type')
  64. ->orderByDesc('sort')->get()->toArray();
  65. $list = DB::table('agent.dbo.recharge_gear')
  66. ->where('first_pay', 0)
  67. ->orderBy('money', 'asc')->where('status', $Status)->get();
  68. foreach ($list as $val) {
  69. $val->favorable_price = $val->favorable_price + $val->give;
  70. $gear = $names;
  71. $val->gear = \GuzzleHttp\json_encode($gear);
  72. }
  73. return apiReturnSuc($list);
  74. }
  75. // 根据支付档位获取渠道
  76. public function getPayChannel(Request $request)
  77. {
  78. $id = $request->id ?: '';
  79. $ChannelNumber = $request->ChannelNumber ?: '';
  80. if (empty($id)) {
  81. return apiReturnFail(__('messages.api.pay.payment_error'));
  82. }
  83. $key="get_channel_{$id}_{$ChannelNumber}";
  84. // if(Redis::exists($key)) {
  85. // $result=json_decode(Redis::get($key));
  86. // }else {
  87. $first = DB::table('agent.dbo.recharge_gear')
  88. ->where('status','>=', 1)
  89. ->where('id', $id)
  90. ->first();
  91. if (empty($first)) {
  92. return apiReturnFail(__('messages.api.pay.payment_error'));
  93. }
  94. $UserID=$request->UserID??"";
  95. $RechargeTimes=DB::table(TableName::QPRecordDB() . 'RecordUserTotalStatistics')->where('UserID', $UserID)->value('RechargeTimes')??0;
  96. if($RechargeTimes>=4) {
  97. $Status = 1;
  98. }else{
  99. $Status=$RechargeTimes+3;
  100. }
  101. //充值对不上
  102. if($first->status!=$Status){
  103. // return apiReturnFail('reloadList','reloadList');
  104. }
  105. $first->favorable_price = $first->favorable_price + $first->give;
  106. $names = DB::table('agent.dbo.admin_configs')
  107. ->where('type', 'pay_method')
  108. ->where('status', '1')
  109. ->select('id', 'name', 'new_pay_type')
  110. ->orderByDesc('sort')->get()
  111. ->map(function ($value) {
  112. return (array)$value;
  113. })->toArray();
  114. // 渠道
  115. if ($ChannelNumber != '') {
  116. $switch = Redis::get("recharge_config_switch_{$ChannelNumber}");
  117. if ($switch) {
  118. $list = DB::connection('write')->table('QPPlatformDB.dbo.ChannelOpenRecharge as go')
  119. ->join('agent.dbo.admin_configs as cf', 'go.ConfigID', '=', 'cf.id')
  120. ->where('go.Channel', $ChannelNumber)
  121. ->where('go.Status', 1)
  122. ->where('cf.type', 'pay_method')
  123. ->orderByDesc('go.Sort')
  124. ->select('go.Sort', 'ConfigID')
  125. ->pluck('Sort', 'ConfigID')->toArray();
  126. }
  127. }
  128. if (!isset($list) || count($list) < 1) { // 默认配置
  129. $list = DB::connection('write')->table('agent.dbo.admin_configs')
  130. ->where('type', 'pay_method')
  131. ->where('status', 1)
  132. ->select('sort as Sort', 'id as ConfigID')
  133. ->pluck('Sort', 'ConfigID')->toArray();
  134. }
  135. $data = [];
  136. foreach ($names as $k => $va) {
  137. if (isset($list[$va['id']])) {
  138. $type = $va['new_pay_type'] == 1 ? 3 : 2;
  139. if ($va['new_pay_type'] == 4) {
  140. $type = 0;
  141. }
  142. // type = 1 sdk跳转 type=2 外跳链接 type=0 默认 type=3 复制信息到APP支付
  143. $data[] = ['id' => $va['id'],
  144. 'name' => $va['name'], 'status' => 1, 'type' => $type, 'sort' => $list[$va['id']]];
  145. }
  146. }
  147. // $first->cpf_first=0;
  148. // if($ChannelNumber==103)$first->cpf_first=1;
  149. $gear = array_values($data);
  150. $gear = collect($gear)->sortByDesc('sort')->toArray();
  151. $first->kefu_switch = 1;
  152. $gear = array_values($gear);
  153. $first->gear = \GuzzleHttp\json_encode($gear);
  154. $result = apiReturnSuc($first);
  155. // Redis::set($key,json_encode($result));
  156. // Redis::expire($key,60);
  157. // }
  158. // $dir = $path = app()->basePath() . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'cache';
  159. // if (!file_exists($dir)) {
  160. // mkdir($dir, 0777);
  161. // }
  162. // $path = $dir . DIRECTORY_SEPARATOR . "get_channel_{$id}_{$ChannelNumber}.json";
  163. // file_put_contents($path, json_encode($result));
  164. return $result;
  165. }
  166. // 返回支付渠道
  167. public function payChannel()
  168. {
  169. $list = DB::table('agent.dbo.admin_configs')->where('type', 'pay_method')
  170. ->where('status', 1)
  171. ->select('id', 'name', 'status')
  172. ->orderByDesc('sort')->get();
  173. foreach ($list as &$value) {
  174. if (in_array($value->id, $this->Sdk)) {
  175. $value->type = 0;
  176. } elseif (in_array($value->id, $this->JumpOut)) {
  177. $value->type = 2;
  178. } else {
  179. $value->type = 0;
  180. }
  181. }
  182. unset($value);
  183. return apiReturnSuc("$list");
  184. }
  185. // 首充可选择渠道
  186. public function firstPayConfig(Request $request)
  187. {
  188. // 推荐充值档位
  189. $recomendar_recharge = DB::table(TableName::QPAccountsDB() . 'SystemStatusInfo')
  190. ->where('StatusName', 'RecomendarRecharge')
  191. ->value('StatusValue');
  192. $first_pay = DB::table('agent.dbo.recharge_gear')->where('status', 1)
  193. ->where('first_pay', 1)->first();
  194. if (!$first_pay) {
  195. return apiReturnSuc(compact('first_pay', 'recomendar_recharge'));
  196. }
  197. $names = DB::table('agent.dbo.admin_configs')
  198. ->where('type', 'pay_method')
  199. ->where('status', 1)
  200. ->select('id', 'name', 'new_pay_type')->orderByDesc('sort')->get()->toArray();
  201. $first_pay->favorable_price = $first_pay->favorable_price + $first_pay->give;
  202. $data = [];
  203. foreach ($names as $k => $va) {
  204. $type = $va->new_pay_type == 1 ? 3 : 2;
  205. if($va->new_pay_type == 4){
  206. $type = 0;
  207. }
  208. // type = 1 sdk跳转 type=2 外跳链接 type=0 默认 type=3 复制信息到APP支付
  209. $data[] = ['id' => $va->id, 'name' => $va->name, 'status' => 1, 'type' => $type];
  210. }
  211. $gear = array_values($data);
  212. $first_pay->gear = \GuzzleHttp\json_encode($gear);
  213. $ChannelNumber = $request->ChannelNumber ?: '';
  214. // $first_pay->cpf_first=0;
  215. // if($ChannelNumber==103)$first_pay->cpf_first=1;
  216. return apiReturnSuc(compact('first_pay', 'recomendar_recharge'));
  217. }
  218. // 引导付费
  219. public function guidePayment(Request $request)
  220. {
  221. $UserID = $request->input('UserID');
  222. // 引导付费标识
  223. // $guide_payment_exists = DB::table(TableName::agent() . 'guide_payment')->where('UserID', $UserID)->first();
  224. $flag = false;
  225. // 查看用户有没有充值过【只要充值过,引导付费就不能充值】
  226. $user_recharge = DB::table(TableName::QPAccountsDB() . 'YN_VIPAccount')
  227. ->where('UserID', $UserID)
  228. ->value('Recharge') ?: 0;
  229. if ($user_recharge > 0) {
  230. $flag = true;
  231. }
  232. // 引导付费礼包列表
  233. $guidePayment = (new RechargeLogic())->guidePayment($this->JumpOut);
  234. // 日期 -- 倒计时【小时】
  235. $countdown = 24;
  236. return apiReturnSuc(['is_pay' => true, 'countdown' => $countdown, 'guidePayment' => $guidePayment]);
  237. }
  238. public static function getVipConfig()
  239. {
  240. if(Redis::exists('vipConfigNew')) {
  241. $res=json_decode(Redis::get('vipConfigNew'),true);
  242. }else{
  243. $res = DB::table('QPAccountsDB.dbo.ProtectLevel')
  244. ->orderBy('Recharge')->get();
  245. Redis::setex('vipConfigNew',600,json_encode($res));
  246. }
  247. return $res;
  248. }
  249. // 充值Vip等级
  250. public function vipConfigNew(Request $request)
  251. {
  252. $config=self::getVipConfig();
  253. }
  254. // 充值Vip等级
  255. public function vipConfig(Request $request)
  256. {
  257. $res=self::getVipConfig();
  258. $list = [];
  259. foreach ($res as $v) {
  260. $v=(object)$v;
  261. $list[] = [
  262. 'id' => $v->ID,
  263. 'money' => $v->Recharge,
  264. 'num' => $v->ID - 1,
  265. 'relief_money' => $v->GrantNum/100,
  266. 'vip' => $v->ID - 1,
  267. ];
  268. }
  269. // 充值礼包
  270. $monthCards = MonthCard::query()
  271. ->select('CardID', 'CardName', 'Price', 'FirstReward', 'DayReward', 'gear', 'CardState')
  272. ->where(['CardType' => 1,])
  273. ->get();
  274. // 充值渠道过滤
  275. $UserID = $request->input('UserID');
  276. $accountInfo = AccountsInfo::query()->where('UserID', $UserID)->first();
  277. $channel = $accountInfo->Channel ?? 0;
  278. $switch = Redis::get("recharge_config_switch_{$channel}");
  279. if ($channel && $switch) {
  280. $channels = DB::connection('write')->table('QPPlatformDB.dbo.ChannelOpenRecharge as go')
  281. ->join('agent.dbo.admin_configs as cf', 'go.ConfigID', '=', 'cf.id')
  282. ->where('go.Channel', $channel)
  283. ->where('go.Status', 1)
  284. ->where('cf.type', 'pay_method')
  285. ->orderByDesc('go.Sort')
  286. ->select(
  287. DB::raw('cf.id as id'),
  288. 'name',
  289. 'config_value',
  290. 'new_pay_type',
  291. DB::raw('go.Sort as sort')
  292. )
  293. ->get()
  294. ->toArray();
  295. }
  296. if (empty($channels)) {
  297. $channels = DB::table('agent.dbo.admin_configs')
  298. ->select('id', 'name', 'config_value', 'new_pay_type', 'sort')
  299. ->where([
  300. 'type' => 'pay_method',
  301. 'status' => 1
  302. ])
  303. ->orderByDesc('sort')
  304. ->get()->toArray();
  305. }
  306. foreach ($monthCards as $k => $v) {
  307. $monthCards[$k]['CardID'] = $monthCards[$k]['CardID'] + 100;
  308. $monthCards[$k]['FirstReward'] = $v['FirstReward']/100;
  309. $monthCards[$k]['DayReward'] = $v['DayReward']/100;
  310. $cardChannels = [];
  311. foreach ($channels as $channel) {
  312. $channel->type = $channel->new_pay_type == 4?0: ($channel->new_pay_type == 1 ? 3 : 2);
  313. $cardChannels[] = [
  314. 'id' => $channel->id,
  315. 'name' => $channel->name,
  316. 'pic' => $channel->config_value,
  317. 'status' => 1,
  318. 'type' => $channel->new_pay_type == 4?0: ($channel->new_pay_type == 1 ? 3 : 2),
  319. 'new_pay_type' => $channel->new_pay_type,
  320. 'sort' => $channel->sort
  321. ];
  322. }
  323. usort($cardChannels, function ($a, $b) {
  324. return $a['sort'] > $b['sort'] ? -1 : 1;
  325. });
  326. $monthCards[$k]['gear'] = json_encode($cardChannels);
  327. }
  328. return apiReturnSuc(compact('list', 'monthCards'));
  329. }
  330. // 用户总充值
  331. public function userTotalRecharge(Request $request)
  332. {
  333. $UserID = $request->input('UserID');
  334. // Util::WriteLog('login_header',$request->header());
  335. try{
  336. $user_total_recharge = DB::table(TableName::QPAccountsDB() . 'YN_VIPAccount')
  337. ->where('UserID', $UserID)
  338. ->value('Recharge') ?: 0;
  339. $userInfo = DB::table(TableName::QPAccountsDB() . 'AccountsInfo')
  340. ->where('UserID', $UserID)
  341. ->first();
  342. if(isset($userInfo)) {
  343. //发VIP奖励
  344. $oldvip=$userInfo->CustomID;
  345. $vipbonus=[];
  346. $newvip=0;
  347. $vipconfig=self::getVipConfig();
  348. foreach ($vipconfig as $v) {
  349. $v=(array)$v;
  350. if($user_total_recharge>$v['MinRecharge']&&$user_total_recharge<=$v['Recharge']) {
  351. $newvip=$v['VIP'];
  352. $vipbonus[]=$v;
  353. break;
  354. }
  355. if($user_total_recharge>$v['Recharge']&&$v['VIP']>$oldvip){
  356. $vipbonus[]=$v;
  357. }
  358. }
  359. Util::WriteLog('vip',[$oldvip,$newvip,$vipbonus]);
  360. if($newvip>$oldvip) {
  361. DB::table(TableName::QPAccountsDB() . 'AccountsInfo')
  362. ->where('UserID', $UserID)
  363. ->update(['CustomID' => $newvip]);
  364. if(count($vipbonus)){
  365. foreach ($vipbonus as $vip){
  366. $amount = $vip['LevelUpBonus']*NumConfig::NUM_VALUE;
  367. $TitleString = __('messages.vip.level_up_bonus', ['vip' => $vip['VIP']]);
  368. $TextString = __('messages.vip.level_up_bonus_body', ['vip' => $vip['VIP']]);
  369. PrivateMail::sendMail(2, $UserID, $TitleString, $TextString, '30000,'.$amount, '', $amount, 3);
  370. }
  371. }
  372. }
  373. }
  374. }catch (\Exception $exception){
  375. Util::WriteLog('control_old_user',$exception);
  376. }
  377. // 引导付费标识
  378. // $guide_payment_exists = DB::table(TableName::agent() . 'guide_payment')->where('UserID', $UserID)->first();
  379. $flag = false;
  380. // 查看用户有没有充值过【只要充值过,引导付费就不能充值】
  381. if ($user_total_recharge > 0) {
  382. $flag = true;
  383. }
  384. // 引导付费礼包列表
  385. // $guidePayment = (new RechargeLogic())->guidePayment($this->JumpOut);
  386. // 日期 -- 倒计时【小时】
  387. $countdown = 24;
  388. return apiReturnSuc(['user_total_recharge'=>$user_total_recharge,'is_pay' => $flag, 'countdown' => $countdown, 'guidePayment' => []]);
  389. // $AccountWithDrawInfo = DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
  390. // ->lock('WITH(NOLOCK)')
  391. // ->where('UserID', $UserID)
  392. // ->first();
  393. //
  394. //
  395. // if (!$AccountWithDrawInfo) {
  396. // $cpf = $this->_getNewCpf($UserID);
  397. // if($cpf){
  398. // $dao = new AccountPayInfo();
  399. // $data = [
  400. // 'PixType' => 1,
  401. // 'BankUserName' => $dao->randUserName(0),
  402. // 'PixNum' => $cpf,
  403. // 'UserID' => $UserID,
  404. // 'Achieves' => 0,
  405. // 'HistoryWithDraw' => 0
  406. // ];
  407. //
  408. // try {
  409. // DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
  410. // ->where('UserID', $UserID)
  411. // ->insert($data);
  412. //
  413. // try {
  414. // DB::table(TableName::QPRecordDB() . 'RecordBankCardBind')
  415. // ->insert([
  416. // 'UserID' => $UserID,
  417. // 'BankCard' => $cpf,
  418. // 'BindDate' => now()
  419. // ]);
  420. // Util::WriteLog('update_pix',[
  421. // 'UserID' => $UserID,
  422. // 'BankCard' => $cpf,
  423. // 'BindDate' => now()
  424. // ]);
  425. // } catch (\Exception $e) {
  426. // Log::error('insert RecordBankCardBind failed', ['data' => $data]);
  427. // }
  428. //
  429. // } catch (\Exception $e) {
  430. // Log::error('insert AccountWithDrawInfo failed', ['data' => $data]);
  431. // }
  432. // }
  433. //
  434. // } else {
  435. // if(!$AccountWithDrawInfo->BankUserName || !$AccountWithDrawInfo->PixNum){
  436. //
  437. // $cpf = $this->_getNewCpf($UserID);
  438. //
  439. // if($cpf){
  440. // $dao = new AccountPayInfo();
  441. // $data = [
  442. // 'PixType' => 1,
  443. // 'BankUserName' => $dao->randUserName(0),
  444. // 'PixNum' => $cpf,
  445. // 'UserID' => $UserID,
  446. // 'Achieves' => 0,
  447. // 'HistoryWithDraw' => 0
  448. // ];
  449. //
  450. // DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
  451. // ->where('UserID', $UserID)
  452. // ->update($data);
  453. // }
  454. //
  455. //
  456. // }
  457. // }
  458. }
  459. private function _getNewCpf($UserID){
  460. return 0;
  461. $where[] = ['payment_code', 'cashPay'];
  462. $where[] = ['pay_status', 1];
  463. $where[] = ['UserID', $UserID];
  464. $build_sql = DB::connection('write')->table('order')
  465. ->where($where)
  466. ->first();
  467. if($build_sql){
  468. $cashPay = new CashPayLogic();
  469. return $cashPay->search($build_sql->order_sn);
  470. }
  471. return 0;
  472. }
  473. }