import config from "@/common/config";
// 登录

export const businessConfig = (params) =>
	uni.$u?.http.post("/common/init", JSON.stringify(params));
// 验证是否升级
export const checkUpgrade = (params) =>{
	const _this = this;
	return new Promise((resolve,reject)=>{
		let system = uni.getSystemInfoSync()
		plus.runtime.getProperty(plus.runtime.appid, function(inf) {
			uni.$u?.http.post("/common/checkUpgrade", JSON.stringify({
				version:system.appVersion,
				platform:system.platform,
				version_wgt:inf.versionCode,
			})).then(res=>{
				console.log(res);
				if(!res || !res.version){
					return reject(true);
				}
				let skip_version = uni.getStorageSync('skip_version')
				//console.log(res.version,skip_version);
				if(res && res.version!=skip_version){
					uni.$emit('closeWebview')
					uni.setStorageSync('upgrade_model',res)
					uni.navigateTo({
						url: '/pages/common/upgrade',
						animationType:"fade-in",
						complete(res1) {
							//console.log(res1);
							return resolve();
						}
					});
				}
				reject();
			}).catch(e=>{
				reject(e);
			})
		});
	});
};
export const businessLogin = (params) =>
	uni.$u?.http.post("/common/login", JSON.stringify(params));
export const businessSendSms = (params) =>
	uni.$u?.http.post("/common/captcha", JSON.stringify(params));
export const businessVerifyCode = (params) =>
	uni.$u?.http.post("/common/verify_captcha", JSON.stringify(params));
export const businessRegister = (params) =>
	uni.$u?.http.post("/common/register", JSON.stringify(params));
export const businessReset = (params) =>
	uni.$u?.http.post("/common/resetpwd", JSON.stringify(params));

export const businessModify = (params) =>
	uni.$u?.http.post(
		"/user/change_password",
		JSON.stringify({
			...params,
		}), {
			header: {
				token: uni.getStorageSync("BusinessToken"),
			},
		}
	);

// 用户信息
export const businessInfoUpdate = (params) =>
	uni.$u?.http.post(
		"/user/profile",
		JSON.stringify({...params}), 
		{
			header: {
				token: uni.getStorageSync("BusinessToken"),
			},
		}
	);
export const businessGetUserInfo = (userID) =>
	uni.$u?.http.post(
		"/user/find",
		JSON.stringify({userIDs: [userID],}), 
		{
			header: {
				token: uni.getStorageSync("BusinessToken"),
			},
		}
	);

export const businessSearchUserInfo = (keyword,searchtype) =>
	uni.$u?.http.post(
		"/user/search",
		JSON.stringify({
			keyword,
			searchtype:(searchtype? searchtype : 'id'),
			page: 1,
			limit: 10
		}), {
			header: {
				token: uni.getStorageSync("BusinessToken"),
			},
		}
	);

export const businessSearchUser = (keyword,searchtype) =>
	uni.$u?.http.post(
		"/friend/search",
		JSON.stringify({
			keyword,
			searchtype:(searchtype? searchtype : 'id'),
			page: 1,
			limit: 99,
		}), {
			header: {
				token: uni.getStorageSync("BusinessToken"),
			},
		}
	);
export const getSpage = (name) =>	uni.$u?.http.get(`/article/singpage?name=${name}`);
export const getArticle = (id) =>	uni.$u?.http.get(`/article/detail?id=${id}`);
export const getFriendCircle = (page=1,limit=10) =>{
	return uni.$u?.http.get(`/friendcircle/list?limit=${limit}&page=${page}`);
}
export const getFriendCircleNewcount = () =>{
	return uni.$u?.http.get("/friendcircle/newcount");
}
export const getFriendCircleInfo = () =>{
	return uni.$u?.http.get("/friendcircle/info");
}
export const upload = (files,data,onProgress) =>{
	if(typeof data == 'function'){
		onProgress = data;
		data = {};
	}
	console.log(typeof files);
	return new Promise((resolve,reject)=>{
		var u = uni.uploadFile({
			url: config.getRegisterUrl()+(data?.url || "/user/upload"),
			filePath: files,
			//files:files.length > 1 ? files : files[0],
			name: "file",
			formData:data,
			header:{
				token:uni.getStorageSync("BusinessToken"),
			},
			success({data,errMsg}){
				console.log(data);
				data = JSON.parse(data);
				if(data.code == 0){
					resolve(data);
				}else{
					reject(data.msg);
				}
			},
			fail(res) {
				console.log(e);
				reject(e);
			}
		});
		u.onProgressUpdate((e)=>{
			var res = {
				'code'		:	99999,
				'progress'	:	e.progress
			}
			onProgress && onProgress.call(this,res);
		})
	})
}