common.js公共方法定义

//请求的域名
export let baseURL = "http://ceshi.com"; //测试域名

/*将base64转换为file*/
export function dataURLtoFile(dataurl, filename) { //将base64转换为文件
    var arr = dataurl.split(','),
        mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], filename, {
        type: mime
    });
}

/*将base64转换为Blob*/
export function dataURLtoBlob(urlData, fileType) {
    let bytes = window.atob(urlData);
    let n = bytes.length;
    let u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bytes.charCodeAt(n);
    }
    return new Blob([u8arr], {
        type: fileType
    });
}


/**
 * 保留4位小数
 */
export function floatNum(number, n) {
    n = n ? parseInt(n) : 0;
    if (n <= 0) {
        return Math.round(number);
    }
    // number = Math.round(number * Math.pow(10, n)) / Math.pow(10, n); //四舍五入
    number = Math.floor(number * 10000) / 10000; //不需要四舍五入
    number = Number(number).toFixed(n); //补足位数加0
    return number;
};


/*判断是否实名*/
export function shiming_remind(_this, callback) {
    var authen_status = _this.$store.state.userInfo.authen_status;
    if (authen_status == 0) {
        // 0:未实名
        _this.$dialog.confirm({
            title: '你还没有实名认证哦',
            message: '请先完成实名认证',
            confirmButtonText: '实名认证',
            showCancelButton: true,
            confirmButtonColor: '#139BFA',
        }).then(() => {
            _this.$router.push('real_name');
        }).catch(() => {});

    } else if (authen_status == 1) {
        // 2:认证失败,重新认证
        _this.$dialog.confirm({
            title: '实名认证审核中',
            confirmButtonText: '确认',
            showCancelButton: true,
            confirmButtonColor: '#139BFA',
        }).then(() => {}).catch(() => {});
    } else {
        // 实名认证已通过
        callback && callback();
    }
}


/*判断是否设置交易密码*/
export function pay_remind(_this, callback) {
    var pay_status = _this.$store.state.userInfo.pay_status;
    if (pay_status == 0) {
        // 0:未设置交易密码
        _this.$dialog.confirm({
            title: '你还没有设置交易密码哦',
            message: '设置交易密码才可以进行下一步',
            confirmButtonText: '设置密码',
            showCancelButton: true,
            confirmButtonColor: '#139BFA',
        }).then(() => {
            _this.$router.push('setup_pay_pwd');
        }).catch(() => {});

    } else {
        // 实名认证已通过
        callback && callback();
    }
}


// main.js引入common.js
//自定义方法引入
import { 
    dataURLtoFile,// base64转file
    dataURLtoBlob,// base64转blob
    floatNum,// 保留四位小数
    shiming_remind,//是否实名
    pay_remind,//是否设置交易密码
} from './assets/js/common.js'
Object.assign(Vue.prototype, {
	'upfile': dataURLtoFile,
	'upblob': dataURLtoBlob,
    'floatNum': floatNum,
    'shiming_remind':shiming_remind,
    'pay_remind':pay_remind,
})

//页面中使用直接this.方法名()调用
mounted(){

},
methods:{
	gopay(){
		//判断是实名
		this.shiming_remind(this,()=>{
			//判断是否设置支付密码
			this.pay_remind(this,()=>{
				//验证通过
				console.log('可以支付')
			})
		})
	},
},
发布了76 篇原创文章 · 获赞 144 · 访问量 3052

猜你喜欢

转载自blog.csdn.net/qq_40745143/article/details/103780428