feat(M08-A): 接入顾客端选店下单入口

This commit is contained in:
Codex
2026-06-24 21:26:32 +08:00
parent c90f6e34a1
commit 4ac2c960e4
20 changed files with 734 additions and 32 deletions
+50 -1
View File
@@ -1,13 +1,17 @@
const { APP_API_BASE_URL, WECHAT_APP_ID } = require('../config/env.js')
const TOKEN_KEY = 'qipai_access_token'
function request(path, options = {}) {
return new Promise((resolve, reject) => {
const token = wx.getStorageSync(TOKEN_KEY)
wx.request({
url: `${APP_API_BASE_URL}${path}`,
method: options.method || 'GET',
data: options.data,
header: {
'x-wechat-appid': WECHAT_APP_ID,
...(token ? { authorization: `Bearer ${token}` } : {}),
...(options.headers || {}),
},
success(response) {
@@ -22,4 +26,49 @@ function request(path, options = {}) {
})
}
module.exports = { request }
function login() {
return new Promise((resolve, reject) => {
wx.login({
success: async ({ code }) => {
try {
const response = await request('/auth/wechat-login', {
method: 'POST',
data: { code },
})
wx.setStorageSync(TOKEN_KEY, response.data.accessToken)
resolve(response.data)
} catch (error) {
reject(error)
}
},
fail: reject,
})
})
}
function ensureLogin() {
const token = wx.getStorageSync(TOKEN_KEY)
if (token) return Promise.resolve({ accessToken: token })
return login()
}
function clearSession() {
wx.removeStorageSync(TOKEN_KEY)
}
function cents(value) {
return `${((Number(value || 0)) / 100).toFixed(2)}`
}
function clientRequestId(prefix) {
return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`
}
module.exports = {
request,
login,
ensureLogin,
clearSession,
cents,
clientRequestId,
}