import request from "@/utils/request";
import {BASE, AUTH} from "@/api/apiGateway"

export function logout(data) {
	return request({
		url: `${AUTH}/auth/oauth/logout`,
        method: "post",
        data
	});
}

export function getUserInfo(token) {
	return request({
		url: `${AUTH}/user/info`,
		method: "get",
		params: { token }
	});
}

export function getPublicKey() {
	return request({
		url: `${AUTH}/auth/oauth/getPublicKey`,
		method: "get"
	});
}

export function login(data) {
	return request({
		url: `${AUTH}/auth/oauth/login`,
		method: "post",
		data
	});
}

export function sendCaptcha(data) {
    return request({
		url: `${AUTH}/auth/oauth/sendCaptcha`,
        method: "get",
        params: data
	});
}

export function forgetPwd(data) {
    return request({
		url: `/patient/basePatient/forgetPwd`,
        method: "post",
        params: data
	});
}

// 解密
export function getHwlyyDecrypt(data) {
    return request({
		url: `${AUTH}/auth/oauth/getHwlyyDecrypt`,
		method: "post",
		data
	});
}


export function imgCaptcha(params) {
  return request({
    url: `${AUTH}/auth/oauth/imgCaptcha`,
    method: 'get',
    params
  })
}


export function getShortMessage(params) {
	return request({
		url: `${AUTH}/auth/oauth/getShortMessage`,
		method: 'get',
		params
	})
}


export function captcha(data) {
	return request({
		url: `${AUTH}/auth/oauth/captcha`,
		method: 'post',
		data
	})
}



export function registerPatientAndLogin(data) {
	return request({
		url: `${AUTH}/auth/oauth/registerPatientAndLogin`,
        method: "post",
        data
	});
}

export function findPatientPw(data) {
	return request({
		url: `${AUTH}/auth/oauth/findPatientPw`,
        method: "post",
        data
	});
}
//注销账号    
export function deleteUser(data) {
	return request({
		url: `${BASE}/patient/deleteUser`,
        method: "post",
        data
	});
}

export function findUserBaseInfo(data) {
    return request({
		url: `${BASE}/user/findUserBaseInfo`,
        method: "get",
        params: data
	});
}

export function findOneUser(data) {
    return request({
		url: `${BASE}/user/queryOne`,
        method: "get",
        params: data
	});
}

export function findOneUserFormat(data) {
    return new Promise((resolve, reject)=>{
		request({
			url: `${BASE}/user/queryOne`,
			method: "get",
			params: data
		})
		.then(res=>{
			if(res && res.obj){
				// "saasAdmin"： "管理员"； regionAdmin" "区域管理员" ; communityAdmin "社区管理员" ;  "  "replenisher"： "补货员"
				var temArr = ['saasAdmin', 'regionAdmin', 'communityAdmin', 'replenisher']
				var role = res.obj.role instanceof Array? res.obj.role : [res.obj.role]
				var roleList = _.map(role, v=>{
					return {
						id: v.id,
						code: v.code,
						name: v.name
					}
				})
				roleList = _.sortBy(roleList, v=>{
					return temArr.indexOf(v.code)
				})

				var curRole = roleList[0]
				var userInfo = {
					roleList,
					curRoleId: curRole.id,
					curRoleCode: curRole.code,
					curRoleName: curRole.name
				}
				resolve(userInfo)
			}
		})
		.catch(err=>{
			reject(err)
		})
	})
}

export function changePwd(data) {
	return request({
	  url: `${BASE}/user/updatePwd`,
	  method: 'post',
	  data
	})
}

export function checkWlyyLogin(data) {
	return request({
	  url: `${AUTH}/auth/oauth/checkWlyyUserLogin`,
	  method: 'post',
	  data
	})
}

export function findLoginRoleMenuTree(data) {
	return request({
		url: `${BASE}/menu/findLoginRoleMenuTree`,
		method: "get",
		params: data
	});
}