接入流程

前言
采用TP5.1的框架實(shí)現(xiàn),我知道這里會有很多的坑,在開發(fā)這個(gè)之前,我就寫了兩篇前奏,因?yàn)檫@個(gè)登錄需要一些參數(shù),這個(gè)和微信小程序的不太一樣
獲取code
首先我們需要調(diào)用my.getAuthCode接口獲取code,然后傳給服務(wù)端
首先登錄一下支付寶開發(fā)者工具

首先我們寫一個(gè)簡單的發(fā)送code的demo,這個(gè)在官網(wǎng)里面有https://opendocs.alipay.com/mini/api/openapi-authorize

后面我這里改了一下js里面的
Page({
? onLoad() {},
? data: {},
? getAuthCode: () => {
? ? my.getAuthCode({
? ? ? scopes: 'auth_user',
? ? ? success: (res) => {
? ? ? ? console.log(res.authCode);
? ? ? ? // my.alert({? ? ? ? //? content: res.authCode,? ? ? ? // });? ? ? ? let code = res.authCode
? ? ? ? console.log(res);
? ? ? ? my.request({
? ? ? ? ? ? url: 'http://localhost/ttft_2/public/api/v1/getcodeali',
? ? ? ? ? ? data: code,
? ? ? ? ? ? method: 'POST',
? ? ? ? ? ? success: (mes) => {
? ? ? ? ? ? ? console.log(mes);
? ? ? ? ? ? }
? ? ? ? });
? ? ? },
? ? });
? },
});
但是這里面又發(fā)現(xiàn)了一個(gè)坑,不能使用http的請求,因此我們需要在服務(wù)器上部署才行,這個(gè)口面再發(fā)一篇,這里先拿到code再說,注釋一下代碼,換成這樣的
Page({
? onLoad() {},
? data: {},
? getAuthCode: () => {
? ? my.getAuthCode({
? ? ? scopes: 'auth_user',
? ? ? success: (res) => {
? ? ? ? console.log(res.authCode);
? ? ? ? my.alert({
? ? ? ? ? content: res.authCode,
? ? ? ? });
? ? ? ? // let code = res.authCode? ? ? ? // console.log(res);? ? ? ? // my.request({? ? ? ? //? ? url: 'http://localhost/ttft_2/public/api/v1/getcodeali',? ? ? ? //? ? data: code,? ? ? ? //? ? method: 'POST',? ? ? ? //? ? success: (mes) => {? ? ? ? //? ? ? console.log(mes);? ? ? ? //? ? }? ? ? ? // });? ? ? },
? ? });
? },
});
其他的就按照官網(wǎng)給的就行,然后我們點(diǎn)擊獲取授權(quán)碼

會彈出這樣的消息,在控制臺中打印的可以復(fù)制

這樣一來,code碼到手了
把code碼發(fā)給服務(wù)端
下載支付寶sdk還是給出地址https://docs.open.alipay.com/54/103419
下載好之后把a(bǔ)op文件放到項(xiàng)目的extend目錄下

另外開發(fā)這個(gè)登錄需要的參數(shù)我們要準(zhǔn)備好,不知道的話,我的前一篇博客里面有
https://blog.csdn.net/qq_45163122/article/details/104148904
我把它寫到配置文件當(dāng)中,(一些公共的資料注意還是一律填寫到配置文件當(dāng)中,方便日后的維護(hù)和管理)

登錄需要的就是以上的四個(gè)參數(shù)
然后開始寫我們的方法
<?php
namespace app\api\model;
use think\facade\Env;
use think\Model;
require Env::get('root_path').'extend/aop/aopClient.php';
require Env::get('root_path').'extend/aop/request/AlipaySystemOauthTokenRequest.php';
class User extends Model
{
? ? public static function getCodeAli($param){
? ? ? ? $aop = new \aopClient();
? ? ? ? $aop->gatewayUrl = config('base.ali_gatewayUrl');
? ? ? ? $aop->appId = config('base.ali_app_id');
? ? ? ? $aop->rsaPrivateKey = config('base.ali_rsaPrivateKey');
? ? ? ? $aop->alipayrsaPublicKey = config('base.ali_alipayrsaPublicKey');
? ? ? ? $aop->apiVersion = '1.0';
? ? ? ? $aop->signType = 'RSA2';
? ? ? ? $aop->postCharset='utf-8';
? ? ? ? $aop->format='json';
? ? ? ? $request = new \AlipaySystemOauthTokenRequest();
? ? ? ? $request->setGrantType("authorization_code");
? ? ? ? $request->setCode($param['code']);
? ? ? ? //$request->setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");? ? ? ? $result = $aop->execute($request);
? ? ? ? return $result;
? ? }
}
就這樣就可以登錄了
這里再貼出支付寶的文檔https://docs.open.alipay.com/api_9/alipay.system.oauth.token/
不過有個(gè)問題,難道我說登錄就登錄了嗎?程序怎么會這么聽話?當(dāng)然不是,既然支付寶工具調(diào)試不了,那就使用api調(diào)試工具,我這里使用Postman

使用的是post請求,把我們的地址放進(jìn)去,輸入code,不過好像有點(diǎn)問題,因?yàn)槲覀兊腸ode碼已經(jīng)過期了需要重新獲取,來,重新獲取一個(gè)新的code

像這個(gè)就是正確的啦,現(xiàn)在服務(wù)器已經(jīng)獲取到了access_token這個(gè)參數(shù),我們就要進(jìn)行下一步操作了。
獲取用戶信息(不能再使用)
通過token接口調(diào)用支付寶會員查詢接口獲取會員信息
來,文檔地址https://docs.open.alipay.com/api_2/alipay.user.info.share
上面我們獲取的那個(gè)access_token是有有效期的,建議存到緩存當(dāng)中
我們把程序改好了在試一次
<?php
namespace app\api\model;
use think\facade\Env;
use think\Model;
require Env::get('root_path').'extend/aop/aopClient.php';
require Env::get('root_path').'extend/aop/request/AlipaySystemOauthTokenRequest.php';
require Env::get('root_path').'extend/aop/request/AlipayUserInfoShareRequest.php';
class User extends Model
{
? ? /**
? ? * 支付寶獲取code
? ? * @param $param
? ? * @return bool|mixed|\SimpleXMLElement
? ? * @throws \Exception
? ? */? ? public static function getCodeAli($param){
? ? ? ? $aop = new \aopClient();
? ? ? ? $aop->gatewayUrl = config('base.ali_gatewayUrl');
? ? ? ? $aop->appId = config('base.ali_app_id');
? ? ? ? $aop->rsaPrivateKey = config('base.ali_rsaPrivateKey');
? ? ? ? $aop->alipayrsaPublicKey = config('base.ali_alipayrsaPublicKey');
? ? ? ? $aop->apiVersion = '1.0';
? ? ? ? $aop->signType = 'RSA2';
? ? ? ? $aop->postCharset='utf-8';
? ? ? ? $aop->format='json';
? ? ? ? $request = new \AlipaySystemOauthTokenRequest();
? ? ? ? $request->setGrantType("authorization_code");
? ? ? ? $request->setCode($param['code']);
? ? ? ? //$request->setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");? ? ? ? $result = $aop->execute($request);
? ? ? ? //return $result;? ? ? ? $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
? ? ? ? $accessToken= $result->$responseNode->access_token;
? ? ? ? return self::getUserInfoAli($accessToken);
? ? }
? ? /**
? ? * 獲取支付寶用戶信息
? ? * @param $accessToken
? ? * @return bool|mixed|\SimpleXMLElement
? ? * @throws \Exception
? ? */? ? public static function getUserInfoAli($accessToken){
? ? ? ? $aop = new \AopClient ();
? ? ? ? $aop->gatewayUrl = config('base.ali_gatewayUrl');
? ? ? ? $aop->appId = config('base.ali_app_id');
? ? ? ? $aop->rsaPrivateKey = config('base.ali_rsaPrivateKey');
? ? ? ? $aop->alipayrsaPublicKey = config('base.ali_alipayrsaPublicKey');
? ? ? ? $aop->apiVersion = '1.0';
? ? ? ? $aop->signType = 'RSA2';
? ? ? ? $aop->postCharset='utf-8';
? ? ? ? $aop->format='json';
? ? ? ? $request = new \AlipayUserInfoShareRequest ();
? ? ? ? $result = $aop->execute ( $request , $accessToken );
? ? ? ? return $result;
? ? }
}
提醒一下,這里code只能使用一次,使用一次之后就會失效
程序?qū)懞弥笳{(diào)用發(fā)現(xiàn)了一個(gè)驚喜,好吧,坑還是有的,按照他的來

提升開發(fā)權(quán)限
找到我們的小程序

一步一步來

調(diào)用,然后再次出錯(cuò),陷入一陣。。。。。

獲取用戶信息改變
好吧既然如此,就上官網(wǎng)查看
它居然說不能使用了
通過前臺調(diào)用用戶接口
my.getOpenUserInfohttps://opendocs.alipay.com/mini/api/ch8chh
現(xiàn)在換一種思路,在小程序端查詢信息存入到數(shù)據(jù)庫中,
改了一下代碼,我在這里貼出來
var app = getApp()
Page({
? data: {
? ? hasUserInfo: false
? },
? getUserInfo() {
? ? my.getAuthCode({
? ? ? scopes: 'auth_user',
? ? ? fail: (error) => {
? ? ? ? console.error('getAuthCode', error);
? ? ? },
? ? ? success: (res) => {
? ? ? ? let code = res.authCode;
? ? ? ? my.request({
? ? ? ? ? url: 'https://www.guizimo.top/demo/public/index.php/api/v1/getcodeali',
? ? ? ? ? method: 'POST',
? ? ? ? ? data:{
? ? ? ? ? ? code:code
? ? ? ? ? },
? ? ? ? });
? ? ? ? my.getAuthUserInfo({
? ? ? ? ? fail: (error) => {
? ? ? ? ? ? console.error('getAuthUserInfo', error);
? ? ? ? ? },
? ? ? ? ? success: (userInfo) => {
? ? ? ? ? ? console.log(`userInfo:`, userInfo);
? ? ? ? ? ? this.setData({
? ? ? ? ? ? ? userInfo,
? ? ? ? ? ? ? hasUserInfo: true,
? ? ? ? ? ? });
? ? ? ? ? ? my.request({
? ? ? ? ? ? ? url: 'https://www.guizimo.top/demo/public/index.php/api/v1/setuserinfoali',
? ? ? ? ? ? ? method: 'POST',
? ? ? ? ? ? ? data:{
? ? ? ? ? ? ? ? avatar:userInfo.avatar,
? ? ? ? ? ? ? ? nickName:userInfo.nickName
? ? ? ? ? ? ? },
? ? ? ? ? ? ? success: (result) => {
? ? ? ? ? ? ? ? console.log(result);
? ? ? ? ? ? ? ? my.alert({
? ? ? ? ? ? ? ? ? content: '登錄成功',
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? }
? ? ? ? ? ? });
? ? ? ? ? }
? ? ? ? });
? ? ? }
? ? });
? },
? clear() {
? ? this.setData({
? ? ? hasUserInfo: false,
? ? ? userInfo: {}
? ? })
? }
})
后臺主要是存入信息,用了兩個(gè)接口我把實(shí)現(xiàn)的方法寫在下面
<?php
namespace app\api\model;
use think\Db;
use think\facade\Cache;
use think\facade\Env;
use think\Model;
require Env::get('root_path').'extend/aop/AopClient.php';
require Env::get('root_path').'extend/aop/request/AlipaySystemOauthTokenRequest.php';
class User extends Model
{
? ? /**
? ? * 支付寶獲取code
? ? * @param $param
? ? * @return bool|mixed|\SimpleXMLElement
? ? * @throws \Exception
? ? */? ? public static function getCodeAli($param){
? ? ? ? //return $param;? ? ? ? $aop = new \aopClient();
? ? ? ? $aop->gatewayUrl = config('base.ali_gatewayUrl');
? ? ? ? $aop->appId = config('base.ali_app_id');
? ? ? ? $aop->rsaPrivateKey = config('base.ali_rsaPrivateKey');
? ? ? ? $aop->alipayrsaPublicKey = config('base.ali_alipayrsaPublicKey');
? ? ? ? $aop->apiVersion = '1.0';
? ? ? ? $aop->signType = 'RSA2';
? ? ? ? $aop->postCharset='utf-8';
? ? ? ? $aop->format='json';
? ? ? ? $request = new \AlipaySystemOauthTokenRequest();
? ? ? ? $request->setGrantType("authorization_code");
? ? ? ? $request->setCode($param['code']);
? ? ? ? //$request->setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");? ? ? ? $result = $aop->execute($request);
? ? ? ? //return $result;? ? ? ? $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
? ? ? ? //$accessToken= $result->$responseNode->access_token;? ? ? ? $userid = $result->$responseNode->user_id;
? ? ? ? //return $userid;? ? ? ? return self::saveUserId($userid);
? ? }
? ? /**
? ? * 保存UserID到數(shù)據(jù)庫
? ? * @param $userid
? ? * @return bool
? ? * @throws \think\db\exception\DataNotFoundException
? ? * @throws \think\db\exception\ModelNotFoundException
? ? * @throws \think\exception\DbException
? ? */? ? public static function saveUserId($userid){
? ? ? ? $res = UserAli::where('userid',$userid)->find();
? ? ? ? if($res){
? ? ? ? ? ? $Uid = $res['id'];
? ? ? ? }else{
? ? ? ? ? ? $Uid = Db::name('user_ali')->insertGetId(['userid'=>$userid]);
? ? ? ? }
? ? ? ? $result = Cache::set('Uid',$Uid);
? ? ? ? return $result;
? ? }
? ? /**
? ? * 保存好信息
? ? * @param $param
? ? * @return int|string
? ? * @throws \think\Exception
? ? * @throws \think\exception\PDOException
? ? */? ? public static function setUserInfoAli($param){
? ? ? ? $Uid = Cache::get('Uid');
? ? ? ? //return $Uid;? ? ? ? $res = UserAli::where('id',$Uid)
? ? ? ? ? ? ->update([
? ? ? ? ? ? ? ? 'avatar' => $param['avatar'],
? ? ? ? ? ? ? ? 'nickName' => $param['nickName']
? ? ? ? ? ? ]);
? ? ? ? return $res;
? ? }
}
演示
最后來演示一波,實(shí)在是太煩了,不過到現(xiàn)在又十分的簡單了
在開發(fā)者工具中調(diào)用接口

注意這個(gè)兩個(gè)接口的狀態(tài)

說明調(diào)用沒有什么大問題,后面還是搭建了一個(gè)環(huán)境來測試,不然每次登錄都要來這里面獲取,真的是很煩。
最后查看數(shù)據(jù)庫,看看我們的數(shù)據(jù)是否存入成功了

現(xiàn)在就大功告成了
不過當(dāng)然還有很多東西需要優(yōu)化,支付寶的登錄和微信小程序的登錄有一些不太一樣,后面也會寫一篇關(guān)于微信小程序登錄的。
參考鏈接:https://blog.csdn.net/qq_45163122/article/details/104185435
有什么問題或者項(xiàng)目咨詢可以來我們官網(wǎng)咨詢