铁雪资源网 Design By www.gsvan.com

微信小程序-获取用户session_key,openid,unionid - 后端为nodejs8.0+

步骤:

1、通过wx.login接口获取code既jscode,传递到后端;

2、后端请求

https://api.weixin.qq.com/sns/jscode2session"color: #ff0000">1、utils文件夹下创建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 }));
 };
 
 /**
  * 发起网络请求
  * @param {string} url 
  * @param {object} params 
  * @return {Promise} 
  */
 static request(url, params, method = "GET", type = "json") {
  console.log("向后端传递的参数", 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("请求的URL", opts.url);
   wx.request(opts);
  });
 };
 
 /**
  * 获取微信数据,传递给后端
  */
 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命令行生成的项目框架,

1、创建common文件夹,创建utils文件,使用request模块请求接口,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在小程序的后台获取

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"text-align: center">详解微信小程序-获取用户session_key,openid,unionid - 后端为nodejs

这个返回结果没有unionid,按照官方的说法,需要在微信开放平台绑定小程序;

参考地址:

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的方法详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

标签:
微信小程序获取session_key,openid,unionid,session_key,openid,unionid

铁雪资源网 Design By www.gsvan.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
铁雪资源网 Design By www.gsvan.com

评论“详解微信小程序-获取用户session_key,openid,unionid - 后端为nodejs”

暂无详解微信小程序-获取用户session_key,openid,unionid - 后端为nodejs的评论...