提交 a42d1d2e 编写于 作者: D devil_gong

微信网页授权支付优化

上级 78a5b165
...@@ -11,11 +11,10 @@ ...@@ -11,11 +11,10 @@
namespace app\plugins\weixinwebauthorization\index; namespace app\plugins\weixinwebauthorization\index;
use think\Controller; use think\Controller;
use app\service\PluginsService; use app\plugins\weixinwebauthorization\service\AuthService;
use app\plugins\weixinwebauthorization\service\Service;
/** /**
* 微信登录 - 登录授权 * 微信网页授权 - 授权处理
* @author Devil * @author Devil
* @blog http://gong.gg/ * @blog http://gong.gg/
* @version 0.0.1 * @version 0.0.1
...@@ -33,52 +32,7 @@ class Auth extends Controller ...@@ -33,52 +32,7 @@ class Auth extends Controller
*/ */
public function Unbind($params = []) public function Unbind($params = [])
{ {
return Service::WeixinUnbind($params); return AuthService::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);
} }
/** /**
...@@ -91,25 +45,10 @@ class Auth extends Controller ...@@ -91,25 +45,10 @@ class Auth extends Controller
*/ */
public function Index($params = []) public function Index($params = [])
{ {
$ret = PluginsService::PluginsData('weixinwebauthorization'); $ret = AuthService::Auth($params]);
if($ret['code'] == 0) if($ret['code'] == 0)
{ {
// 参数校验 return redirect($ret['data']);
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);
} else { } else {
$this->assign('msg', $ret['msg']); $this->assign('msg', $ret['msg']);
return $this->fetch('public/tips_error'); return $this->fetch('public/tips_error');
...@@ -117,7 +56,7 @@ class Auth extends Controller ...@@ -117,7 +56,7 @@ class Auth extends Controller
} }
/** /**
* 回调 * 授权回调
* @author Devil * @author Devil
* @blog http://gong.gg/ * @blog http://gong.gg/
* @version 1.0.0 * @version 1.0.0
...@@ -126,31 +65,7 @@ class Auth extends Controller ...@@ -126,31 +65,7 @@ class Auth extends Controller
*/ */
public function Callback($params = []) public function Callback($params = [])
{ {
// 参数校验 $ret = AuthService::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']);
if($ret['code'] == 0) if($ret['code'] == 0)
{ {
// 是否订单支付授权,进入订单详情 // 是否订单支付授权,进入订单详情
...@@ -170,88 +85,5 @@ class Auth extends Controller ...@@ -170,88 +85,5 @@ class Auth extends Controller
return $this->fetch('public/tips_error'); 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
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\plugins\weixinwebauthorization\index;
use think\Controller;
use app\plugins\weixinwebauthorization\service\AuthService;
/**
* 微信登录 - 微信里面支付
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-01T21:51:08+0800
*/
class Pay extends Controller
{
/**
* 支付授权
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2019-05-25T14:44:32+0800
* @param [array] $params [输入参数]
*/
public function Index($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);
}
// 调用授权
$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
...@@ -15,14 +15,162 @@ use app\service\UserService; ...@@ -15,14 +15,162 @@ use app\service\UserService;
use app\service\PluginsService; use app\service\PluginsService;
/** /**
* 微信登录服务层 * 授权 - 服务层
* @author Devil * @author Devil
* @blog http://gong.gg/ * @blog http://gong.gg/
* @version 0.0.1 * @version 0.0.1
* @datetime 2016-12-01T21:51:08+0800 * @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 * @author Devil
......
...@@ -145,7 +145,7 @@ class Weixin ...@@ -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'])) 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 ...@@ -253,8 +253,8 @@ class Weixin
private function PayHtml($pay_data, $redirect_url) private function PayHtml($pay_data, $redirect_url)
{ {
// 支付跳转地址 // 支付跳转地址
$success_url = PluginsHomeUrl('weixinwebauthorization', 'auth', 'paytips', ['status'=>0]); $success_url = PluginsHomeUrl('weixinwebauthorization', 'pay', 'tips', ['status'=>0]);
$error_url = PluginsHomeUrl('weixinwebauthorization', 'auth', 'paytips', ['status'=>-1]); $error_url = PluginsHomeUrl('weixinwebauthorization', 'pay', 'tips', ['status'=>-1]);
// 支付代码 // 支付代码
exit('<html> exit('<html>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册