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,75 @@
|
||||
const requestUtil = require('./request')
|
||||
|
||||
let pending = null
|
||||
|
||||
function deviceId() {
|
||||
let id = wx.getStorageSync('deviceId')
|
||||
if (!id) {
|
||||
id = Date.now().toString(36) + Math.random().toString(36).slice(2, 8)
|
||||
wx.setStorageSync('deviceId', id)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
function captureSharer(query) {
|
||||
if (!query || !query.sharer) return
|
||||
const selfId = wx.getStorageSync('openid')
|
||||
if (query.sharer !== selfId) wx.setStorageSync('sharer', query.sharer)
|
||||
}
|
||||
|
||||
function login() {
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.login({
|
||||
success(res) {
|
||||
if (!res.code) {
|
||||
reject(new Error('微信登录未返回 code'))
|
||||
return
|
||||
}
|
||||
requestUtil
|
||||
.request({
|
||||
url: '/api/login',
|
||||
method: 'POST',
|
||||
data: { code: res.code, deviceId: deviceId() }
|
||||
})
|
||||
.then((data) => {
|
||||
wx.setStorageSync('token', data.token)
|
||||
wx.setStorageSync('openid', data.openid)
|
||||
wx.setStorageSync('loginDemo', !!data.demo)
|
||||
resolve(data)
|
||||
})
|
||||
.catch(reject)
|
||||
},
|
||||
fail() {
|
||||
reject(new Error('微信登录失败'))
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function ensureLogin() {
|
||||
if (wx.getStorageSync('token')) {
|
||||
return Promise.resolve({
|
||||
token: wx.getStorageSync('token'),
|
||||
openid: wx.getStorageSync('openid')
|
||||
})
|
||||
}
|
||||
if (!pending) {
|
||||
pending = login().finally(() => {
|
||||
pending = null
|
||||
})
|
||||
}
|
||||
return pending
|
||||
}
|
||||
|
||||
function sourceSharer() {
|
||||
const sharer = wx.getStorageSync('sharer') || ''
|
||||
const openid = wx.getStorageSync('openid') || ''
|
||||
if (!sharer || sharer === openid) return ''
|
||||
return sharer
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
captureSharer: captureSharer,
|
||||
ensureLogin: ensureLogin,
|
||||
sourceSharer: sourceSharer
|
||||
}
|
||||
Reference in New Issue
Block a user