123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- <?php
- namespace App\Http\Controllers;
- use App\Jobs\Dwbs\ImportOrderJob;
- use App\Jobs\TongbuOrderJob;
- use App\Models\HuiFu\QJEnroll;
- use App\Models\Season;
- use App\Models\ZBS\BonusLog;
- use App\Models\ZBS\InteGift;
- use App\Models\ZBS\InteGiftOrder;
- use App\Models\ZBS\InteUser;
- use Carbon\Carbon;
- use Illuminate\Encryption\Encrypter;
- use Illuminate\Http\Request;
- use App\Models\User;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Crypt;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class RankController extends Controller{
- public function Rank(Request $request){
- $user=InteUser::where('integral','>','0');
- $ids=User::where('level',3)->pluck('id');
- if (Auth::user()->level==3){
- $where=function ($q)use($ids){
- $q->whereIn('user_id',$ids);
- };
- }else{
- $where=function ($q)use($ids){
- $q->whereNotIn('user_id',$ids);
- };
- }
- $total=$user->where($where)->count();
- $page_index=$request->input('page_index');
- $page_size=$request->input('page_size');
- $list=$user->where($where)->with(['user:id,headimgurl,nickname'])->select('integral','user_id','rank')
- ->OrderBy('rank')
- // ->OrderByDesc('id')
- ->take($page_size)
- ->skip(($page_index-1)*$page_size)->get();
- $data=[];
- foreach ($list as $k=>$v){
- $data[]=[
- 'nickname'=>$v->user->nickname,
- 'avatar'=>$v->user->headimgurl,
- 'integral'=>$v->integral,
- 'rank'=>$v->rank,
- ];
- }
- return $this->success_list($data,'',$total);
- }
- /*获取用户是否报名*/
- public function GetUserSignStatus(){
- $user=Auth::user();
- $signstatus=false;
- $status=true;
- $time=null;
- $qjUser=QJEnroll::where('user_id',$user->id)->where('is_pay',1)->first();
- if ($qjUser){
- $signstatus=true;
- $time=$qjUser->created_at->toDateTimeString();
- }
- if (Carbon::now()->toDateTimeString()>'2024-11-16 00:00:00'){
- $status=false;
- }
- return $this->success(['signstatus'=>$signstatus,'status'=>$status,'time'=>$time]);
- }
- /*获取用户基本*/
- public function GetUserInfo(){
- $users=Auth::user();
- $user=User::find($users->id);
- if ($user->status==1){
- return $this->error(20014,'','用户已冻结!');
- }
- if ($user->deleted_at){
- return $this->error(20014,'','用户已冻结!');
- }
- $inteuser=InteUser::where('user_id',$users->id)->first();
- if (!$inteuser){
- $rank=0;
- $integral=0;
- }else{
- $rank=$inteuser->rank;
- $integral=$inteuser->integral;
- }
- // $season=Season::where('is_ing',1)->first();
- $data=[
- 'nickname'=>$user->nickname,
- 'avatar'=>$user->headimgurl,
- 'tag'=>$user->level,
- 'integral'=>$integral,
- 'season'=>48,
- 'start_time'=>'2024-11-18 00:00:00',
- 'end_time'=>'2024-12-09 00:00:00',
- 'rank'=>$rank
- ];
- return $this->success($data);
- }
- public function GetInte(Request $request){
- $inte=InteUser::where('user_id',Auth::user()->id)->value('integral');
- if(!$inte){
- $inte=0;
- }
- return $this->success(['integral'=>$inte]);
- }
- public function address(Request $request){
- try {
- $address=InteUser::where('user_id',Auth::user()->id)->first();
- if ($address->address_info){
- $re=Crypt::decryptString($address->address_info);
- return $this->success( json_decode($re));
- }
- $data=[
- 'username'=>'',
- 'mobile'=>'',
- 'province'=>'',
- 'city'=>'',
- 'area'=>'',
- 'address'=>'',
- ];
- return $this->success($data);
- }catch (\Exception $exception){
- $data=[
- 'username'=>'',
- 'mobile'=>'',
- 'province'=>'',
- 'city'=>'',
- 'area'=>'',
- 'address'=>'',
- ];
- return $this->success($data);
- }
- }
- public function UpdateAddAddress(Request $request){
- $address=InteUser::where('user_id',Auth::user()->id)->first();
- if (!$address){
- return $this->error(50021,'','不在兑换名单内!');
- }
- $data=[
- 'username'=>$request->input('username'),
- 'mobile'=>$request->input('mobile'),
- 'province'=>$request->input('province'),
- 'city'=>$request->input('city'),
- 'area'=>$request->input('area'),
- 'address'=>$request->input('address'),
- ];
- $str=Crypt::encryptString(json_encode($data));
- $address->address_info=$str;
- $address->save();
- return $this->success($data);
- }
- //礼品详情
- public function gift_list(Request $request)
- {
- // $idarr=[816,10134,10135,10136,19646,31072, 31073, 31074, 31075, 31076, 32229, 32419, 55489, 55490, 66751, 81486, 214445, 218633, 248080,
- //249089,39516];
- if (time()<strtotime('2024-12-12 08:59:59') || time()>strtotime('2024-12-12 17:00:00')){
- return $this->success_list([]);
- }
- // if (!in_array(Auth::user()->id,$idarr)){
- // return $this->success_list([]);
- // }
- $gift_data = InteGift::orderByDesc('gift_num')->get();
- foreach ($gift_data as $k => $v) {
- if (!Cache::has('GIFT_NUMS::' . $v->id)) {
- Cache::remember('GIFT_NUMS::' . $v->id, Carbon::now()->addDays(2), function () use ($v) {
- return $v->exchange_num;
- });//获取当前礼品的已兑换量
- }
- if (!Cache::has('GIFT_TOTAL::' . $v->id)) {
- Cache::remember('GIFT_TOTAL::' . $v->id, Carbon::now()->addDays(2), function () use ($v) {
- return $v->gift_num;
- });//获取当前礼品的总量
- }
- $exchange_num = Cache::get('GIFT_NUMS::' . $v->id);
- $data[$k]['id'] = $v->id;//礼品ID
- $data[$k]['name'] = $v->name;//礼品名称
- $data[$k]['price'] = $v->integral;//礼品所需积分
- $data[$k]['img'] = $v->imgurl;//礼品图片
- $data[$k]['surplus'] = $v->gift_num - $exchange_num;//礼品剩余数量
- if ($data[$k]['surplus'] <= 0) {
- $data[$k]['surplus'] = 0;//礼品剩余数量
- }
- $data[$k]['total'] = $v->gift_num;//礼品数量
- }
- return $this->success_list($data);
- }
- //礼品兑换
- public function exchange(Request $request)
- {
- if (time()<strtotime('2024-12-12 08:59:59') || time()>strtotime('2024-12-12 17:00:00')){
- return $this->error(50021, '', '当前时间未在兑换时间内');
- }
- $address=InteUser::where('user_id',Auth::user()->id)->first();
- if (!$address){
- return $this->error(50021,'','请先在礼品列表页面填写地址!');
- }else{
- if (!$address->address_info){
- return $this->error(50021,'','请先在礼品列表页面填写地址!');
- }
- }
- if (!$address){
- return $this->error(50021,'','请重新登录!');
- }
- // if (time() < 0) {
- // return $this->error(50021, '', '兑换已经结束');
- // }
- $data = $request->all();
- $gift = Cache::remember('GIFT::' . $data['gift_id'], Carbon::now()->addDays(1), function () use ($data) {
- return InteGift::where('id', $data['gift_id'])->first();
- });
- $users = InteUser::where('user_id',Auth::user()->id)->first();
- if (($request->num) * $gift->integral > $users->integral || $users->integral == 0) {
- return $this->error(600, '', '学分不足');//表示当前用户的奖学金不够进行兑换
- }
- /*
- * 下面涉及到缓存
- * **/
- $num = Cache::get('GIFT_NUMS::' . $data['gift_id']);//获取当前礼品的已兑换量
- $totalNums = Cache::get('GIFT_TOTAL::' . $data['gift_id']);//获取当前礼品的中总量
- if ($num >= $totalNums) {//判断是否已经兑换完毕
- return $this->error(300, '', '库存不足');
- }
- Cache::increment('GIFT_NUMS::' . $data['gift_id'], $data['num']);//增加缓存记录的已兑换量
- $num = Cache::get('GIFT_NUMS::' . $data['gift_id']);
- if ($num > $totalNums) {
- //做个记录
- Cache::decrement('GIFT_NUMS::' . $data['gift_id'], $data['num']);//减少缓存记录的已兑换量
- return $this->error(300, '', '库存不足');
- }
- DB::beginTransaction();
- try {
- $gifts = InteGift::where('id', $request->gift_id)->first();
- $data['integral'] = $gift->integral;
- $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);
- $data['uid'] = $users->id;
- $data['order_number'] = $code;
- $res = InteGiftOrder::where('type', 0)->where('gift_id', $request->gift_id)->where('uid', $users->id)->first();
- if ($res) {
- $res->num += $request->num;
- if (!$res->save()) {
- throw new \Exception('1');
- }
- } else {
- $gift_order = new InteGiftOrder();
- $gift_order->uid = $data['uid'];
- $gift_order->order_number = $data['order_number'];
- $gift_order->gift_id = $data['gift_id'];
- $gift_order->integral = $data['integral'];
- $gift_order->num = $data['num'];
- $gift_order->name = $gift->name;
- $gift_order->type = 0;
- if (!$gift_order->save()) {
- throw new \Exception(1);
- }
- }
- $users->integral -= $gift->integral * $request->num;
- if (!$users->save()) {
- throw new \Exception(1);
- }
- $gifts->exchange_num += $request->num;
- if (!$gifts->save()) {
- throw new \Exception(1);
- }
- $bonusLog = new BonusLog();
- $bonusLog->integral_then = $users->integral;
- $bonusLog->remark = $gift->name . '*' . $data['num'];
- $bonusLog->integral = '-' . $data['integral'] * $data['num'];
- $bonusLog->title = '礼品兑换';
- $bonusLog->user_id = $users->id;
- if (!$bonusLog->save()) {
- throw new \Exception(1);
- }
- DB::commit();
- return $this->success([]);
- } catch (\Exception $exception) {
- DB::rollBack();
- Cache::decrement('GIFT_NUMS::' . $data['gift_id'], $data['num']);//减少缓存记录的已兑换量
- Log::error($exception);
- return $this->error(40025, '', '兑换失败请重试!');
- }
- }
- //兑换详情
- public function record_detail(Request $request)
- {
- // return $this->success_list([],'',0);
- $user=InteUser::where('user_id',Auth::user()->id)->first();
- $gift_order = InteGiftOrder::where('uid', $user->id)->with(['get_gift'])->where('type', 0)->get();
- $num = InteGiftOrder::where('uid', $user->id)->sum('num');
- $data = [];
- if ($gift_order) {
- foreach ($gift_order as $k => $v) {
- // $gift=DB::table('gift')->where('id',$v->gift_id)->first();
- $data[$k]['name'] = $v->name;
- $data[$k]['gift_info'] = $v->get_gift;
- $data[$k]['imgurl'] = $v->get_gift->imgurl;
- $data[$k]['url'] = $v->get_gift->imgurl;
- $data[$k]['img'] = $v->get_gift->imgurl;
- $data[$k]['num'] = $v->num;
- // $data[$k]['cover']=$v->get_gift->cover;
- $data[$k]['id'] = $v->id;
- $data[$k]['type'] = $v->type;
- $data[$k]['cost'] = $v->integral * $v->num;
- $data[$k]['timestamp'] = strtotime($v->created_at) * 1000;
- }
- }
- return $this->success_list($data,'',$num);
- }
- /*
- * 作废订单
- * ***/
- public function destory_order(Request $request)
- {
- if (time() > Carbon::parse('2024-12-12 17:00:00')->timestamp) {
- return $this->error(50021, '', '兑换已经结束,不能作废订单');
- }
- $id = $request->input('user_id');
- DB::beginTransaction();
- try {
- $user = InteUser::where('user_id',Auth::user()->id)->first();
- $gift_order = InteGiftOrder::where('id', $request->gift_id)->first();
- if ($gift_order->type == 1) {
- return $this->error('50024', '', '该订单已经作废,请返回重进');
- }
- $gift = InteGift::where('id', $gift_order->gift_id)->first();
- $gift_order->type = 1;
- if (!$gift_order->save()) {
- throw new \Exception(1);
- }
- $user->integral += $gift_order->num * $gift_order->integral;
- if (!$user->save()) {
- throw new \Exception(1);
- }
- $gift->exchange_num -= $gift_order->num;
- if (!$gift->save()) {
- throw new \Exception(1);
- }
- $bonusLog = new BonusLog();
- $bonusLog->integral_then = $user->integral;
- $bonusLog->remark = $gift_order->name . '*' . $gift_order->num;
- $bonusLog->integral = '+' . $gift_order->integral * $gift_order->num;
- $bonusLog->title = '`取消兑换`';
- $bonusLog->user_id = $user->id;
- if (!$bonusLog->save()) {
- throw new \Exception(1);
- }
- DB::commit();
- Cache::decrement('GIFT_NUMS::' . $gift_order->gift_id, $gift_order->num);//减少缓存记录的已兑换量
- return $this->success([]);
- } catch (\Exception $exception) {
- DB::rollBack();
- return $this->error(40021, '', '作废订单失败请重试');
- }
- }
- public function data_or(Request $request){
- $allow=$request->header('access-control-allow-origin:');
- $ip_arr=[
- '8.210.68.53', '8.217.7.212', '8.210.126.117', '47.243.206.78', '8.210.170.223'
- ];
- $real_ip=$request->header('ali-cdn-real-ip');
- if (!in_array($real_ip,$ip_arr)){
- return $this->error(50001,$real_ip,'请求错误!');
- }
- $data=$request->input('info');
- DB::connection('mysql_zb')
- ->table('data_or')
- ->insert(['data'=>$data,'created_at'=>Carbon::now()->toDateTimeString()]);
- $id=DB::connection('mysql_zb')
- ->table('data_or')->max('id');
- $customKey = base64_decode('RUFBOTI3OTdGNEVEQTgwOUQyM0YzMUZDRDY1MTIwRkM='); // 从第一个项目获取的密钥(需要解码)
- $encryptedData = $data; // 加密后的数据
- // 使用自定义密钥创建加密器实例
- $encrypter = new Encrypter($customKey, config('app.cipher')); // cipher 通常是 AES-256-CBC
- // 尝试解密数据
- try {
- $decryptedData = $encrypter->decryptString($encryptedData);
- dispatch(new TongbuOrderJob(['data'=>$decryptedData,'id'=>$id]));
- // TongbuOrderJob::dispatch(['data'=>$decryptedData]);
- } catch (\Exception $exception) {
- Log("解密失败: " . $exception->getMessage());
- }
- return $this->success();
- }
- public function data_orls(Request $request){
- $allow=$request->header('access-control-allow-origin:');
- // $ip_arr=[
- // '8.210.68.53', '8.217.7.212', '8.210.126.117', '47.243.206.78', '8.210.170.223'
- // ];
- // $real_ip=$request->header('ali-cdn-real-ip');
- // if (!in_array($real_ip,$ip_arr)){
- // return $this->error(50001,$real_ip,'请求错误!');
- // }
- $data_in=$request->input('info');
- // Log::error($data_in);
- DB::connection('mysql_zb')
- ->table('data_or_ls')
- ->insert(['data'=>$data_in]);
- $customKey = base64_decode('RUFBOTI3OTdGNEVEQTgwOUQyM0YzMUZDRDY1MTIwRkM='); // 从第一个项目获取的密钥(需要解码)
- $encryptedData = $data_in; // 加密后的数据
- // 使用自定义密钥创建加密器实例
- $encrypter = new Encrypter($customKey, config('app.cipher')); // cipher 通常是 AES-256-CBC
- // 尝试解密数据
- try {
- $decryptedData = $encrypter->decryptString($encryptedData);
- $data=json_decode($decryptedData,true);
- $datas=$data['data'];
- $datas['Code']=$data['Code'];
- $datas['data']=$datas['SalesSku'];
- unset($datas['SalesSku']);
- // Log::error($datas);
- dispatch(new ImportOrderJob(json_encode($datas)));
- } catch (\Exception $exception) {
- Log("解密失败: " . $exception->getMessage());
- }
- $data=json_decode($decryptedData,true);
- $datas=$data['data'];
- $datas['Code']=$data['Code'];
- $datas['data']=$datas['SalesSku'];
- unset($datas['SalesSku']);
- return $this->success();
- }
- public function data_oruser(Request $request){
- $allow=$request->header('access-control-allow-origin:');
- $ip_arr=[
- '8.210.68.53', '8.217.7.212', '8.210.126.117', '47.243.206.78', '8.210.170.223'
- ];
- $real_ip=$request->header('ali-cdn-real-ip');
- if (!in_array($real_ip,$ip_arr)){
- return $this->error(50001,$real_ip,'请求错误!');
- }
- $data=$request->input('info');
- DB::connection('mysql_zb')
- ->table('data_or_user')
- ->insert(['data'=>$data,'created_at'=>Carbon::now()->toDateTimeString()]);
- return $this->success();
- }
- public function Access_token(Request $request){
- $allow=$request->header('access-control-allow-origin:');
- $data=$request->input('info');
- DB::connection('mysql_zb')
- ->table('data_or')
- ->insert(['data'=>$data]);
- return $this->success();
- }
- //礼品详情
- public function giftCache(Request $request)
- {
- $gift_data = InteGift::get();
- foreach ($gift_data as $k => $v) {
- // if (!Cache::has('GIFT_NUMS::' . $v->id)) {
- // Cache::remember('GIFT_NUMS::' . $v->id, Carbon::now()->addDays(3), function () use ($v) {
- // return $v->exchange_num;
- // });//获取当前礼品的已兑换量
- // }
- // if (!Cache::has('GIFT_TOTAL::' . $v->id)) {
- // Cache::remember('GIFT_TOTAL::' . $v->id, Carbon::now()->addDays(3), function () use ($v) {
- // return $v->gift_num;
- // });//获取当前礼品的总量
- // }
- dump($v->name.Cache::get('GIFT_TOTAL::' . $v->id).'===='.Cache::get('GIFT_NUMS::' . $v->id).'===='.$v->gift_num);
- }
- }
- }
|