123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- <?php
- namespace App\Http\Controllers;
- use App\Handlers\ExpressHandler;
- use App\Handlers\UploadFilesHandler;
- use App\Models\Enroll;
- use App\Models\User;
- use Carbon\Carbon;
- use EasyWeChat\Factory;
- use EasyWeChatComposer\EasyWeChat;
- use Illuminate\Http\Request;
- 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 EnrollController extends Controller{
- public function pays(Request $request)
- {
- $now=Carbon::now()->timestamp;
- $time=strtotime('2024-11-12 10:00:00');
- $time2=strtotime('2024-11-12 13:00:00');
- if (!Cache::get('UUIOENROLL::'.Auth::user()->id)){
- $nums=Cache::get('SignUserNum');
- if ($nums>1950){
- return $this->error(50014,'','报名已满!');
- }
- }
- //时间限制
- // $arr=['39516',10135,81486,55490,810,31074,31076,816,31072,32419,66751,31075,10136,32229,10134,218633,214445];
- $arr=DB::table('enroll_yun')->pluck('user_id')->toArray();
- if (!in_array(Auth::user()->id,$arr)){
- return $this->error(50214,'','报名还未开始,请等待客服通知!');
- if ($now<$time){
- return $this->error(50214,'','报名还未开始,请等待客服通知!');
- }
- if ($now>$time2){
- return $this->error(50214,'','报名时间已过!');
- }
- }
- // $create_time=User::where('id',Auth::user()->id)->value('created_at');
- // if ($create_time>'2024-09-12 00:00:00'){
- // return $this->error(50021,'','您的注册时间不满60天,无法报名线下实战营。');
- // }
- //
- // //年龄限制
- // $cre_num=Auth::user()->cre_num;
- // if ($cre_num){
- // try {
- // $cre_nums=Crypt::decryptString($cre_num);
- // $year=mb_substr($cre_nums,6,4);
- // $new_year=Carbon::now()->year;
- // if (($new_year-$year)>60){
- // return $this->error(50021,'','您的身份证年龄已超过公司规定,无法报名线下实战营。');
- // }
- // }catch (\Exception $exception){
- //
- // }
- // }
- //// 连续参加限制
- // if (Auth::user()->level==2){
- // $re=DB::table('enroll_limit')->where('uuid',Auth::user()->id)->first();
- // if ($re){
- // return $this->error(50021,'','您已经参加过前两届');
- // }
- // }
- //已经报名,返回刷新
- $enroll=Enroll::where('season',71)->where('user_id',Auth::user()->id)->first();
- if ($enroll){
- return $this->error(50213,'','你已经成功获取名额,请重新进入');
- }
- //没有获取到名额
- $lock=Cache::lock('foo',4);
- try {
- $lock->block(3);
- $num=Cache::increment('SignUserNum',1);
- optional($lock)->release();
- // //名额是否超限制
- if ($num>1950){
- return $this->error(50014,'','报名已满!');
- }
- }catch (\Exception $exception){
- return $this->error(50014,'','报名已满!');
- }
- Cache::put('UUIOENROLL::'.Auth::user()->id,1,Carbon::now()->addDays(2));
- //没有超过限制则进行支付
- DB::beginTransaction();
- try{
- // $options = $this->options();
- // $payment = Factory::payment($options);
- // $jssdk = $payment->jssdk;
- $cost=599;
- // $cost=599;
- $user=Auth::user();
- $expire=date('YmdHis', time() + 130);
- $nu = time() . str_pad($user->id, 6, 0, STR_PAD_LEFT) . random_int(11111111, 99999999);
- // $attributes = [
- // 'time_expire' => $expire,
- // 'trade_type' => 'JSAPI', // 支付方式,小程序支付使用JSAPI
- // 'body' => '第63届大卫博士线上创业实战营', // 订单说明
- // 'detail' => '第63届大卫博士线上创业实战营直播报名订单支付', // 订单说明
- // 'out_trade_no' => $nu, // 自定义订单号
- // 'total_fee' =>$cost*100, // 单位:分
- // 'openid' => $user->openid, // 当前用户的openId
- //// 'notify_url' =>'http://api.app.cliu.cc/api/Enroll/message',
- // ];
- // $result = $payment->order->unify($attributes);
- // if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
- $enroll=new Enroll();
- $enroll->user_id = $user->id;
- $enroll->season = 71;
- $enroll->type = 0;
- $enroll->enroll_no = $nu;
- $enroll->cost = $cost;
- $enroll->is_pay = 0;
- $enroll->redis_num = $num;
- if (!$enroll->save()){
- throw new \Exception('1');
- }
- // $prepayId = $result['prepay_id'];
- // $config = $jssdk->sdkConfig($prepayId);
- $config=[];
- DB::commit();
- return response()->json([
- 'code'=>200,
- 'msg'=>'成功',
- 'data'=>$config
- ]);
- // }
- }catch (\Exception $exception){
- DB::rollBack();
- Log::error($exception->getMessage());
- return $this->error(500214,'','报名失败,请重试');
- }
- }
- public function GetStatus(){
- return $this->success(['status'=>1]);
- $options = $this->options();
- $payment = Factory::payment($options);
- $id=Auth::user()->id;
- $res = Enroll::where('user_id', $id)->where('season', 71)->orderByDesc('created_at')->get();
- $s = false;
- foreach ($res as $k => $v) {
- $re = $payment->order->queryByOutTradeNumber($v->enroll_no);
- if (isset($re['transaction_id'])) {
- $enroll = Enroll::find($v->id);
- $enroll->is_pay = 1;
- $enroll->trade_no = $re['transaction_id'];
- $enroll->save();
- $s = true;
- }
- }
- if ($s) {
- Log::error('测试测试');
- return $this->success(['status'=>1]);
- } else {
- return $this->success(['status'=>0]);
- }
- }
- public function message(Request $request)
- {
- Log::error('支付回调111');
- $options = $this->options();
- $payment = Factory::payment($options);
- $response = $payment->handlePaidNotify(function ($message, $fail) {
- $out_trade_no = $message['out_trade_no']; // 商户订单号
- $trade_no = $message['transaction_id']; // 微信支付订单号
- $order = Enroll::where('enroll_no', '=', $out_trade_no)->first();
- if (empty($order)) {
- return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
- }
- $r = Cache::get(md5('codes' . $order->user_id));
- if (!$r) {
- if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- // 用户是否支付成功
- if ($message['result_code'] === 'SUCCESS') {
- $enroll = Enroll::where('enroll_no', '=', $out_trade_no)->first();
- $enroll->is_pay = 1;
- $enroll->trade_no = $trade_no;
- $enroll->save();
- }
- } else {
- return $fail('通信失败,请稍后再通知我');
- }
- }
- return true;
- });
- return $response;
- }
- protected function options(){
- return [
- 'app_id' => config('wechat.payment.hulian.app_id'),//config('wechat.payment.default.app_id'),
- 'mch_id' => config('wechat.payment.hulian.mch_id'),
- 'sub_mch_id' => '',
- 'key' => config('wechat.payment.hulian.key'),
- 'notify_url' =>url('api/Enroll/message'),
- 'cert_path' =>config('wechat.payment.hulian.cert_path'),
- 'key_path' =>config('wechat.payment.hulian.key_path'),
- ];
- }
- public function GetEnrollInfo(){
- $enroll_start_time='2024-11-12 10:00:00';
- $enroll_end_time='2024-11-19 18:00:00';
- $live_start_time='2024-11-19 08:00:00';
- $live_end_time='2024-11-20 18:00:00';
- $re=Enroll::where('user_id',Auth::user()->id)->first();
- $path='';
- if ($re){
- $pay_status=0;
- $status=1;
- if ($re->is_pay==1){
- $pay_status=1;
- $path=$re->message;
- }
- }else{
- $status=0;
- $pay_status=0;
- }
- // $season=1;
- $season=71;
- $limit_num=1950;
- $cost=599;
- if (!Cache::has('SignUserNum')){
- Cache::forever('SignUserNum',0);
- }
- $data=['start_time'=>$live_start_time,
- 'end_time'=>$live_end_time,
- 'enroll_start_time'=>$enroll_start_time,
- 'enroll_end_time'=>$enroll_end_time,
- 'season'=>$season,'cost'=>$cost,
- 'limit_num'=>$limit_num,'num'=>Cache::get('SignUserNum'),
- 'status'=>$status,'pay_status'=>$pay_status,'path'=>$path
- ];
- return $this->success($data);
- }
- public function GetAddress(){
- try {
- $id=Auth::user()->id;
- $address=Enroll::where('user_id',$id)->first();
- if ($address){
- if ($address->address_info){
- $re=Crypt::decryptString($address->address_info);
- $arr=json_decode($re,true);
- $arr['mobile']=mb_substr($arr['mobile'],0,3).'****'.mb_substr($arr['mobile'],'-4');
- // $arr['address']='*******';
- return $this->success($arr);
- }
- }else{
- return $this->error(500214,'','当前用户未报名');
- }
- $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=Enroll::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 text_notify_url(){
- }
- }
|