RankController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Jobs\Dwbs\ImportOrderJob;
  4. use App\Jobs\TongbuOrderJob;
  5. use App\Jobs\TongbuOrderNewJob;
  6. use App\Models\HuiFu\QJEnroll;
  7. use App\Models\Season;
  8. use App\Models\ZBS\BonusLog;
  9. use App\Models\ZBS\InteGift;
  10. use App\Models\ZBS\InteGiftOrder;
  11. use App\Models\ZBS\InteUser;
  12. use Carbon\Carbon;
  13. use Illuminate\Encryption\Encrypter;
  14. use Illuminate\Http\Request;
  15. use App\Models\User;
  16. use Illuminate\Support\Facades\Auth;
  17. use Illuminate\Support\Facades\Cache;
  18. use Illuminate\Support\Facades\Crypt;
  19. use Illuminate\Support\Facades\DB;
  20. use Illuminate\Support\Facades\Log;
  21. class RankController extends Controller{
  22. public function Rank(Request $request){
  23. $user=InteUser::where('integral','>','0');
  24. $ids=User::where('level',3)->pluck('id');
  25. if (Auth::user()->level==3){
  26. $where=function ($q)use($ids){
  27. $q->whereIn('user_id',$ids);
  28. };
  29. }else{
  30. $where=function ($q)use($ids){
  31. $q->whereNotIn('user_id',$ids);
  32. };
  33. }
  34. $total=$user->where($where)->count();
  35. $page_index=$request->input('page_index');
  36. $page_size=$request->input('page_size');
  37. $list=$user->where($where)->with(['user:id,headimgurl,nickname'])->select('integral','user_id','rank')
  38. ->OrderBy('rank')
  39. // ->OrderByDesc('id')
  40. ->take($page_size)
  41. ->skip(($page_index-1)*$page_size)->get();
  42. $data=[];
  43. foreach ($list as $k=>$v){
  44. $data[]=[
  45. 'nickname'=>$v->user->nickname,
  46. 'avatar'=>$v->user->headimgurl,
  47. 'integral'=>$v->integral,
  48. 'rank'=>$v->rank,
  49. ];
  50. }
  51. return $this->success_list($data,'',$total);
  52. }
  53. /*获取用户是否报名*/
  54. public function GetUserSignStatus(){
  55. $user=Auth::user();
  56. $signstatus=false;
  57. $status=true;
  58. $time=null;
  59. $qjUser=QJEnroll::where('user_id',$user->id)->where('is_pay',1)->first();
  60. if ($qjUser){
  61. $signstatus=true;
  62. $time=$qjUser->created_at->toDateTimeString();
  63. }
  64. if (Carbon::now()->toDateTimeString()>'2024-11-16 00:00:00'){
  65. $status=false;
  66. }
  67. return $this->success(['signstatus'=>$signstatus,'status'=>$status,'time'=>$time]);
  68. }
  69. /*获取用户基本*/
  70. public function GetUserInfo(){
  71. $users=Auth::user();
  72. $user=User::find($users->id);
  73. if ($user->status==1){
  74. return $this->error(20014,'','用户已冻结!');
  75. }
  76. if ($user->deleted_at){
  77. return $this->error(20014,'','用户已冻结!');
  78. }
  79. $inteuser=InteUser::where('user_id',$users->id)->first();
  80. if (!$inteuser){
  81. $rank=0;
  82. $integral=0;
  83. }else{
  84. $rank=$inteuser->rank;
  85. $integral=$inteuser->integral;
  86. }
  87. // $season=Season::where('is_ing',1)->first();
  88. $data=[
  89. 'nickname'=>$user->nickname,
  90. 'avatar'=>$user->headimgurl,
  91. 'tag'=>$user->level,
  92. 'integral'=>$integral,
  93. 'season'=>48,
  94. 'start_time'=>'2024-11-18 00:00:00',
  95. 'end_time'=>'2024-12-09 00:00:00',
  96. 'rank'=>$rank
  97. ];
  98. return $this->success($data);
  99. }
  100. public function GetInte(Request $request){
  101. $inte=InteUser::where('user_id',Auth::user()->id)->value('integral');
  102. if(!$inte){
  103. $inte=0;
  104. }
  105. return $this->success(['integral'=>$inte]);
  106. }
  107. public function address(Request $request){
  108. try {
  109. $address=InteUser::where('user_id',Auth::user()->id)->first();
  110. if ($address->address_info){
  111. $re=Crypt::decryptString($address->address_info);
  112. return $this->success( json_decode($re));
  113. }
  114. $data=[
  115. 'username'=>'',
  116. 'mobile'=>'',
  117. 'province'=>'',
  118. 'city'=>'',
  119. 'area'=>'',
  120. 'address'=>'',
  121. ];
  122. return $this->success($data);
  123. }catch (\Exception $exception){
  124. $data=[
  125. 'username'=>'',
  126. 'mobile'=>'',
  127. 'province'=>'',
  128. 'city'=>'',
  129. 'area'=>'',
  130. 'address'=>'',
  131. ];
  132. return $this->success($data);
  133. }
  134. }
  135. public function UpdateAddAddress(Request $request){
  136. $address=InteUser::where('user_id',Auth::user()->id)->first();
  137. if (!$address){
  138. return $this->error(50021,'','不在兑换名单内!');
  139. }
  140. $data=[
  141. 'username'=>$request->input('username'),
  142. 'mobile'=>$request->input('mobile'),
  143. 'province'=>$request->input('province'),
  144. 'city'=>$request->input('city'),
  145. 'area'=>$request->input('area'),
  146. 'address'=>$request->input('address'),
  147. ];
  148. $str=Crypt::encryptString(json_encode($data));
  149. $address->address_info=$str;
  150. $address->save();
  151. return $this->success($data);
  152. }
  153. //礼品详情
  154. public function gift_list(Request $request)
  155. {
  156. // $idarr=[816,10134,10135,10136,19646,31072, 31073, 31074, 31075, 31076, 32229, 32419, 55489, 55490, 66751, 81486, 214445, 218633, 248080,
  157. //249089,39516];
  158. if (time()<strtotime('2024-12-12 08:59:59') || time()>strtotime('2024-12-12 17:00:00')){
  159. return $this->success_list([]);
  160. }
  161. // if (!in_array(Auth::user()->id,$idarr)){
  162. // return $this->success_list([]);
  163. // }
  164. $gift_data = InteGift::orderByDesc('gift_num')->get();
  165. foreach ($gift_data as $k => $v) {
  166. if (!Cache::has('GIFT_NUMS::' . $v->id)) {
  167. Cache::remember('GIFT_NUMS::' . $v->id, Carbon::now()->addDays(2), function () use ($v) {
  168. return $v->exchange_num;
  169. });//获取当前礼品的已兑换量
  170. }
  171. if (!Cache::has('GIFT_TOTAL::' . $v->id)) {
  172. Cache::remember('GIFT_TOTAL::' . $v->id, Carbon::now()->addDays(2), function () use ($v) {
  173. return $v->gift_num;
  174. });//获取当前礼品的总量
  175. }
  176. $exchange_num = Cache::get('GIFT_NUMS::' . $v->id);
  177. $data[$k]['id'] = $v->id;//礼品ID
  178. $data[$k]['name'] = $v->name;//礼品名称
  179. $data[$k]['price'] = $v->integral;//礼品所需积分
  180. $data[$k]['img'] = $v->imgurl;//礼品图片
  181. $data[$k]['surplus'] = $v->gift_num - $exchange_num;//礼品剩余数量
  182. if ($data[$k]['surplus'] <= 0) {
  183. $data[$k]['surplus'] = 0;//礼品剩余数量
  184. }
  185. $data[$k]['total'] = $v->gift_num;//礼品数量
  186. }
  187. return $this->success_list($data);
  188. }
  189. //礼品兑换
  190. public function exchange(Request $request)
  191. {
  192. if (time()<strtotime('2024-12-12 08:59:59') || time()>strtotime('2024-12-12 17:00:00')){
  193. return $this->error(50021, '', '当前时间未在兑换时间内');
  194. }
  195. $address=InteUser::where('user_id',Auth::user()->id)->first();
  196. if (!$address){
  197. return $this->error(50021,'','请先在礼品列表页面填写地址!');
  198. }else{
  199. if (!$address->address_info){
  200. return $this->error(50021,'','请先在礼品列表页面填写地址!');
  201. }
  202. }
  203. if (!$address){
  204. return $this->error(50021,'','请重新登录!');
  205. }
  206. // if (time() < 0) {
  207. // return $this->error(50021, '', '兑换已经结束');
  208. // }
  209. $data = $request->all();
  210. $gift = Cache::remember('GIFT::' . $data['gift_id'], Carbon::now()->addDays(1), function () use ($data) {
  211. return InteGift::where('id', $data['gift_id'])->first();
  212. });
  213. $users = InteUser::where('user_id',Auth::user()->id)->first();
  214. if (($request->num) * $gift->integral > $users->integral || $users->integral == 0) {
  215. return $this->error(600, '', '学分不足');//表示当前用户的奖学金不够进行兑换
  216. }
  217. /*
  218. * 下面涉及到缓存
  219. * **/
  220. $num = Cache::get('GIFT_NUMS::' . $data['gift_id']);//获取当前礼品的已兑换量
  221. $totalNums = Cache::get('GIFT_TOTAL::' . $data['gift_id']);//获取当前礼品的中总量
  222. if ($num >= $totalNums) {//判断是否已经兑换完毕
  223. return $this->error(300, '', '库存不足');
  224. }
  225. Cache::increment('GIFT_NUMS::' . $data['gift_id'], $data['num']);//增加缓存记录的已兑换量
  226. $num = Cache::get('GIFT_NUMS::' . $data['gift_id']);
  227. if ($num > $totalNums) {
  228. //做个记录
  229. Cache::decrement('GIFT_NUMS::' . $data['gift_id'], $data['num']);//减少缓存记录的已兑换量
  230. return $this->error(300, '', '库存不足');
  231. }
  232. DB::beginTransaction();
  233. try {
  234. $gifts = InteGift::where('id', $request->gift_id)->first();
  235. $data['integral'] = $gift->integral;
  236. $code = time() . str_pad($users->id, 4, 0, STR_PAD_LEFT) . str_pad($gift->id, 4, 0, STR_PAD_LEFT) . str_pad(random_int(1, 999999), 6, 0, STR_PAD_LEFT);
  237. $data['uid'] = $users->id;
  238. $data['order_number'] = $code;
  239. $res = InteGiftOrder::where('type', 0)->where('gift_id', $request->gift_id)->where('uid', $users->id)->first();
  240. if ($res) {
  241. $res->num += $request->num;
  242. if (!$res->save()) {
  243. throw new \Exception('1');
  244. }
  245. } else {
  246. $gift_order = new InteGiftOrder();
  247. $gift_order->uid = $data['uid'];
  248. $gift_order->order_number = $data['order_number'];
  249. $gift_order->gift_id = $data['gift_id'];
  250. $gift_order->integral = $data['integral'];
  251. $gift_order->num = $data['num'];
  252. $gift_order->name = $gift->name;
  253. $gift_order->type = 0;
  254. if (!$gift_order->save()) {
  255. throw new \Exception(1);
  256. }
  257. }
  258. $users->integral -= $gift->integral * $request->num;
  259. if (!$users->save()) {
  260. throw new \Exception(1);
  261. }
  262. $gifts->exchange_num += $request->num;
  263. if (!$gifts->save()) {
  264. throw new \Exception(1);
  265. }
  266. $bonusLog = new BonusLog();
  267. $bonusLog->integral_then = $users->integral;
  268. $bonusLog->remark = $gift->name . '*' . $data['num'];
  269. $bonusLog->integral = '-' . $data['integral'] * $data['num'];
  270. $bonusLog->title = '礼品兑换';
  271. $bonusLog->user_id = $users->id;
  272. if (!$bonusLog->save()) {
  273. throw new \Exception(1);
  274. }
  275. DB::commit();
  276. return $this->success([]);
  277. } catch (\Exception $exception) {
  278. DB::rollBack();
  279. Cache::decrement('GIFT_NUMS::' . $data['gift_id'], $data['num']);//减少缓存记录的已兑换量
  280. Log::error($exception);
  281. return $this->error(40025, '', '兑换失败请重试!');
  282. }
  283. }
  284. //兑换详情
  285. public function record_detail(Request $request)
  286. {
  287. // return $this->success_list([],'',0);
  288. $user=InteUser::where('user_id',Auth::user()->id)->first();
  289. $gift_order = InteGiftOrder::where('uid', $user->id)->with(['get_gift'])->where('type', 0)->get();
  290. $num = InteGiftOrder::where('uid', $user->id)->sum('num');
  291. $data = [];
  292. if ($gift_order) {
  293. foreach ($gift_order as $k => $v) {
  294. // $gift=DB::table('gift')->where('id',$v->gift_id)->first();
  295. $data[$k]['name'] = $v->name;
  296. $data[$k]['gift_info'] = $v->get_gift;
  297. $data[$k]['imgurl'] = $v->get_gift->imgurl;
  298. $data[$k]['url'] = $v->get_gift->imgurl;
  299. $data[$k]['img'] = $v->get_gift->imgurl;
  300. $data[$k]['num'] = $v->num;
  301. // $data[$k]['cover']=$v->get_gift->cover;
  302. $data[$k]['id'] = $v->id;
  303. $data[$k]['type'] = $v->type;
  304. $data[$k]['cost'] = $v->integral * $v->num;
  305. $data[$k]['timestamp'] = strtotime($v->created_at) * 1000;
  306. }
  307. }
  308. return $this->success_list($data,'',$num);
  309. }
  310. /*
  311. * 作废订单
  312. * ***/
  313. public function destory_order(Request $request)
  314. {
  315. if (time() > Carbon::parse('2024-12-12 17:00:00')->timestamp) {
  316. return $this->error(50021, '', '兑换已经结束,不能作废订单');
  317. }
  318. $id = $request->input('user_id');
  319. DB::beginTransaction();
  320. try {
  321. $user = InteUser::where('user_id',Auth::user()->id)->first();
  322. $gift_order = InteGiftOrder::where('id', $request->gift_id)->first();
  323. if ($gift_order->type == 1) {
  324. return $this->error('50024', '', '该订单已经作废,请返回重进');
  325. }
  326. $gift = InteGift::where('id', $gift_order->gift_id)->first();
  327. $gift_order->type = 1;
  328. if (!$gift_order->save()) {
  329. throw new \Exception(1);
  330. }
  331. $user->integral += $gift_order->num * $gift_order->integral;
  332. if (!$user->save()) {
  333. throw new \Exception(1);
  334. }
  335. $gift->exchange_num -= $gift_order->num;
  336. if (!$gift->save()) {
  337. throw new \Exception(1);
  338. }
  339. $bonusLog = new BonusLog();
  340. $bonusLog->integral_then = $user->integral;
  341. $bonusLog->remark = $gift_order->name . '*' . $gift_order->num;
  342. $bonusLog->integral = '+' . $gift_order->integral * $gift_order->num;
  343. $bonusLog->title = '`取消兑换`';
  344. $bonusLog->user_id = $user->id;
  345. if (!$bonusLog->save()) {
  346. throw new \Exception(1);
  347. }
  348. DB::commit();
  349. Cache::decrement('GIFT_NUMS::' . $gift_order->gift_id, $gift_order->num);//减少缓存记录的已兑换量
  350. return $this->success([]);
  351. } catch (\Exception $exception) {
  352. DB::rollBack();
  353. return $this->error(40021, '', '作废订单失败请重试');
  354. }
  355. }
  356. public function data_or(Request $request){
  357. $allow=$request->header('access-control-allow-origin:');
  358. $ip_arr=[
  359. '8.210.68.53', '8.217.7.212', '8.210.126.117', '47.243.206.78', '8.210.170.223'
  360. ];
  361. $real_ip=$request->header('ali-cdn-real-ip');
  362. if (!in_array($real_ip,$ip_arr)){
  363. return $this->error(50001,$real_ip,'请求错误!');
  364. }
  365. $data=$request->input('info');
  366. DB::connection('mysql_zb')
  367. ->table('data_or')
  368. ->insert(['data'=>$data,'created_at'=>Carbon::now()->toDateTimeString()]);
  369. $id=DB::connection('mysql_zb')
  370. ->table('data_or')->max('id');
  371. $customKey = base64_decode('RUFBOTI3OTdGNEVEQTgwOUQyM0YzMUZDRDY1MTIwRkM='); // 从第一个项目获取的密钥(需要解码)
  372. $encryptedData = $data; // 加密后的数据
  373. // 使用自定义密钥创建加密器实例
  374. $encrypter = new Encrypter($customKey, config('app.cipher')); // cipher 通常是 AES-256-CBC
  375. // 尝试解密数据
  376. try {
  377. $decryptedData = $encrypter->decryptString($encryptedData);
  378. dispatch(new TongbuOrderNewJob(['data'=>$decryptedData,'id'=>$id]));
  379. // dispatch(new TongbuOrderJob(['data'=>$decryptedData,'id'=>$id]));
  380. // TongbuOrderJob::dispatch(['data'=>$decryptedData]);
  381. } catch (\Exception $exception) {
  382. Log("解密失败: " . $exception->getMessage());
  383. }
  384. return $this->success();
  385. }
  386. public function data_orls(Request $request){
  387. $allow=$request->header('access-control-allow-origin:');
  388. // $ip_arr=[
  389. // '8.210.68.53', '8.217.7.212', '8.210.126.117', '47.243.206.78', '8.210.170.223'
  390. // ];
  391. // $real_ip=$request->header('ali-cdn-real-ip');
  392. // if (!in_array($real_ip,$ip_arr)){
  393. // return $this->error(50001,$real_ip,'请求错误!');
  394. // }
  395. $data_in=$request->input('info');
  396. // Log::error($data_in);
  397. DB::connection('mysql_zb')
  398. ->table('data_or_ls')
  399. ->insert(['data'=>$data_in]);
  400. $customKey = base64_decode('RUFBOTI3OTdGNEVEQTgwOUQyM0YzMUZDRDY1MTIwRkM='); // 从第一个项目获取的密钥(需要解码)
  401. $encryptedData = $data_in; // 加密后的数据
  402. // 使用自定义密钥创建加密器实例
  403. $encrypter = new Encrypter($customKey, config('app.cipher')); // cipher 通常是 AES-256-CBC
  404. // 尝试解密数据
  405. try {
  406. $decryptedData = $encrypter->decryptString($encryptedData);
  407. $data=json_decode($decryptedData,true);
  408. $datas=$data['data'];
  409. $datas['Code']=$data['Code'];
  410. $datas['data']=$datas['SalesSku'];
  411. unset($datas['SalesSku']);
  412. // Log::error($datas);
  413. dispatch(new ImportOrderJob(json_encode($datas)));
  414. } catch (\Exception $exception) {
  415. Log("解密失败: " . $exception->getMessage());
  416. }
  417. $data=json_decode($decryptedData,true);
  418. $datas=$data['data'];
  419. $datas['Code']=$data['Code'];
  420. $datas['data']=$datas['SalesSku'];
  421. unset($datas['SalesSku']);
  422. return $this->success();
  423. }
  424. public function data_oruser(Request $request){
  425. $allow=$request->header('access-control-allow-origin:');
  426. $ip_arr=[
  427. '8.210.68.53', '8.217.7.212', '8.210.126.117', '47.243.206.78', '8.210.170.223'
  428. ];
  429. $real_ip=$request->header('ali-cdn-real-ip');
  430. if (!in_array($real_ip,$ip_arr)){
  431. return $this->error(50001,$real_ip,'请求错误!');
  432. }
  433. $data=$request->input('info');
  434. DB::connection('mysql_zb')
  435. ->table('data_or_user')
  436. ->insert(['data'=>$data,'created_at'=>Carbon::now()->toDateTimeString()]);
  437. return $this->success();
  438. }
  439. public function Access_token(Request $request){
  440. $allow=$request->header('access-control-allow-origin:');
  441. $data=$request->input('info');
  442. DB::connection('mysql_zb')
  443. ->table('data_or')
  444. ->insert(['data'=>$data]);
  445. return $this->success();
  446. }
  447. //礼品详情
  448. public function giftCache(Request $request)
  449. {
  450. $gift_data = InteGift::get();
  451. foreach ($gift_data as $k => $v) {
  452. // if (!Cache::has('GIFT_NUMS::' . $v->id)) {
  453. // Cache::remember('GIFT_NUMS::' . $v->id, Carbon::now()->addDays(3), function () use ($v) {
  454. // return $v->exchange_num;
  455. // });//获取当前礼品的已兑换量
  456. // }
  457. // if (!Cache::has('GIFT_TOTAL::' . $v->id)) {
  458. // Cache::remember('GIFT_TOTAL::' . $v->id, Carbon::now()->addDays(3), function () use ($v) {
  459. // return $v->gift_num;
  460. // });//获取当前礼品的总量
  461. // }
  462. dump($v->name.Cache::get('GIFT_TOTAL::' . $v->id).'===='.Cache::get('GIFT_NUMS::' . $v->id).'===='.$v->gift_num);
  463. }
  464. }
  465. }