import {
	BrowseRecordAdd
} from '@/api/applist.js'
import sha256 from 'crypto-js/sha256'

//防抖函数
export function debounce(func, fnThis, wait) {
	var timer;
	return () => {
		timer && clearTimeout(timer);
		timer = setTimeout(() => {
			func.call(fnThis)
			timer = null
		}, wait);
	};
}

//节流函数
export function debounce1(func, fnThis, wait) {
	var timer;
	return () => {
		clearTimeout(timer);
		timer = setTimeout(func.bind(fnThis), wait);
	};
}

let goMiniAppFlag = false
//跳转小程序
export function goMiniApp(data, item) {

	if (goMiniAppFlag) return
	goMiniAppFlag = true

	let myData = JSON.parse(data)

	wx.navigateToMiniProgram({
		...myData,
		success: (res) => {
			// 打开成功
			goMiniAppFlag = false
			item && BrowseRecordAdd(item.id)
		},
		fail: () => {
			goMiniAppFlag = false
		}
	})
}

//跳转页面
export function goPage(url) {
	uni.navigateTo({
		url
	});
}

//根据类型跳转
export function clickJumpType(item) {
	if (item.jumpUrlType == 'goMiniApp') {
		goMiniApp(item.jumpUrl)
	}
	if (item.jumpUrlType == 'goPage') {
		goPage(item.jumpUrl)
	}
	if (item.jumpUrlType == 'goWebView') {
		goPage(`/pages/webview/webview?src=${item.jumpUrl}`)
	}
}


function base64_encode(str) {
	var c1, c2, c3;
	var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	var i = 0,
		len = str.length,
		string = '';

	while (i < len) {
		c1 = str.charCodeAt(i++) & 0xff;
		if (i == len) {
			string += base64EncodeChars.charAt(c1 >> 2);
			string += base64EncodeChars.charAt((c1 & 0x3) << 4);
			string += "==";
			break;
		}
		c2 = str.charCodeAt(i++);
		if (i == len) {
			string += base64EncodeChars.charAt(c1 >> 2);
			string += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
			string += base64EncodeChars.charAt((c2 & 0xF) << 2);
			string += "=";
			break;
		}
		c3 = str.charCodeAt(i++);
		string += base64EncodeChars.charAt(c1 >> 2);
		string += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
		string += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
		string += base64EncodeChars.charAt(c3 & 0x3F)
	}
	return string
}


//微信支付调起支付
export function wxPay(data) {
	
	wx.requestPayment({
		...data
	})

}
