has('openid')) { if ($request->openid != 123) { $user = User::where('openid', $request->openid)->first(); if (!$user) { if ($request->has('id')) { Log::error($request->get('openid') . '<==>' . '{TEST}' . $request->get('id')); } return $this->response()->array([ 'code' => 300, 'msg' => '没有绑定手机号', ]); } if ($user->township == null) { if ($request->has('latitude')) { if ($request->latitude != '') { $key='926f74d687d39c232ca67606a93fcefa'; $lat=$request->latitude; $lon=$request->longitude; $arr=['lon'=>$lon,'lat'=>$lat,'ver'=>1]; $url='https://api.tianditu.gov.cn/geocoder?postStr='.json_encode($arr,true).'&type=geocode&tk='.$key; $client = new Client(); $response = $client->request('GET', $url); $ad = json_decode($response->getBody()->getContents()); Log::error('{--weizhi--}' . json_encode($ad)); try { if ($ad->status == 0 && $ad->msg == 'ok') { $s['province'] = $ad->result->addressComponent->province; $s['city'] = $ad->result->addressComponent->city == null ? $ad->result->addressComponent->county : $ad->result->addressComponent->city; $s['area'] = $ad->result->addressComponent->county; $s['township'] = $ad->result->addressComponent->address; DB::table('user')->where('openid', $request->openid)->update($s); } } catch (\Exception $exception) { Log::error('(-----位置出错----)' . $request->openid); } } } } $openid = ['openid' => $request->openid]; $token = Auth::guard('api')->fromUser($user); $token = ['token' => $token]; $user_info = $this->user_info($user->id);//获取用户的基本信息 $user_status = $handler->judege();//获取此时处于哪个阶段 $user_status = ['status' => $user_status]; $user_data = array_merge($user_info, $user_status, $token, $openid); return $this->response()->array([ 'code' => 200, 'msg' => 'ok', 'data' => $user_data ]); } else { Log::error('{-------第一步----}'); $code = $request->code; Log::error('{-------第二步----}' . $code); $header_type=$request->header('type'); Log::error('headerType'.$header_type); if ($header_type){ $config = [ 'app_id' => 'wxa9d1325fd98f5a7d', 'secret' => 'b8cd4371804815530a1e1134e83a9488', 'log' => [ 'level' => 'debug', 'file' => __DIR__.'/wechat.log', ], ]; }else{ $config = [ 'app_id' => 'wx0348d5a266f867a3', 'secret' => 'c383d63a939a0d686e7b13b284a00bec', 'log' => [ 'level' => 'debug', 'file' => __DIR__.'/wechat.log', ], ]; } $miniProgram=Factory::miniProgram($config); //下面两句加上就正常了 // $cache = new \Symfony\Component\Cache\Simple\FilesystemCache('', 0, storage_path('framework/cache/data')); // $miniProgram->rebind('cache', $cache); // $miniProgram=EasyWeChat::miniProgram(); // $miniProgram = app('wechat.mini_program'); $data = $miniProgram->auth->session($code); // Log::error(); Log::error('{-------第三步----}' . json_encode($data)); if (isset($data['errcode'])) { return $this->error(401,$data); } Log::error('{-------第四步----}' . $data['openid']); if ($header_type){ $id=$request->input('HeaderID'); if ($id){ $user=User::find($id); if (!$user){ return $this->response()->array([ 'code' => 300, 'msg' => '没有绑定手机号', 'data' => [ 'openid' => $data['openid'] ] ]); } $user->openid=$data['openid']; $user->save(); }else{ $user = User::where('openid', $data['openid'])->first(); $user1=[]; } }else{ $user = User::where('openid', $data['openid'])->first(); // Log::error('{-------第五步----}' . json_encode($user)); if (isset($data['unionid'])){ $user1 = DB::connection('mysql_dwbs')->table('users')->where('unionid',$data['unionid'])->whereBetween('cert_status',[3,6])->first(); }else{ $user1=[]; } } if (!$user && !$user1) { return $this->response()->array([ 'code' => 300, 'msg' => '没有绑定手机号', 'data' => [ 'openid' => $data['openid'] ] ]); } if (!$user){ $userss=User::where('phone',$user1->mobile)->first(); if ($userss){ return $this->response()->array([ 'code' => 300, 'msg' => '没有绑定手机号', 'data' => [ 'openid' => $data['openid'] ] ]); } $user=new User(); $user->openid=$data['openid']; $user->unionid=$data['unionid']; $user->phone=$user1->mobile; $user->dwbs_id=$user1->id; if ($user1->level!=3){ $a_user=DB::connection('mysql_dwbs')->table('users')->where('id',$user1->agent_id)->first(); if ($a_user->level!=3){ $a_user=DB::connection('mysql_dwbs')->table('users')->where('id',$a_user->agent_id)->first(); } $re=DB::table('service')->where('phone',$a_user->mobile)->first(); $user->war_names=$re->war_names; }else{ $re=DB::table('service')->where('phone',$user1->mobile)->first(); $user->war_names=$re->war_names; } $user->avatar=$user1->headimgurl; $user->nickname=$user1->nickname; $user->save(); $user=User::find($user->id); }else{ try{ if ($request->input('nickname')){ $user->nickname=$request->input('nickname'); $user->avatar=$request->input('avatar'); $user->save(); } }catch (\Exception $exception){ } } try{ if ($user->township == null) { if ($request->has('latitude')) { if ($request->latitude != '') { $key='926f74d687d39c232ca67606a93fcefa'; $lat=$request->latitude; $lon=$request->longitude; $arr=['lon'=>$lon,'lat'=>$lat,'ver'=>1]; $url='https://api.tianditu.gov.cn/geocoder?postStr='.json_encode($arr,true).'&type=geocode&tk='.$key; $client = new Client(); $response = $client->request('GET', $url); $ad = json_decode($response->getBody()->getContents()); try { if ($ad->status == 0 && $ad->msg == 'ok') { $s['province'] = $ad->result->addressComponent->province; $s['city'] = $ad->result->addressComponent->city == null ? $ad->result->addressComponent->county : $ad->result->addressComponent->city; $s['area'] = $ad->result->addressComponent->county; $s['township'] = $ad->result->addressComponent->address; DB::table('user')->where('openid', $data['openid'])->update($s); } } catch (\Exception $exception) { Log::error('(-----位置出错----)' . $data['openid']); } } } } }catch (\Exception $exception){ Log::error('保存地址出错======'.$exception); } try{ if (isset($data['unionid'])){ $user->unionid=$data['unionid']; $user->save(); } }catch (\Exception $exception){ Log::error('保存Unionid出错'); } // DB::table('user')->where('id', $user->id)->update(['avatar' => $request->avatar, 'nickname' => $request->nickname]); $token = Auth::guard('api')->fromUser($user); $openid = ['openid' => $user->openid]; $token = ['token' => $token]; $user_info = $this->user_info($user->id);//获取用户的基本信息 $user_status = $handler->judege();//获取此时处于哪个阶段 $user_status = ['status' => $user_status]; $user_data = array_merge($user_info, $user_status, $token, $openid); return $this->response()->array([ 'code' => 200, 'message' => 'ok', 'data' => $user_data ]); } } } public function user_info($id) { $handler = new JudegeHandler(); $time = Carbon::today();//现在的时间 $basic=DB::table('basic')->get(); foreach ($basic as $k=>$v){ $re[$v->keys]=$v->value; } $season=$re['season']; $now = Carbon::now()->timestamp; $user_data = DB::table('user')->where('id', $id)->first();//登录用户的信息 $enrol_count=Enroll::where('uid',$id)->where('status',0)->count(); $signup = DB::table('enroll')->where([['uid', '=', $id], ['season', '=', $re['season']], ['status', '=', 0]])->first();//当前用户是否报名 $count = DB::table('enroll')->join('user','user.id','=','enroll.uid')->where([ ['enroll.season', '=', $re['season']], ['enroll.status', '=', 0]])->count();//当前用户是否报名 $banner_1 = $re['banner_1']; $banner_2 = $re['banner_2']; $banner_3 = $re['banner_3']; $banner_4 = $re['banner_4']; $round_end = $re['round_end_time']; $repair_start = $re['repair_start']; $repair_end = $re['repair_end']; $money = $re['cost']; $day=ceil(($round_end-$now)/(3600*24)); $key=Cache::get(md5($id)); $link=''; if ($signup){ if (!$key){ $t=$now-$re['round_start_time']; if ($t>0&&$t<3600*24*7*3){ if ($t<3600*24*7){ $week=1; }elseif (3600*24*7<$t&&$t<3600*24*7*2){ $week=2; }elseif (3600*24*7*2<$t&&$t<3600*24*7*3){ $week=3; } $re=DB::table('course_img')->where('season',$season)->where('week',$week)->first(); if ($re){ if ($re->course_img){ $link=$re->course_img; } } }else{ $re=DB::table('course_img')->where('season',$season)->where('week',0)->first(); if ($re){ if ($re->course_img){ $link=$re->course_img; } } } } } if ($id==20){ $re=DB::table('course_img')->where('season',$season)->where('week',3)->first(); if ($re){ if ($re->course_img){ $link=$re->course_img; } } } $upload_status=false; if (mb_strlen($user_data->phone)<11){ if ($signup){ $upload_status=true; } } $s=0; if ($user_data->phone==18333333333){ $s=0; }else{ if($user_data->dwbs_id){ $dw=DWBSUser::where('id',$user_data->dwbs_id)->first(); if (!$dw){ $s=1; } }else{ $s=1; } } $user_back = [ 'id' => $user_data->id, 'phone' => $user_data->phone, 'user_status' => $s, 'enrol_count'=>$enrol_count, 'name' => $user_data->nickname, 'avatar' => $user_data->avatar, 'super_name' => '', 'season' => (int)$season, 'start_time' => (int)$basic[2]->value * 1000, 'end_time' => (int)($round_end * 1000), 'money' => $money, 'day' => $day, 'banner_1' => $banner_1, 'banner_2' => $banner_2, 'banner_3' => $banner_3, 'banner_4' => $banner_4, 'repair_start' => (int)$repair_start, 'repair_end' => (int)$repair_end, 'count' => (int)$count, 'link'=>$link, 'upload_status' => $upload_status, ];//需要返回用户信息的一部分 if (!$signup) { $da = [ 'signuped' => false, 'grade' => '', 'progress' => 0, 'total' => 0, 'enroll_time' =>0, 'sales' => 0, 'rank' => 0, ];//未报名用户返回的部分信息 } else { $user_sign = [ 'signuped' => true, 'enroll_time' => strtotime($signup->created_at)*1000, ];//报名用户返回的部分信息 $da = $handler->grade(); $da = array_merge($user_sign, $da); } $da = array_merge($user_back, $da);//返回用户信息 return $da; } public function weixinUrl(){ $client=new Client(); $config = [ 'app_id' => 'wxa9d1325fd98f5a7d', 'secret' => 'b8cd4371804815530a1e1134e83a9488', 'log' => [ 'level' => 'debug', 'file' => __DIR__.'/wechat.log', ], ]; $miniProgram=Factory::miniProgram($config); $access_tokken=$miniProgram->access_token; $url='https://api.weixin.qq.com/wxa/query_urllink?access_token='.$access_tokken; $re=$client->post($url); dd($re); } }