

// app.js
const homeApi_empower = "https://aipush.aidsleep.cn";
App({
  onLaunch() {
    // 在需要隐藏 tabBar 的页面的脚本中调用
    wx.hideTabBar({
      animation: false, // 是否需要动画效果，默认为 false
      success: function () {
        console.log('TabBar 已经被隐藏');
      },
      fail: function (err) {
        console.error('隐藏 TabBar 失败', err);
      }
    });

    this.globalData.SystemInfo = wx.getDeviceInfo()
    this.data.system = this.globalData.SystemInfo.platform;


    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.request({
          url: `${homeApi_empower}/wxssk`,
          method: 'POST',
          data: {
            code: res.code,
          },
          success: function (loginRes) {
            // 处理服务器返回的结果  
            console.log('登录获取结果:', loginRes.data);
            if (loginRes.data) {
              wx.setStorageSync("openid", loginRes.data.openid);
              wx.setStorageSync("token", loginRes.data.token);
              wx.setStorageSync("unionid", loginRes.data.unionid);

              // 判断扫码控制权限
              wx.request({
                url: `${homeApi_empower}/wxstatus`,
                data: {
                  unionid: wx.getStorageSync('unionid'),
                  openid: wx.getStorageSync('openid'),
                  token: wx.getStorageSync('token'),
                },
                method: 'POST',
                success: function (intervalRes) {
                  console.log('intervalRes=' + JSON.stringify(intervalRes));
                  console.log('intervalRes.data=' + JSON.stringify(intervalRes.data));
                  //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
                  if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
                    wx.setStorageSync('hasHotelAuth', true);
                    wx.setStorageSync('hotelEmpower', intervalRes.data.hotel);
                    wx.setStorageSync('roomEmpower', intervalRes.data.room);
                    wx.setStorageSync('hname', intervalRes.data.hname);
                    wx.setStorageSync('rname', intervalRes.data.rname);
                  } else {
                    wx.setStorageSync('hasHotelAuth', false);
                  }
                },
                fail: function (error) {

                }
              });
              // 判断今日睡眠报告权限
              wx.request({
                url: 'https://aipush.aidsleep.cn/srpwithrange',
                method: 'POST',
                data: {
                  token: "89835e65993fee4a6a6cbbe4c271da51e5521822934e13769e61cadabaed72c3" || wx.getStorageSync('token'),
                  udi: wx.getStorageSync('unionid'),
                  pg: 1,
                  lm: 1
                },
                success: (res) => {
                  var loadData = res.data.data;
                  if ((Array.isArray(loadData) && loadData.length > 0) || (loadData && !Array.isArray(loadData)) ) {
                    wx.setStorageSync('hasReportAuth', true);
                  } else {
                    wx.setStorageSync('hasReportAuth', false);
                  }
                },
                fail: () => {
                  wx.setStorageSync('hasReportAuth', false);
                }
              });

            }
          }
        })
      }
    })
  },
  onShow() {
    wx.hideTabBar()
  },
  onHide: function () {
    wx.setStorageSync('userName', '');
    wx.setStorageSync('isLogin', false);
    wx.setStorageSync('deviceList', []);
    wx.setStorageSync('doDevices', []);
  },
  buf2hex: function (buffer) {
    return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('')
  },
  buf2string: function (buffer) {
    var arr = Array.prototype.map.call(new Uint8Array(buffer), x => x)
    var str = ''
    for (var i = 0; i < arr.length; i++) {
      str += String.fromCharCode(arr[i])
    }
    return str
  },
  data: {
    service_uuid: "0000FFF0-0000-1000-8000-00805F9B34FB",
    characteristic_write_uuid: "0000FFF1-0000-1000-8000-00805F9B34FB",
    characteristic_read_uuid: "0000FFF2-0000-1000-8000-00805F9B34FB",
    name: "LZ-OTA",
    md5Key: "",
    system: 'ios',
    userName: "",
    isLogin: false
  },
  globalData: {
    userInfo: null,
    selectedTabIndex: 0
  }
})
