import APIConfig from './api.js';
import efunRequest from '../utils/efunRequest';
import request from '../utils/request';
import Loading from '../components/Loading';
export default class user {
	// 个人中心
	static userMember() {
		return request(`http://ott80test-base.yifangjiaoyu.cn/mobile/user/member`, {
			method: 'get'
		});
	}

	//更新个人信息
	static update_UserInfo(opts) {
		return request(APIConfig.getUserUrl(``), opts);
	}

	/** 
	* //上传头像
	* @return 返回Promise 
	*/
	static async uploadImage(photo_url) {
		Loading.show();
		const file_uid = await getuid();
		let formData = new FormData();
		formData.append('file', {
			uri: photo_url,
			name: file_uid + '.jpg',
			type: 'image/jpeg'
		});
		let options = {};
		options.body = formData;
		options.headers = { 'Content-Type': 'multipart/form-data', uid: file_uid };
		options.method = 'POST';
		return fetch(APIConfig.getUploadImg(), options)
			.then((response) => {
				console.log('res1', response);
				if (response.status >= 200 && response.status < 300) {
					Loading.hide();
					return response;
				}
				const error = new Error(response.statusText);
				error.response = response;
				Loading.hide();
				throw error;
			})
			.then((response) => {
				Loading.hide();
				return response.json();
			})
			.then((data) => {
				console.log('data', data);
				if (!data.success) {
					const code = data;
				}
				Loading.hide();
				return data;
			})
			.catch((err) => {
				console.log('err', err);
				Loading.hide();
				return {
					err
				};
			});
	}

	//绑定微信
	static bind_wechat(opts) {
		return request(APIConfig.getUserUrl() + `/wechatBind`, opts);
	}

	//获取验证码
	static getVerificationCode(phone) {
		return request(APIConfig.getUserUrl() + `/sendCode?mobile=` + phone);
	}
	//绑定手机号
	static bind_phone(opts) {
		return request(APIConfig.getUserUrl() + `/mobileBind`, opts);
	}

	//手机号登陆注册
	static mobileLoginAndReg(opts) {
		return request(APIConfig.getUserUrl() + `/mobileLoginAndReg`, opts);
	}

	//微信登陆
	static wechatLogin(opts) {
		return request(APIConfig.getUserUrl(``), opts);
	}
	//跳过
	static jumpLogin(deviceCode, channel) {
		console.log('====================================');
		console.log('deviceCode', deviceCode);
		console.log('channel', channel);
		console.log('====================================');
		return request(APIConfig.getIsLogin() + `?deviceCode=` + deviceCode + '&channel=' + channel, { method: 'GET' });
	}
}

async function getuid() {
	const uid = await storage
		.load({
			key: 'userInfo'
		})
		.catch((error) => {
			return '';
		});
	if (uid === '') {
		return '';
	}
	return JSON.parse(uid).uid;
}