helper.class.js 4.38 KB
/**
 * 定义常用方法类
 */

// 引入组件
import CallApp from 'callapp-lib';

export default class Helper {
	
	// 构造函数
    constructor(context) {
		
		// 设置上下文
		this.context = context;
    }
	
	/**
	 * 设置唤起App弹窗
	 * @param {object} param [唤起App定向页面参数]
	 * @param {function} callback [回调函数]
	 */
	schemeLayer = function(param, callback) {
		
		// 唤起App操作
		function _doApp(callApp, path, param) {
			callApp.open({
			    path: path,
				param: param
			});
		}
		
		// 设置平台固定参数
		let path = this.context.isiPhone.value ? "ios-path" : "shop/schemaFilter";
		let intentPackage = this.context.mode == "prod" ? "com.cnlive.strike" : "com.cnlive.strike.debug";
		let universalHost = this.context.mode == "prod" ? "applelinkstest.cnlive.com" : "applelinkstest.cnlive.com";
		
		// H5唤起App参数
		let options = {
                scheme: {
                    protocol: "cnlive"
                },
                intent: {
                    package: intentPackage,
                    scheme: "cnlive"
                },
                universal: {
                    host: universalHost + "/app_store.html",
                    pathKey: "page"
                },
                appstore: "https://sj.qq.com/myapp/detail.htm?apkName=com.cnlive.strike",
                yingyongbao: "https://sj.qq.com/myapp/detail.htm?apkName=com.cnlive.strike",
                fallback: "http://wjjh5.cnlive.com/apkdownload.php"
            }
			
			// 实例化对象
			let callApp = new CallApp(options);
			
			// 回调
			if (typeof callback === "function") {
				
				// 点击确定执行数据
				let options = {
					success: _doApp,
					success_params: {
						callApp, path, param
					}
				}
				
				// 回调函数
				callback(options);
			}
	}

	/**
	 * 正则校验URL
	 * @param {string} strURL [URL地址]
	 */
	checkURL = function(strURL) {
		
		// 校验参数
		if (strURL) {

			// 正则
			let reg = /(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/;
			
			// 校验匹配
			if (reg.test(strURL)) {
				return true;
			}
		}

		return false;
	}

	/* 加密/解密算法 开始 */

	/**
	 * 加密算法
	 * @param {string} word [原文]
	 */
	encrypt = function(word) {
		
		let CryptoJS = require("crypto-js/crypto-js");
		
		// KEY和IV
		const KEY = CryptoJS.enc.Utf8.parse("cnlive!$@cnlive_");
		const IV = CryptoJS.enc.Utf8.parse("yrEpH8Nx6umBLc1f");
		
		// 明文编码
		let srcs = CryptoJS.enc.Utf8.parse(word);
		
		// crypt加密
		var encrypted = CryptoJS.AES.encrypt(srcs, KEY, {
			iv: IV,
			mode: CryptoJS.mode.CBC,
			padding: CryptoJS.pad.Pkcs7
		});
		
		return encrypted.toString();
	}

	/**
	 * 解密算法
	 * @param {string} word [密文]
	 */
	decrypt = function(word) {
		
		let CryptoJS = require("crypto-js/crypto-js");
		
		// KEY和IV
		const KEY = CryptoJS.enc.Utf8.parse("cnlive!$@cnlive_");
		const IV = CryptoJS.enc.Utf8.parse("yrEpH8Nx6umBLc1f");
		
		let srcs = word;
		
		// let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
		// let srcs = CryptoJS.enc.Base64.stringify(word);
		let decrypt = CryptoJS.AES.decrypt(srcs, KEY, { iv: IV, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
		let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
		return decryptedStr.toString();
	}

	/* 加密/解密算法 结束 */

	/* 数字格式化 开始 */

	/**
	 * 获取输入数字的整数部分
	 */
	getInteger() {
		return (number) => {
			return parseInt(number);
		}
	}

	/**
	 * 获取输入数字的小数部分
	 */
	getDecimal() {
		return (number) => {

			// 拆分数字
			let numberArr = number.toString().split(".");

			// 判断是否有效数部分
			if (numberArr.length === 2) {
				return numberArr[1];
			} else {
				return "00";
			}
		}
	}

	/* 数字格式化 结束 */

	/* 数据格式处理 开始 */

	/**
	 * 对象内指定字符串内容替换
	 * @param {Object} o [目标对象]
	 * @param {String} key [目标字段名]
	 * @param {String} oldString [原始字符串]
	 * @param {String} newString [新字符串]
	 */
	objectStringReplaceAll(o, key, oldString, newString) {

		// 判断对象是否为数组
		if (typeof o === "object" && o !== null && o.length) {

			// 遍历数组,替换内容
			for (let i = 0; i < o.length; i++) {
				o[i][key] = o[i][key].replace(new RegExp(oldString, "gm"), newString);
			}
		}
	}

	/* 数据格式处理 结束 */

}