


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
App({ globalData:{ studentid:'', username:'', apiurl: 'http://XXX' }, getUserInfo(){ var that = this return new Promise((resovle,reject)=>{ if(this.userInfo) resovle(this.userInfo); //调用用户授权 api 获取用户信息 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) { // 从后台被 scheme 重新打开 }, });
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
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) { //设置UserId this.setData({studentid: app.globalData.studentid}); } } ); }, onShow() { // 页面显示 }, onReady() { } });
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
"Alipay": { //校园码支付宝小程序正式环境 "AlipayPublicKey": "", "AppId": "", "CharSet": "UTF-8", "GatewayUrl": "https://openapi.alipay.com/gateway.do", "PrivateKey": "", "SignType": "RSA2", "Uid": "" }
, 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
private void ConfigureAlipay(IServiceCollection services) { var alipayOptions = Configuration.GetSection("Alipay").Get<AlipayOptions>(); //检查RSA私钥 AlipayConfigChecker.Check(alipayOptions.SignType, alipayOptions.PrivateKey); services.AddAlipay(options => options.SetOption(alipayOptions)).AddAlipayF2F(); } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //配置alipay服务 ConfigureAlipay(services); ......
After getting the authorization code passed from the front end, use the authorization to obtain the user information
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; } /// <summary> /// 获取用户信息 /// </summary> /// <param name="auth_code"></param> /// <returns></returns> [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() }); } }
Related articles:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

SublimeText3 Chinese version
Chinese version, very easy to use