// //app.js
// App({
//   onLaunch: function () {
//     // 展示本地存储能力
//     var logs = wx.getStorageSync('logs') || []
//     logs.unshift(Date.now())
//     wx.setStorageSync('logs', logs)

//     // 登录
//     wx.login({
//       success: res => {
//         // 发送 res.code 到后台换取 openId, sessionKey, unionId
//       }
//     })
//     // 获取用户信息
//     wx.getSetting({
//       success: res => {
//         if (res.authSetting['scope.userInfo']) {
//           // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
//           wx.getUserInfo({
//             success: res => {
//               // 可以将 res 发送给后台解码出 unionId
//               this.globalData.userInfo = res.userInfo

//               // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
//               // 所以此处加入 callback 以防止这种情况
//               if (this.userInfoReadyCallback) {
//                 this.userInfoReadyCallback(res)
//               }
//             }
//           })
//         }
//       }
//     })
//   },
//   globalData: {
//     userInfo: null
//   }
// })
import WeAppRedux from './redux/index.js';
import createStore from './redux/createStore.js';
import reducer from './store/reducer.js';

import ENVIRONMENT_CONFIG from './config/envConfig.js'
import PAGE_CONFIG from './config/pageConfig.js'

const { Provider } = WeAppRedux;
const store = createStore(reducer) // redux store
let md5 = require('utils/md5.js')
let netUrl = 'http://testapi.oytour.com/api/common/post';
// let netUrl = 'https://reborn.oytour.com/api/common/post';

App(
  Provider(store)(
    {
      globalData: {
        emitter: null,
        netcallController: null,
        ENVIRONMENT_CONFIG,
        PAGE_CONFIG,
        queryUser:{}
      },
      onShow: function (e) {
        if (e.scene == 1007 || e.scene == 1008) {
          try {
            this.globalData.netcall && this.globalData.netcall.destroy()
            this.globalData.nim && this.globalData.nim.destroy({
              done: function () {
              }
            })
          } catch (e) {
          }
        }
      },
      onLaunch: function (e) {
        let userInfo = {
          account: '17308037817',
          nick: '罗超',
          gender: 0,
          createTime: 1514969730701,
          updateTime: 1514969730701,
        }
        if (userInfo) {
          this.globalData.userInfo = userInfo
        }
        let systemInfo = wx.getSystemInfoSync()
        this.globalData.videoContainerSize = {
          width: systemInfo.windowWidth,
          height: systemInfo.windowHeight
        }
        this.globalData.isPushBeCallPage = false
      },
      token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9',
      // 请求接口
      $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=' + "" + '&' + 'key=' + '')
          wx.request({
            url: netUrl,
            method: 'POST',
            data: {
              cmd: url,
              msg: data,
              timestamp: timestamp,
              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.navigateTo({
                //   url: '/pages/login/login'
                // })
                wx.showToast({
                  title: res.data.message,
                  icon: 'none',
                  duration: 1000
                })
              } else {
                wx.showToast({
                  title: res.data.message,
                  icon: 'none',
                  duration: 1000
                })
                reject(res.data)
              }
            }
          })
        })
        return promise;
      },
    }
  )
)
