init
This commit is contained in:
108
app/src/modules/wx.ts
Normal file
108
app/src/modules/wx.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import { useConfig } from '@kevisual/use-config';
|
||||
|
||||
type WxConfig = {
|
||||
appId: string;
|
||||
appSecret: string;
|
||||
};
|
||||
|
||||
const config = useConfig<{ wx: WxConfig }>();
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
export const fetchToken = async (code: string): Promise<WxToken> => {
|
||||
const { appId, appSecret } = config.wx;
|
||||
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(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();
|
||||
console.log(data);
|
||||
return data;
|
||||
};
|
||||
|
||||
// refreshToken(token.refresh_token)
|
||||
Reference in New Issue
Block a user