支付宝小程序前端
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 重新打开 },});
上面的代码调取后端webapi http://xxx/api/alipay/getuserinfo 来获取用户信息,并把取到的userid,username 存到全局变量 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() { }});
本来官方只提供了.net framwork 的sdk,但网上已经有人移植了.net core 的版本,运行 install-package alipay.aopsdk.core 进行安装,在 appsettings.json 进行如下的配置,写上你的小程序公匙,私匙,appid 等参数 uid 可以不写
"alipay": { //校园码支付宝小程序正式环境 "alipaypublickey": "", "appid": "", "charset": "utf-8", "gatewayurl": "https://openapi.alipay.com/gateway.do", "privatekey": "", "signtype": "rsa2", "uid": "" }
然后在后端core还需要注入service
startup.cs 代码就补贴全部了,只贴相关的,这段代码就干这么个事,读取 appsettings.json 并注入服务
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); ......
在得到从前端传过来的授权码之后,利用授权得到用户信息
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() }); } }
相关文章:
支付宝sdk怎么用啊?
微信小程序和支付宝小程序对比区别介绍
以上就是支付宝小程序开发-利用支付宝的sdk获取用户user id的详细内容。
