Files
plugin-library/Assets/12.WeixinMinigame/Runtime/wechat-default/unity-sdk/storage.js
stary 0d6faa56f4
All checks were successful
Plugin Library CI / publish (00.BuildOriginality) (push) Successful in 13s
Plugin Library CI / publish (00.StaryEvo) (push) Successful in 17s
Plugin Library CI / publish (00.StaryEvoTools) (push) Successful in 35s
Plugin Library CI / publish (01.HybridCLR) (push) Successful in 15s
Plugin Library CI / publish (02.InformationSave) (push) Successful in 3s
Plugin Library CI / publish (03.YooAsset) (push) Successful in 33s
Plugin Library CI / publish (04.AudioCore) (push) Successful in 3s
Plugin Library CI / publish (05.TableTextConversion) (push) Successful in 5s
Plugin Library CI / publish (06.UIFarme) (push) Successful in 15s
Plugin Library CI / publish (07.RKTools) (push) Successful in 2s
Plugin Library CI / publish (08.UniTask) (push) Successful in 3s
Plugin Library CI / publish (09.CodeChecker) (push) Successful in 16s
Plugin Library CI / publish (10.StoryEditor) (push) Successful in 3s
Plugin Library CI / publish (10.XNode) (push) Successful in 3s
Plugin Library CI / publish (11.PointCloudTools) (push) Successful in 2s
Plugin Library CI / publish (12.WeixinMinigame) (push) Successful in 2m32s
微信小程序编译
2026-04-19 00:16:03 +08:00

157 lines
4.2 KiB
JavaScript

/* eslint-disable no-underscore-dangle */
const PreLoadKeys = '$PreLoadKeys';
const storage = {
_cacheData: {},
_handleList: [],
isRunning: false,
isCallDeletedAll: false,
getData(key, defaultValue) {
let v = this._cacheData[key];
if (v === null) {
return defaultValue;
}
if (typeof v !== 'undefined') {
return v;
}
if (this.isCallDeletedAll) {
return defaultValue;
}
try {
v = wx.getStorageSync(key);
this._cacheData[key] = v !== '' ? v : null;
return v === '' ? defaultValue : v;
}
catch (e) {
// console.error(e);
return defaultValue;
}
},
setData(key, value) {
this._cacheData[key] = value;
this._handleList.push({
type: 'setData',
key,
value,
});
this._doRun();
},
deleteKey(key) {
this._cacheData[key] = null;
this._handleList.push({
type: 'deleteKey',
key,
});
this._doRun();
},
deleteAll() {
Object.keys(this._cacheData).forEach((key) => {
this._cacheData[key] = null;
});
this.isCallDeletedAll = true;
this._handleList.push({
type: 'deleteAll',
});
this._doRun();
},
_doRun() {
if (this.isRunning || this._handleList.length === 0) {
return false;
}
this.isRunning = true;
const task = this._handleList.shift();
if (!task) {
this.isRunning = false;
this._doRun();
return;
}
if (task.type === 'setData') {
wx.setStorage({
key: task.key || 'defaultKey',
data: task.value,
fail({ errMsg }) {
console.error(errMsg);
},
complete: () => {
this.isRunning = false;
this._doRun();
},
});
}
else if (task.type === 'deleteKey') {
wx.removeStorage({
key: task.key || 'defaultKey',
fail({ errMsg }) {
console.error(errMsg);
},
complete: () => {
this.isRunning = false;
this._doRun();
},
});
}
else if (task.type === 'deleteAll') {
wx.clearStorage({
fail({ errMsg }) {
console.error(errMsg);
},
complete: () => {
this.isRunning = false;
this._doRun();
},
});
}
else {
this.isRunning = false;
this._doRun();
}
},
init() {
if (Array.isArray(PreLoadKeys) && PreLoadKeys.length > 0) {
const key = PreLoadKeys.shift();
wx.getStorage({
key,
success(res) {
storage._cacheData[key] = res.data;
storage.init();
},
fail() {
storage._cacheData[key] = null;
storage.init();
},
});
}
},
};
setTimeout(() => {
storage.init();
}, 0);
export default {
WXStorageGetIntSync(key, defaultValue) {
return +(storage.getData(key, defaultValue) || '');
},
WXStorageSetIntSync(key, value) {
storage.setData(key, value);
},
WXStorageGetFloatSync(key, defaultValue) {
return +(storage.getData(key, defaultValue) || '');
},
WXStorageSetFloatSync(key, value) {
storage.setData(key, value);
},
WXStorageGetStringSync(key, defaultValue) {
return storage.getData(key, defaultValue) || '';
},
WXStorageSetStringSync(key, value) {
storage.setData(key, value);
},
WXStorageDeleteAllSync() {
storage.deleteAll();
},
WXStorageDeleteKeySync(key) {
storage.deleteKey(key);
},
WXStorageHasKeySync(key) {
return storage.getData(key, '') !== '';
},
};