Skip to content

Commit

Permalink
v1.12.11 (#47)
Browse files Browse the repository at this point in the history
* 稍微重构一下

* v1.12.10

* 代码风格调整

* v1.12.11

* v1.12.11

* utils云函数加入部署指引

* v1.12.11
  • Loading branch information
Zing22 committed Jan 24, 2024
1 parent cf8a241 commit 81bb76f
Show file tree
Hide file tree
Showing 51 changed files with 747 additions and 577 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# :pencil:中大猫谱
# :pencil:中大猫谱(笃行猫谱)

*——拍照记录校园内猫猫的成长轨迹* :cat::smiley_cat::heart_eyes_cat:

Expand All @@ -20,7 +20,7 @@
<img src="https://user-images.githubusercontent.com/8966971/212461459-d9600a11-f01c-4837-81af-47794c39dd62.jpg" height = "200"/>

## 体验一下
打开微信,扫码或搜索“中大猫谱”
打开微信,扫码或搜索“笃行猫谱”(原名中大猫谱,23年底更名)

<img src="./readme/qrcode1.png" height = "150" alt="中大猫谱菊花码" />

Expand Down Expand Up @@ -52,13 +52,13 @@ Laf版暂时没有录制部署视频,部署流程请参考:[猫谱Laf版文

考虑到wx云开发的[新收费标准](https://developers.weixin.qq.com/community/minihome/doc/0000a680588d3891fa2ec250c51401)(约50元/月),我们将代码迁移到[Laf云开发](https://laf.run/signup?code=Ld9h8b4)平台。

Laf云是一个开源的云开发平台,可以私有部署到自己的服务器上,也可以使用目前Laf官方的付费资源,约7元/月。
Laf云是一个开源的云开发平台,可以私有部署到自己的服务器上,也可以使用目前Laf官方的付费资源,约13元/月。

国内高校同学可以免费使用,参考文档进行申请:[【腾讯文档】中大猫谱-Laf高校代金券计划](https://docs.qq.com/doc/DSGhxbVhuUnJqaUhF)

# :dancers:开发团队

本项目的开发工作100%用爱发电:sparkles:,特别感谢每一位为此付出的小伙伴。
本项目的开发工作100%用爱发电,特别感谢每一位为此付出的小伙伴。

:computer:代码开发:[Zing22](https://github.com/Zing22)[Jazon](https://github.com/jxzhn)[Junius](https://github.com/JunDeer)[yw](https://github.com/WondrousWisdomcard)[Lauzeyu](https://github.com/parsifal486)

Expand Down
20 changes: 10 additions & 10 deletions functions/__interceptor__.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ function compareDateStrings(dateString1: string, dateString2: string, n: number)
}


export async function main(ctx: FunctionContext) {
export async function main(ctx: FunctionContext, next: Function) {
// 请求的实际IP
const ip = ctx.headers['x-real-ip']
const { host } = ctx.headers;
const { APPID, DEV_IPS } = cloud.env;
const { APPID, DEV_IPS } = process.env;

if (ip === undefined && host === `${APPID}.${APPID}:8000`) {
// 触发器触发
return true;
return await next(ctx);
}

// 白名单ip,用于开发
if (DEV_IPS && DEV_IPS.split(",").includes(ip)) {
return true;
return await next(ctx);
}


let signKey = cloud.env.SIGN_KEY;
let signKey = process.env.SIGN_KEY;
if (signKey) {
// 开启了签名检查
const { signdata, signstr } = ctx.headers;
Expand All @@ -48,8 +48,8 @@ export async function main(ctx: FunctionContext) {
if (!isValid) {
console.log("invalid sign");
console.log(ctx.headers);
console.log(cloud.env);
return false;
console.log(process.env);
return "invalid sign";
}

// 检查时间
Expand All @@ -60,10 +60,10 @@ export async function main(ctx: FunctionContext) {
if (!timeCheck) {
console.log("time check failed", now, signdata);
console.log(ctx.headers);
console.log(cloud.env);
return false;
console.log(process.env);
return "time check failed";
}
}

return true;
return await next(ctx);
}
13 changes: 6 additions & 7 deletions functions/commentCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
import cloud from '@lafjs/cloud'
import axios from 'axios';

exports.main = async function (ctx: FunctionContext) {
// body, query 为请求参数, user 是授权对象
const { body, query } = ctx
import { getAccessToken } from '@/getAccessToken'

export default async function (ctx: FunctionContext) {
const { body } = ctx

if (body && body.deploy_test === true) {
// 进行部署检查
return "v1.0";
return "v1.1";
}

const openid = ctx.user?.openid;
const access_token = await cloud.invoke('getAccessToken', {});
// console.log("OpenID:", openid);
// console.log("AccessToken:", access_token);
const access_token = await getAccessToken();

try {
const url = 'https://api.weixin.qq.com/wxa/msg_sec_check';
Expand Down
27 changes: 15 additions & 12 deletions functions/countPhoto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,8 @@ const db = cloud.database();
const _ = db.command;
const MAX_LIMIT = 100;

exports.main = async function (ctx: FunctionContext) {
// body, query 为请求参数, user 是授权对象
const { body, query } = ctx;

if (body && body.deploy_test === true) {
// 进行部署检查
return "v1.0";
}

export async function countPhoto () {
// 先取出 mphoto 更新时间为一小时前的猫猫(因为每小时自动执行一次)
var frontOneHour = new Date(new Date().getTime() - 1 * 60 * 60 * 1000);
var condition = _.or([{
Expand Down Expand Up @@ -46,10 +39,10 @@ exports.main = async function (ctx: FunctionContext) {
const count_best = (await db.collection('photo').where({ cat_id: cat._id, best: true, verified: true }).count()).total;
const count_total = (await db.collection('photo').where({ cat_id: cat._id, verified: true }).count()).total;
var stat = await db.collection('cat').doc(cat._id).update({
// TODO: 过渡一下,后续清理数据库中的残留photo_count字段
// photo_count: count_best,
photo_count_best: count_best,
photo_count_total: count_total
// TODO: 过渡一下,后续清理数据库中的残留photo_count字段
// photo_count: count_best,
photo_count_best: count_best,
photo_count_total: count_total
});
stat.cat_id = cat._id;
stats.push(stat);
Expand All @@ -60,4 +53,14 @@ exports.main = async function (ctx: FunctionContext) {
}


export default async function (ctx: FunctionContext) {
const { body } = ctx;

if (body && body.deploy_test === true) {
// 进行部署检查
return "v1.1";
}

return await countPhoto();
}

59 changes: 21 additions & 38 deletions functions/curdOp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// curdOp 数据库操作云函数
import cloud from '@lafjs/cloud'
import { login } from '@/login'
import { isManager } from '@/isManager'
import { deleteFiles } from '@/deleteFiles'

const db = cloud.database();

// 操作对应collection需要的等级
Expand Down Expand Up @@ -98,14 +102,12 @@ const permissionAuthor = {
"read": {},
}

exports.main = async function (ctx: FunctionContext) {
// body, query 为请求参数, user 是授权对象
// console.log("ctx:", ctx);
export default async function (ctx: FunctionContext) {
const { body } = ctx;

if (body && body.deploy_test === true) {
// 进行部署检查
return "v1.3";
return "v1.4";
}

var openid = ctx.user.openid; // 用户的 OpenID
Expand All @@ -123,7 +125,7 @@ exports.main = async function (ctx: FunctionContext) {
console.log("curdOp param:", body);
// TODO, 不要login了
if (!openid) {
openid = (await cloud.invoke("login", { body: { wx_code: body.wx_code } })).openid;
openid = (await login(body.wx_code)).openid;
if (!openid) {
return;
}
Expand Down Expand Up @@ -208,15 +210,7 @@ exports.main = async function (ctx: FunctionContext) {
async function check_permission(collection, item_id, openid, level, allowAuthor) {
console.log(`Check premission for ${openid} with level ${level}, allowAuthor: ${allowAuthor}.`);
// 是否满足管理员等级
const isManager = await cloud.invoke('isManager', {
user: {
openid: openid,
},
body: {
req: level
}
});
if (isManager) {
if (await isManager(openid, level)) {
return true
}

Expand All @@ -231,28 +225,17 @@ async function check_permission(collection, item_id, openid, level, allowAuthor)

// 删除图片
async function delete_photo_for_news(item_id) {
db.collection('news').doc(item_id).get().then(res => {
var item = res.data;
// 删除云储存的图片
console.log("Photo path:", item.photosPath);
console.log("Cover path:", item.coverPath);
if (item.photosPath && item.photosPath.length > 0) {
cloud.invoke("deleteFiles", {
body: {
fileIDs: item.photosPath
}
}).then(res => {
console.log("删除公告图片", item.photosPath);
});
}
if (item.coverPath) {
cloud.invoke("deleteFiles", {
body: {
fileIDs: [item.coverPath],
}
}).then(res => {
console.log("删除公告封面", item.coverPath);
});
}
});
let item = (await db.collection('news').doc(item_id).get()).data;

// 删除云储存的图片
console.log("Photo path:", item.photosPath);
console.log("Cover path:", item.coverPath);
if (item.photosPath && item.photosPath.length > 0) {
await deleteFiles(item.photosPath);
console.log("删除公告图片", item.photosPath);
}
if (item.coverPath) {
await deleteFiles([item.coverPath]);
console.log("删除公告封面", item.coverPath);
}
}
28 changes: 15 additions & 13 deletions functions/deleteFiles.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
// deleteFiles 删除云储存文件
// SDK: http://docs.minio.org.cn/docs/master/javascript-client-api-reference#removeObject
import cloud from '@lafjs/cloud'
const Minio = require('minio')
import * as Minio from 'minio';

import { getAppSecret } from '@/getAppSecret'

function getFilePath(fullPath: string) {
// 形如:`https://${OSS_ENDPOINT}:${OSS_PORT}/${OSS_BUCKET}/`
return fullPath.split('/').slice(4).join("/");
}

exports.main = async function (ctx: FunctionContext) {
// body, query 为请求参数, user 是授权对象
const { body, query } = ctx
if (body && body.deploy_test === true) {
// 进行部署检查
return "v1.0";
}

const { OSS_ENDPOINT, OSS_PORT, OSS_BUCKET, OSS_SECRET_ID, OSS_SECRET_KEY } = await cloud.invoke("getAppSecret", {});
export async function deleteFiles(fileIDs: Array<string>) {
const { OSS_ENDPOINT, OSS_PORT, OSS_BUCKET, OSS_SECRET_ID, OSS_SECRET_KEY } = await getAppSecret(false);

const fileIDs = ctx.body.fileIDs;

const client = new Minio.Client({
bucketName: OSS_BUCKET,
endPoint: OSS_ENDPOINT,
Expand All @@ -37,4 +29,14 @@ exports.main = async function (ctx: FunctionContext) {
});
console.log(`Removed the object ${fileName} from bucket ${OSS_BUCKET}`);
}
}

export default async function (ctx: FunctionContext) {
const { body } = ctx
if (body && body.deploy_test === true) {
// 进行部署检查
return "v1.1";
}

return await deleteFiles(body.fileIDs)
}
10 changes: 5 additions & 5 deletions functions/deployTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cloud from '@lafjs/cloud'
import crypto from 'crypto';
import { getAppSecret } from '@/getAppSecret'

async function checkFuncs(funcs) {
let res = {
Expand Down Expand Up @@ -71,13 +72,12 @@ function genRandomKey() {
}


exports.main = async function (ctx: FunctionContext) {
// body, query 为请求参数, user 是授权对象
const { body, query } = ctx
export default async function (ctx: FunctionContext) {
const { body } = ctx

if (body && body.deploy_test === true) {
// 进行部署检查
return "v1.1";
return "v1.2";
}

const { opType } = body;
Expand All @@ -87,7 +87,7 @@ exports.main = async function (ctx: FunctionContext) {
} else if (opType == 'database') {
return await checkDb(body.dbName, body.dbInit);
} else if (opType == 'resetSecret') {
return await cloud.invoke("getAppSecret", { body: { reset: true } });
return await getAppSecret(true);
} else if (opType == 'genRSAKeys') {
return genRSAKeys();
} else if (opType == 'genRandomKey') {
Expand Down
26 changes: 16 additions & 10 deletions functions/getAccessToken.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import cloud from '@lafjs/cloud'
import axios from 'axios';

import { getAppSecret } from "@/getAppSecret"

const db = cloud.database();

exports.main = async function (ctx: FunctionContext) {
// body, query 为请求参数, user 是授权对象
const { body, query } = ctx

if (body && body.deploy_test === true) {
// 进行部署检查
return "v1.0";
}
const { MP_APPID, MP_SECRET } = await cloud.invoke("getAppSecret", {});
export async function getAccessToken() {
const { MP_APPID, MP_SECRET } = await getAppSecret(false);

// 读取数据库
const record = (await db.collection('setting').doc("accessToken").get()).data;
Expand Down Expand Up @@ -48,6 +43,17 @@ exports.main = async function (ctx: FunctionContext) {
console.log("创建数据库记录", data);
await db.collection('setting').doc("accessToken").set(data);
}

return data.accessToken;
}

export default async function (ctx: FunctionContext) {
const { body } = ctx

if (body && body.deploy_test === true) {
// 进行部署检查
return "v1.1";
}

return await getAccessToken();
}
5 changes: 2 additions & 3 deletions functions/getAllSci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import cloud from '@lafjs/cloud'
const db = cloud.database()
const MAX_LIMIT = 100

exports.main = async function (ctx: FunctionContext) {
// body, query 为请求参数, user 是授权对象
const { body, query } = ctx;
export default async function (ctx: FunctionContext) {
const { body } = ctx;

if (body && body.deploy_test === true) {
// 进行部署检查
Expand Down
Loading

0 comments on commit 81bb76f

Please sign in to comment.