Files
past-go/weapp/utils/storage-guard.js
Blizzard 4c9708b562 feat: add WeChat Pay, Redis logging, storage tiers and personal albums
Replace virtual payments with WeChat Pay API v3 and verify callbacks and order polling. Cache WeChat access tokens in Redis and write structured Zap logs. Support configurable capacity plans across the API, admin and mini program, with purchase snapshots and quota enforcement. Allow personal albums before binding and share them with the current partner. Update deployment configuration and domain routing.
2026-11-02 23:40:49 +08:00

44 lines
1.5 KiB
JavaScript

// utils/storage-guard.js
// 存储空间统一拦截:上传照片/图片前检查剩余空间,超出时在按钮处直接阻止上传。
const { api } = require('./request');
// 只有无限容量套餐免配额检查,付费有限套餐同样需要校验。
function quotaExceeded(d, extraBytes) {
if (!d) return false;
const unlimited = d.is_unlimited || d.isUnlimited;
if (unlimited) return false;
const free = d.quota_bytes != null ? d.quota_bytes : (d.quotaBytes != null ? d.quotaBytes : (d.free_bytes || d.freeBytes || 0));
const used = d.used_bytes || d.usedBytes || 0;
return used + (extraBytes || 0) > free;
}
// 空间不足时的统一提示
function showQuotaBlocked() {
wx.showModal({
title: '存储空间不足',
content: '当前存储空间已满,请选择更大容量的套餐后再上传照片。',
confirmText: '去升级',
cancelText: '取消',
success: function (res) {
if (res.confirm) wx.navigateTo({ url: '/pages/storage/storage' });
}
});
}
// 上传前检查:空间充足 resolve(true);不足则弹窗提示并 resolve(false);查询失败不阻断上传(由服务端二次校验兜底)。
function guardStorageSpace(extraBytes) {
return api.storageInfo()
.then(function (d) {
if (quotaExceeded(d, extraBytes || 0)) {
showQuotaBlocked();
return false;
}
return true;
})
.catch(function () {
return true;
});
}
module.exports = { guardStorageSpace, quotaExceeded, showQuotaBlocked };