|
|
@ -4,6 +4,7 @@ namespace App\Http\Controllers\Weixin; |
|
|
|
use App\Http\Controllers\Weixin\CommonController; |
|
|
|
use Illuminate\Http\Request; |
|
|
|
use App\Common\ReturnCode; |
|
|
|
use App\Common\ReturnData; |
|
|
|
use App\Common\WechatAuth; |
|
|
|
use App\Common\Helper; |
|
|
|
|
|
|
@ -27,7 +28,7 @@ class UserController extends CommonController |
|
|
|
$res = curl_request($url,$postdata,'GET'); |
|
|
|
$data['user_info'] = $res['data']; |
|
|
|
|
|
|
|
if($res['code'] != ReturnCode::SUCCESS_CODE){unset($_SESSION['weixin_user_info']);$this->error_jump('请先登录',route('weixin_login'));} |
|
|
|
if($res['code'] != ReturnData::SUCCESS){unset($_SESSION['weixin_user_info']);$this->error_jump('请先登录',route('weixin_login'));} |
|
|
|
|
|
|
|
return view('weixin.user.index', $data); |
|
|
|
} |
|
|
@ -509,7 +510,7 @@ class UserController extends CommonController |
|
|
|
$url = env('APP_API_URL')."/user_goods_history_delete"; |
|
|
|
$res = curl_request($url,$postdata,'POST'); |
|
|
|
|
|
|
|
if($res['code'] != ReturnCode::SUCCESS_CODE){$this->error_jump(ReturnCode::FAIL);} |
|
|
|
if($res['code'] != ReturnData::SUCCESS){$this->error_jump(ReturnCode::FAIL);} |
|
|
|
|
|
|
|
$this->success_jump(ReturnCode::SUCCESS); |
|
|
|
} |
|
|
@ -523,7 +524,7 @@ class UserController extends CommonController |
|
|
|
$url = env('APP_API_URL')."/user_goods_history_clear"; |
|
|
|
$res = curl_request($url,$postdata,'POST'); |
|
|
|
|
|
|
|
if($res['code'] != ReturnCode::SUCCESS_CODE){$this->error_jump(ReturnCode::FAIL);} |
|
|
|
if($res['code'] != ReturnData::SUCCESS){$this->error_jump(ReturnCode::FAIL);} |
|
|
|
|
|
|
|
$this->success_jump(ReturnCode::SUCCESS); |
|
|
|
} |
|
|
@ -531,43 +532,39 @@ class UserController extends CommonController |
|
|
|
//微信网页授权登录
|
|
|
|
public function oauth(Request $request) |
|
|
|
{ |
|
|
|
$wechat_auth = new WechatAuth(sysconfig('CMS_WX_APPID'),sysconfig('CMS_WX_APPSECRET')); |
|
|
|
|
|
|
|
// 获取code码,用于和微信服务器申请token。 注:依据OAuth2.0要求,此处授权登录需要用户端操作
|
|
|
|
if(!isset($_GET['code'])) |
|
|
|
if (!isset($_SESSION['weixin_oauth']['userinfo'])) |
|
|
|
{ |
|
|
|
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://'; |
|
|
|
$callback_url = $http_type . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; //回调地址,当前页面
|
|
|
|
//生成唯一随机串防CSRF攻击
|
|
|
|
$state = md5(uniqid(rand(), true)); |
|
|
|
$_SESSION['weixin_oauth']['state'] = $state; //存到SESSION
|
|
|
|
$authorize_url = $wechat_auth->get_authorize_url($callback_url, $state); |
|
|
|
$wechat_auth = new WechatAuth(sysconfig('CMS_WX_APPID'),sysconfig('CMS_WX_APPSECRET')); |
|
|
|
|
|
|
|
header("Location: $authorize_url");exit; |
|
|
|
} |
|
|
|
|
|
|
|
// 依据code码去获取openid和access_token,自己的后台服务器直接向微信服务器申请即可
|
|
|
|
if (isset($_GET['code'])) |
|
|
|
{ |
|
|
|
// 获取code码,用于和微信服务器申请token。 注:依据OAuth2.0要求,此处授权登录需要用户端操作
|
|
|
|
if(!isset($_GET['code'])) |
|
|
|
{ |
|
|
|
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://'; |
|
|
|
$callback_url = $http_type . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; //回调地址,当前页面
|
|
|
|
//生成唯一随机串防CSRF攻击
|
|
|
|
$state = md5(uniqid(rand(), true)); |
|
|
|
$_SESSION['weixin_oauth']['state'] = $state; //存到SESSION
|
|
|
|
$authorize_url = $wechat_auth->get_authorize_url($callback_url, $state); |
|
|
|
|
|
|
|
header("Location: $authorize_url");exit; |
|
|
|
} |
|
|
|
|
|
|
|
// 依据code码去获取openid和access_token,自己的后台服务器直接向微信服务器申请即可
|
|
|
|
$_SESSION['weixin_oauth']['code'] = $_GET['code']; |
|
|
|
|
|
|
|
if($_GET['state'] != $_SESSION['weixin_oauth']['state']) |
|
|
|
{ |
|
|
|
exit("您访问的页面不存在或已被删除!"); |
|
|
|
$this->error_jump('您访问的页面不存在或已被删除'); |
|
|
|
} |
|
|
|
|
|
|
|
//得到 access_token 与 openid
|
|
|
|
$_SESSION['weixin_oauth']['token'] = $wechat_auth->get_access_token($_GET['code']); |
|
|
|
} |
|
|
|
|
|
|
|
// 依据申请到的access_token和openid,申请Userinfo信息。
|
|
|
|
if (isset($_SESSION['weixin_oauth']['token'])) |
|
|
|
{ |
|
|
|
// 依据申请到的access_token和openid,申请Userinfo信息。
|
|
|
|
$_SESSION['weixin_oauth']['userinfo'] = $wechat_auth->get_user_info($_SESSION['weixin_oauth']['token']['access_token'], $_SESSION['weixin_oauth']['token']['openid']); |
|
|
|
} |
|
|
|
|
|
|
|
$postdata = array( |
|
|
|
'openid' => $_SESSION['weixin_oauth']['token']['openid'], |
|
|
|
'openid' => $_SESSION['weixin_oauth']['userinfo']['openid'], |
|
|
|
'unionid' => isset($_SESSION['weixin_oauth']['userinfo']['unionid']) ? $_SESSION['weixin_oauth']['userinfo']['unionid'] : '', |
|
|
|
'nickname' => $_SESSION['weixin_oauth']['userinfo']['nickname'], |
|
|
|
'sex' => $_SESSION['weixin_oauth']['userinfo']['sex'], |
|
|
@ -578,11 +575,9 @@ class UserController extends CommonController |
|
|
|
); |
|
|
|
$url = env('APP_API_URL')."/wx_oauth_register"; |
|
|
|
$res = curl_request($url,$postdata,'POST'); |
|
|
|
|
|
|
|
if($res['code'] != ReturnCode::SUCCESS_CODE){$this->error_jump('系统错误');} |
|
|
|
if($res['code'] != ReturnData::SUCCESS){$this->error_jump('系统错误');} |
|
|
|
|
|
|
|
$_SESSION['weixin_user_info'] = $res['data']; |
|
|
|
|
|
|
|
header('Location: '.route('weixin_user'));exit; |
|
|
|
} |
|
|
|
|
|
|
@ -617,7 +612,7 @@ class UserController extends CommonController |
|
|
|
$url = env('APP_API_URL')."/wx_login"; |
|
|
|
$res = curl_request($url,$postdata,'POST'); |
|
|
|
|
|
|
|
if($res['code'] != ReturnCode::SUCCESS_CODE){$this->error_jump('登录失败');} |
|
|
|
if($res['code'] != ReturnData::SUCCESS){$this->error_jump('登录失败');} |
|
|
|
|
|
|
|
$_SESSION['weixin_user_info'] = $res['data']; |
|
|
|
|
|
|
|