import { touTiaoLogin,getTouTiaoUserInfo } from "./http";
import { msg } from "./utils";

export function ttLogin(params){
	return new Promise((resolve, reject)=>{
		touTiaoLogin(params).then(res=>{ 
			if(res.status){
				if(typeof res.data == "string"){
					tt.getUserInfo({
						withCredentials: true,
						success(result){
							getTouTiaoUserInfo({ 
								encrypted_data: result.encryptedData,session_key: res.data,iv: result.iv,
								raw_data: result.rawData,signature: result.signature
							}).then(user=>{
								if(user.status){
									resolve(user.data);
								}else{
									reject(user.msg);
								}
							}).catch(err=>{
								reject(err);
							});
						},
						fail(res){
							reject(res.errMsg);
						}
					});
				}else{
					resolve(res.data);
				}
			}else{
				reject(res.msg);
			}
		}).catch(err=>{
			reject(err);
		});
	});
}