import {
	appSecret,
	aesKey,
	aesIV,
} from './config'

const CryptoJS = require('crypto-js');

const key = CryptoJS.enc.Utf8.parse(aesKey);
const iv = CryptoJS.enc.Utf8.parse(aesIV);

export default {
	brower() {
		var u = navigator.userAgent,
			app = navigator.appVersion;
		//移动终端浏览器版本信息

		return {
			mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端,
			ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端,
			android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器,
			iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器,
			iPad: u.indexOf('iPad') > -1, //是否iPad,
			ie: u.indexOf('Trident') > -1, //IE内核,
			opera: u.indexOf('Presto') > -1, //opera内核,
			webkit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核,
			firefox: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核,
			safari: u.indexOf('Safari') == -1, //是否web应该程序，没有头部与底部,
			wx: u.match(/MicroMessenger/i), //是否在微信打开,
			weiBo: u.match(/WeiBo/i), //是否是在微博中打开,
			qq: u.match(/QQ/i) //是否在QQ打开
		}
	},
	getQrCode(url) {
		let str = url.toLowerCase();
		// if (str.startsWith('http://')) {
		str = str.split('?id=');
		if (str.length > 1) {
			return str[str.length - 1].toUpperCase();
		}
		// }
		return url;
	},
	delLastPartNum(number) {
		number = number.substring(0, number.lastIndexOf('-') != -1 ? number.lastIndexOf('-') : number.length) //去掉箱号
		return number
	},
	formatDate(date, needTime = false) {
		var date = new Date(date);
		var YY = date.getFullYear() + '-';
		var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
		var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
		if (needTime) {
			var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
			var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
			var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
			return YY + MM + DD + " " + hh + mm + ss;
		}

		return YY + MM + DD
	},
	toFixed(value, decimal = 2) {
		let num = value || 0;
		try {
			num = parseFloat(num).toFixed(decimal);
		} catch (e) {}
		return num;
	},
	arrayToImage(imageRawData) {
		return 'data:image/png;base64,' + btoa(new Uint8Array(imageRawData).reduce((data, byte) => data + String.fromCharCode(
			byte), ''))
	},
	/**
	 * 生成签名 http用
	 * @param param
	 * @returns {string}
	 */
	sign(param) {
		let sign = '',
			key = [],
			p = Object.assign({}, param)
	
		//参数排序
		p.key = appSecret
		_.forIn(p, (v, k) => {
			key.push(k)
		})
		key.sort()
	
		//生成签名字符串
		key.map((v, k) => {
			p[v] = (p[v] != null && p[v] != undefined) ? p[v] : '';
			if (typeof(p[v]) === 'object') {
				p[v] = JSON.stringify(p[v]);
			}
			sign += `&${v}=${p[v]}`;
		})
		sign = sign.substr(1)
		sign = CryptoJS.MD5(CryptoJS.MD5(sign)+ appSecret)
		
		sign = sign.toString()
	
		return sign
	}
}
