作者:sami,来自原文地址
微信小程序的API 重写了网络请求和本地缓存的访问方式
所以为了使用parse的js sdk 需要修改这两块
增加localStorage变量
var localStorage = {
getItem:function(path){
return wx.getStorageSync(path);
},
setItem:function(path, value){
return wx.setStorageSync(path, value);
},
removeItem:function(path){
wx.removeStorageSync(path)
},
clear:function(){
wx.clearStorageSync()
}
};
var StorageController = {
async: 0,
getItem: function (path) {
return localStorage.getItem(path);
},
setItem: function (path, value) {
try {
localStorage.setItem(path, value);
} catch (e) {
// Quota exceeded, possibly due to Safari Private Browsing mode
}
},
removeItem: function (path) {
localStorage.removeItem(path);
},
clear: function () {
localStorage.clear();
}
};
StorageController 可以按照浏览器里的方式使用
修改RESTController 增加 wxRequest 方法
wxRequest: function(method, url, data, payloadString,headers){
var promise = new _ParsePromise2.default();
var jsKey = _CoreManager2.default.get('JAVASCRIPT_KEY');
var masterKey = _CoreManager2.default.get('MASTER_KEY');
var appId = _CoreManager2.default.get('APPLICATION_ID');
var payload = JSON.parse(payloadString);
headers = headers || {};
headers["content-type"] = "application/json";
headers["X-Parse-Application-Id"] = appId;
headers["X-Parse-JavaScript-Key"] = jsKey;
if(masterKey){
headers["X-Parse-Master-Key"] = masterKey;
}
wx.request({
url: url,
data: data,
header: headers,
method:payload._method,
success: function(res) {
console.log(res)
var response;
try {
//response = JSON.parse(res.data);
promise.resolve(res.data, res.status, res);
} catch (e) {
promise.reject(e.toString());
}
},
fail: function(err){
console.log(err)
promise.reject(err.toString());
}
});
return promise;
}
//修改请求方法
//return RESTController.ajax(method, url, payloadString);
return RESTController.wxRequest(method, url, data);
//默认的ajax方法替换为微信小程序的网络请求方法