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

export const businessConfig = (params) =>
	uni.$u?.http.post("/common/init", JSON.stringify(params));
// 验证是否升级
export const checkUpgrade = (params) =>
	uni.$u?.http.post("/common/checkUpgrade", JSON.stringify(params));
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/info",
		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 getArticle = (id,type) =>	uni.$u?.http.post("/article/detail",JSON.stringify({id,type:(type? type : 'id')}));
export const getFriendCircle = (page=1,limit=10) =>{
	return uni.$u?.http.get("/friendcircle/list",JSON.stringify({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()+"/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);
		})
	})
}