App({
  onLaunch: function () {
    this.getmessagelist()
    let token=wx.getStorageSync('token');
    console.log('woshitoe',token)
    if(token!=""){
      this.getInfoFun()
    }
   
    //自动更新版本
    const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调

    })
    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好,是否重启应用?',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })
    updateManager.onUpdateFailed(function () {
      // 新的版本下载失败
      wx.showModal({
        title: '更新提示',
        content: '新版本下载失败',
        showCancel: false
      })
    })
  },
  post: function (url, data, showLoad) {

    wx.showNavigationBarLoading()
    var promise = new Promise((resolve, reject) => {
      //init
      let that = this;
      let postData = data
      // let baseUrl = 'https://binhai.w.broing.cn/api'; 
      // let baseUrl ='http://binhaitest.w.brotop.cn/api'
      // 现在用的
      // let baseUrl = 'https://binhai.w.broing.cn/api'
      let baseUrl = 'http://binhaitest.w.brotop.cn/api'
      //网络请求  
      wx.request({
        url: baseUrl + url,
        data: postData,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'token': wx.getStorageSync('token') || ''
        },
        success: function (res) { //返回取得的数据
          console.log('ssssssssssssssssssssssssssssssssssss',res)
          if (res.data.code == '200') {
            resolve(res.data)
          } else if (res.data.code == '5000') {
            wx.hideLoading();
            reject(res.data)
            // wx.removeStorageSync('token');
            wx.removeStorageSync('login');
            // wx.navigateTo({
            //   url: '/pages/register/register?type=1',
            // })
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel: false
            })
            reject(res.data)
          }
          setTimeout(function () {
            wx.hideNavigationBarLoading()
          }, 600)
        },
        fail: function (e) {
          reject('网络出错');
          wx.hideNavigationBarLoading()
        }
      })
    });
    return promise;
  },

  // 开放商城公共接口
  postk: function (url, data, showLoad) {
    wx.showNavigationBarLoading()
    var promise = new Promise((resolve, reject) => {
      //init
      let that = this;
      let postData = data
      // let baseUrl = 'https://binhai.w.broing.cn/api'; 
      // let baseUrl ='http://binhaitest.w.brotop.cn/api'
      let baseUrl = 'http://binhaidispark.t.brotop.cn/api'
   
     
      //网络请求  
      wx.request({
        url: baseUrl + url,
        data: postData,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'token': wx.getStorageSync('token') || ''
        },
        success: function (res) { //返回取得的数据
          if (res.data.code == '1') {
            resolve(res.data)
          } else if (res.data.code == '5000') {
            wx.hideLoading();
            reject(res.data)
            wx.removeStorageSync('token');
            wx.removeStorageSync('login');
            // wx.navigateTo({
            //   url: '/pages/register/register?type=1',
            // })
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel: false
            })
            reject(res.data)
          }
          setTimeout(function () {
            wx.hideNavigationBarLoading()
          }, 600)
        },
        fail: function (e) {
          reject('网络出错');
          wx.hideNavigationBarLoading()
        }
      })
    });
    return promise;
  },

  upload(filetype, file) {

    var promise = new Promise((resolve, reject) => {
      wx.showNavigationBarLoading()
      wx.showLoading({
        title: '上传中',
      })
      let url = 'http://binhaidispark.t.brotop.cn/api/common/upload';
    
      let head = {
        'token': wx.getStorageSync('token'),
        'XX-Device-Type': ''
      }
      let typename = {
        filetype: filetype
      }
      wx.uploadFile({
        url: url, //仅为示例,非真实的接口地址
        filePath: file,
        name: 'file',
        header: head,
        formData: typename,
        success: function (res) {
          console.log('上传文件后', res)
          let temdata = JSON.parse(res.data);
          console.log(temdata)
          let urlobj = {
            url: temdata.data.url
          }
          resolve(urlobj);

        },
        fail: function (res) {
          reject('网络出错');
          wx.hideNavigationBarLoading()
          wx.hideLoading()
        },
        complete: () => {
          wx.hideNavigationBarLoading()
          wx.hideLoading()
        },
      })
    });
    return promise;
  },

  
  // 获取个人信息
  getInfoFun() {
    let that= this;
    let u = that.interface.readteacher;
   that.post(u, {}).then((r) => {
      console.log('3489523498',r)
      if (r.code == 200) {

        wx.setStorageSync('login', r.msg.login_new)
        // that.globalData.login_new=r.msg.login_new;
        // console.log(  that.globalData.login_new)
        // t.setData({
        //   obj: r.msg
        // })
      }
    })
  },

  getmessagelist() {
    let that = this
    let url = '/information/get_all';
    let data = {
      page: 1,
      pageNum: 10,

    }
    this.post(url, data, "POST").then((r) => {
      // console.log(r)
      // that.setData({
      //   messagelist: that.data.messagelist.concat(r.msg)
      // })

      let newlist = [];
      r.msg.forEach(function (value, index, array) {
        console.log(newlist)
        if (value.status == 1) {
          newlist.push(value)
        }
      })

      console.log('77889944556',newlist)
      if (newlist.length != 0) {
        wx.showTabBarRedDot({
          index: 1,
          success: function (red) {

            wx.setTabBarBadge({
              index: 1,
              text: newlist.length.toString(),
            })
          }
        })
      }


    }).catch((err) => { })
  },

  onShow: function() {
   console.log('hahahahhahahahhhahh')
  },


  //接口管理
  interface: {
    openid: '/getopenid/get', // 获取openid
    getcode: '/getopenid/getcode', // 获取验证码
    readteacher: '/getopenid/readteacher', // 获取个人信息
    login: '/getopenid/smslogin', // 登录
    jifenlist: '/order/jifenlist', // 积分列表
    address: '/address/index', // 地址列表
    addaddress: '/address/add', // 添加地址
    editaddress: '/address/edit', // 修改地址
    deladdress: '/address/delete', // 删除地址
    mraddress: '/address/mo', // 修改默认地址
    adshow: '/address/editshow', //地址详情(数据回显)
    about: '/wb/wb', // 关于我们
    order: '/order/orderlist', // 获取订单列表
    payorder: '/order/payorder', // 订单页点击支付
    disorder: '/order/disorder', // 订单列表取消订单
    orderrefund: '/order/orderrefund', // 退换货详情
    // upload: 'https://binhai.w.broing.cn/api/common/upload', // 上传图片
    upload: 'http://binhaitest.w.brotop.cn/api',
    refund: '/order/refund', // 退货提交

    delorder: '/order/delorder', // 删除订单
    okorder: '/order/okorder', // 确认订单
    orderInfo: '/order/orderInfo', // 订单详情
    kuaidi: '/kuaidi/kuaidi', //物流信息
    collectdel: '/product/collectdel', //收藏删除 
    collectList: '/getopenid/collectlist', //收藏列表
    deleteseach: '/product/deleteseach', //删除搜索历史
    order_pay: '/order/pay', //订单支付
    now_add: '/order/now_add', //点击结算
    hotsreach: '/product/hotsreach', // 热门搜索
    searchhis: '/product/searchhis', // 商品搜索历史
    search: '/product/search', // 商品搜索结果列表
    getPid: '/cap/getpid', //  分类页面
    prev_pay: '/shop/prev_pay', // 购物车点击结算
    gwcshop: '/shop/gwcshop', //购物车数量修改保存
    deletes: '/shop/deletes', //购物车删除
    shopcar: '/shop/shopcar', // 购物车页面
    now_prev_pay: '/shop/now_prev_pay', // 直接购买
    addshop: '/shop/addshop', // 添加购物车
    collect: '/product/collect', // 收藏
    getInfo: '/product/getInfo', //商品详情
    good_list: '/product/get', //  商品
    index: '/cap/get', // 首页大小分类
    banner: '/banner/index', // 轮播图 
    notice: '/cap/notice', // 公告 
    youfei: '/shop/youfei1', // 单算邮费
    youfei1: '/shop/youfei', // 購物車邮费

    shopcarNum: '/shop/shopcarnum', //  购物车数量
    orderlist: '/shop/orderlist', //  订单各状态数量
    
    order_read: '/shop/read', //   订单数
  },
  globalData: {
    login_new:"",
    baseUrlimg :'http://binhaidispark.t.brotop.cn/'
  }
})