// app.js
App({
  onLaunch: function () {
    wx.getSystemInfo({
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;
        let capsule = wx.getMenuButtonBoundingClientRect();
        if (capsule) {
          this.globalData.Custom = capsule;
          this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
        } else {
          this.globalData.CustomBar = e.statusBarHeight + 50;
        }
      }
    })
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function (res) {
        if (res.hasUpdate) {
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，请重启应用。',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  updateManager.applyUpdate()
                }
              }
            })
          })

          updateManager.onUpdateFailed(function () {
            wx.showModal({
              title: '已经有新版本了哟~',
              content: '新版本已经上线啦，请您删除当前小程序，重新搜索打开',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  updateManager.applyUpdate()
                }
              }
            })
          })
        }
      })
    }
  },

  globalData: {
    versionCode: 'V1',
    userInfo: {},
    // headerUrl: "http://42.193.40.239:8017/" //线上
    headerUrl: "http://192.168.110.186:8017/" //线下
    // headerUrl: "https://city.sczysoft.com/df/"
  },
  // https://city.sczysoft.com/df/doc.html //接口
  // https://city.sczysoft.com/dffile //查看拼接
  publicService:'656693c8-bd84-11ee-8332-525400286c11',//为民服务id
  
  // 测试地址
  // FILE_SERVER_URL: "http://42.193.40.239:8888", //查看 
  // FILE_SERVER_UP_URL: "http://42.193.40.239:8017/", //上传
  // 线上地址
  FILE_SERVER_URL: "https://city.sczysoft.com/dffile", //查看 
  FILE_SERVER_UP_URL: "https://city.sczysoft.com/df/", //上传

  // 文件类型
  FileType: {
    avatar: "SysPhoto", // 用户头像
    resumptionAcs: "WorkFile", // 履职附件
    msgAcs: "MsgAcs", // 消息附件
    eventAcs: "eventAcs", //突发/应急事件上报

    userSign: "UserSign", //用户签名


    EmergencyImg: "Content", // 应急预案---一图一表
    EmergencyFile: "EmergencyFile", // 应急预案---文件
    EmergencyPlanFile: "EmergencyPlanFile", // 应急演练---文件
  },

  // 请求函数封装
  axios: function (type, con, url, data, showLoading) {
    try {
      if (showLoading) {
        wx.showLoading({
          title: '加载中...',
          icon: 'none'
        })
      }
      return new Promise((resolve, reject) => {
        wx.request({
          url: `${this.globalData.headerUrl}${con}${url}`,
          method: type,
          data: data,
          header: {
            'content-type': 'application/json;charset=UTF-8',
            'userToken': this.globalData.userInfo.tokenValue || ""
          },
          success: function (res) {
            var data = res.data
            resolve(data)
          },
          fail: function (err) {
            reject(err)
          },
          complete: function (params) {
            if (showLoading) wx.hideLoading();
          }
        })
      })
    } catch {}
  },

  /** 上传文件 */
  uploadFile: function (otcid, otctype, filePath) {
    let params = {
      otcid,
      otctype,
      userId: this.globalData.userInfo.userId,
    }
    console.info(params)
    return new Promise((resolve, reject) => {
      wx.uploadFile({
        url: this.FILE_SERVER_UP_URL + 'common/upload/uploadFile',
        filePath: filePath,
        name: 'file',
        formData: params,
        success: function (res) {
          if (res.statusCode === 200) {
            resolve(res.data);
          } else {
            reject()
          }
        },
        fail: function (err) {
          reject(err);
        }
      })
    }).catch()
  },

  // 设置监听器
  watch: function (ctx, obj) {
    Object.keys(obj).forEach(key => {
      this.observer(ctx.data, key, ctx.data[key], function (value) {
        obj[key].call(ctx, value)
      })
    })
  },

  // 监听属性，并执行监听函数
  observer: function (data, key, val, fn) {
    Object.defineProperty(data, key, {
      configurable: true,
      enumerable: true,
      get: function () {
        return val
      },
      set: function (newVal) {
        if (newVal === val) return
        fn && fn(newVal)
        val = newVal
      },
    })
  }
})