import {
    wxLogin,
    bindUserPhone,
    checkUserPhone,
} from "../request/api/LoginApi";
import { getWeChatAuthCode } from "./AuthManager";

const loginAuth = async () => {
    try {
        const openIdCode = await getWeChatAuthCode();
        console.log("获取到的微信授权code:", openIdCode);
        const response = await wxLogin({
            openIdCode: [openIdCode],
            grant_type: "wechat",
            scope: "server",
            clientId: "2",
        });
        console.log("获取到的微信授权response:", response);

        if (response.access_token) {
            uni.setStorageSync("token", response.access_token);
            return response;
        } else {
            throw new Error(response.message || "登录失败");
        }
    } catch (err) {
        throw err;
    }
};

const bindPhone = async (params) => {
    try {
        const response = await bindUserPhone(params);
        return response;
    } catch (error) {
        throw err;
    }
};

const checkPhone = async (phone) => {
    try {
        const response = await checkUserPhone(phone);
        return response;
    } catch (error) {
        throw err;
    }
};

export { loginAuth, bindPhone, checkPhone };
