uniapp h5支付宝授权获取user_id
1、uniapp: app.vue// #ifdef H5if(!uni.getStorageSync('token')){if (/AlipayClient/.test(window.navigator.userAgent)) {var alipay = new Alipay();let token = awa...
·
1、uniapp: app.vue
在onLaunch 中:
// #ifdef H5
if(!uni.getStorageSync('token')){
if (/AlipayClient/.test(window.navigator.userAgent)) {
var alipay = new Alipay();
let token = await alipay.login();
this.setTokenAndBack(token);
}
}
// #endif
2、新建:apipay.js
export default class Alipay {
async login() {
let token = '';
if (router.$Route.path.indexOf('public/login') == -1) {
uni.setStorageSync('fromLogin', router.$Route);
}
// #ifdef H5
if (/AlipayClient/.test(window.navigator.userAgent)) {
this.alipayOfficialAccountLogin();
}
// #endif
}
// #ifdef H5
alipayOfficialAccountLogin() {
let oUrl = window.location.href;
/* window.location = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='
+ store.state.init.initData.wechat.appid +
`&redirect_uri=${API_URL}user/wxOfficialAccountLogin&response_type=code&scope=snsapi_userinfo&state=` +
oUrl; */
window.location = 'https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id='
+ '202***********48' + '&scope=auth_user&redirect_uri='
+ `${API_URL}user/alipayLogin&state=`
+ oUrl;
throw 'stop';
}
// #endif
}
3、新建:JbAlipay.php
在上面步骤中的redirect_uri指定的方法中,调用JbAlipay中的方法:
public function alipayLogin()
{
$param = $this->request->param();
$data = file_get_contents('php://input');
$param = empty($param) ? [] : $param;
// $param['alipayLogin'] = "alipayLogin---------------------";
/* [ log ] array (
'app_id' => '20210*******048',
'source' => 'alipay_wallet',
'scope' => 'auth_userinfo',
'state' => 'ini',
'auth_code' => '10fb62fe9****71a0f75b1fXC95',
'addon' => '*****',
'controller' => 'user',
'action' => 'alipayLogin',
'alipayLogin' => 'alipayLogin---------------------',
)
*/
if(isset($param['auth_code'])){//auth_code换取access_token与user_id
$result = JbAlipay::do_auth_code($param['auth_code']);
if($result['code'] == 10000){ //成功获取
// 解析来源页面
/* [avatar] => https://tfs.alipayobjects.com/images/partner/TB1R*******XXa.SXXa
[city] => 南宁市
[gender] => m
[nick_name] => 阿**昭
[province] => 广西壮族自治区
[user_id] => 20887******710 */
/*
if (1==1) {
// 登录页接参Token
header('Location:' . $procotol . $host . '/pages/public/login?token=' . $ret->getToken());
exit();
}
}else{
$this->error("支付宝授权:" . $result['code'] . " " .$result['msg']);
}
}
$this->error("支付宝授权:auth_code 异常");
}
<?php
namespace aop\jb;
require_once EXTEND_PATH . 'aop/AopCertClient.php';
require_once EXTEND_PATH . 'aop/SignData.php';
require_once EXTEND_PATH . 'aop/request/AlipaySystemOauthTokenRequest.php';
require_once EXTEND_PATH . 'aop/request/AlipayUserInfoShareRequest.php';
class JbAlipay
{
public static function do_auth_code($code){
$aop = new \AopCertClient();
$appCertPath = "应用证书路径(要确保证书文件可读),例如:/home/admin/cert/appCertPublicKey.crt";
$appCertPath = EXTEND_PATH . 'aop/jb/appCertPublicKey_2021*******048.crt';
$alipayCertPath = "支付宝公钥证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayCertPublicKey_RSA2.crt";
$alipayCertPath = EXTEND_PATH . 'aop/jb/alipayCertPublicKey_RSA2.crt';
$rootCertPath = "支付宝根证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayRootCert.crt";
$rootCertPath = EXTEND_PATH . 'aop/jb/alipayRootCert.crt';
$aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
$aop->appId = "20210*****4048";
$aop->rsaPrivateKey = 'MIIEpAIBAAKCAQEAqphyjx1An1*******/WQA8eC**YRKSwAVfg==' ;
$aop->format = "json";
$aop->signType= "RSA2";
//调用getPublicKey从支付宝公钥证书中提取公钥
$aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);
//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
$aop->isCheckAlipayPublicCert = true;
//调用getCertSN获取证书序列号
$aop->appCertSN = $aop->getCertSN($appCertPath);
//调用getRootCertSN获取支付宝根证书序列号
$aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath);
$request = new \AlipaySystemOauthTokenRequest();
$request->setCode($code);
$request->setGrantType("authorization_code");
$response = $aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$access_token = $response->$responseNode->access_token;
$user_id = $response->$responseNode->user_id;
if($access_token){ //AlipayUserInfoShareRequest
// alipay.user.info.share
$userRequest = new \AlipayUserInfoShareRequest();
$info = $aop->execute($userRequest, $access_token); //这里传入获取的access_token
$responseNodeName = str_replace(".", "_", $userRequest->getApiMethodName()) . "_response";
//用户信息
$userInfo = $info->$responseNodeName;
/* [avatar] => https://tfs.alipayobjects.com/images/partner/TB1RMb******XXa.SXXa
[city] => 南宁市
[gender] => m
[nick_name] => 阿**昭
[province] => 广西壮族自治区
[user_id] => 20887******57710 */
return json_decode( json_encode( $userInfo),true);
}
return ['code'=> 9999999, "msg"=>"JB未知异常"];
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)