//app.js
let md5 = require('utils/md5.js')

App({
  // 小程序初始化时
  onLaunch: function(options) {
    
  },
  // 小程序显示
  onShow: function(options) {},
  // 小程序隐藏
  onHide: function() {},
  // 错误监听
  onError: function(msg) {},
  // 用户token
  token: '',
  isLogin: wx.getStorageSync('admin') ? 1 :0,
  // 全局状态
  state: {
    admin: {
      account: wx.getStorageSync('admin') ? wx.getStorageSync('admin').account:'',
      address: wx.getStorageSync('admin') ? wx.getStorageSync('admin').address:'',
      businessCardPhotos: wx.getStorageSync('admin') ? wx.getStorageSync('admin').businessCardPhotos:null,
      contact: wx.getStorageSync('admin') ? wx.getStorageSync('admin').contact:'',
      customerAccountId: wx.getStorageSync('admin') ? wx.getStorageSync('admin').customerAccountId:0,
      housePhotos: wx.getStorageSync('admin') ? wx.getStorageSync('admin').housePhotos:null,
      images: wx.getStorageSync('admin') ? wx.getStorageSync('admin').images:null,
      isMember: wx.getStorageSync('admin') ? wx.getStorageSync('admin').isMember :0,
      name: wx.getStorageSync('admin') ? wx.getStorageSync('admin').name :'',
      photo: wx.getStorageSync('admin') ? wx.getStorageSync('admin').photo :'',
      secretKey: wx.getStorageSync('admin') ? wx.getStorageSync('admin').secretKey :'',
      token: wx.getStorageSync('admin') ? wx.getStorageSync('admin').token :'',
      customerName: wx.getStorageSync('admin') ? wx.getStorageSync('admin').customerName : '',
      id: wx.getStorageSync('admin') ? wx.getStorageSync('admin').id : ''
    },
    site:{
      companyId: wx.getStorageSync('site') ? wx.getStorageSync('site').companyId : 0,
      cityId: wx.getStorageSync('site') ? wx.getStorageSync('site').cityId : 262,
      siteName: wx.getStorageSync('site') ? wx.getStorageSync('site').siteName : '成都站'
    },
    companyId: wx.getStorageSync('site') ? wx.getStorageSync('site').companyId : 0,
    cityId: wx.getStorageSync('site') ? wx.getStorageSync('site').cityId:262,
    siteName: wx.getStorageSync('site') ? wx.getStorageSync('site').siteName :'成都站'
  },
  // 请求接口
  $api: function (url, data) {
    var promise = new Promise((resolve, reject) => {
      wx.showLoading({
        title: '加载中',
      });
      let timestamp = (new Date()).valueOf()
      let sign = md5('cmd=' + url + '&' + 'msg=' + encodeURIComponent(data).toLowerCase() + '&' + 'timestamp=' + timestamp + '&' + 'token=' + getApp().state.admin.token + '&' + 'key=' + '')
      wx.request({
        // url: 'http://192.168.2.214:8082/api/Common/Post',
        url: 'http://192.168.2.65:8025/api/Common/Post', //春姐
        // url: 'https://reborn.oytour.com/api/common/post',  //线上
        // url: ' http://test.viitto.com/api/common/post',
       
        method: 'POST',
        data: {
          cmd: url,
          msg: data,
          timestamp: timestamp,
          token: getApp().state.admin.token,
          sign: sign
        },
        success(res) {
          wx.hideLoading()
          if (res.data.resultCode === 1) { 
            resolve(res.data.data);
          } else if (res.data.resultCode === 10000 || res.data.resultCode === 10001) {
            wx.reLaunch({
              url: '/pages/login/login'
            })
          } else {
            // wx.showToast({
            //   title: res.data.message,
            //   icon: 'none',
            //   duration: 1000
            // })
            reject(res.data)
          }
        }
      })
    }) 
    return promise;
  },
  // 请求接口
  $apiSaveFormId: function (url, data) {
    var promise = new Promise((resolve, reject) => {
      let timestamp = (new Date()).valueOf()
      let sign = md5('cmd=' + url + '&' + 'msg=' + encodeURIComponent(data).toLowerCase() + '&' + 'timestamp=' + timestamp + '&' + 'token=' + getApp().state.admin.token + '&' + 'key=' + '')
      wx.request({
        url: 'http://192.168.2.214:8082/api/Common/Post',
        // url: 'http://192.168.2.65:8025/api/Common/Post', //春姐
        // url: 'https://reborn.oytour.com/api/common/post',  //线上
        // url: ' http://test.viitto.com/api/common/post',

        method: 'POST',
        data: {
          cmd: url,
          msg: data,
          timestamp: timestamp,
          token: getApp().state.admin.token,
          sign: sign
        },
        success(res) {

        }
      })
    })
    return promise;
  },
  $apiJavaData: function (url, msg) {
    let groupId = 2;
    let token = this.state.admin.token;
    let secretKey = this.state.admin.secretKey;
    let data = {};
    var promise = new Promise((resolve, reject) => {
      wx.showLoading({
        title: '加载中...',
      });
      let timestamp = (new Date()).valueOf()
      let sign = md5('msg=' + encodeURIComponent(JSON.stringify(msg)).toLowerCase() + '&' + 'timestamp=' + timestamp + '&' + 'token=' + token + '&' + 'key=' + secretKey)
      wx.request({
        // url: 'https://efficient.oytour.com/' + url, //线上
        // url: 'http://47.96.12.235:9001/' + url,       //测试 
        url: 'http://192.168.2.215:9000/'+ url,

        method: 'POST',
        data: {
          msg: msg,
          timestamp: timestamp,
          token: token,
          sign: sign,
          groupId: groupId,
        },
        success(res) {
          wx.hideLoading()
          if (res.data.resultCode === 1) {
            resolve(res.data);
          } else if (res.data.resultCode === 10000 || res.data.resultCode === 10001) {
            // wx.reLaunch({
            //   url: '/pages/login/login'
            // })
            // wx.showToast({
            //   title: res.data.message,
            //   icon: 'none',
            //   duration: 1000
            // })
            // reject(res.data)
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 1000
            })
            reject(res.data)
          }
        }
      })
    })
    return promise;
  },
  // 
  $apiJavaNew: function (url, msg) {
    let groupId=2;
    let token = this.state.admin.token;
    let secretKey = this.state.admin.secretKey;
    let data={};
    var promise = new Promise((resolve, reject) => {
      wx.showLoading({
        title: '加载中...',
      });
      let timestamp = (new Date()).valueOf()
      let sign = md5('msg=' + encodeURIComponent(JSON.stringify(msg)).toLowerCase() + '&' + 'timestamp=' + timestamp + '&' + 'token=' + token + '&' + 'key=' + secretKey)
      wx.request({
        // url: 'https://efficient.oytour.com/'+ url, //线上
        // url: 'http://47.96.12.235:9001/' + url,       //测试 
        url: 'http://192.168.2.215:9000/'+ url,

        method: 'POST',
        data: {
          msg: msg,
          timestamp: timestamp,
          token: token,
          sign: sign,
          groupId: groupId,
        },
        success(res) {
          wx.hideLoading()
          if (res.data.resultCode === 1) {
            resolve(res.data.data);
          } else if (res.data.resultCode === 10000 || res.data.resultCode === 10001) {
            // wx.reLaunch({
            //   url: '/pages/login/login'
            // })
            // wx.showToast({
            //   title: res.data.message,
            //   icon: 'none',
            //   duration: 1000
            // })
            // reject(res.data)
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 1000
            })
            reject(res.data)
          }
        }
      })
    })
    return promise;
  },
  // 抽奖接口2
  $apiJava: function (url, msg, groupId, uid, token, secretKey) {
    
    var promise = new Promise((resolve, reject) => {
      wx.showLoading({
        title: '加载中',
      });
      let timestamp = (new Date()).valueOf()
      let sign = md5('msg=' + encodeURIComponent(JSON.stringify(msg)).toLowerCase() + '&' + 'timestamp=' + timestamp + '&' + 'token=' + token + '&' + 'key=' + secretKey)
      wx.request({
        // url: 'https://efficient.oytour.com/'+ url, //线上
        url: 'http://47.96.12.235:9001/' + url,       //测试 
        
        method: 'POST',
        data: {
          msg: msg,
          timestamp: timestamp,
          token: token,
          sign: sign,
          uid: uid,
          groupId: groupId,
        },
        success(res) {
          wx.hideLoading()
          if (res.data.resultCode === 1) {
            resolve(res.data.data);
          } else if (res.data.resultCode === 10000 || res.data.resultCode === 10001) {
            // wx.reLaunch({
            //   url: '/pages/login/login'
            // })
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 1000
            })
            reject(res.data)
          }
        }
      })
    })
    return promise;
  },
 
  globalData: {
    userInfo: null,
    awardsConfig: {},
    runDegs: 0
  }
})