const app = getApp()

Page({
  data: {
    // 是否显示信息
    showSystemInfo: false,
    //系统信息
    systemInfo: null,
    //系统信息字符串展示
    systemInfoStr: null,
    //是否是K8容器中运行的小程序
    isKEightDevice: false,

    /**
     * 初始化刷脸服务
     */
    //入参打印
    initObj: null,
    //结果
    initResult: null,
    //初始化入参
    initParams: {},

    /**
     * 调用1：N刷脸
     */
    //入参打印
    oneVNObj: null,
    //结果
    oneVNResult: null,
    //调用服务入参
    oneVNParams: {},
    //服务编码
    oneVNServiceCode: "",

    /**
     * 调用订阅授权服务
     */
    //入参打印
    authObj: null,
    //结果
    authResult: null,
    //调用服务入参
    authParams: {},
    //服务编码
    authServiceCode: "",

    /**
     * ampe通道调用参数
     */
    ampe_params: [
      {
        name: '隐藏导航栏（仅本次运⾏有效）',
        aromeCode: "",
        number: "I1",
        initParams: {
          "action": "deviceConfig",
          "event": "open",
          "params": {
            "deviceConfig": {
              "hideNavigationBar": 1
            }
          }
        }
      },
      {
        name: '开启导航栏（仅本次运⾏有效）',
        aromeCode: "",
        number: "I2",
        initParams: {
          "action": "deviceConfig",
          "event": "open",
          "params": {
            "deviceConfig": {
              "hideNavigationBar": 2
            }
          }
        }
      },
      {
        name: '隐藏通知栏（仅本次运⾏有效）',
        aromeCode: "",
        number: "I1",
        initParams: {
          "action": "deviceConfig",
          "event": "open",
          "params": {
            "deviceConfig": {
              "hideNotificationBar": 1
            }
          }
        }
      },
      {
        name: '开启通知栏（仅本次运⾏有效）',
        aromeCode: "",
        number: "I2",
        initParams: {
          "action": "deviceConfig",
          "event": "open",
          "params": {
            "deviceConfig": {
              "hideNotificationBar": 2
            }
          }
        }
      },
      {
        name: '关闭⼩程序',
        aromeCode: "",
        number: "I1",
        initParams: {
          "action": "deviceConfig",
          "event": "open",
          "params": {
            "deviceConfig": {
              "exitApplet": 1
            }
          }
        }
      },
      {
        name: "刷脸初始化请求",
        number: "A",
        aromeCode: "",
        initParams: {
          "action": "initFace",
          "event": "open",
          "taskId": "5",
          "params": {
            "serviceId": "",
            "query": "",
            "scope": ""
          }
        }
      },
      {
        name: '调用刷脸（老年食堂）',
        aromeCode: "",
        number: "H1",
        initParams: {
          "action": "doStorageFaceInfo",
          "params": {
            "appid": "2021003181682188",
            "orgOutId": "hanghuijingquTest3",
            "componentOutId": "day_label_sign_online",
            "isvPid": "2088721034139660"
          }
          // "params": {
          //   "appId":"2021004147600083",
          //   "orgOutId":"huanggangzhangzheshitang",
          //   "componentOutId":"day_label_sign_online",
          //   "isvPid":"2088741779695636"
          // }
        }
      },
      {
        name: '调用刷证/输证入库（老年食堂）',
        aromeCode: "",
        number: "H1",
        initParams: {
          "action": "doStorageFaceInfo",
          "params": {
            "uniqueId": "2021003181682188",
            "name": "刘琳琳",
            "certNo": "430822199903010465",
            "phone": "15674931071"
          }
        }
      },
      {
        name: '启动NFC轮询',
        aromeCode: "",
        number: "G1",
        initParams: {
          "action": "startCPUCardLoopRead",
          "params": {
            "intervalTime": "5000",
          }
        }
      },
      {
        name: '启动NFC轮询2',
        aromeCode: "",
        number: "G9",
        initParams: {
          "action": "startCPUCardLoopRead",
          "params": {
            "intervalTime": "5000",
            "isComeBackFailed": true
          }
        }
      },
      {
        name: '停止NFC轮询',
        aromeCode: "",
        number: "G2",
        initParams: {
          "action": "stopCPUCardLoopRead",
        }
      },

      {
        name: 'NFC监听',
        aromeCode: "CPUCardListener",
        number: "G3",
        initParams: {
        }
      },
      {
        name: '单次读取NFC',
        aromeCode: "",
        number: "G4",
        initParams: {
          "action": "readOnceCPUCardInfo",
        }
      },
      {
        name: '发送NFC-apdu命令',
        aromeCode: "",
        number: "G5",
        initParams: {
          "action": "sendCPUCardCommand",
          "params": {
            "commandApdu": "0084000008",
            "isPsamCommand": false,
          }
        }
      },
      {
        name: '启动蜂鸣器',
        aromeCode: "",
        number: "G6",
        initParams: {
          "action": "startBuzzer",
          "params": {
            "beep": "50",
          }
        }
      },
      {
        name: '冷复位',
        aromeCode: "",
        number: "G7",
        initParams: {
          "action": "psamCardColdReset",
        }
      },
      {
        name: 'CPU',
        aromeCode: "",
        number: "G8",
        initParams: {
          "action": "sendCPUCardApduCommands",
          "event": "open",
          "params": {
            "commandApdus": ["0084000008", "0084000008", "0084000008"],
            "type": "1",
            "slot": "1"
          }
        }
      },
      {
        name: '社保卡单次读取',
        aromeCode: "",
        number: "F1",
        initParams: {
          "action": "getSocialSecurityCardInfo",
        }
      },

      {
        name: '社保卡持续推送接受',
        aromeCode: "socialSecurityCardListener",
        number: "F2",
        initParams: {

        }
      },
      {
        name: '社保卡持续推送注册',
        aromeCode: "",
        number: "F1",
        initParams: {
          "action": "socialSecurityCardConfig",
        }
      },

      {
        name: "授权弹框",
        aromeCode: "",
        number: "Z1",
        initParams: {}
      },
      {
        name: "开始超时监听",
        aromeCode: "ampeStartOperationWatcher",
        number: "Z1",
        initParams: {}
      },
      {
        name: "超时监听",
        aromeCode: "ampeOperationTimeout",
        number: "Z1",
        initParams: {}
      },
      // {
      //   name: "刷脸初始化请求",
      //   number: "A",
      //   aromeCode: "",
      //   initParams: {
      //     "action": "initFace",
      //     "event": "open",
      //     "taskId": "5",
      //     "params": {
      //       "serviceId": "",
      //       "query": "",
      //       "scope": ""
      //     }
      //   }
      // },
      {
        name: "刷脸请求1：1",
        number: "B1",
        aromeCode: "",
        initParams: {
          "action": "getFaceInfoPersonCard",
          "event": "open",
          "taskId": "7",
          "params": {
            "certName": "",
            "certNo": "",
            "certPhotoBase64Str": ""
          }
        }
      },
      {
        name: "刷脸请求1：N",
        number: "B2",
        aromeCode: "",
        initParams: {
          "action": "getFaceInfo",
          "event": "open",
          "taskId": "2"
        }
      },
      {
        name: "开启刷⼆维码（单次）",
        number: "C1",
        aromeCode: "",
        initParams: {
          "action": "getQRCode",
          "event": "open",
          "taskId": "1"
        }
      },
      {
        name: "二维码持续监听",
        aromeCode: "initQRListener",
        number: "Z1",
        initParams: {}
      },
      {
        name: "开启刷身份证（单次）",
        number: "D1",
        aromeCode: '',
        initParams: {
          "action": "getIDCardInfo",
          "event": "open",
          "taskId": "3",
          "params": {
            "infoType": "base,photo,other"
          }
        }
      },
      {
        name: "身份证持续监听",
        aromeCode: "initIDCardListener",
        number: "Z2",
        initParams: {}
      },
      {
        name: "⼿输身份证号",
        aromeCode: "",
        number: "D2",
        initParams: {
          "action": "enterIDNumber",
          "event": "open",
          "taskId": "9"
        }
      },
      {
        name: "设备信息请求",
        aromeCode: "",
        number: "E",
        initParams: {
          "action": "getDeviceInfo",
          "event": "open",
          "taskId": "4"
        }
      },
      {
        name: "语⾳播报请求",
        aromeCode: "",
        number: "F",
        initParams: {
          "action": "speech",
          "event": "open",
          "taskId": "8",
          "params": {
            "text": "要播报的⽂字内容",
            "pitch": 1.0,
            "rate": 1.0
          }
        }
      },

      {
        name: "⼩票打印机⼩程序",
        number: "G",
        aromeCode: "",
        initParams: {
          "action": "receiptPrinting",
          "event": "open",
          "taskId": "10",
          "params": {
            "printList": [{
              "text": "测试⽂字打印",
              "textSize": 26,
              "bold": false,
              "algin": "left"
            }, {
              "imageBase64": "测试图⽚打印，实际传⼊base64图⽚值/9j/4AAQSkZJRgABAgAAAQABABntRS5Sj//",
              "width": 376,
              "height": 376
            }]
          }
        }
      },
    ]

  },
  //切换系统信息的显示
  changeMsg() {
    let { showSystemInfo } = this.data;
    this.setData({
      showSystemInfo: !showSystemInfo
    })
  },
  //初始化接口
  doInit() {
    let that = this
    let params = JSON.parse(JSON.stringify(that.data.initParams))

    this.setData({
      initObj: JSON.stringify(params)
    })
    my.call("aromeInitBpaasService", params, (res) => {
      that.setData({
        initResult: JSON.stringify(res)
      })
    })
  },
  //调用刷脸
  doOneVN() {
    let that = this
    let params = JSON.parse(JSON.stringify(that.data.oneVNParams))

    that.setData({
      oneVNObj: JSON.stringify(params) + "----服务编码: " + that.data.oneVNServiceCode
    })
    console.log(JSON.stringify(params) + "----服务编码: " + that.data.oneVNServiceCode)
    my.call('aromeStartBpaasService', {
      serviceCode: that.data.oneVNServiceCode,
      params: params,
      taskId: parseInt(Math.random() * 1000000 + 1)
    }, (res) => {
      that.setData({
        oneVNResult: JSON.stringify(res)
      })
      app.data.authParams.user_id = res.alipayUid || ""
      app.data.authParams.ftoken = res.ftoken || ""
    })
  },
  //调用授权服务
  doAuth() {
    let that = this
    let params = JSON.parse(JSON.stringify(that.data.authParams))

    that.setData({
      authObj: JSON.stringify(params) + "----服务编码: " + that.data.authServiceCode
    })
    my.call('aromeStartBpaasService', {
      serviceCode: that.data.authServiceCode,
      params: params,
      taskId: parseInt(Math.random() * 1000000 + 1)
    }, (res) => {
      that.setData({
        authResult: JSON.stringify(res)
      })
    })
  },
  //关闭结果
  close(e) {
    let type = e.currentTarget.dataset.type
    switch (type) {
      case "init":
        this.setData({
          initResult: null,
          initObj: null
        })
        break;
      case "oneVN":
        this.setData({
          oneVNResult: null,
          oneVNObj: null
        })
        break;
      case "auth":
        this.setData({
          authObj: null,
          authResult: null
        })
        break;

      default:
        break;
    }
  },
  //进入设置页
  doSetting(e) {
    let route = {
      init: "/pages/setInit/setInit",
      oneVN: "/pages/setOneVN/setOneVN",
      auth: "/pages/setAuth/setAuth"
    }
    let type = e.currentTarget.dataset.type
    my.navigateTo({
      url: route[type]
    })
  },
  //更新配置信息
  updateConfig() {
    this.data.initParams = app.data.initParams;

    this.data.oneVNParams = app.data.oneVNParams;
    this.data.oneVNServiceCode = app.data.oneVNServiceCode;

    this.data.authParams = app.data.authParams;
    this.data.authServiceCode = app.data.authServiceCode;
  },
  onLoad(query) {


  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    this.setData({
      systemInfo: app.data.systemInfo,
      systemInfoStr: JSON.stringify(app.data.systemInfo),
      isKEightDevice: app.data.isKEightDevice,
      initObj: null,
      initResult: null,
      oneVNObj: null,
      oneVNResult: null,
      authObj: null,
      authResult: null
    });
    this.updateConfig();
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
  },
});
