The Alipay applet can only obtain the user's nickname and avatar on the front end, but this is far from enough. We at least need to obtain the user's Alipay User ID. At this time, we must use Alipay's SDK to obtain it on the back end. , of course the front end needs to issue an httprequest request. The following is modified based on the examples in the first two articles
Alipay applet front end
app.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | App({
globalData:{
studentid:'',
username:'',
apiurl: 'http:
},
getUserInfo(){
var that = this
return new Promise((resovle,reject)=>{
if (this.userInfo) resovle(this.userInfo);
my.getAuthCode({
scopes: 'auth_user',
success:(res) =>{
if (res.authCode) {
my.httpRequest({
url: that.globalData.apiurl + '/api/AliPay/GetUserInfo',
method: 'GET',
data: {
auth_code: res.authCode
},
dataType: 'json',
success: function (res) {
that.globalData.studentid = res.data.data.student_id;
that.globalData.username = res.data.data.user_name;
my.getAuthUserInfo({
scopes: ['auth_user'],
success: (res) => {
that.userInfo = res;
resovle(that.userInfo);
},
fail:() =>{
reject({});
}
});
console.log('返回UserDetail', res.data.data);
},
fail: function (res) {
my.alert({content: 'fail'});
},
complete: function (res) {
my.hideLoading();
}
});
}
},
fail:() =>{
reject({});
}
});
});
},
onLaunch(options) {
},
onShow(options) {
},
});
|
Copy after login
The above code adjustment Get the backend webapi http://XXX/api/AliPay/GetUserInfo to obtain user information, and store the obtained userid and username into the global variable globalData
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | const app = getApp();
Page({
data: {
src: '',
username: '',
studentid: ''
},
imageError: function (e) {
console.log('image 发生错误', e.detail.errMsg)
},
imageLoad: function (e) {
console.log('image 加载成功', e);
},
onLoad(query) {
app.getUserInfo().then(
user => {
console.info(user);
if (user.avatar.length > 0) {
this.setData({src: user.avatar});
}
else {
this.setData({src: '/images/tou.png'});
}
if (app.globalData.username)
{
this.setData({username: app.globalData.username});
}
else
{
this.setData({username: user.nickName});
}
if (app.globalData.studentid)
{
this.setData({studentid: app.globalData.studentid});
}
}
);
},
onShow() {
},
onReady() {
}
});
|
Copy after login
Originally, the official only provides .net framwork SDK, but someone has already transplanted the .net core version on the Internet. Run Install-Package Alipay.AopSdk.Core to install it. Configure the following in appsettings.json and write in your mini program’s public key, private key, appid and other parameters. uid does not need to be written
1 2 3 4 5 6 7 8 9 10 | "Alipay" : {
"AlipayPublicKey" : "" ,
"AppId" : "" ,
"CharSet" : "UTF-8" ,
"GatewayUrl" : "https://openapi.alipay.com/gateway.do" ,
"PrivateKey" : "" ,
"SignType" : "RSA2" ,
"Uid" : ""
}
|
Copy after login
, and then you need to inject Service
in the backend core. The Startup.cs code subsidizes all, only the relevant ones are posted. This code does just that. Read Get appsettings.json and inject it into the service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | private void ConfigureAlipay(IServiceCollection services)
{
var alipayOptions = Configuration.GetSection( "Alipay" ).Get<AlipayOptions>();
AlipayConfigChecker.Check(alipayOptions.SignType, alipayOptions.PrivateKey);
services.AddAlipay(options => options.SetOption(alipayOptions)).AddAlipayF2F();
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
ConfigureAlipay(services);
......
|
Copy after login
After getting the authorization code passed from the front end, use the authorization to obtain the user information
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | private AlipayUserInfoShareResponse GetShareResponse(string auth_code)
{
var alipaySystemOauthTokenRequest = new AlipaySystemOauthTokenRequest
{
Code = auth_code,
GrantType = "authorization_code"
};
var oauthTokenResponse = _alipayService.Execute(alipaySystemOauthTokenRequest);
AlipayUserInfoShareRequest requestUser = new AlipayUserInfoShareRequest();
AlipayUserInfoShareResponse userinfoShareResponse = _alipayService.Execute(requestUser, oauthTokenResponse.AccessToken);
return userinfoShareResponse;
}
[HttpGet]
[Route( "GetUserInfo" )]
public ActionResult GetUserInfo(string auth_code)
{
try
{
AlipayUserInfoShareResponse userinfoShareResponse = GetShareResponse(auth_code);
return new JsonResult( new { data = userinfoShareResponse });
}
catch (Exception ex)
{
log.Error( "错误:" + ex.ToString());
return new JsonResult( new { data = ex.ToString() });
}
}
|
Copy after login
Related articles:
Alipay How to use SDK?
Introduction to the comparison and differences between WeChat mini program and Alipay mini program
The above is the detailed content of Alipay mini program development - using Alipay's SDK to obtain user ID. For more information, please follow other related articles on the PHP Chinese website!