EnrollController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Handlers\ExpressHandler;
  4. use App\Handlers\UploadFilesHandler;
  5. use App\Models\Enroll;
  6. use App\Models\User;
  7. use Carbon\Carbon;
  8. use EasyWeChat\Factory;
  9. use EasyWeChatComposer\EasyWeChat;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Auth;
  12. use Illuminate\Support\Facades\Cache;
  13. use Illuminate\Support\Facades\Crypt;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Log;
  16. class EnrollController extends Controller{
  17. public function pays(Request $request)
  18. {
  19. $now=Carbon::now()->timestamp;
  20. $time=strtotime('2025-02-10 10:00:00');
  21. $time2=strtotime('2025-02-11 18:05:00');
  22. if (!Cache::get('UUIOENROLL::'.Auth::user()->id)){
  23. $nums=Cache::get('SignUserNum');
  24. if ($nums>1950){
  25. return $this->error(50014,'','报名已满!');
  26. }
  27. }
  28. //时间限制
  29. // $arr=['39516',10135,81486,55490,810,31074,31076,816,31072,32419,66751,31075,10136,32229,10134,218633,214445];
  30. $arr=DB::table('enroll_yun')->pluck('user_id')->toArray();
  31. if (!in_array(Auth::user()->id,$arr)){
  32. return $this->error(50214,'','报名还未开始,请等待客服通知!');
  33. // if ($now<$time){
  34. // return $this->error(50214,'','报名还未开始,请等待客服通知!');
  35. // }
  36. // if ($now>$time2){
  37. // return $this->error(50214,'','报名时间已过!');
  38. // }
  39. }
  40. // if ($now<$time){
  41. // return $this->error(50214,'','报名还未开始,请等待客服通知!');
  42. // }
  43. // if ($now>$time2){
  44. // return $this->error(50214,'','报名时间已过!');
  45. // }
  46. // $create_time=User::where('id',Auth::user()->id)->value('created_at');
  47. // if ($create_time>'2024-12-09 00:00:00'){
  48. // return $this->error(50021,'','您的注册时间不满60天,无法报名线下实战营。');
  49. // }
  50. //
  51. //// 连续参加限制
  52. if (Auth::user()->level==2){
  53. // //年龄限制
  54. $cre_num=Auth::user()->cre_num;
  55. if ($cre_num){
  56. try {
  57. $cre_nums=Crypt::decryptString($cre_num);
  58. $year=mb_substr($cre_nums,6,4);
  59. $new_year=Carbon::now()->year;
  60. if (($new_year-$year)>61){
  61. return $this->error(50021,'','您的身份证年龄已超过公司规定,无法报名线下实战营。');
  62. }
  63. }catch (\Exception $exception){
  64. }
  65. }
  66. // $re=DB::table('enroll_limit')->where('uuid',Auth::user()->id)->first();
  67. // if ($re){
  68. // return $this->error(50021,'','您已经参加过前两届');
  69. // }
  70. }
  71. //已经报名,返回刷新
  72. $enroll=Enroll::where('season',72)->where('user_id',Auth::user()->id)->first();
  73. if ($enroll){
  74. return $this->error(50213,'','你已经成功获取名额,请重新进入');
  75. }
  76. //没有获取到名额
  77. $lock=Cache::lock('foo',4);
  78. try {
  79. $lock->block(3);
  80. $num=Cache::increment('SignUserNum',1);
  81. optional($lock)->release();
  82. // //名额是否超限制
  83. if ($num>1950){
  84. return $this->error(50014,'','报名已满!');
  85. }
  86. }catch (\Exception $exception){
  87. return $this->error(50014,'','报名已满!');
  88. }
  89. Cache::put('UUIOENROLL::'.Auth::user()->id,1,Carbon::now()->addDays(2));
  90. //没有超过限制则进行支付
  91. DB::beginTransaction();
  92. try{
  93. // $options = $this->options();
  94. // $payment = Factory::payment($options);
  95. // $jssdk = $payment->jssdk;
  96. $cost=599;
  97. // $cost=599;
  98. $user=Auth::user();
  99. $expire=date('YmdHis', time() + 130);
  100. $nu = time() . str_pad($user->id, 6, 0, STR_PAD_LEFT) . random_int(11111111, 99999999);
  101. // $attributes = [
  102. // 'time_expire' => $expire,
  103. // 'trade_type' => 'JSAPI', // 支付方式,小程序支付使用JSAPI
  104. // 'body' => '第63届大卫博士线上创业实战营', // 订单说明
  105. // 'detail' => '第63届大卫博士线上创业实战营直播报名订单支付', // 订单说明
  106. // 'out_trade_no' => $nu, // 自定义订单号
  107. // 'total_fee' =>$cost*100, // 单位:分
  108. // 'openid' => $user->openid, // 当前用户的openId
  109. //// 'notify_url' =>'http://api.app.cliu.cc/api/Enroll/message',
  110. // ];
  111. // $result = $payment->order->unify($attributes);
  112. // if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  113. $enroll=new Enroll();
  114. $enroll->user_id = $user->id;
  115. $enroll->season = 72;
  116. $enroll->type = 0;
  117. $enroll->enroll_no = $nu;
  118. $enroll->cost = $cost;
  119. $enroll->is_pay = 0;
  120. $enroll->redis_num = $num;
  121. if (!$enroll->save()){
  122. throw new \Exception('1');
  123. }
  124. // $prepayId = $result['prepay_id'];
  125. // $config = $jssdk->sdkConfig($prepayId);
  126. $config=[];
  127. DB::commit();
  128. return response()->json([
  129. 'code'=>200,
  130. 'msg'=>'成功',
  131. 'data'=>$config
  132. ]);
  133. // }
  134. }catch (\Exception $exception){
  135. DB::rollBack();
  136. Log::error($exception->getMessage());
  137. return $this->error(500214,'','报名失败,请重试');
  138. }
  139. }
  140. public function GetStatus(){
  141. return $this->success(['status'=>1]);
  142. $options = $this->options();
  143. $payment = Factory::payment($options);
  144. $id=Auth::user()->id;
  145. $res = Enroll::where('user_id', $id)->where('season', 72)->orderByDesc('created_at')->get();
  146. $s = false;
  147. foreach ($res as $k => $v) {
  148. $re = $payment->order->queryByOutTradeNumber($v->enroll_no);
  149. if (isset($re['transaction_id'])) {
  150. $enroll = Enroll::find($v->id);
  151. $enroll->is_pay = 1;
  152. $enroll->trade_no = $re['transaction_id'];
  153. $enroll->save();
  154. $s = true;
  155. }
  156. }
  157. if ($s) {
  158. Log::error('测试测试');
  159. return $this->success(['status'=>1]);
  160. } else {
  161. return $this->success(['status'=>0]);
  162. }
  163. }
  164. public function message(Request $request)
  165. {
  166. Log::error('支付回调111');
  167. $options = $this->options();
  168. $payment = Factory::payment($options);
  169. $response = $payment->handlePaidNotify(function ($message, $fail) {
  170. $out_trade_no = $message['out_trade_no']; // 商户订单号
  171. $trade_no = $message['transaction_id']; // 微信支付订单号
  172. $order = Enroll::where('enroll_no', '=', $out_trade_no)->first();
  173. if (empty($order)) {
  174. return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
  175. }
  176. $r = Cache::get(md5('codes' . $order->user_id));
  177. if (!$r) {
  178. if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  179. // 用户是否支付成功
  180. if ($message['result_code'] === 'SUCCESS') {
  181. $enroll = Enroll::where('enroll_no', '=', $out_trade_no)->first();
  182. $enroll->is_pay = 1;
  183. $enroll->trade_no = $trade_no;
  184. $enroll->save();
  185. }
  186. } else {
  187. return $fail('通信失败,请稍后再通知我');
  188. }
  189. }
  190. return true;
  191. });
  192. return $response;
  193. }
  194. protected function options(){
  195. return [
  196. 'app_id' => config('wechat.payment.hulian.app_id'),//config('wechat.payment.default.app_id'),
  197. 'mch_id' => config('wechat.payment.hulian.mch_id'),
  198. 'sub_mch_id' => '',
  199. 'key' => config('wechat.payment.hulian.key'),
  200. 'notify_url' =>url('api/Enroll/message'),
  201. 'cert_path' =>config('wechat.payment.hulian.cert_path'),
  202. 'key_path' =>config('wechat.payment.hulian.key_path'),
  203. ];
  204. }
  205. public function GetEnrollInfo(){
  206. $enroll_start_time='2025-02-10 14:00:00';
  207. $enroll_end_time='2025-02-11 18:00:00';
  208. $live_start_time='2025-02-18 08:00:00';
  209. $live_end_time='2025-02-19 18:00:00';
  210. $re=Enroll::where('user_id',Auth::user()->id)->first();
  211. $path='';
  212. if ($re){
  213. $pay_status=0;
  214. $status=1;
  215. if ($re->is_pay==1){
  216. $pay_status=1;
  217. $path=$re->message;
  218. }
  219. }else{
  220. $status=0;
  221. $pay_status=0;
  222. }
  223. // $season=1;
  224. $season=72;
  225. $limit_num=1950;
  226. $cost=599;
  227. if (!Cache::has('SignUserNum')){
  228. Cache::forever('SignUserNum',0);
  229. }
  230. $data=['start_time'=>$live_start_time,
  231. 'end_time'=>$live_end_time,
  232. 'enroll_start_time'=>$enroll_start_time,
  233. 'enroll_end_time'=>$enroll_end_time,
  234. 'season'=>$season,'cost'=>$cost,
  235. 'limit_num'=>$limit_num,'num'=>Cache::get('SignUserNum'),
  236. 'status'=>$status,'pay_status'=>$pay_status,'path'=>$path
  237. ];
  238. return $this->success($data);
  239. }
  240. public function GetAddress(){
  241. try {
  242. $id=Auth::user()->id;
  243. $address=Enroll::where('user_id',$id)->first();
  244. if ($address){
  245. if ($address->address_info){
  246. $re=Crypt::decryptString($address->address_info);
  247. $arr=json_decode($re,true);
  248. $arr['mobile']=mb_substr($arr['mobile'],0,3).'****'.mb_substr($arr['mobile'],'-4');
  249. // $arr['address']='*******';
  250. return $this->success($arr);
  251. }
  252. }else{
  253. return $this->error(500214,'','当前用户未报名');
  254. }
  255. $data=[
  256. 'username'=>'',
  257. 'mobile'=>'',
  258. 'province'=>'',
  259. 'city'=>'',
  260. 'area'=>'',
  261. 'address'=>'',
  262. ];
  263. return $this->success($data);
  264. }catch (\Exception $exception){
  265. $data=[
  266. 'username'=>'',
  267. 'mobile'=>'',
  268. 'province'=>'',
  269. 'city'=>'',
  270. 'area'=>'',
  271. 'address'=>'',
  272. ];
  273. return $this->success($data);
  274. }
  275. }
  276. public function UpdateAddAddress(Request $request){
  277. $address=Enroll::where('user_id',Auth::user()->id)->first();
  278. if (!$address){
  279. return $this->error(50021,'','当前用户没有报名!');
  280. }
  281. $data=[
  282. 'username'=>$request->input('username'),
  283. 'mobile'=>$request->input('mobile'),
  284. 'province'=>$request->input('province'),
  285. 'city'=>$request->input('city'),
  286. 'area'=>$request->input('area'),
  287. 'address'=>$request->input('address'),
  288. ];
  289. $str=Crypt::encryptString(json_encode($data));
  290. $address->address_info=$str;
  291. $address->save();
  292. return $this->success($data);
  293. }
  294. public function text_notify_url(){
  295. }
  296. }