小程序 utils 工具类

// 保存当前路径
function setPath() {
    wx.setStorageSync({
        key: "PATH",
        data: getCurrentPageUrlWithArgs()
    })
}
// 登录后跳回之前页面
function backTo() {
    let PATH = wx.getStorageSync("PATH")
    //console.log(PATH)
    if (PATH) {
        if (
            // tabbar 页面都写这里 || 
             PATH == 'pages/index/index'  //首页
        
        ) {
            wx.switchTab({url: "/" + PATH})
        } else {
            wx.reLaunch({url: "/" + PATH})
        }
        wx.removeStorage({
            key: 'PATH'
        })
    } else {
        wx.switchTab({url: "/pages/index/index"})
    }
}
//获取当前路径地址及参数
function getCurrentPageUrlWithArgs() {
    let pages = getCurrentPages() //获取加载的页面
    let currentPage = pages[pages.length - 1] //获取当前页面的对象
    let url = currentPage.route //当前页面url
    let options = currentPage.options //如果要获取url中所带的参数可以查看options
    //拼接url的参数
    let urlWithArgs = url + '?'
    for (let key in options) {
        let value = options[key]
        urlWithArgs += key + '=' + value + '&'
    }
    urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)
    return urlWithArgs
}

// 富文本图片100%宽度设置
function formatRichText(html) {
    let newContent = html.replace(/]*>/gi, function (match, capture) {
        match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
        match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
        match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
        return match;
    });
    newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
        match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
        return match;
    });
    newContent = newContent.replace(/]*\/>/gi, '');
    newContent = newContent.replace(/\

你可能感兴趣的:(小程序 utils 工具类)