RankController.php 19 KB

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