57 lines
1.5 KiB
JavaScript
57 lines
1.5 KiB
JavaScript
import {
|
|
wxLogin,
|
|
bindUserPhone,
|
|
checkUserPhone,
|
|
} from "../request/api/LoginApi";
|
|
import { getWeChatAuthCode } from "./AuthManager";
|
|
import { useAppStore } from "@/store";
|
|
import { clientId } from "@/constant/base";
|
|
import { NOTICE_EVENT_LOGIN_SUCCESS } from "@/constant/constant";
|
|
|
|
const loginAuth = (e) => {
|
|
uni.setStorageSync("token", "");
|
|
const appStore = useAppStore();
|
|
appStore.setHasToken(false);
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
const openIdCode = await getWeChatAuthCode(e);
|
|
console.log("获取到的微信授权code:", openIdCode);
|
|
const params = {
|
|
openIdCode: [openIdCode],
|
|
grant_type: "wechat",
|
|
scope: "server",
|
|
clientId: clientId,
|
|
};
|
|
console.log("获取到的微信授权params:", JSON.stringify(params));
|
|
const response = await wxLogin(params);
|
|
console.log("获取到的微信授权response:", response);
|
|
|
|
if (response.access_token) {
|
|
uni.setStorageSync("token", response.access_token);
|
|
const appStore = useAppStore();
|
|
appStore.setHasToken(true);
|
|
// 登录成功后,触发登录成功事件
|
|
uni.$emit(NOTICE_EVENT_LOGIN_SUCCESS);
|
|
resolve();
|
|
} else {
|
|
reject(response.message || "登录失败");
|
|
}
|
|
});
|
|
};
|
|
|
|
const bindPhone = async (params) => {
|
|
try {
|
|
const response = await bindUserPhone(params);
|
|
return response;
|
|
} catch (error) {
|
|
throw err;
|
|
}
|
|
};
|
|
|
|
const checkPhone = async () => {
|
|
const response = await checkUserPhone();
|
|
return response;
|
|
};
|
|
|
|
export { loginAuth, bindPhone, checkPhone };
|