zoukankan      html  css  js  c++  java
  • 适合用于uni的工具 utils

    互交反馈
    export function myAlert(e,text,icon,mask){
        if(!icon){icon="none"}
        switch (e) {
            case 0:
                uni.showToast({//消息提示框。
                    title:text,
                    mask:mask,
                    icon:icon,
                    duration: 2000
                });
                break;
            case 1:
                uni.showLoading(text);//显示 loading 提示框
                break;
            case 2://这是一个模态弹窗
                return new Promise(function (resolve, reject) {
                    uni.showModal({
                        title: text[0],//['提示','内容:这是一个模态弹窗']
                        content: text[1],
                        success: function (res) {
                            if (res.confirm) {
                                console.log('用户点击确定');
                                resolve(true);//成功返回,resolve是Promise的回调方式
                                return true;
                            } else if (res.cancel) {
                                console.log('用户点击取消');
                                resolve(false);//成功返回,resolve是Promise的回调方式
                                return false
                            }
                        }
                    });
                });
                break;
            case 3://​显示操作菜单
                return new Promise(function (resolve, reject) {
                    uni.showActionSheet({
                        itemList: text,//['A', 'B', 'C']
                        success: function (res) {
                            console.log('选中了第' + (res.tapIndex + 1) + '个按钮');
                            return res.tapIndex
                        },
                        fail: function (res) {
                            console.log(res.errMsg);
                        }
                    });
                });
    
                break;
            case 4:
                uni.hideToast();//隐藏消息提示框。
                break;
            case 5://这是一个模态弹窗
                uni.hideLoading();//隐藏 loading 提示框。
                break;
        }
    }
    互交反馈
    页面跳转
    export function gotoPage(e, type) {
        switch (type) {
            case 0: //保留当前页面,跳转到应用内的某个页面
                uni.navigateTo({
                    url: e
                });
                break;
            case 1: //关闭所有页面,打开到应用内的某个页面。
                uni.reLaunch({
                    url: e
                });
                break;
            case 2: //关闭当前页面,跳转到应用内的某个页面。
                uni.redirectTo({
                    url: e
                });
                break;
            case 3: //跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。
                uni.switchTab({
                    url: e
                });
                break;
            case 4: //关闭当前页面,返回上一页面或多级页面
    
                /**
                 * 需要特殊返回的地方(例如第三方支付),先存历史记录
                 * var i = window.location.hash.substr(1);
                 * uni.setStorageSync('historyUrl', i);
                 * */
    
                if (uni.getStorageSync('historyUrl')!=0) { //如果有缓存历史,就走历史记录
                    console.warn("有缓存历史,就走缓存历史");
                    uni.redirectTo({
                        url: uni.getStorageSync('historyUrl'), //获取历史
                        success() {
                            uni.removeStorageSync('historyUrl');
                        }
                    });
                } else if (getCurrentPages().length == 1) {//历史记录长度只有一条
                    if(uni.getStorageSync('landingInfo')!=0){
                        console.warn("有登录 回主页");
                        uni.switchTab({//有登录 回主页
                            url: "/pages/home/index"
                        });
                    }else{//没登录 去登录
                        console.warn("没登录 去登录");
                        uni.reLaunch({
                            url: "/"
                        });
                    }
                } else if (window.location.hash==('#/'+getCurrentPages()[getCurrentPages().length - 2].route)) {//上上条记录是当前页面
                    if(uni.getStorageSync('landingInfo')!=0){
                        console.warn("上上条记录是当前页面,有登录 回主页");
                        uni.reLaunch({//有登录 回主页
                            url: "/pages/home/index"
                        });
                    }else{//没登录 去登录
                        console.warn("上上条记录是当前页面,没登录 去登录");
                        uni.reLaunch({
                            url: "/"
                        });
                    }
                } else {
                    console.warn("后退");
                    uni.navigateBack({
                        delta: e
                    });
                }
    
                break;
            case "newPage": //关闭当前页面,返回上一页面或多级页面
                if (e) {
                    // #ifdef H5
                    window.open(e);
                    // #endif
                } else {
                    console.log('当前的地址错误:', e)
                }
    
                break;
            default:
                console.log("请选择路由类型")
        }
    }
    页面跳转
    验证登陆
    export function appLogin(back) {
        let that = this;
        let thisLanding = uni.getStorageSync('landingInfo');
        if (thisLanding.token) { //判断有没有token
            return thisLanding
        } else {
            if (window.location.hash != '#/') {
                if (window.location.hash.indexOf('app=') == -1) { //判断是否是app(-1表示不存在)
                    uni.showModal({
                        title: '温馨提示',
                        content: '请先登陆',
                        success: function (res) {
                            if (res.confirm) {
                                var i = window.location.hash.substr(1);
                                uni.setStorageSync('historyUrl', i);
                                that.$gotoPage('/', 0)
                            } else if (res.cancel) {
                                if (back == 'back') {
                                    var i = window.location.hash.substr(1);
                                    uni.setStorageSync('historyUrl', i);
                                    that.$gotoPage('/', 0)
                                }
                                // if (back == 'back') {
                                //     that.$gotoPage(1, 4)
                                // }
                                console.log('用户点击取消');
                            }
                        }
                    });
                } else {
                    try {
                        if (window.location.hash.indexOf('app=android') == -1) {
                            window.webkit.messageHandlers.appLogin.postMessage('不能为空');
                        } else {
                            window.Android.appLogin();
                        }
                    } catch (e) {
                    }
                }
            }
            return false
        }
    }
    验证登陆

    记得布全局

    import Vue from 'vue'
    import App from './App'
    
    import {myAjax,apiUrlList} from 'appConfig/ajax.js'
    import upFile from 'appConfig/upFile.js'
    import {myAlert,gotoPage,appLogin} from 'appConfig/utils.js'
    
    
    Vue.config.productionTip = false;
    
    App.mpType = 'app';
    Vue.prototype.$http =myAjax ;
    Vue.prototype.$upFile =upFile ;
    Vue.prototype.$apiUrlList =apiUrlList ;
    Vue.prototype.$limit =10 ;//每页数量
    Vue.prototype.$myAlert =myAlert;//互交反馈
    Vue.prototype.$gotoPage =gotoPage;//页面跳转
    Vue.prototype.$appLogin =appLogin;//应用登录
    
    
    const app = new Vue({
        ...App
    });
    app.$mount();
    全局部署
  • 相关阅读:
    input file 上传图片并显示
    关于npm ---- npm 命令行运行多个命令
    webpack4.x 配置
    React的生命周期
    HTML5 meta 属性整理
    css 命名规范
    html5 标签 meter 和 progress
    .NET Linq TO XML 操作XML
    .NET 字符串指定规则添加换行
    Linux Centos上部署ASP.NET网站
  • 原文地址:https://www.cnblogs.com/caitangbutian/p/12937999.html
Copyright © 2011-2022 走看看