App({
    onLaunch: function () {
        if (wx.getStorageSync('token')) {
            this.getmessagelist()
        }
        this.getToken();
        //自动更新版本
        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, baseUrl;
            // 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'
            // 滨海
            var entrance_type = wx.getStorageSync("entrance_type");
            if (entrance_type == '1' || entrance_type == '0' || url == '/getopenid/get' || url.indexOf('/wb/wb') > -1) {
                //劳保商城入口
                baseUrl = 'https://binhai.w.broing.cn/api';
                //  baseUrl = 'http://binhai.shs.broing.cn/api'
            } else if (entrance_type == '2') {
                //工会商城入口
                baseUrl = 'https://binhaigh.w.broing.cn/api';
                // baseUrl = 'http://gonghui.shs.broing.cn/api'
            } else if (entrance_type == '3') {
                //办公入口
                baseUrl = 'https://bgcb.w.broing.cn/api';
            }
            //  天远甄选
            // if (entrance_type == '1' || url == '/getopenid/get' || url.indexOf('address') > -1) {
            //   //劳保商城入口
            //   baseUrl = 'https://binhai2.w.broing.cn/api';
            // } else if (entrance_type == '2') {
            //   //工会商城入口
            //   baseUrl = 'https://binhaigh2.w.broing.cn/api';
            // } else if (entrance_type == '3') {
            //   //工会商城入口
            //   baseUrl = 'https://bgcb2.w.broing.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 == '200') {
                        resolve(res.data)
                    } else if (res.data.code == '5000') {
                        wx.hideLoading();
                        reject(res.data)
                        wx.removeStorageSync('login');
                        wx.showModal({
                            title: '提示',
                            content: res.data.msg,
                            showCancel: false
                        })
                        wx.removeStorageSync('token');
                        setTimeout(() => {
                            wx.navigateTo({
                                url: '/pages/register/register?type=1',
                            })
                        }, 1200)
                    } 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;
    },
    getToken() {
        let that = this;
        let u = that.interface.getToken;
        wx.login({
            success: (result) => {
                that.postk(u, {
                    code: result.code
                }).then((r) => {
                    console.log(r)
                    wx.setStorageSync('token', r.data.token)
                    wx.setStorageSync('login', r.data.login_new1)
                    wx.setStorageSync('gh_login', r.data.login_new2)
                    wx.setStorageSync('cb_login', r.data.login_new3)
                    wx.setStorageSync('sq', r.data.sq1)
                    wx.setStorageSync('gh_sq', r.data.sq2)
                    wx.setStorageSync('cb_sq', r.data.sq3)
                })
            },
            fail: (res) => { },
            complete: (res) => { },
        })
    },
    // 开放商城公共接口
    postk: function (url, data, showLoad) {
        wx.showNavigationBarLoading()
        var promise = new Promise((resolve, reject) => {
            //init
            let that = this;
            let postData = data
            // 滨海
            let baseUrl = 'https://binhaidispark.w.broing.cn/api'
            // 天远甄选
            // let baseUrl = 'https://binhaidispark2.w.broing.cn/api'

            //网络请求  
            wx.request({
                url: baseUrl + url,
                data: postData,
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded',
                    'token': wx.getStorageSync('token') || ''
                    //   '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.showToast({
                            title: res.data.msg,
                            icon: 'none',

                        })
                        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 = 'https://binhaidispark.w.broing.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 () { },

    //接口管理
    interface: {
        openid: '/getopenid/get', // 获取openid
        getcode: '/getopenid/getcode', // 获取验证码,
        getToken: '/user/getToken',
        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: 'https://binhai.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', //   订单数
        orderMixed: '/order/zhifu',
    },
    globalData: {
        login_new: "",
        // 滨海
        baseUrlimg: 'https://binhaidispark.w.broing.cn/',
        // 天远
        // baseUrlimg: 'https://binhaidispark2.w.broing.cn/api',
    }
})
// 滨海wx73497072e7340d61
// 天远wx2980402f625c4f80