開放接口列表
wx.login 登錄
wx.getUserInfo 獲取用戶信息
wx.chooseAddress 獲取用戶收貨地址
wx.requestPayment 發(fā)起微信支付
wx.addCard 添加卡券
wx.openCard 打開卡券
.......................................................

wx.getUserProfile
1、功能描述
獲取用戶信息。頁面產(chǎn)生點(diǎn)擊事件(例如 button 上 bindtap 的回調(diào)中)后才可調(diào)用,每次請(qǐng)求都會(huì)彈出授權(quán)窗口,用戶同意后返回 userInfo。該接口用于替換 wx.getUserInfo,詳見 用戶信息接口調(diào)整說明。


2、示例代碼
頁面代碼

js代碼

運(yùn)行效果


3.wx.login
功能描述
調(diào)用接口獲取登錄憑證(code)。通過憑證進(jìn)而換取用戶登錄態(tài)信息,包括用戶在當(dāng)前小程序的唯一標(biāo)識(shí)(openid)、微信開放平臺(tái)帳號(hào)下的唯一標(biāo)識(shí)(unionid,若當(dāng)前小程序已綁定到微信開放平臺(tái)帳號(hào))及本次登錄的會(huì)話密鑰(session_key)等。用戶數(shù)據(jù)的加解密通訊需要依賴會(huì)話密鑰完成。


小程序登錄wx.login()
小程序可以通過微信官方提供的登錄能力方便地獲取微信提供的用戶身份標(biāo)識(shí),快速建立小程序內(nèi)的用戶體系

說明
調(diào)用 wx.login() 獲取 臨時(shí)登錄憑證code ,并回傳到開發(fā)者服務(wù)器。
調(diào)用 auth.code2Session 接口,換取 用戶唯一標(biāo)識(shí) OpenID 、 用戶在微信開放平臺(tái)帳號(hào)下的唯一標(biāo)識(shí)UnionID(若當(dāng)前小程序已綁定到微信開放平臺(tái)帳號(hào)) 和 會(huì)話密鑰 session_key。
之后開發(fā)者服務(wù)器可以根據(jù)用戶標(biāo)識(shí)來生成自定義登錄態(tài),用于后續(xù)業(yè)務(wù)邏輯中前后端交互時(shí)識(shí)別用戶身份
小程序登錄code2Session
接口應(yīng)在服務(wù)器端調(diào)用,詳細(xì)說明參見服務(wù)端API。
功能描述
登錄憑證校驗(yàn)。通過 wx.login 接口獲得臨時(shí)登錄憑證 code 后傳到開發(fā)者服務(wù)器調(diào)用此接口完成登錄流程。更多使用方法詳見小程序登錄。
HTTPS 調(diào)用
GET https://api.weixin.qq.com/sns/jscode2session


請(qǐng)求數(shù)據(jù)示例
GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
返回?cái)?shù)據(jù)示例
{
"openid":"xxxxxx",
"session_key":"xxxxx",
"unionid":"xxxxx",
"errcode":0,
"errmsg":"xxxxx"
}

示例代碼
1、頁面添加登錄按鈕

2、js代碼testLogin
可以看到這個(gè)code是變化的,不能作為登錄的唯一憑證,需要用到下面的code2Session,即調(diào)用 auth.code2Session 接口,換取 用戶唯一標(biāo)識(shí) OpenID


3、這個(gè)code不是唯一的,不能作為登錄的憑證,需要進(jìn)行下一步的驗(yàn)證
如圖所示,用usercode換取openid

打開上次“小白開發(fā)微信小程序21--網(wǎng)絡(luò)API(ASP.NET版”代碼,首先添加包引用,newtonsoft.json

在model文件夾添加類HttpHelper

HttpHelper.cs完整代碼:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace webapitest.Models
{
/// <summary>
/// http請(qǐng)求幫助類
/// </summary>
public class HttpHelper
{
/// <summary>
/// https get請(qǐng)求
/// </summary>
/// <param name="Url">請(qǐng)求地址</param>
/// <returns>System.String.</returns>
public string HttpsGet(string Url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
}
}
在WebXinController中添加方法wxLogin,

完成代碼:
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using webapitest.Models;
namespace webapitest.Controllers
{
/// <summary>
/// 微信小程序控制器
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class WebXinController : ControllerBase
{
List<Book> booklist = null;
private static List<Book> getBookList()
{
List<Book> books = new List<Book>();
Book b1 = new Book();
b1.id = 9032;
b1.title = "java開發(fā)指南";
b1.author = "楊強(qiáng)標(biāo)";
b1.publisher = "吉林出版公司";
b1.image = "img01.jpg";
books.Add(b1);
Book b2 = new Book();
b2.id = 7232;
b2.title = "web項(xiàng)目指導(dǎo)";
b2.author = "毛二平";
b2.publisher = "山東出版公司";
b2.image = "img02.jpg";
books.Add(b2);
Book b3 = new Book();
b3.id = 6832;
b3.title = "小程序教程";
b3.author = "風(fēng)間云";
b3.publisher = "海南出版公司";
b3.image = "img03.jpg";
books.Add(b3);
return books;
}
/// <summary>
/// 圖書列表
/// </summary>
/// <returns></returns>
[HttpGet("findBookList")]
public List<Book> findBookList()
{
booklist = getBookList();
return booklist;
}
/// <summary>
/// 用戶登錄
/// </summary>
/// <returns></returns>
[HttpGet("wxLogin")]
public string wxLogin(string userCode)
{
string appid = "wxd20d97e66580e91e";
string secret = "5100c604bcb817cd0fa0cd801fccd239";
string jscode = userCode;
//https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
string requesturl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + jscode + "&grant_type=authorization_code";
HttpHelper httpHelper = new HttpHelper();
string jsonresult = httpHelper.HttpsGet(requesturl);
JObject jsonobj = JsonConvert.DeserializeObject<JObject>(jsonresult);//序列化成json對(duì)象
string useropenid = jsonobj.GetValue("openid").ToString();//獲取指定屬性的值
string txt = useropenid;
return txt;
}
}
}
小程序頁面js代碼

完成代碼
// pages/myapi/myapi.js
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
userInfo: {},//用戶信息
useropenid: "",
usercode:""
},
//登錄
testLogin() {
let that=this;
wx.login({
success(res) {
if (res.code) {
that.setData({
usercode:res.code
});
console.log(res.code);
}
}
})
},
//微信登錄
doLogin() {
let that = this;
wx.login({
success(res) {
if (res.code) {
const code = res.code;
that.setData({
usercode:res.code
});
wx.request({
url: 'https://localhost:5001/api/WebXin/wxLogin',
data: { "userCode": code },//傳遞參數(shù)
method: "GET",//當(dāng)前請(qǐng)求的請(qǐng)求方式
header: {
'content-type': 'application/json' // 默認(rèn)值
},
success(res) {
const tokendata = res.data;
if (tokendata != "error") {
wx.setStorageSync('token', tokendata);//存儲(chǔ)用戶token
that.setData({
useropenid: tokendata
});
wx.showToast({
title: '登錄成功',
icon: 'success',
duration: 2000
})
}
}
});
}
}
})
},
getUserProfile() {
wx.getUserProfile({
desc: '用于完善會(huì)員資料',
success: (res) => {
this.setData({
userInfo: res.userInfo
})
}
})
},
// 添加設(shè)備
putdevice() {
wx.showToast({
title: '設(shè)備添加成功',
icon: 'success',
duration: 2000
})
},
// 添加課程
putcourse() {
wx.showToast({
title: '課程添加失敗',
icon: 'error',
duration: 3000
})
},
// 刪除
delbook() {
wx.showModal({
title: '提示',
content: '您是否同意【最美鄉(xiāng)村】評(píng)選結(jié)果?',
confirmText: "贊成",
cancelText: "反對(duì)",
success(res) {
if (res.confirm) {
wx.showToast({
title: '你投了贊成票',
icon: 'success',
duration: 2000
})
} else if (res.cancel) {
wx.showToast({
title: '你投了反對(duì)票',
icon: 'error',
duration: 3000
})
}
}
})
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad(options) {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面顯示
*/
onShow() {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面隱藏
*/
onHide() {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面卸載
*/
onUnload() {
},
/**
* 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動(dòng)作
*/
onPullDownRefresh() {
},
/**
* 頁面上拉觸底事件的處理函數(shù)
*/
onReachBottom() {
},
/**
* 用戶點(diǎn)擊右上角分享
*/
onShareAppMessage() {
}
})
點(diǎn)我登錄

成功拉到了openid,這個(gè)openid才是小程序用戶的唯一標(biāo)識(shí),這也是作為token。文章來源:http://www.zghlxwxcb.cn/news/detail-611550.html
帥是一個(gè)境界,相信自己的傳說。文章來源地址http://www.zghlxwxcb.cn/news/detail-611550.html
到了這里,關(guān)于小白開發(fā)微信小程序23--開放接口API的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!