import request from '../js/request'

/**
 * 发送验证码
 * @param {*} params.type 0:登录 1:注册 2:修改密码 6:忘记密码 4:更换手机号第二步获取验证码 5:更换手机号第一步获取验证码
 * @returns
 */
export function apiGetYms(params) {
    return request({
        url: '/user-boot/appUser/authCode',
        data: {
            phone: params.phone,
            devCode: uni.getStorageSync('devCode'),
            type: params.type,
        },
        method: 'POST',
    })
}

/**
 * 登录
 * @param {*} params.type 0:ysm 1:pwd
 * @returns
 */
export function apiLogin(params) {
    console.log(uni.getStorageSync('devCode'))
    return request({
        url: '/shiningCloud/user/login',
        data: {
            phone: params.phone,
            devCode: uni.getStorageSync('devCode'),
            key: params.key.trim(),
            type: params.type,
        },
        method: 'POST',
    })
}

/**
 * 验证码登录
 * @param {*} params.type 0:ysm 1:pwd
 * @returns
 */
export function apiYsmLogin(params) {
    uni.setStorageSync('access_token', 'Basic bmpjbmFwcDpuamNucHFz')
    return request({
        url: '/pqs-auth/oauth/token',
        data: {
            grant_type: 'sms_code',
            phone: params.phone,
            smsCode: params.smsCode.trim(),
        },
        method: 'POST',
    })
}

/**
 * 注册
 * @param {*} params
 * @returns
 */
export function apiRegister(params) {
    return request({
        url: '/user-boot/appUser/register',
        data: {
            phone: params.phone,
            devCode: uni.getStorageSync('devCode'),
            code: params.code.trim(),
        },
        method: 'POST',
    })
}

/**
 * app用户注册完自动登录
 * @param params
 * @returns {*}
 */
export function autoLogin(phone) {
    return request({
        url: '/pqs-auth/oauth/autoLogin',
        data: {
            phone: phone,
        },
        method: 'POST',
    })
}

// 第一次登录设置密码
export function apiSetPsd(params) {
    return request({
        url: '/shiningCloud/user/setPsd',
        data: {
            userId: uni.getStorageSync('userInfo').userIndex,
            devCode: uni.getStorageSync('devCode'),
            password: params.password.trim(),
        },
        method: 'POST',
    })
}

// 重置密码
export function apiReSetPsd(params) {
    return request({
        url: '/user-boot/appUser/resetPsd',
        data: {
            phone: params.phone,
            code: params.code.trim(),
            devCode: uni.getStorageSync('devCode'),
            password: params.password.trim(),
        },
        method: 'POST',
    })
}

// 更换手机号第一步
export function apiComfirmCode(params) {
    return request({
        url: '/user-boot/appUser/confirmCode',
        data: {
            devCode: uni.getStorageSync('devCode'),
            phone: params.phone,
            code: params.code.trim(),
        },
        method: 'POST',
    })
}

// 重新绑定手机号
export function apiRebindPhone(params) {
    return request({
        url: '/user-boot/appUser/rebindPhone',
        data: {
            devCode: uni.getStorageSync('devCode'),
            userId: uni.getStorageSync('userInfo').userIndex,
            phoneNew: params.phone,
            code: params.code.trim(),
        },
        method: 'POST',
    })
}

// 角色升级
export function roleUpdate({ userId, referralCode }) {
    return request({
        url: '/user-boot/appRole/roleUpdate',
        method: 'post',
        data: {
            devCode: uni.getStorageSync('devCode'),
            referralCode,
            userId,
        },
    })
}

// 密码登录
export function apiPwdLogin(params) {
    return request({
        url: '/pqs-auth/oauth/token',
        header: {
            Authorization: 'Basic bmpjbnRlc3Q6bmpjbnBxcw==', // 客户端信息加密摘要认证
        },
        params,
        method: 'POST',
    })
}

//登录获取公钥
export function gongkey(data) {
    console.log(data)
    return request({
        url: '/user-boot/user/generateSm2Key',
        method: 'get',
        data: data,
    })
}

// 修改手机号
export function apiModifyPsd(params) {
    return request({
        url: '/user-boot/appUser/modifyPsd',
        data: {
            devCode: uni.getStorageSync('devCode'),
            userId: uni.getStorageSync('userInfo').userIndex,
            phone: params.phone,
            password: params.password.trim(),
            code: params.code.trim(),
        },
        method: 'POST',
    })
}

// 更改用户信息
export function apiUpdateUser(params) {
    return request({
        url: '/user-boot/user/updateAppUser',
        data: {
            ...params,
        },
        header: {
            'Content-Type': 'application/json',
        },
        method: 'PUT',
    })
}
// 更改用户信息
export function apiDeleteUser(params) {
    return request({
        url: '/user-boot/user/delete',
        data: {
            id: uni.getStorageSync('userInfo').userIndex,
        },
        method: 'DELETE',
    })
}

// 更新用户推送标识

export function apiUpdatePush(params) {
    uni.getPushClientId({
        success: (res) => {
            console.log('🚀 ~ apiUpdatePush111 ~ res:', res)
            let push_clientid = res.cid
            console.log(push_clientid, 'push_clientid')
            request({
                url: '/user-boot/appUser/updateDevCode',
                data: {
                    devCode: push_clientid,
                    userId: uni.getStorageSync('userInfo').userIndex,
                },
                method: 'POST',
            })
        },
        fail(err) {
            console.log('🚀 ~ apiUpdatePush222 ~ res:', err)
        },
    })
}
// 获取当前版本号
export function getLastData() {
    return request({
        url: '/cs-system-boot/appVersion/getLastData?versionType=APP',
        method: 'POST',
    })
}
