您好,登錄后才能下訂單哦!
這篇文章主要介紹了小程序中如何利用支付寶的SDK獲取用戶User ID,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
支付寶小程序前端
app.js
App({ globalData:{ studentid:'', username:'', apiurl: 'http://XXX' }, getUserInfo(){ var that = this return new Promise((resovle,reject)=>{ if(this.userInfo) resovle(this.userInfo); //調(diào)用用戶授權(quán) 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) { // 從后臺(tái)被 scheme 重新打開(kāi) }, });
上面的代碼調(diào)取后端webapi http://XXX/api/AliPay/GetUserInfo 來(lái)獲取用戶信息,并把取到的userid,username 存到全局變量 globalData 里面
const app = getApp(); Page({ data: { src: '', username: '', studentid: '' }, imageError: function (e) { console.log('image 發(fā)生錯(cuò)誤', e.detail.errMsg) }, imageLoad: function (e) { console.log('image 加載成功', e); }, onLoad(query) { // 頁(yè)面加載 app.getUserInfo().then( user => { console.info(user); //設(shè)置頭像 if (user.avatar.length > 0) { this.setData({src: user.avatar}); } else{ this.setData({src: '/images/tou.png'}); } //設(shè)置用戶名 if (app.globalData.username) { this.setData({username: app.globalData.username}); } else { this.setData({username: user.nickName}); } if(app.globalData.studentid) { //設(shè)置UserId this.setData({studentid: app.globalData.studentid}); } } ); }, onShow() { // 頁(yè)面顯示 }, onReady() { } });
本來(lái)官方只提供了.net framwork 的SDK,但網(wǎng)上已經(jīng)有人移植了.net core 的版本,運(yùn)行 Install-Package Alipay.AopSdk.Core 進(jìn)行安裝,在 appsettings.json 進(jìn)行如下的配置,寫上你的小程序公匙,私匙,appid 等參數(shù) uid 可以不寫
"Alipay": { //校園碼支付寶小程序正式環(huán)境 "AlipayPublicKey": "", "AppId": "", "CharSet": "UTF-8", "GatewayUrl": "https://openapi.alipay.com/gateway.do", "PrivateKey": "", "SignType": "RSA2", "Uid": "" }
然后在后端core還需要注入Service
Startup.cs 代碼就補(bǔ)貼全部了,只貼相關(guān)的,這段代碼就干這么個(gè)事,讀取 appsettings.json 并注入服務(wù)
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服務(wù) ConfigureAlipay(services); ......
在得到從前端傳過(guò)來(lái)的授權(quán)碼之后,利用授權(quán)得到用戶信息
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("錯(cuò)誤:" + ex.ToString()); return new JsonResult(new { data = ex.ToString() }); } }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“小程序中如何利用支付寶的SDK獲取用戶User ID”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。