123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- /**
- * Created by PhpStorm.
- * User: guanxl
- * Date: 2018/9/7
- * Time: 14:07
- */
-
- namespace App\Http\Controllers;
-
- use App\Services\CookieService;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- use App\Libs\Curl;
-
- class AppController
- {
- public function index(Request $request){
- Log::info('1232');
- $token = $request->get("token");
- $data = Redis::hGetAll("oauth:".$token);
- if($data){
- CookieService::setOauth($data);
- header("location:http://activity.test.care001.cn/brainstorming?activity_id=709");
- }
- }
-
- /**
- * 根据code获取网页授权access_token
- */
- public function getAccessTokenByCode($code) {
- $uri = sprintf("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", env("WX_APP_ID"), env("WX_APP_SECRET"), $code);
- Log::info($uri);
- $curl = new Curl($uri);
- $curl->httpHeader('Content-Type', 'application/json');
- $res = $curl -> httpGet();
- $resData = json_decode($res, true);
-
- if(isset($resData['access_token'])){
- return $resData;
- }
- return null;
- }
-
- /**
- * 根据openid和access_token获取用户信息,前提是scope = snsapi_userinfo
- */
- public function getUserInfo(Request $request) {
- $code = $request -> get("code");
- $activity_id = $request -> get("activity_id");
- $access = $this->getAccessTokenByCode($code);
-
- if(empty($access)) {
- return null;
- }
-
- $uri = sprintf("https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN", $access['access_token'], $access['openid']);
-
- $curl = new Curl($uri);
- $curl->httpHeader('Content-Type', 'application/json');
- $res = $curl -> httpGet();
- $resData = json_decode($res, true);
-
- if(isset($resData['openid'])){
- $data["oauth_open_id"] = $resData['openid'];
- $data["oauth_avatar"] = $resData['headimgurl'];
- $data["oauth_nickname"] = $resData['nickname'];
- $data["oauth_platform"] = "wechat";
- $data["oauth_guid"] = $resData['openid'];
- CookieService::setOauth($data);
- }
- header("location:" . env("WEB_URL") . "?activity_id=$activity_id");
- }
- }
|