const Http = require("./utils/HttpBasics.js")
const config = require("./config/config.js");
App({
  data: {},
  globalData: {
    token: null,
    imgHttps: 'https://formall.oss-accelerate.aliyuncs.com/douyin/drawable-xhdpi/',
    platform: null, //手机型号
    systemInfo: "",
    curHtml: "",
    marketName: "",
    mouldType: 1,
    appId: config.weapp.AppId,
    selected: 0,
    goHomeUrl: "/index/index",
    type: "", //存放跳转标记
    previewFlag: false, //解决图片预览调用noshow
    skipUrl: "", //存放授权跳转地址
    skip: "",
    avatarUrl: "", //用户头像
    nickName: "", //用户姓名
    templateId: [], //订阅消息模板id 
    ifshowtab: false,
  },
  onLaunch: function () {
    let that = this
    tt.setStorageSync('locationFlag', true);
    try {
      var res = tt.getSystemInfoSync();
      that.statusBarHeight = res.statusBarHeight
      console.log(`手机型号为`, that.statusBarHeight);
      this.globalData.platform = res.platform
    } catch (error) {
      console.log(`获取系统信息失败`);
    }
    that.login();
    tt.getSystemInfo({
      success(res) {
        console.log(res, "**");
        that.globalData.systemInfo = res

      },
      fail(res) {
        console.log(`getSystemInfo 调用失败`);
      },
    });

  },

  statusBarHeight: 0,
  getTemplateId(that) {
    // console.log("隐藏")
    //  tt.hideTabBar()
    Http.get({
      url: config.api.templateId,
      data: {
        pageNum: 1,
        pageSize: 100
      }
    }).then(res => {
      const {
        code,
        data
      } = res
      if (code == 200) {
        // this.setData({
        //   templateId: data.list
        // })
        that.globalData.templateId = data.list
      } else {
        return
      }
    })
  },

  login() {
    let that = this;
    tt.login({
      success: (res => {
        let {
          code
        } = res
        console.log(code, "code");
        Http.post({
          url: config.api.login,
          data: {
            code: code,
            appId: that.globalData.appId,
            systemInfo: JSON.stringify(this.globalData.systemInfo)
          }
        }).then(res => {
          tt.setStorageSync("openId", res.data.openId);
          if (res.data.mall) {
            that.globalData.marketName = res.data.mall.name ? res.data.mall.name : ""
          }
          that.globalData.mouldType = res.data.mouldType ? res.data.mouldType : 0;
          if (res.data.subMalls) {
            const squareList = JSON.parse(res.data.subMalls)
            tt.setStorageSync("squareList", squareList)
            if (res.data.mall) {
              tt.setStorageSync("selectedMall", res.data.mall.tenantId)
            }
          }
          Http.setToken(res.data.token);
          that.globalData.token = res.data.token
          that.getTemplateId(that); //获取订阅消息模板id
          if (that.tokenCallback) {
            that.tokenCallback(res.data.token);
          }

        }).catch(err => {
          console.log(err, 'getUserInfo');
          tt.showModal({
            title: '提示',
            showCancel: false,
            content: '登录失败，请重新尝试',
            success: (res) => {
              if (res.cancel) {
                //点击取消,默认隐藏弹框
              } else {
                //点击确定
                tt.reLaunch({
                  url: '/pages/index/index',
                })
              }
            }
          });
        })
      }),
    });
  }
})