import apps from "./apps";

function initWorkbench(that) {
    let entrance = [{
        label: "通用",
        icon: "work-tongyong",
        appid: "wx197f219a82a89d7b",
        list: apps.getapps()
    }, {
        label: "E-订单",
        appid: "wxc1b6ae925ac1d06a",
        icon: "work-E-dingdan",
        list: apps.getedd()
    }, {
        label: "E-服务",
        appid: "wxc1b6ae925ac1d06a",
        icon: "work-E-fuwu",
        list: apps.geteservice()
    }, {
        label: "营销工具",
        icon: "work-yingxiaogongju",
        appid: "wx197f219a82a89d7b",
        list: apps.gettool()
    }, {
        label: "CRM",
        appid: "wx197f219a82a89d7b",
        icon: "work-CRM",
        list: apps.getcrm()
    }];
    wx.removeStorageSync('auth')
    wx.setStorageSync('auth', apps.authList)
    that.setData({
        entrance,
        auth: JSON.stringify(apps.authList),
        userMsg: JSON.stringify(wx.getStorageSync('userMsg')),
        site: JSON.stringify(wx.getStorageSync('siteP')),
        templetList: JSON.stringify(wx.getStorageSync('templetList'))
    })
    /* 获取首页banner */
    let banner = wx.getStorageSync('banner_list').find(v => v.location == "index_top");
    if (banner) that.setData({
        bannerList: banner.ads
    })
    //用于消息跳转对照权限以及路径
    wx.setStorageSync('authComparison', entrance.map(v => {
        v = v.list.map(s => {
            s.appid = v.appid;
            return s
        })
        return v
    }).flat())
}

function isHistory() {
    let history = getApp().globalData.LaunchOptions;
    if (history && !getCurrentPages().some(v => v.__route__ == history.path) && history.path != 'pages/login/phone') {
        let url = `/${history.path}?`;
        for (const key in history.query) {
            url += `${key}=${history.query[key]}&`
        }
        wx.navigateTo({
            url,
            success(res) {
                wx.showToast({
                    title: '已恢复页面',
                    icon: "none"
                });
                getApp().globalData.LaunchOptions = null;
            }
        })
    } else {
        getApp().globalData.LaunchOptions = null;
    }
}

function isRemindChangePassword() {
    if (getApp().globalData.remindchangepassword) wx.showModal({
        title: "提示",
        content: "当前密码为初始密码，请前往修改密码",
        confirmText: '前往修改',
        cancelText: "下次再说",
        success: ({
            confirm
        }) => {
            getApp().globalData.remindchangepassword = null;
            if (confirm) wx.navigateTo({
                url: '/pages/tabbar/mine/changePassword/index',
            })
        },
    })
}
module.exports = {
    initWorkbench,
    isHistory,
    isRemindChangePassword
}