This commit is contained in:
2025-11-28 20:05:12 +08:00
parent 39fab83b3d
commit f9169bcd8b
5 changed files with 410 additions and 0 deletions

View File

@@ -0,0 +1,125 @@
import { CustomError } from '@kevisual/router';
import { useConfig } from '@kevisual/use-config';
export const config = useConfig()
const wxOpen = {
appId: config.WX_OPEN_APP_ID,
appSecret: config.WX_OPEN_APP_SECRET,
}
const wx = {
appId: config.WX_MP_APP_ID,
appSecret: config.WX_MP_APP_SECRET,
}
console.log('wx config', wx, wxOpen);
export type WxTokenResponse = {
access_token: string;
expires_in: number;
refresh_token: string;
openid: string;
scope: string;
unionid: string;
};
export type WxToken = {
access_token: string;
expires_in: number;
refresh_token: string;
openid: string;
scope: string;
unionid: string;
};
/**
* 根据code获取token
* @param code
* @returns
*/
export const fetchToken = async (code: string, type: 'open' | 'mp' = 'open'): Promise<WxToken> => {
let appId = wxOpen.appId;
let appSecret = wxOpen.appSecret;
if (type === 'mp') {
appId = wx.appId;
appSecret = wx.appSecret;
}
if (!appId || !appSecret) {
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`;
const res = await fetch(wxUrl);
const data = await res.json();
console.log('query token', data);
return data;
};
type UserInfo = {
openid: string;
nickname: string;
sex: number;
language: string;
province: string;
country: string;
headimgurl: string;
privilege: string[];
unionid: string;
};
/**
* 获取用户信息
* @param token
* @param openid
* @returns
*/
export const getUserInfo = async (token: string, openid: string): Promise<UserInfo> => {
const phoneUrl = `https://api.weixin.qq.com/sns/userinfo?access_token=${token}&openid=${openid}`;
const res = await fetch(phoneUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
});
const data = await res.json();
console.log(data);
return data;
};
// getUserInfo(token.access_token, token.openid)
type AuthRes = {
errcode: number;
errmsg: string;
};
/**
* errorcode 0: 正常
* @param token
* @param openid
* @returns
*/
export const getAuth = async (token: string, openid: string): Promise<AuthRes> => {
const authUrl = `https://api.weixin.qq.com/sns/auth?access_token=${token}&openid=${openid}`;
const res = await fetch(authUrl);
const data = await res.json();
// console.log(data)
return data;
};
// getAuth(token.access_token, token.openid)
type RefreshToken = {
access_token: string;
expires_in: number;
refresh_token: string;
openid: string;
scope: string;
};
export const refreshToken = async (refreshToken: string): Promise<RefreshToken> => {
const { appId } = config.wx;
const refreshUrl = `https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=${appId}&grant_type=refresh_token&refresh_token=${refreshToken}`;
const res = await fetch(refreshUrl);
const data = await res.json();
return data;
};
// refreshToken(token.refresh_token)