helper.class.js 7.28 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: "https://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);
			}
		}
	}

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

	/* 定位相关处理 开始 */
	
	/**
	 * 获取用户地理位置坐标
	 * @param {Function} success [成功回调函数,获取定位坐标]
	 * @param {Function} error [错误回调函数,错误代码和信息]
	 * @param {Function} notSupport [浏览器不支持定位方法]
	 */
	getLocation(success, error, notSupport) {
		if (navigator.geolocation) {	// 支持定位
			navigator.geolocation.getCurrentPosition(success, error);
		} else {	// 不支持定位
			if (typeof notSupport === "function") {
				notSupport();
			}
		}
	}

	/**
	 * GPS坐标转换为高德火星坐标,调用gcj_encrypt函数,传入wgsLat(纬度)和wgsLon(经度)参数
	 */
	GPS = {
		PI : 3.14159265358979324,
		x_pi : 3.14159265358979324 * 3000.0 / 180.0,
		delta : function (lat, lon) {
			// Krasovsky 1940
			//
			// a = 6378245.0, 1/f = 298.3
			// b = a * (1 - f)
			// ee = (a^2 - b^2) / a^2;
			var a = 6378245.0; //  a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
			var ee = 0.00669342162296594323; //  ee: 椭球的偏心率。
			var dLat = this.transformLat(lon - 105.0, lat - 35.0);
			var dLon = this.transformLon(lon - 105.0, lat - 35.0);
			var radLat = lat / 180.0 * this.PI;
			var magic = Math.sin(radLat);
			magic = 1 - ee * magic * magic;
			var sqrtMagic = Math.sqrt(magic);
			dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * this.PI);
			dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * this.PI);
			return {'lat': dLat, 'lon': dLon};
		},
	
		//GPS---高德
		wgs84ToGcj02 : function ( wgsLat , wgsLon ) {

			// 将坐标转成浮点数
			wgsLat = parseFloat(wgsLat);
			wgsLon = parseFloat(wgsLon);

			if (this.outOfChina(wgsLat, wgsLon)) {
				return {'lat': wgsLat, 'lon': wgsLon};
			}
	
			var d = this.delta(wgsLat, wgsLon);
			return {'lat' : wgsLat + d.lat,'lon' : wgsLon + d.lon};
		},
		outOfChina : function (lat, lon) {
			if (lon < 72.004 || lon > 137.8347) {
				return true;
			}
			if (lat < 0.8293 || lat > 55.8271) {
				return true;
			}
			return false;
		},
		transformLat : function (x, y) {
			var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
			ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
			ret += (20.0 * Math.sin(y * this.PI) + 40.0 * Math.sin(y / 3.0 * this.PI)) * 2.0 / 3.0;
			ret += (160.0 * Math.sin(y / 12.0 * this.PI) + 320 * Math.sin(y * this.PI / 30.0)) * 2.0 / 3.0;
			return ret;
		},
		transformLon : function (x, y) {
			var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
			ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
			ret += (20.0 * Math.sin(x * this.PI) + 40.0 * Math.sin(x / 3.0 * this.PI)) * 2.0 / 3.0;
			ret += (150.0 * Math.sin(x / 12.0 * this.PI) + 300.0 * Math.sin(x / 30.0 * this.PI)) * 2.0 / 3.0;
			return ret;
		}
	};

	/* 定位相关处理 结束 */

}