EnrollController.php 11 KB

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