app.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //app.js
  2. App({
  3. onLaunch: function () {
  4. // 展示本地存储能力
  5. var logs = wx.getStorageSync('logs') || []
  6. logs.unshift(Date.now())
  7. wx.setStorageSync('logs', logs)
  8. },
  9. onshow:function(){
  10. },
  11. //网络请求
  12. netPost(options = {}) {
  13. wx.showLoading({ mask: true, title: '', })
  14. wx.request({
  15. url: this.globalData.postUrl + options._url,
  16. data: options._data || {},
  17. method: "POST",
  18. dataType: "json",
  19. header: this.globalData.header,
  20. success: (res) => {
  21. if (res.data.errcode > 0) {
  22. if (typeof options._success == "function") {
  23. options._success(res.data);
  24. }
  25. } else {
  26. this.xcxErrorToast({ title: res.data.errmsg || '服务器返回错误!' });
  27. return;
  28. }
  29. },
  30. fail: (res) => {
  31. if (typeof options._fail == "function") {
  32. options._fail(res);
  33. }
  34. if (typeof options._fail == "string") { //请求失败的弹框提示
  35. wx.showToast({ title: options._fail, icon: 'loading', duration: 2000 });
  36. }
  37. },
  38. complete: (res) => {
  39. if (typeof options._complete == "function") {
  40. options._complete(res);
  41. }
  42. wx.hideLoading()
  43. }
  44. });
  45. },
  46. //全局属性
  47. globalData: {
  48. userInfo: {},
  49. postUrl: (wx.getExtConfigSync().request_url || '(后台接口地址)'),
  50. header: {
  51. 'content-type': 'application/x-www-form-urlencoded',
  52. 'Cookie': ''
  53. },
  54. }
  55. })