From a42d1d2e267d4830c7ca9d4b0425469f4e2785b7 Mon Sep 17 00:00:00 2001 From: devil_gong Date: Mon, 27 May 2019 10:16:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E7=BD=91=E9=A1=B5=E6=8E=88?= =?UTF-8?q?=E6=9D=83=E6=94=AF=E4=BB=98=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixinwebauthorization/index/Auth.php | 182 +----------------- .../weixinwebauthorization/index/Pay.php | 78 ++++++++ .../service/{Service.php => AuthService.php} | 152 ++++++++++++++- extend/payment/Weixin.php | 6 +- 4 files changed, 238 insertions(+), 180 deletions(-) create mode 100644 application/plugins/weixinwebauthorization/index/Pay.php rename application/plugins/weixinwebauthorization/service/{Service.php => AuthService.php} (50%) diff --git a/application/plugins/weixinwebauthorization/index/Auth.php b/application/plugins/weixinwebauthorization/index/Auth.php index 0d31d7215..806a58ef5 100644 --- a/application/plugins/weixinwebauthorization/index/Auth.php +++ b/application/plugins/weixinwebauthorization/index/Auth.php @@ -11,11 +11,10 @@ namespace app\plugins\weixinwebauthorization\index; use think\Controller; -use app\service\PluginsService; -use app\plugins\weixinwebauthorization\service\Service; +use app\plugins\weixinwebauthorization\service\AuthService; /** - * 微信登录 - 登录授权 + * 微信网页授权 - 授权处理 * @author Devil * @blog http://gong.gg/ * @version 0.0.1 @@ -33,52 +32,7 @@ class Auth extends Controller */ public function Unbind($params = []) { - return Service::WeixinUnbind($params); - } - /** - * 支付提示 - * @author Devil - * @blog http://gong.gg/ - * @version 1.0.0 - * @datetime 2019-05-25T20:15:23+0800 - * @param [array] $params [输入参数] - */ - public function PayTips($params = []) - { - // 自定义链接 - $this->assign('to_url', MyUrl('index/order/index')); - $this->assign('to_title', '我的订单'); - - // 状态 - if(isset($params['status']) && $params['status'] == 0) - { - $this->assign('msg', '支付成功'); - return $this->fetch('public/tips_success'); - } else { - $this->assign('msg', '支付失败'); - return $this->fetch('public/tips_error'); - } - } - - /** - * 支付授权 - * @author Devil - * @blog http://gong.gg/ - * @version 1.0.0 - * @datetime 2019-05-25T14:44:32+0800 - * @param [array] $params [输入参数] - */ - public function Pay($params = []) - { - // 订单url处理 - if(!empty($params['id'])) - { - $url = MyUrl('index/order/detail', ['id'=>intval($params['id']), 'is_pay_auto'=>1, 'is_pay_submit'=>1]); - session('plugins_pay_order_detail_url', $url); - } - - // 调用授权 - return $this->Index($params); + return AuthService::WeixinUnbind($params); } /** @@ -91,25 +45,10 @@ class Auth extends Controller */ public function Index($params = []) { - $ret = PluginsService::PluginsData('weixinwebauthorization'); + $ret = AuthService::Auth($params]); if($ret['code'] == 0) { - // 参数校验 - if(empty($ret['data']['appid'])) - { - $this->assign('msg', 'appid未配置'); - return $this->fetch('public/tips_error'); - } - - // 回调地址 - $redirect_uri = urlencode(PluginsHomeUrl('weixinwebauthorization', 'auth', 'callback')); - - // 授权方式 - $auth_type = (isset($ret['data']['auth_type']) && $ret['data']['auth_type'] == 1) ? 'snsapi_userinfo' : 'snsapi_base'; - - // 授权code - $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$ret['data']['appid'].'&redirect_uri='.$redirect_uri.'&response_type=code&scope='.$auth_type.'&state=login#wechat_redirect'; - return redirect($url); + return redirect($ret['data']); } else { $this->assign('msg', $ret['msg']); return $this->fetch('public/tips_error'); @@ -117,7 +56,7 @@ class Auth extends Controller } /** - * 回调 + * 授权回调 * @author Devil * @blog http://gong.gg/ * @version 1.0.0 @@ -126,31 +65,7 @@ class Auth extends Controller */ public function Callback($params = []) { - // 参数校验 - if(empty($params['code'])) - { - $this->assign('msg', '授权code为空'); - return $this->fetch('public/tips_error'); - } - - // 远程获取access_token - $ret = $this->RemoteAccessToken($params); - if($ret['code'] != 0) - { - $this->assign('msg', $ret['msg']); - return $this->fetch('public/tips_error'); - } - - // 获取用户信息 - $ret = $this->UserInfo($ret['data']); - if($ret['code'] != 0) - { - $this->assign('msg', $ret['msg']); - return $this->fetch('public/tips_error'); - } - - // 处理用户数据 - $ret = Service::WeixinAuthBind($ret['data']); + $ret = AuthService::Callback($params); if($ret['code'] == 0) { // 是否订单支付授权,进入订单详情 @@ -170,88 +85,5 @@ class Auth extends Controller return $this->fetch('public/tips_error'); } } - - /** - * 获取用户信息 - * @author Devil - * @blog http://gong.gg/ - * @version 1.0.0 - * @date 2019-05-24 - * @desc description - * @param [array] $params [输入参数] - */ - private function UserInfo($params = []) - { - // 参数校验 - if(empty($params['access_token'])) - { - return DataReturn('access_token为空', -1); - } - if(empty($params['openid'])) - { - return DataReturn('openid为空', -1); - } - - // 获取用户详细信息 - $url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$params['access_token'].'&openid='.$params['openid'].'&lang=zh_CN'; - $data = json_decode(file_get_contents($url), true); - if(empty($data['openid'])) - { - if(empty($data['errmsg'])) - { - return DataReturn('获取用户信息失败', -100); - } else { - return DataReturn($data['errmsg'], -100); - } - } - return DataReturn('获取成功', 0, $data); - } - - /** - * 远程获取access_token - * @author Devil - * @blog http://gong.gg/ - * @version 1.0.0 - * @date 2019-05-24 - * @desc description - * @param [array] $params [输入参数] - */ - private function RemoteAccessToken($params = []) - { - $ret = PluginsService::PluginsData('weixinwebauthorization'); - if($ret['code'] == 0) - { - // 参数校验 - if(empty($ret['data']['appid'])) - { - return DataReturn('appid未配置', -1); - } - if(empty($ret['data']['secret'])) - { - return DataReturn('secret未配置', -1); - } - if(empty($params['code'])) - { - return DataReturn('code授权码为空', -1); - } - - // 获取access_token - $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$ret['data']['appid'].'&secret='.$ret['data']['secret'].'&code='.$params['code'].'&grant_type=authorization_code'; - $data = json_decode(file_get_contents($url), true); - if(empty($data['access_token'])) - { - if(empty($data['errmsg'])) - { - return DataReturn('获取access_token失败', -100); - } else { - return DataReturn($data['errmsg'], -100); - } - } - return DataReturn('获取成功', 0, $data); - - } else { - return DataReturn($ret['msg'], -1); - } - } } ?> \ No newline at end of file diff --git a/application/plugins/weixinwebauthorization/index/Pay.php b/application/plugins/weixinwebauthorization/index/Pay.php new file mode 100644 index 000000000..755343ebd --- /dev/null +++ b/application/plugins/weixinwebauthorization/index/Pay.php @@ -0,0 +1,78 @@ +intval($params['id']), 'is_pay_auto'=>1, 'is_pay_submit'=>1]); + session('plugins_pay_order_detail_url', $url); + } + + // 调用授权 + $ret = AuthService::Auth($params]); + if($ret['code'] == 0) + { + return redirect($ret['data']); + } else { + $this->assign('msg', $ret['msg']); + return $this->fetch('public/tips_error'); + } + } + + /** + * 支付提示 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @datetime 2019-05-25T20:15:23+0800 + * @param [array] $params [输入参数] + */ + public function Tips($params = []) + { + // 自定义链接 + $this->assign('to_url', MyUrl('index/order/index')); + $this->assign('to_title', '我的订单'); + + // 状态 + if(isset($params['status']) && $params['status'] == 0) + { + $this->assign('msg', '支付成功'); + return $this->fetch('public/tips_success'); + } else { + $this->assign('msg', '支付失败'); + return $this->fetch('public/tips_error'); + } + } +} +?> \ No newline at end of file diff --git a/application/plugins/weixinwebauthorization/service/Service.php b/application/plugins/weixinwebauthorization/service/AuthService.php similarity index 50% rename from application/plugins/weixinwebauthorization/service/Service.php rename to application/plugins/weixinwebauthorization/service/AuthService.php index c99984a28..97d042ee2 100755 --- a/application/plugins/weixinwebauthorization/service/Service.php +++ b/application/plugins/weixinwebauthorization/service/AuthService.php @@ -15,14 +15,162 @@ use app\service\UserService; use app\service\PluginsService; /** - * 微信登录服务层 + * 授权 - 服务层 * @author Devil * @blog http://gong.gg/ * @version 0.0.1 * @datetime 2016-12-01T21:51:08+0800 */ -class Service +class AuthService { + /** + * 授权 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @datetime 2019-02-07T08:21:54+0800 + * @param [array] $params [输入参数] + */ + public static function Auth($params = []) + { + $ret = PluginsService::PluginsData('weixinwebauthorization'); + if($ret['code'] == 0) + { + // 参数校验 + if(empty($ret['data']['appid'])) + { + return DataReturn('appid未配置', -1); + } + + // 回调地址 + $redirect_uri = urlencode(PluginsHomeUrl('weixinwebauthorization', 'auth', 'callback')); + + // 授权方式 + $auth_type = (isset($ret['data']['auth_type']) && $ret['data']['auth_type'] == 1) ? 'snsapi_userinfo' : 'snsapi_base'; + + // 授权code + $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$ret['data']['appid'].'&redirect_uri='.$redirect_uri.'&response_type=code&scope='.$auth_type.'&state=login#wechat_redirect'; + return DataReturn('操作成功', 0, $url); + } + return $ret; + } + + /** + * 回调 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @datetime 2019-02-07T08:21:54+0800 + * @param [array] $params [输入参数] + */ + public static function Callback($params = []) + { + // 参数校验 + if(empty($params['code'])) + { + return DataReturn('授权code为空', -1); + } + + // 远程获取access_token + $ret = self::RemoteAccessToken($params); + if($ret['code'] != 0) + { + return DataReturn($ret['msg'], -1); + } + + // 获取用户信息 + $ret = self::RemoteUserInfo($ret['data']); + if($ret['code'] != 0) + { + return DataReturn($ret['msg'], -1); + } + + // 处理用户数据 + return self::WeixinAuthBind($ret['data']); + } + + /** + * 获取用户信息 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @date 2019-05-24 + * @desc description + * @param [array] $params [输入参数] + */ + public static function RemoteUserInfo($params = []) + { + // 参数校验 + if(empty($params['access_token'])) + { + return DataReturn('access_token为空', -1); + } + if(empty($params['openid'])) + { + return DataReturn('openid为空', -1); + } + + // 获取用户详细信息 + $url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$params['access_token'].'&openid='.$params['openid'].'&lang=zh_CN'; + $data = json_decode(file_get_contents($url), true); + if(empty($data['openid'])) + { + if(empty($data['errmsg'])) + { + return DataReturn('获取用户信息失败', -100); + } else { + return DataReturn($data['errmsg'], -100); + } + } + return DataReturn('获取成功', 0, $data); + } + + /** + * 远程获取access_token + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @date 2019-05-24 + * @desc description + * @param [array] $params [输入参数] + */ + public static function RemoteAccessToken($params = []) + { + $ret = PluginsService::PluginsData('weixinwebauthorization'); + if($ret['code'] == 0) + { + // 参数校验 + if(empty($ret['data']['appid'])) + { + return DataReturn('appid未配置', -1); + } + if(empty($ret['data']['secret'])) + { + return DataReturn('secret未配置', -1); + } + if(empty($params['code'])) + { + return DataReturn('code授权码为空', -1); + } + + // 获取access_token + $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$ret['data']['appid'].'&secret='.$ret['data']['secret'].'&code='.$params['code'].'&grant_type=authorization_code'; + $data = json_decode(file_get_contents($url), true); + if(empty($data['access_token'])) + { + if(empty($data['errmsg'])) + { + return DataReturn('获取access_token失败', -100); + } else { + return DataReturn($data['errmsg'], -100); + } + } + return DataReturn('获取成功', 0, $data); + + } + return $ret; + } + /** * 微信解绑 * @author Devil diff --git a/extend/payment/Weixin.php b/extend/payment/Weixin.php index 898ed83a5..5c91466e3 100755 --- a/extend/payment/Weixin.php +++ b/extend/payment/Weixin.php @@ -145,7 +145,7 @@ class Weixin // 微信中打开 if(!empty($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false && empty($params['user']['weixin_web_openid'])) { - exit(header('location:'.PluginsHomeUrl('weixinwebauthorization', 'auth', 'pay', input()))); + exit(header('location:'.PluginsHomeUrl('weixinwebauthorization', 'pay', 'index', input()))); } // 获取支付参数 @@ -253,8 +253,8 @@ class Weixin private function PayHtml($pay_data, $redirect_url) { // 支付跳转地址 - $success_url = PluginsHomeUrl('weixinwebauthorization', 'auth', 'paytips', ['status'=>0]); - $error_url = PluginsHomeUrl('weixinwebauthorization', 'auth', 'paytips', ['status'=>-1]); + $success_url = PluginsHomeUrl('weixinwebauthorization', 'pay', 'tips', ['status'=>0]); + $error_url = PluginsHomeUrl('weixinwebauthorization', 'pay', 'tips', ['status'=>-1]); // 支付代码 exit(' -- GitLab