airbnb / goji-js

React ❤️ Mini Program
https://goji.js.org
MIT License
224 stars 27 forks source link

Temporary bugfix for getStorage issue on WeChat SDK 2.24.0 #163

Closed malash closed 2 years ago

malash commented 2 years ago

Add these code to your app.js file at the first line.

app.js 的开头添加如下代码:

TypeScript version:

TypeScript 版本:

const patchedGetStorage: typeof wx.getStorage = options => {
  if (!wx.getStorageInfoSync().keys.includes(options.key)) {
    options?.fail?.({ errMsg: 'getStorage:fail data not found' });

    return;
  }
  const data = wx.getStorageSync(options.key);
  options?.success?.({ errMsg: 'getStorage:ok', data });
};

Object.defineProperty(wx, 'getStorage', {
  value: patchedGetStorage,
  writable: false,
  enumerable: true,
  configurable: true,
});

or use JavaScript version:

或者使用 JavaScript 版本:

const patchedGetStorage = options => {
  var _options$success;

  if (!wx.getStorageInfoSync().keys.includes(options.key)) {
    var _options$fail;

    options === null || options === void 0 ? void 0 : (_options$fail = options.fail) === null || _options$fail === void 0 ? void 0 : _options$fail.call(options, {
      errMsg: 'getStorage:fail data not found'
    });
    return;
  }

  const data = wx.getStorageSync(options.key);
  options === null || options === void 0 ? void 0 : (_options$success = options.success) === null || _options$success === void 0 ? void 0 : _options$success.call(options, {
    errMsg: 'getStorage:ok',
    data
  });
};

Object.defineProperty(wx, 'getStorage', {
  value: patchedGetStorage,
  writable: false,
  enumerable: true,
  configurable: true
});

Ref: https://developers.weixin.qq.com/community/develop/doc/000280984dc3781994cdb8c465b800 Ref: https://developers.weixin.qq.com/community/develop/doc/00062e41d4cb082992cdcd7e656400 Ref: https://developers.weixin.qq.com/community/develop/doc/0006ae11bccd902a93cd714ac51000 Ref: https://developers.weixin.qq.com/community/develop/doc/0000624e1f49387a94cde28a151000

malash commented 2 years ago

This bug should be fixed since WeChat reverted their SDK.