Initial commit of the WeChat mini program, Express backend, and admin console. Co-authored-by: Cursor <cursoragent@cursor.com>
43 lines
1.5 KiB
JavaScript
43 lines
1.5 KiB
JavaScript
const requestUtil = require('./request')
|
|
const authUtil = require('./auth')
|
|
|
|
// Runs a logged-in call; if the stored token has expired, logs in again and retries once.
|
|
function withLogin(run) {
|
|
return authUtil
|
|
.ensureLogin()
|
|
.then(run)
|
|
.catch((error) => {
|
|
if (error.statusCode !== 401) throw error
|
|
return authUtil.ensureLogin().then(run)
|
|
})
|
|
}
|
|
|
|
function authed(url, method, data) {
|
|
return withLogin(() =>
|
|
requestUtil.request({ url: url, method: method || 'GET', data: data, auth: true })
|
|
)
|
|
}
|
|
|
|
function uploadTo(url, filePath) {
|
|
return withLogin(() => requestUtil.upload({ url: url, filePath: filePath }))
|
|
}
|
|
|
|
module.exports = {
|
|
currentActivity: () => requestUtil.request({ url: '/api/activity/current' }),
|
|
pastActivities: () => requestUtil.request({ url: '/api/activity/past' }),
|
|
activity: (id) => requestUtil.request({ url: '/api/activity/' + id }),
|
|
|
|
profile: () => authed('/api/user/profile'),
|
|
saveProfile: (data) => authed('/api/user/profile', 'POST', data),
|
|
uploadAvatar: (filePath) => uploadTo('/api/user/avatar', filePath),
|
|
|
|
createPast: (data) => authed('/api/activity/past', 'POST', data),
|
|
pinPast: (id, pinned) => authed('/api/activity/past/' + id + '/pin', 'POST', { pinned: pinned }),
|
|
removePast: (id) => authed('/api/activity/past/' + id, 'DELETE'),
|
|
uploadMedia: (filePath) => uploadTo('/api/upload', filePath),
|
|
|
|
signup: (data) => authed('/api/signup', 'POST', data),
|
|
mySignups: () => authed('/api/signup/mine'),
|
|
checkin: (data) => authed('/api/checkin/scan', 'POST', data)
|
|
}
|