70 lines
2.3 KiB
TypeScript
70 lines
2.3 KiB
TypeScript
import cloud from '@lafjs/cloud';
|
|
import * as crypto from 'crypto';
|
|
const appid = 'wxdd145ced49158a1e'; // 微信小程序 AppId
|
|
const appsecret = '7f295245ffc4884b03306c64e30654db'; // 微信小程序 AppSecret
|
|
|
|
const appid1 = "wxe69a671ea2c93af3"; // 广告微信小程序 AppId
|
|
const appsecret1 = '1eef42cd8810024eaea6dac961f9b3b0'; // 广告微信小程序 AppSecret
|
|
//定时获取Token
|
|
export default async function (ctx: FunctionContext) {
|
|
let res = await getWxToken();
|
|
let res1= await getWxToken1();
|
|
return { res: res }
|
|
}
|
|
|
|
/**获取微信Token*/
|
|
async function getWxToken() {
|
|
const api_url = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${appsecret}`;
|
|
const ret = await cloud.fetch({
|
|
url: api_url,
|
|
method: "get",
|
|
});
|
|
if (ret?.data?.access_token) {
|
|
cloud.shared.set('wxaccess_token', ret.data.access_token);
|
|
return ret.data.access_token;
|
|
} else {
|
|
//access_token = null;
|
|
return null;
|
|
}
|
|
}
|
|
/**获取广告版本token*/
|
|
async function getWxToken1() {
|
|
const api_url = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid1}&secret=${appsecret1}`;
|
|
const ret = await cloud.fetch({
|
|
url: api_url,
|
|
method: "get",
|
|
});
|
|
if (ret?.data?.access_token) {
|
|
//console.log(ret.data.access_token);
|
|
cloud.shared.set('wxaccess_token1', ret.data.access_token);
|
|
PostToken(ret.data.access_token)
|
|
return ret.data.access_token;
|
|
} else {
|
|
//access_token = null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function PostToken(wxToken) {
|
|
const token =`ZxCKV476ksJvu9j2bnqQIelYNeHP0XWz`;
|
|
const url = `https://backend.gravity-engine.com/event_center/api/v1/base/wx/access_token/refresh/?access_token=${token}`;
|
|
const time=Math.floor(Date.now()/1000);
|
|
const appkey =`zKUewiWbjqpbn7jzvrmPvhygftARptfwBxco69kXOsLuJnQrSDe5HgdYF0hEqNxk`;
|
|
const str: string = `app_key=${appkey}&wx_access_token=${wxToken}¤t_time=${time}`;
|
|
const sign: string = crypto.createHash('md5').update(str).digest('hex').toLowerCase();
|
|
const data = {
|
|
"wx_access_token": wxToken,
|
|
"current_time": time,
|
|
"sign": sign
|
|
}
|
|
const res = await fetch(url, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(data)
|
|
})
|
|
const token1 = await res.json()
|
|
//console.log(data11);
|
|
return "ok"
|
|
} |