fix: 修复 VoiceController Map.of 兼容性 + ExploreController 参数不匹配
- VoiceController: Map.of() -> Collections.singletonMap() 兼容 Java 8 - ExploreController: 补齐 takeoutService.roll() 缺失的 taste/priceRange/allergies 参数 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
88
miniapp/utils/api.js
Normal file
88
miniapp/utils/api.js
Normal file
@@ -0,0 +1,88 @@
|
||||
const app = getApp();
|
||||
|
||||
// 客户端缓存:key → { data, expireAt }
|
||||
const CACHE_PREFIX = 'api_cache_';
|
||||
const DEFAULT_TTL = 30 * 60 * 1000; // 默认 30 分钟
|
||||
|
||||
function request(method, path, data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.request({
|
||||
url: app.globalData.baseUrl + path,
|
||||
method: method,
|
||||
data: data,
|
||||
header: { 'Content-Type': 'application/json' },
|
||||
success: (res) => {
|
||||
if (res.statusCode === 200 && res.data.code === 200) {
|
||||
resolve(res.data.data);
|
||||
} else {
|
||||
reject(res.data);
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 带客户端缓存的 GET 请求
|
||||
* @param {string} path 请求路径
|
||||
* @param {object} data 请求参数
|
||||
* @param {number} ttlMs 缓存时长(毫秒),默认 30 分钟
|
||||
*/
|
||||
function getWithCache(path, data, ttlMs) {
|
||||
const cacheKey = CACHE_PREFIX + path + '_' + JSON.stringify(data || {});
|
||||
const ttl = ttlMs || DEFAULT_TTL;
|
||||
|
||||
// 读缓存
|
||||
try {
|
||||
const cached = wx.getStorageSync(cacheKey);
|
||||
if (cached && cached.expireAt > Date.now()) {
|
||||
return Promise.resolve(cached.data);
|
||||
}
|
||||
} catch (e) {
|
||||
// 读取失败,忽略
|
||||
}
|
||||
|
||||
return request('GET', path, data).then((result) => {
|
||||
// 写缓存
|
||||
try {
|
||||
wx.setStorageSync(cacheKey, {
|
||||
data: result,
|
||||
expireAt: Date.now() + ttl
|
||||
});
|
||||
} catch (e) {
|
||||
// 写入失败,忽略(存储满了等)
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除过期缓存
|
||||
*/
|
||||
function clearExpiredCache() {
|
||||
try {
|
||||
const info = wx.getStorageInfoSync();
|
||||
const keys = info.keys || [];
|
||||
const now = Date.now();
|
||||
keys.forEach((key) => {
|
||||
if (key.startsWith(CACHE_PREFIX)) {
|
||||
const cached = wx.getStorageSync(key);
|
||||
if (cached && cached.expireAt < now) {
|
||||
wx.removeStorageSync(key);
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
// 静默失败
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
get: (path, data) => request('GET', path, data),
|
||||
post: (path, data) => request('POST', path, data),
|
||||
getWithCache,
|
||||
clearExpiredCache
|
||||
};
|
||||
37
miniapp/utils/location.js
Normal file
37
miniapp/utils/location.js
Normal file
@@ -0,0 +1,37 @@
|
||||
const app = getApp();
|
||||
|
||||
function getLocation() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const cached = app.globalData.location;
|
||||
if (cached) {
|
||||
resolve(cached);
|
||||
return;
|
||||
}
|
||||
|
||||
wx.getLocation({
|
||||
type: 'gcj02',
|
||||
success: (res) => {
|
||||
const loc = { latitude: res.latitude, longitude: res.longitude };
|
||||
app.globalData.location = loc;
|
||||
resolve(loc);
|
||||
},
|
||||
fail: (err) => {
|
||||
// 常见错误:未授权、隐私协议未同意
|
||||
const msg = err.errMsg || '';
|
||||
if (msg.indexOf('auth deny') >= 0 || msg.indexOf('authorize') >= 0) {
|
||||
wx.showModal({
|
||||
title: '需要定位权限',
|
||||
content: '请在设置中允许小程序获取位置,或点击右上角手动选择位置',
|
||||
confirmText: '去设置',
|
||||
success: (res) => {
|
||||
if (res.confirm) wx.openSetting();
|
||||
}
|
||||
});
|
||||
}
|
||||
reject(new Error('定位失败'));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { getLocation };
|
||||
18
miniapp/utils/storage.js
Normal file
18
miniapp/utils/storage.js
Normal file
@@ -0,0 +1,18 @@
|
||||
function get(key, defaultValue) {
|
||||
try {
|
||||
const value = wx.getStorageSync(key);
|
||||
return value !== '' ? value : defaultValue;
|
||||
} catch (e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
function set(key, value) {
|
||||
wx.setStorageSync(key, value);
|
||||
}
|
||||
|
||||
function remove(key) {
|
||||
wx.removeStorageSync(key);
|
||||
}
|
||||
|
||||
module.exports = { get, set, remove };
|
||||
Reference in New Issue
Block a user