詳解微信小程序-獲取用戶session_key,openid,unionid - 后端為nodejs
微信小程序-獲取用戶session_key,openid,unionid - 后端為nodejs8.0+
步驟:
1、通過wx.login接口獲取code既jscode,傳遞到后端;
2、后端請(qǐng)求
地址,就能獲取到openid和unionid。
小程序接口promise化和封裝
1、utils文件夾下創(chuàng)建wechat.js文件
/**
* Promise化小程序接口
*/
class Wechat {
/**
* 登陸
* @return {Promise}
*/
static login() {
return new Promise((resolve, reject) => wx.login({ success: resolve, fail: reject }));
};
/**
* 獲取用戶信息
* @return {Promise}
*/
static getUserInfo() {
return new Promise((resolve, reject) => wx.getUserInfo({ success: resolve, fail: reject }));
};
/**
* 發(fā)起網(wǎng)絡(luò)請(qǐng)求
* @param {string} url
* @param {object} params
* @return {Promise}
*/
static request(url, params, method = "GET", type = "json") {
console.log("向后端傳遞的參數(shù)", params);
return new Promise((resolve, reject) => {
let opts = {
url: url,
data: Object.assign({}, params),
method: method,
header: { 'Content-Type': type },
success: resolve,
fail: reject
}
console.log("請(qǐng)求的URL", opts.url);
wx.request(opts);
});
};
/**
* 獲取微信數(shù)據(jù),傳遞給后端
*/
static getCryptoData() {
let code = "";
return this.login()
.then(data => {
code = data.code;
console.log("login接口獲取的code:", code);
return this.getUserInfo();
})
.then(data => {
console.log("getUserInfo接口", data);
let obj = {
js_code: code,
};
return Promise.resolve(obj);
})
.catch(e => {
console.log(e);
return Promise.reject(e);
})
};
/**
* 從后端獲取openid
* @param {object} params
*/
static getMyOpenid(params) {
let url = 'https://xx.xxxxxx.cn/api/openid';
return this.request(url, params, "POST", "application/x-www-form-urlencoded");
};
}
module.exports = Wechat;
2、修改小程序的app.js文件
let wechat = require('./utils/wechat.js');
App({
onLaunch() {
this.getUserInfo();
},
getUserInfo() {
wechat.getCryptoData()
.then(d => {
return wechat.getMyOpenid(d);
})
.then(d => {
console.log("從后端獲取的openid", d.data);
})
.catch(e => {
console.log(e);
})
}
})
后端nodejs,是用的express命令行生成的項(xiàng)目框架,
1、創(chuàng)建common文件夾,創(chuàng)建utils文件,使用request模塊請(qǐng)求接口,promise化request
const request = require("request");
class Ut {
/**
* promise化request
* @param {object} opts
* @return {Promise<[]>}
*/
static promiseReq(opts = {}) {
return new Promise((resolve, reject) => {
request(opts, (e, r, d) => {
if (e) {
return reject(e);
}
if (r.statusCode != 200) {
return reject(`back statusCode:${r.statusCode}`);
}
return resolve(d);
});
})
};
};
module.exports = Ut;
2、新增路由,appId、secret在小程序的后臺(tái)獲取
router.post("/openid", async (req, res) => {
const Ut = require("../common/utils");
try {
console.log(req.body);
let appId = "wx70xxxxxxbed01b";
let secret = "5ec6exxxxxx49bf161a79dd4";
let { js_code } = req.body;
let opts = {
url: `https://api.weixin.qq.com/sns/jscode2session?appid=${appId}&secret=${secret}&js_code=${js_code}&grant_type=authorization_code`
}
let r1 = await Ut.promiseReq(opts);
r1 = JSON.parse(r1);
console.log(r1);
res.json(r1);
}
catch (e) {
console.log(e);
res.json('');
}
})
結(jié)果:

這個(gè)返回結(jié)果沒有unionid,按照官方的說法,需要在微信開放平臺(tái)綁定小程序;
參考地址:
https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-login.html
https://mp.weixin.qq.com/debug/wxadoc/dev/api/uinionID.html
以上所述是小編給大家介紹的微信小程序獲取session_key,openid,unionid的方法詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
梳理總結(jié)JavaScript的23個(gè)String方法
文章主要介紹了梳理總結(jié)JavaScript的23個(gè)String方法,JavaScript?中的String類型用于表示文本型的數(shù)據(jù)。它是由無符號(hào)整數(shù)值作為元素而組成的集合,更多詳細(xì)內(nèi)容需要的朋友可以參考一下2022-07-07
JavaScript之DOM插入更新刪除_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了JavaScript之DOM插入更新刪除,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
JS奇技之利用scroll來監(jiān)聽resize詳解
這篇文章主要給大家介紹了JS奇技之利用scroll來監(jiān)聽resize的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-06-06
微信小程序購物車、父子組件傳值及calc的注意事項(xiàng)總結(jié)
這篇文章主要給大家介紹了關(guān)于微信小程序購物車、父子組件傳值及calc的注意事項(xiàng)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
Javascript的嚴(yán)格模式strict mode詳細(xì)介紹
這篇文章主要介紹了Javascript的嚴(yán)格模式strict mode詳細(xì)介紹,重點(diǎn)介紹了嚴(yán)格模式的使用方法及使用strict mode后對(duì)javascript語法上帶來的改變,需要的朋友可以參考下2014-06-06
npm install 、npm install --save 和&n
這篇文章主要介紹了npm install 、npm install --save 和 npm install --save-dev的區(qū)別介紹,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
JavaScript中使用構(gòu)造器創(chuàng)建對(duì)象無需new的情況說明
JS中創(chuàng)建對(duì)象可以直接使用直接量的方式,這里討論的是定義一個(gè)構(gòu)造器(function)的情況2012-03-03

