• PC网站支付宝扫码登录


    1.电脑网站支付宝登录,拼接授权链接,在浏览器上访问授权链接即可
    登录 - 支付宝欢迎登录支付宝,支付宝-全球领先的独立第三方支付平台,致力于为广大用户提供安全快速的电子支付/网上支付/安全支付/手机支付体验以及转账收款/水电煤缴费/信用卡还款等生活服务应用;为广大为从事电子商务的网站提供支付产品/支付服务的在线订购和技术支持等服务,帮助商家快速接入支付工具,高效、安全、快捷地开展电子商务。icon-default.png?t=N7T8https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=商户的APPID&scope=auth_user&redirect_uri=ENCODED_URL&state=init

    2.使用 auth_code 换取 access_token 及用户 userId

    composer安装 alipaysdk/easysdk依赖包

    1. /**
    2. * Created by PhpStorm.
    3. * User: Administrator
    4. * Date: 2020/8/4
    5. * Time: 16:47
    6. */
    7. namespace app\api\controller;
    8. use Alipay\EasySDK\Kernel\Factory;
    9. use Alipay\EasySDK\Kernel\Config;
    10. use app\common\controller\Api;
    11. class Alipay extends Api
    12. {
    13. protected $noNeedLogin = '*';
    14. protected $noNeedRight = '*';
    15. protected $app_id;
    16. protected $private_key;
    17. protected $ali_public_key; //
    18. public function _initialize()
    19. {
    20. parent::_initialize();
    21. if (!\think\Config::get('fastadmin.usercenter')) {
    22. $this->error(__('User center already closed'));
    23. }
    24. $this->app_id = '20***************08';
    25. $this->private_key = '你的私钥';
    26. $this->ali_public_key = '你的公钥';
    27. }
    28. /**
    29. * auth_token获取用户信息
    30. * @return array
    31. * @throws \Exception
    32. */
    33. public function getUserInfo()
    34. {
    35. $auth_token = $this->request->post('auth_token');
    36. Factory::setOptions($this->getOptions());
    37. //设置系统参数(OpenAPI中非biz_content里的参数)
    38. $textParams = array(
    39. "code" => "{$auth_token}",
    40. "grant_type" => "authorization_code"
    41. );
    42. //设置业务参数(OpenAPI中biz_content里的参数)
    43. $bizParams = array();
    44. $resJson = Factory::util()->generic()->execute("alipay.system.oauth.token", $textParams, $bizParams)->httpBody;
    45. $resJsonToArray = json_decode($resJson, true);
    46. if (isset($resJsonToArray['alipay_system_oauth_token_response'])) {
    47. $this->success('授权成功',$resJsonToArray['alipay_system_oauth_token_response']);
    48. } else {
    49. $this->error('授权失败',$resJsonToArray);
    50. }
    51. }
    52. /**
    53. * 【新版】配置
    54. * @return Config
    55. */
    56. private function getOptions()
    57. {
    58. $options = new Config();
    59. $options->protocol = 'https';
    60. $options->gatewayHost = 'openapi.alipay.com';
    61. $options->signType = 'RSA2';
    62. $options->appId = $this->app_id;
    63. // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
    64. $options->merchantPrivateKey = $this->private_key;
    65. //$options->alipayCertPath = '<-- 请填写您的支付宝公钥证书文件路径,例如:/foo/alipayCertPublicKey_RSA2.crt -->';
    66. //$options->alipayRootCertPath = '<-- 请填写您的支付宝根证书文件路径,例如:/foo/alipayRootCert.crt" -->';
    67. //$options->merchantCertPath = '<-- 请填写您的应用公钥证书文件路径,例如:/foo/appCertPublicKey_2019051064521003.crt -->';
    68. //注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
    69. // $options->alipayPublicKey = '<-- 请填写您的支付宝公钥,例如:MIIBIjANBg... -->';
    70. $options->alipayPublicKey = $this->ali_public_key;
    71. //可设置异步通知接收服务地址(可选)
    72. // $options->notifyUrl = "";
    73. //可设置AES密钥,调用AES加解密相关接口时需要(可选)
    74. // $options->encryptKey = "";
    75. return $options;
    76. }
    77. /**
    78. * 返回给前端获取code
    79. * 【新旧都可用】
    80. * InfoStr APP登录需要的的infostr
    81. * @return String
    82. */
    83. public function infoStr()
    84. {
    85. $infoStr = http_build_query([
    86. 'apiname' => 'com.alipay.account.auth',
    87. 'method' => 'alipay.open.auth.sdk.code.get',
    88. 'app_id' => $this->app_id,
    89. 'app_name' => 'mc',
    90. 'biz_type' => 'openservice',
    91. 'pid' => $this->pid,
    92. 'product_id' => 'APP_FAST_LOGIN',
    93. 'scope' => 'kuaijie',
    94. 'target_id' => time(), //商户标识该次用户授权请求的ID,该值在商户端应保持唯一
    95. 'auth_type' => 'AUTHACCOUNT', // AUTHACCOUNT代表授权;LOGIN代表登录
    96. 'sign_type' => 'RSA2',
    97. ]);
    98. $infoStr .= '&sign=' . $this->enRSA2($infoStr);
    99. return $infoStr;
    100. }
    101. /**
    102. * 【生成签名sign】
    103. * enRSA2 RSA加密
    104. * @param String $data
    105. * @return String
    106. */
    107. private function enRSA2($data)
    108. {
    109. $str = chunk_split(trim($this->private_key), 64, "\n");
    110. $key = "-----BEGIN RSA PRIVATE KEY-----\n$str-----END RSA PRIVATE KEY-----\n";
    111. // print_r($key);die;
    112. // $key = file_get_contents(storage_path('rsa_private_key.pem')); 为文件时这样引入
    113. $signature = '';
    114. //$signature = openssl_sign($data, $signature, $key, OPENSSL_ALGO_SHA256)?base64_encode($signature):NULL;
    115. $signature = openssl_sign($data, $signature, $key, OPENSSL_ALGO_SHA256) ? base64_encode($signature) : NULL;
    116. return $signature;
    117. }
    118. /**
    119. * myHttpBuildQuery 返回一个 http Get 传参数组
    120. * 之所以不用 自带函数 http_build_query 时间带 ‘:’ 会被转换
    121. *
    122. * @param Array
    123. * @return String
    124. */
    125. private function myHttpBuildQuery($dataArr)
    126. {
    127. ksort($dataArr);
    128. $signStr = '';
    129. foreach ($dataArr as $key => $val) {
    130. if (empty($signStr)) {
    131. $signStr = $key . '=' . $val;
    132. } else {
    133. $signStr .= '&' . $key . '=' . $val;
    134. }
    135. }
    136. return $signStr;
    137. }
    138. }

    前端将auth_code 提交到getUserInfo方法获取唯一表示userid

  • 相关阅读:
    Error: [vuex] do not mutate vuex store state outside mutation handlers.
    PCB设计仿真之探讨源端串联端接
    ChatGLM2-6B 部署
    WordPress主题WoodMart v7.3.2 WooCommerce主题和谐汉化版下载
    【计算机网络】计算机网络、互联网、互连网、因特网、万维网
    逆矩阵的算法
    java对象以及引用方式
    【Pygame小游戏】这款“吃掉一切”游戏简直奇葩了?通通都吃掉嘛?(附源码免费领)
    SplitMask:大规模数据集是自我监督预训练的必要条件吗?
    C++提高:03STL- 常用容器_1
  • 原文地址:https://blog.csdn.net/qq_40088333/article/details/133851167