// app.js
const utils = require('/utils/util.js')
const baseIp= "http://localhost:9747"
// 图片预览地址

App({
  onLaunch(options) {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        if(res.code){
          const _this = this
          // 登录
          wx.showLoading({
            title: '登陆中...',
            mask: true,
          });
          wx.request({
            method: 'POST',
            url: _this.globalData.interfaceUrls.loginUrl,
            data: {
              wxCode: res.code,
              dsKey: options.query.deskey
            },
            success(res) {
      
              wx.hideLoading()
              if (!res.data.code || res.data.code!= "200") {
                utils.simleInfoWithBack('登录失败，请稍后再试')
                return
              }
              _this.globalData.launchPara=options.query.deskey;
              const resData = res.data.data
              if (resData) {
                _this.globalData.userWxInfo = {
                  openid: resData.openid,
                  session_key: resData.session_key,
                  token: resData.token,
                  // 当前用户所关联的户号信息
                  bindAccountInfo: resData.swUserManagementVos,
                  // 当前用户所对应的水务公司
                  currentDsKey:""
                }
                if(resData.swUserManagementVos.length!=0){
                  _this.globalData.currentAccountInfo=(resData.swUserManagementVos||[])[0];
                  _this.globalData.userWxInfo.currentDsKey=(resData.swUserManagementVos||[])[0].dsKey;
                };
                _this.globalData.waterCompanys=resData.waterCompany;
                if (resData.swUserManagementVos.length == 0) {
                  _this.globalData.refresh=0;
                  wx.navigateTo({
                    url:  '/pages/FirstBangDing/FirstBangDing',
                  })
                } else {
                  _this.globalData.refresh=1;
                  wx.switchTab({
                    url: '/pages/homepage/homepage',
                    success:function () {
                      var page = getCurrentPages().pop();
                      if (page == undefined || page == null) return;
                        page.onLoad();   //重新刷新页面
                      }
                  })
                }
              } else {
                utils.simleInfo('登录失败，请稍后再试')
              }
            },
            fail: error => {
              wx.hideLoading()
              utils.simleInfo('登录失败，请稍后再试')
            }
          })
        }else{
          utils.simleInfo('登录失败，请稍后再试')
        }
      }
    })
  },
  globalData: {
    // 当前用户信息
    userWxInfo: {},
    // 系统中存在的水务公司
    waterCompanys: [],
    // 小程序启动时带的参数
    launchPara: null,
    // 当前用户所选择的账户信息
    currentAccountInfo:null,
    // 是否刷新主页数据
    refresh:0,
    // 系统接口地址
    interfaceUrls: {
      loginUrl: baseIp+ '/wechat/login', // 登录+获取绑定列表
      accountBind: baseIp+ '/wechat/account/bind', // 账号绑定
      homePageInfo: baseIp+ '/wechat/account/info/', // 首页获取用水量等信息
      billList: baseIp+ '/wechat/bill/list', // 首页获取用水量等信息
      accountList: baseIp+ '/wechat/account/list', // 首页获取用水量等信息
      accountUnbind: baseIp+ '/wechat/account/unbind', // 户号解绑方法
      repairRegistration: baseIp+  '/wechat/registration/repair',//报修登记
      feedback: baseIp+  '/wechat/registration/feedback',//投诉建议
    },
    imgPreviewUrl: "http://192.168.0.215:9000/common/sys/sysfile/preview?fileId=",
    // 统一管理图片路径
    images: {
      logo: '/static_file/logo.png',
      background: '/static_file/background.png',
      card: '/static_file/card.png',
      tabBar: {
        home: '/static_file/homepage.png',
        homeActive: '/static_file/homepageCheck.png',
        profile: '/static_file/personalCenter.png',
        profileActive: '/static_file/personalCenterCheck.png',
        tzd: '/static_file/tzd.png',
        phone: '/static_file/phone.png',
        yl: '/static_file/yl.png',
        jcsfcjtzd:'/static_file/jcsfcjtzd.jpg'
      }
    },
    // 用户通知公告
    notices:[]
  }
})
