//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)
    //                }
    //             }
    //          })
    //       }
    //    }
    // })





  },
  ajax(url, params, successData, errorData, completeData) {
    var https = "https://shipin.wx.broteam.cn/index.php/"
    wx.request({
      url: https + url,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: params,
      success: (res) => {
        successData(res)
      },
      error(res) {
        errorData(res)
      }
    })
  },
  // 多张图片上传    
  uploadImg: function (data) {
    var that = this,
      i = data.i ? data.i : 0,
      success = data.success ? data.success : 0,
      fail = data.fail ? data.fail : 0

    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'fileData', // 这里根据自己的实际情况修改
      formData: null,
      success: function (resp) {
        success++
        console.log(resp)
        console.log(i)
        /*
          这里肯能有bug,失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时,
          这里的succes才+1
        */
      },
      fail: function (res) {
        fail++
        console.log('fail: ' + i + 'fail: ' + fail)
      },
      complete: function (res) {
        console.log(i)
        i++


        if (i == data.path.length) { //当图片传完时,停止调用          
          console.log('执行完毕');
          console.log('成功:' + success + " 失败:" + fail);
        } else { //若图片还没有传完,则继续调用函数
          console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data);
        }


        if (i == data.path.length) { // 当图片传完时,停止调用
          console.log('执行完毕')
          console.log('成功:' + success + '失败:' + fail)
        } else { // 若图片还没有传完,则继续调用函数
          console.log(i)
          data.i = i
          data.success = success
          data.fail = fail
          that.uploadimg(data)
        }
      }
    })
  },
  getNowDate() {
    var now = new Date();
    var week = now.getDay()
    var str = "星期";
    switch (now.getDay()) {
      case 0:
        str = str + "日";
        break;
      case 1:
        str = str + "一";
        break;
      case 2:
        str = str + "二";
        break;
      case 3:
        str = str + "三";
        break;
      case 4:
        str = str + "四";
        break;
      case 5:
        str = str + "五";
        break;
      case 6:
        str = str + "六";
        break;
    }
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    if (month < 10) {
      month = '0' + month;
    };
    if (day < 10) {
      day = '0' + day;
    };
    //  如果需要时分秒,就放开
    // var h = now.getHours();
    // var m = now.getMinutes();
    // var s = now.getSeconds();
    var formatDate = year + '-' + month + '-' + day + '('+str+')';
    return formatDate;
  },

  globalData: {
    imgUrlUp: "https://shipin.wx.broteam.cn/index.php/MyComment/upload",
    userInfo: null,
    // 下导航
    tabBar: [{
      name: "首页",
      state: true,
      img: "http://omega.wx.bronet.cn/public/static/images/menu_icon1_select@2x_81.png",
      selectImg: "http://omega.wx.bronet.cn/public/static/images/menu_icon1_select@2x.png",
      num: null,
      url: "../index/index"
    },
    {
      name: "购物车",
      state: false,
      img: "http://omega.wx.bronet.cn/public/static/images/92@2x.png",
      selectImg: "http://omega.wx.bronet.cn/public/static/images/92@2x_55.png",
      num: 0,
      url: "../shopCart/shopCart"
    },
    {
      name: "我的",
      state: false,
      img: "http://omega.wx.bronet.cn/public/static/images/menu_icon3_select@2x.png",
      selectImg: "http://omega.wx.bronet.cn/public/static/images/menu_icon3_select@2x_57.png",
      num: null,
      url: "../me/me"
    },
    ],
    unique_id: null, //公众号与小程序共有id
    shoppingCart: null,
    nearest_spot: null,//就近自提地址及id
    nearest_store: null,//到店堂食地址及id
    orderRemarkValue: null,
    btnStart: true //选择的自提还是堂食
  }
})