小程序模板网

在微信小程序里使用 facebook 的Parse JS SDK

2018-04-18 缠齿羊

作者:sami,来自原文地址 
微信小程序的API 重写了网络请求和本地缓存的访问方式

所以为了使用parse的js sdk 需要修改这两块

  1. 增加localStorage变量
 

  1. var localStorage = {
  2. getItem:function(path){
  3. return wx.getStorageSync(path);
  4. },
  5. setItem:function(path, value){
  6. return wx.setStorageSync(path, value);
  7. },
  8. removeItem:function(path){
  9. wx.removeStorageSync(path)
  10. },
  11. clear:function(){
  12. wx.clearStorageSync()
  13. }
  14. };
  15. var StorageController = {
  16. async: 0,
  17.  
  18. getItem: function (path) {
  19. return localStorage.getItem(path);
  20. },
  21. setItem: function (path, value) {
  22. try {
  23. localStorage.setItem(path, value);
  24. } catch (e) {
  25. // Quota exceeded, possibly due to Safari Private Browsing mode
  26. }
  27. },
  28. removeItem: function (path) {
  29. localStorage.removeItem(path);
  30. },
  31. clear: function () {
  32. localStorage.clear();
  33. }
  34. };
  35. StorageController 可以按照浏览器里的方式使用
  1. 修改RESTController 增加 wxRequest 方法
 

  1. wxRequest: function(method, url, data, payloadString,headers){
  2. var promise = new _ParsePromise2.default();
  3. var jsKey = _CoreManager2.default.get('JAVASCRIPT_KEY');
  4. var masterKey = _CoreManager2.default.get('MASTER_KEY');
  5. var appId = _CoreManager2.default.get('APPLICATION_ID');
  6. var payload = JSON.parse(payloadString);
  7.  
  8. headers = headers || {};
  9. headers["content-type"] = "application/json";
  10. headers["X-Parse-Application-Id"] = appId;
  11. headers["X-Parse-JavaScript-Key"] = jsKey;
  12. if(masterKey){
  13. headers["X-Parse-Master-Key"] = masterKey;
  14. }
  15. wx.request({
  16. url: url,
  17. data: data,
  18. header: headers,
  19. method:payload._method,
  20. success: function(res) {
  21. console.log(res)
  22. var response;
  23. try {
  24. //response = JSON.parse(res.data);
  25. promise.resolve(res.data, res.status, res);
  26. } catch (e) {
  27. promise.reject(e.toString());
  28. }
  29.  
  30. },
  31. fail: function(err){
  32. console.log(err)
  33. promise.reject(err.toString());
  34. }
  35. });
  36. return promise;
  37. }
  38.  
  39. //修改请求方法
  40. //return RESTController.ajax(method, url, payloadString);
  41. return RESTController.wxRequest(method, url, data);
  42. //默认的ajax方法替换为微信小程序的网络请求方法


易优小程序(企业版)+灵活api+前后代码开源 码云仓库:starfork
本文地址:https://www.eyoucms.com/wxmini/doc/course/23646.html 复制链接 如需定制请联系易优客服咨询:800182392 点击咨询
QQ在线咨询