import cloud from '@lafjs/cloud'; const db = cloud.database(); import crypto from 'crypto'; import { createHash } from 'crypto'; import Utils from "@/Utils"; const mchid = "1719895437";//商户id const serialNo = "2A97ED6ED441E55600EBC8830B9EC889ED41496E";//证书序列号 const geoip = require('geoip-lite'); const db1 = cloud.mongo.db; export default async function (ctx: FunctionContext) { //let ret1 = await db.collection("users").update({addLevel:0},{multi:true}); //return ret1; // const now = new Date(Date.now()+8*3600000); // now.setHours(0, 0, 0, 0); // 设置为 0 点 0 分 0 秒 0 毫秒 // const timestamp = now.getTime(); // console.log(timestamp) //await updateAddLevel(); // if (total != null) { // total = JSON.parse(total); // } else { // total = {}; // } // if (total["all"]) { // let isHave = false; // for (let i = 0; i < total["all"].length; i++) { // if (total["all"][i].id == uid) { // isHave = true; // total["all"][i].useravatar = res1.data?.useravatar, // total["all"][i].username = res1.data?.username, // total["all"][i].useravatarIcon = res1.data?.useravatarIcon, // total["all"][i].addLevel = add; // total["all"][i].time = Date.now(); // break; // } // } // if (isHave == false) { // let length = total["all"].length; // if (length < 10 || add > total["all"][length - 1].addLevel) { // total["all"].push({ // id: uid, // addLevel: add, // time: Date.now(), // username: res1.data?.username, // useravatar: res1.data?.useravatar, // useravatarIcon: res1.data?.useravatarIcon, // levelAmount: levelAmount // }); // } // } // } else { // total["all"] = [{ // id: uid, // addLevel: add, // time: Date.now(), // username: res1.data?.username, // useravatar: res1.data?.useravatar, // useravatarIcon: res1.data?.useravatarIcon, // levelAmount: levelAmount // }]; // } // total["all"].sort((a, b) => { // if (a.addLevel !== b.addLevel) { // return b.addLevel - a.addLevel; // } // return a.time - b.time; // }); // if (total["all"].length >= 10) { // total["all"] = total["all"].slice(0, 10); // } // if (total[address]) { // let isHave = false; // for (let i = 0; i < total[address].length; i++) { // if (total[address][i].id == uid) { // isHave = true; // total[address][i].useravatar = res1.data?.useravatar, // total[address][i].username = res1.data?.username, // total[address][i].useravatarIcon = res1.data?.useravatarIcon, // total[address][i].addLevel = add; // total[address][i].time = Date.now(); // break; // } // } // if (isHave == false) { // let length = total[address].length; // if (length < 10 || add > total[address][length - 1].addLevel) { // total[address].push({ // id: uid, // addLevel: add, // time: Date.now(), // username: res1.data?.username, // useravatar: res1.data?.useravatar, // useravatarIcon: res1.data?.useravatarIcon, // levelAmount: levelAmount // }); // } // } // } else { // total[address] = [{ // id: uid, // addLevel: add, // time: Date.now(), // username: res1.data?.username, // useravatar: res1.data?.useravatar, // useravatarIcon: res1.data?.useravatarIcon, // levelAmount: levelAmount // }]; // } // total[address].sort((a, b) => { // if (a.addLevel !== b.addLevel) { // return b.addLevel - a.addLevel; // } // return a.time - b.time; // }); // if (total[address].length >= 10) { // total[address] = total[address].slice(0, 10); // } // if (!total["role"]) { // total["role"] = {}; // } // if (levelTJ == 0) { // if (!total["role"][address]) { // total["role"][address] = 1; // } else { // total["role"][address] += 1; // //console.log(address+"真实:"+total[address].length+"----"+total["role"][address]); // let length = total[address].length; // if (length < 10) { // total["role"][address] = length; // } // //console.error(address + "真实:" + total[address].length + "----" + total["role"][address]) // // const res3 = await db.collection("sRank").where({ name: address }).getOne(); // // let length1=res3.data.totoal.length; // // if(length1<100&&total["role"][address]>length1){ // // total["role"][address]=length1; // // } // //console.error(address+":"+length); // } // } // //console.log(address); // let ret1 = await db.collection("sRank").where({ _id: db1 }).update({ total: JSON.stringify(total) }); // let rank = -1; // for (let i = 0; i < total[address].length; i++) { // if (total[address][i].id == uid) { // rank = i + 1; // break; // } // } // return rank; } function stringToHash(input: string, algorithm: 'sha1' | 'sha256' | 'sha512' = 'sha256'): string { return createHash(algorithm).update(input).digest('hex'); } async function updateAddLevel(){ // const ret = await db1.collection('users').updateMany({ // addLevel: { $exists: true } // }, // [ // { // $set: { // addLevel: { $floor: ["$addLevel"] } // } // } // ]); let rankInfo = await db.collection("sRank").where({ _id: "691093093cf0c4532ba4fd9c" }).getOne(); let total = rankInfo.data.total; total = JSON.parse(total); for (var key in total) { if (key !== "role") { for (let i = 0; i < total[key].length; i++) { total[key][i].addLevel = Math.floor(total[key][i].addLevel); } } } await db.collection("sRank").where({ _id: "691093093cf0c4532ba4fd9c" }).update({ total: JSON.stringify(total) }); } const propName = { "金币包1": "gold_1", "金币包2": "gold_2", "金币包3": "gold_3", "金币包4": "gold_4", "金币包5": "gold_5", "金币包6": "gold_6", "无限体力组合包1": "unlimited_health_bundle_1", "无限体力组合包2": "unlimited_health_bundle_2", "无限体力组合包3": "unlimited_health_bundle_3", "局内购买冻结": "freeze_in_game", "局内购买锤子": "hammer_in_game", "局内购买魔棒": "wand_in_gamerefill", "补满体力": "health" }; function getTodayStartTimestamp() { const date = new Date(); date.setHours(0, 0, 0, 0); return date.getTime(); } async function getWXOpenId(code) { let appid = "wxdd145ced49158a1e"; let appsecret = "7f295245ffc4884b03306c64e30654db"; const api_url = `https://api.weixin.qq.com/sns/jscode2session`; const param = `appid=${appid}&secret=${appsecret}&js_code=${code}&grant_type=authorization_code`; let res = null; try { res = await cloud.fetch(`${api_url}?${param}`, { headers: { "Accept-Encoding": 'gzip' } }); } catch { } if (res == null || res.data.errcode > 0) { return null } return [res?.data?.openid, res?.data?.session_key] } /**获取当前用户数据 */ async function getUserData(dbname: string, openid: any, isFirst?) { let { data } = await db.collection(dbname).where({ openid: openid }).get(); if (data.length) { return data[0]; } return null; } async function delayExecution(seconds: number) { return new Promise((resolve) => { setTimeout(() => { // 在这里编写需要延时执行的代码 resolve("延时执行完成"); }, seconds * 1000); }); } async function getIosOrder(openid) { let res = await db.collection("order").where({ state:0,type: "ios",openid: openid }).get(); let list = []; if (res.data.length > 0) { for (let i = 0; i < res.data.length; i++) { let ret = await getOrder(res.data[i].outTradeNo); if (ret.trade_state == "SUCCESS") { list.push(res.data[i]); } else { console.log(Date.now()-res.data[i].time); if (Date.now() - res.data[i].time > 3 * 3600000) { let ret = await db.collection('order').where({ outTradeNo: res.data[i].outTradeNo }).remove({ multi: true }); } } } } return list; } /**获取订单*/ async function getOrder(outTradeNo) { const url = `https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/${outTradeNo}?mchid=1719895437`; const nonceStr = crypto.randomBytes(16).toString('hex'); const timestamp = Math.floor(Date.now() / 1000).toString(); let au = await sign(outTradeNo, nonceStr, timestamp); let res = await fetch(url, { method: 'GET', // 默认是 GET,可省略 headers: { 'Authorization': au, // 认证令牌 'Content-Type': 'application/json', // 声明请求体类型(GET 请求通常不需要) 'Accept': 'application/json', // 声明期望的响应类型 } }) const data = await res.json(); return data; } async function sign(outTradeNo, nonceStr, timestamp) { const data = `GET\n/v3/pay/transactions/out-trade-no/${outTradeNo}?mchid=1719895437\n${timestamp}\n${nonceStr}\n\n` // 创建签名对象 const sign = crypto.createSign('RSA-SHA256'); // 更新签名内容 sign.update(data); // 获取存储桶 const bucket = cloud.storage.bucket('j3k323ol-colorblock-oss'); // 获取文件列表 const res = await bucket.readFile('apiclient_key.pem'); const privateKeyPem = await res.Body.transformToString(); // 使用私钥签名(PKCS1 填充) const signature = sign.sign({ key: privateKeyPem, padding: crypto.constants.RSA_PKCS1_PADDING }, 'base64'); // 构建Authorization头 const authorization = `WECHATPAY2-SHA256-RSA2048 mchid="${mchid}",nonce_str="${nonceStr}",signature="${signature}",timestamp="${timestamp}",serial_no="${serialNo}"`; return authorization; }