fix
This commit is contained in:
18
app/src/modules/config.ts
Normal file
18
app/src/modules/config.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import dotenv from 'dotenv';
|
||||
import path from 'path';
|
||||
export const env = dotenv.config({
|
||||
path: [path.resolve(process.cwd(), '.env'), path.resolve(process.cwd(), '.env.wxopen')],
|
||||
});
|
||||
console.log(env.parsed);
|
||||
|
||||
export const config = {
|
||||
domain: env.parsed?.DOMAIN,
|
||||
wx: {
|
||||
appId: env.parsed?.WX_MP_APP_ID,
|
||||
appSecret: env.parsed?.WX_MP_APP_SECRET,
|
||||
},
|
||||
wxOpen: {
|
||||
appId: env.parsed?.WX_OPEN_APP_ID,
|
||||
appSecret: env.parsed?.WX_OPEN_APP_SECRET,
|
||||
},
|
||||
};
|
||||
@@ -1,11 +1,5 @@
|
||||
import { useConfig } from '@kevisual/use-config';
|
||||
|
||||
type WxConfig = {
|
||||
appId: string;
|
||||
appSecret: string;
|
||||
};
|
||||
|
||||
const config = useConfig<{ wx: WxConfig }>();
|
||||
import { CustomError } from '@kevisual/router';
|
||||
import { config } from './config.ts';
|
||||
|
||||
export type WxTokenResponse = {
|
||||
access_token: string;
|
||||
@@ -25,12 +19,26 @@ export type WxToken = {
|
||||
unionid: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* 根据code获取token
|
||||
* @param code
|
||||
* @returns
|
||||
*/
|
||||
export const fetchToken = async (code: string): Promise<WxToken> => {
|
||||
const { appId, appSecret } = config.wx;
|
||||
let appId = config.wxOpen.appId;
|
||||
let appSecret = config.wxOpen.appSecret;
|
||||
if (!appId && !appSecret) {
|
||||
appId = config.wx.appId;
|
||||
appSecret = config.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(data)
|
||||
console.log('query token', data);
|
||||
return data;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user