import Vue from 'vue'
import md5 from './md5.js';
import App from '@/App.vue'

export function getSign(params, timestamp) {

	var relurt = '';
	if (typeof params == "string") {
		relurt = paramsStrSort(params, timestamp);
	} else if (typeof params == "object") {
		var relurt = jsonSort(params);
		relurt = paramsStrSort(relurt, timestamp);
	}
	return relurt;
}
export function paramsStrSort(paramsStr, timestamp) {
	console.log('paramsStr',paramsStr)
	let author = Vue.prototype.$author;
	let sp_paramsStr = paramsStr.split('&')
	let newParamsStr = []
	for (var i = 0; i < sp_paramsStr.length; i++) {
		if (sp_paramsStr[i] != '' && sp_paramsStr[i] != 'null' && sp_paramsStr[i] != 'undefined') {
			newParamsStr.push(sp_paramsStr[i])
		}
	}
	newParamsStr = newParamsStr.join('&')
	var newUrl = newParamsStr + '@' + timestamp + '@' + author.authkey;
	return md5(newUrl);
}
export function jsonSort(jsonObj) {
	let arr = [];
	for (var key in jsonObj) {
		if (jsonObj[key] != undefined)
			arr.push(key);

	}
	arr.sort();
	let str = '';
	for (var i in arr) {
		str += jsonObj[arr[i]] + "&"
	}
	const relurt = str.substr(0, str.length - 1)
	return relurt;
}
