This commit is contained in:
2025-11-30 21:32:01 +08:00
parent b7f1095e4a
commit d4ff2862bd
11 changed files with 413 additions and 34 deletions

View File

@@ -42,7 +42,7 @@ export const fetchToken = async (code: string, type: 'open' | 'mp' = 'open'): Pr
appSecret = wx.appSecret;
}
if (!appId || !appSecret) {
throw new CustomError(500, 'appId or appSecret is not set');
throw new CustomError(500, 'appId or appSecret is not set');
}
console.log('fetchToken===', appId, appSecret, code);
const wxUrl = `https://api.weixin.qq.com/sns/oauth2/access_token?appid=${appId}&secret=${appSecret}&code=${code}&grant_type=authorization_code`;
@@ -65,7 +65,8 @@ type UserInfo = {
};
/**
* 获取用户信息
* 获取用户信息 通过授权登录
* 例子:微信内部网页,开放平台授权网页,确定登录的时候
* @param token
* @param openid
* @returns
@@ -80,10 +81,34 @@ export const getUserInfo = async (token: string, openid: string): Promise<UserIn
},
});
const data = await res.json();
console.log(data);
return data;
};
/**
* 公众号获取用户信息,
* 微信公众号扫码,
* 订阅登录,非授权,
* 而是根据你关注了用户才登录
* @param token
* @param openid
* @returns
*/
export const getUserInfoByMp = async (token: string, openid: string) => {
// const phoneUrl = `https://api.weixin.qq.com/sns/userinfo?access_token=${token}&openid=${openid}`;
const phoneUrl = `https://api.weixin.qq.com/cgi-bin/user/info?access_token=${token}&openid=${openid}&lang=zh_CN`;
const res = await fetch(phoneUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
});
const data = await res.json();
return data;
};
// getUserInfo(token.access_token, token.openid)
type AuthRes = {
@@ -123,3 +148,12 @@ export const refreshToken = async (refreshToken: string): Promise<RefreshToken>
};
// refreshToken(token.refresh_token)
export const post = async (url: string, data: any) => {
const res = await fetch(url, {
method: 'POST',
body: JSON.stringify(data),
});
return await res.json();
};