app.js 2.04 KB
//app.js
let fetchApi = require('./http/fetch-api.js');
let api = require('./http/api');
let config = require('./config');
let router = require('./router/index');
let store = require('./utils/stroage');

require('./http/mock-data');

App({
  get: fetchApi.fetch,
  post: (params) => {
    params.method = 'post';
    return fetchApi.fetch(params);
  },
  api: api,
  config: config,
  router: router,
  store: store,
  globalData: {
    indexInfo: null,
    userInfo: null,
    wxcode: store.getItem("wxcode"),
    tlMemberCode: "",
    curCoupon: null,
    questionResult: null, //答题的结果 弹力值 elasticValue
    posterWishList: [], //海报用
    giftData: null, // 实物奖  userTabel prizeDetail用
  },
  onLaunch: function () {
    this.share();
  },
  //重写分享方法
  share: function () {
    //监听路由切换
    //间接实现全局设置分享内容
    wx.onAppRoute(function (res) {
      //获取加载的页面
      let pages = getCurrentPages();
      //获取当前页面的对象
      let view = pages[pages.length - 1];
      let data;
      if (view) {
        data = view.data;
        if (!data.isOverShare) {
          data.isOverShare = true;
          view.onShareAppMessage = function (res) {
            //分享配置
            return {
              title: '弹弹弹 看你有多弹',
              path: 'pages/index/index'
            };
          };
        }
      }
    })
  },
  // 获取用户基本信息
  queryIndex(query = {
    auth: true
  }) {
    return new Promise((resolve, reject) => {
      this.post({
        url: this.api.index,
        data: {},
        loading: true,
      }).then((result) => {
        this.globalData.indexInfo = result;
        this.globalData.userInfo = result.userInfo;
        if (result.isNeedAuth == 1 && auth == true) {
          //需要授权
          this.router.push({
            path: "authorize"
          })
          reject();
          // resolve();
        } else {
          resolve(result)
        }
      }).catch((err) => {
        reject();
      });
    });
  }
})