import md5 from 'blueimp-md5';
import axios from '@/utils/http';
const base = process.env.VUE_APP_SERVER + '/user';
function encryptPwd(pwd: string) {
    return md5(pwd + md5(pwd.length));
}
const user = {
    isSetPwd(params: any) {
        return axios.post(`${base}/isSetPwd`, {
            country: '86',
            phone: '',
            email: '',
            ...params,
        });
    },
    login(params: any) {
        const { pwd } = params;
        if ( pwd ) {
            params.pwd =  encryptPwd(pwd);
        }
        return axios.post(`${base}/login`, {
            login_type: 1,
            country: '86',
            phone: '',
            email: '',
            pwd: '',
            code: '',
            ...params,
        });
    },
    sendSms(phone: string, templateId: number = 0, params: any) {
        return axios.post(`${base}/sendSms`, {
            phone,
            template_id: templateId,
            ...params,
        });
    },
    sendEmail(email: string, templateId: number = 0, params: any) {
        return axios.post(`${base}/sendEmail`, {
            email,
            template_id: templateId,
            ...params,
        });
    },
    setPwd(codeType: number, pwd: string, code: string, params: any) {
        pwd = encryptPwd(pwd);
        return axios.post(`${base}/setPwd`, {
            code_type: codeType,
            pwd,
            code,
            country: '86',
            ...params,
        });
    },
    setPwdCode(codeType: number, code: string, params: any) {
        return axios.post(`${base}/setPwdCode`, {
            code_type: codeType,
            code,
            country: '86',
            ...params,
        });
    },
    getUserInfo(id: number = 0) {
        return axios.get(`${base}/${id}`);
    },
    updateUserInfo(id: number = 0, params: any) {
        return axios.patch(`${base}/${id}`, {
            ...params,
        });
    },
    setPhone(code: string, newPhone: string, randomToken: string, params: any) {
        return axios.post(`${base}/setPhone`, {
            code,
            new_phone: newPhone,
            random_token: randomToken,
            ...params,
        });
    },
    setPhoneCode(codeType: string, code: string, params: any) {
        return axios.post(`${base}/setPhoneCode`, {
            code_type: codeType,
            code,
            country: '86',
            ...params,
        });
    },
};
export default user;
