App({
  globalData: {
    version: "1.0.1.20200311",
    //是否使用测试支付数据
    useTestPayInfo: false,
    printerId: null,
    printerName: null,
    TEST_URL: "https://cashier.test.miyahub.net/",
    RELEASE_URL: "https://cashier.miyahub.com/",
    BASE_URL: null,
    appId: "2021001107603212",
    backScreenAppId:"2021001107617164",
    //小程序容器版本
    containerVersionCode: null,
    localAccountInfo: null,
    //模拟器上需要sn有值
    sn: "xx",
    //是否登录完成
    isLoginOk = false
  },

  onLaunch(options) {
    // 第一次打开
    // options.query == {number:1}
    console.info('App onLaunch');
    //初始化baseUrl
    this.globalData.BASE_URL = this.globalData.TEST_URL;
    //获取容器版本信息
    my.ix.getVersion({
      success: (r) => {
        console.log("getVersion success==>" + JSON.stringify(r));
        getApp().globalData.containerVersionCode = r.versionCode;
      }
    });
    //添加后屏小程序
    my.ix.addExtensionApp({
      appId: this.globalData.backScreenAppId,
      // 填入后屏小程序的 AppID
      type: "screen",
      success: (res) => {
        console.log("addExtensionApp success:", res)
      },
      fail: (res) => {
        console.log("addExtensionApp fail:", res)
      },
    })
  },
  onShow(options) {
    // 从后台被 scheme 重新打开
    // options.query == {number:1}
    //开启打印机状态检测
    my.ix.startMonitorPrinter({
      success: (res1) => {
        console.log("startMonitorPrinter success==>" + JSON.stringify(res1));
        //查询打印机一下
        my.ix.queryPrinter({
          success: (res) => {
            console.log("查询打印机成功：" + JSON.stringify(res));
            //如果回调中的设备数为0，则重置打印机id为null，打印机名称为null
            if (res.usb != null && res.usb.length != 0) {
              getApp().globalData.printerId = res.usb[0].id;
              getApp().globalData.printerName = res.usb[0].name;
            } else {
              getApp().globalData.printerId = null;
              getApp().globalData.printerName = null;
            }
          },
          fail: (res) => {
            console.log("查询打印机失败：" + JSON.stringify(res));
            getApp().globalData.printerId = null;
            getApp().globalData.printerName = null;
          }
        });
        //开启打印机状态检测
        my.ix.onMonitorPrinter({
          success: (res) => {
            console.log("onMonitorPrinter success data:" + JSON.stringify(res));
            //如果回调中的设备数为0，则重置打印机id为null，打印机名称为null
            if (res.usb != null && res.usb.length != 0) {
              getApp().globalData.printerId = res.usb[0].id;
              getApp().globalData.printerName = res.usb[0].name;
            } else {
              getApp().globalData.printerId = null;
              getApp().globalData.printerName = null;
            }
          },
          fail: (res) => {
            console.log("onMonitorPrinter fail data:" + JSON.stringify(res));
          }
        });
      },
      fail: (res) => {
        console.log("startMonitorPrinter fail==>" + JSON.stringify(res));
        //即使开启检测失败也查询一次
        //查询打印机一下
        my.ix.queryPrinter({
          success: (res) => {
            console.log("查询打印机成功：" + JSON.stringify(res));
            //如果回调中的设备数为0，则重置打印机id为null，打印机名称为null
            if (res.usb != null && res.usb.length != 0) {
              getApp().globalData.printerId = res.usb[0].id;
              getApp().globalData.printerName = res.usb[0].name;
            } else {
              getApp().globalData.printerId = null;
              getApp().globalData.printerName = null;
            }
          },
          fail: (res) => {
            console.log("查询打印机失败：" + JSON.stringify(res));
            getApp().globalData.printerId = null;
            getApp().globalData.printerName = null;
          }
        });
      }
    });
  },
});
