Add 卓米会 mini program and admin server.
Initial commit of the WeChat mini program, Express backend, and admin console. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
const crypto = require('crypto')
|
||||
const { HttpError } = require('./http')
|
||||
|
||||
async function code2session(code, deviceId) {
|
||||
const appid = process.env.WX_APPID
|
||||
const secret = process.env.WX_SECRET
|
||||
if (!appid || !secret) {
|
||||
const seed = deviceId || 'guest'
|
||||
return {
|
||||
openid: `demo_${crypto.createHash('sha1').update(seed).digest('hex').slice(0, 16)}`,
|
||||
demo: true
|
||||
}
|
||||
}
|
||||
|
||||
const url = new URL('https://api.weixin.qq.com/sns/jscode2session')
|
||||
url.search = new URLSearchParams({
|
||||
appid,
|
||||
secret,
|
||||
js_code: code,
|
||||
grant_type: 'authorization_code'
|
||||
}).toString()
|
||||
|
||||
let session
|
||||
try {
|
||||
const res = await fetch(url, { signal: AbortSignal.timeout(8000) })
|
||||
session = await res.json()
|
||||
} catch (error) {
|
||||
throw new HttpError(502, '微信登录服务不可用')
|
||||
}
|
||||
if (!session.openid) throw new HttpError(400, session.errmsg || '微信登录失败')
|
||||
return { openid: session.openid, demo: false }
|
||||
}
|
||||
|
||||
module.exports = { code2session }
|
||||
Reference in New Issue
Block a user