Last active
March 4, 2024 01:49
-
-
Save romain130492/8476582ece774c7677863aa9d93c1ebf to your computer and use it in GitHub Desktop.
Wechat : Login the user, get the openId, getUserInfo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* We login the user to our mini-program | |
* We get the user Info (the user info don't contain the unionId) | |
* We make a request to our back-end to request the unionId. | |
* * For security reason wechat doesn't allow us to make that request on the front-end : https://api.weixin.qq.com/sns/jscode2session?appid=${process.env.APP_ID}&secret=${process.env.APP_SECRET}&js_code=${js_code} | |
* * To make that request we need(see at the bottom) : {secret,appId, js_code(login code)} | |
/** Client-Side **/ | |
import http from '../utils/http.js' | |
/** | |
* @description Handle the login to wechat | |
* https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserInfo.html | |
* @returns {Promise} Return the user's wechat information. | |
* {"openId", "nickName","gender", "city", "province", "country", "avatarUrl" } | |
*/ | |
export function handleWxLogin() { | |
return new Promise((resolve, reject) => { | |
wx.login({ | |
success : async function (res) { | |
const js_code = res.code; | |
const userInfo = getUserInfo() | |
const openID = getOpenID(js_code) | |
const payload = { | |
...await userInfo, | |
...{openID:await openID} | |
} | |
console.info('handleWxLogin payload:', payload); | |
resolve(payload) | |
}, | |
fail: function(err){ | |
console.log('wx.login error:',err); | |
reject(err) | |
} | |
}) | |
}) | |
} | |
/** | |
* @description GET request to get the OpenID from the server. | |
* @param {String} js_code | |
* @returns {Promise} Return openID | |
*/ | |
async function getOpenID(js_code){ | |
// A GET request to our back-end. | |
return http.get(`wxUnionID?js_code=${js_code}`).then(res => { | |
if(res){ | |
return res.data.openid; | |
} | |
}).catch(err => { | |
console.warn('getOpenID error:',err,); | |
}) | |
} | |
/** | |
* @description Get the userInfo with wx.getUserInfo | |
* https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserInfo.html | |
* @returns {Promise} returns an object | |
*/ | |
function getUserInfo(){ | |
return new Promise((resolve, reject) => { | |
wx.getUserInfo({ | |
success: async function(res) { | |
const userInfo = res.userInfo; | |
resolve(userInfo) | |
}, | |
fail: function(err){ | |
console.warn('getUserInfo error:',err,); | |
reject('getUserInfo error:',err) | |
} | |
}) | |
}) | |
} | |
/** BACKEND Side **/ | |
const bent = require("bent"); | |
/** | |
* @description We get the openid from weixin. | |
* https://developers.weixin.qq.com/miniprogram/en/dev/api-backend/open-api/login/auth.code2Session.html | |
* @param {String} js_code (the code provided during the login) | |
* APP_ID and APP_SECRET can be found on the developper dashboard. | |
* @returns {Promise} {session_key, openid} | |
*/ | |
module.exports = { | |
wxUnionID: async ctx => { | |
const query = ctx.request.query; | |
const js_code = query.js_code; | |
const getJSON = bent("json"); | |
const user = await getJSON( | |
`https://api.weixin.qq.com/sns/jscode2session?appid=${process.env.APP_ID}&secret=${process.env.APP_SECRET}&js_code=${js_code}` | |
); | |
console.info('user:', user) | |
if(user.openid){ | |
ctx.send({ | |
statusCode: 200, | |
error: null, | |
data:user, | |
}); | |
}else{ | |
ctx.send({ | |
statusCode: 400, | |
error: user, | |
data:null, | |
}); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment