Files
autoAiWorkSys/api/controller_front/invite.js
张成 41e03daa50 1
2025-12-16 15:55:42 +08:00

300 lines
7.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 推广邀请管理控制器(客户端接口)
* 提供客户端调用的推广邀请相关接口
*/
const Framework = require("../../framework/node-core-framework.js");
module.exports = {
/**
* @swagger
* /api/invite/info:
* post:
* summary: 获取邀请信息
* description: 根据设备SN码获取邀请码和邀请链接
* tags: [前端-推广邀请]
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - sn_code
* properties:
* sn_code:
* type: string
* description: 设备SN码
* responses:
* 200:
* description: 获取成功
*/
'POST /invite/info': async (ctx) => {
try {
// 从body中获取sn_code
const body = ctx.getBody();
const { sn_code } = body;
if (!sn_code) {
return ctx.fail('请提供设备SN码');
}
const { pla_account } = await Framework.getModels();
// 根据sn_code查找用户
const user = await pla_account.findOne({
where: { sn_code }
});
if (!user) {
return ctx.fail('用户不存在');
}
// 生成邀请码基于用户ID和sn_code
const invite_code = `INV${user.id}_${Date.now().toString(36).toUpperCase()}`;
const invite_link = `https://work.light120.com/invite?code=${invite_code}`;
// 保存邀请码到用户记录可以保存到invite_code字段如果没有则保存到备注或其他字段
// 这里暂时不保存到数据库,每次生成新的
return ctx.success({
invite_code,
invite_link,
user_id: user.id,
sn_code: user.sn_code
});
} catch (error) {
console.error('获取邀请信息失败:', error);
return ctx.fail('获取邀请信息失败: ' + error.message);
}
},
/**
* @swagger
* /api/invite/statistics:
* post:
* summary: 获取邀请统计
* description: 根据设备SN码获取邀请统计数据
* tags: [前端-推广邀请]
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - sn_code
* properties:
* sn_code:
* type: string
* description: 设备SN码
* responses:
* 200:
* description: 获取成功
*/
'POST /invite/statistics': async (ctx) => {
try {
// 从body中获取sn_code
const body = ctx.getBody() || {};
const { sn_code } = body;
if (!sn_code) {
return ctx.fail('请提供设备SN码');
}
const { pla_account } = await Framework.getModels();
// 根据sn_code查找用户
const user = await pla_account.findOne({
where: { sn_code }
});
if (!user) {
return ctx.fail('用户不存在');
}
// 查询邀请统计
const { invite_record } = await Framework.getModels();
// 查询总邀请数
const totalInvites = await invite_record.count({
where: {
inviter_id: user.id,
is_delete: 0
}
});
// 查询已发放奖励的邀请数(活跃邀请)
const activeInvites = await invite_record.count({
where: {
inviter_id: user.id,
reward_status: 1,
is_delete: 0
}
});
// 查询总奖励天数
const rewardRecords = await invite_record.findAll({
where: {
inviter_id: user.id,
reward_status: 1,
is_delete: 0
},
attributes: ['reward_value']
});
const totalRewardDays = rewardRecords.reduce((sum, record) => {
return sum + (record.reward_value || 0);
}, 0);
return ctx.success({
totalInvites: totalInvites || 0
});
} catch (error) {
console.error('获取邀请统计失败:', error);
return ctx.fail('获取邀请统计失败: ' + error.message);
}
},
/**
* @swagger
* /api/invite/generate:
* post:
* summary: 生成邀请码
* description: 为用户生成新的邀请码
* tags: [前端-推广邀请]
* responses:
* 200:
* description: 生成成功
*/
'POST /invite/generate': async (ctx) => {
try {
// 从body中获取sn_code
const body = ctx.getBody() || {};
const { sn_code } = body;
if (!sn_code) {
return ctx.fail('请提供设备SN码');
}
const { pla_account } = await Framework.getModels();
// 根据sn_code查找用户
const user = await pla_account.findOne({
where: { sn_code }
});
if (!user) {
return ctx.fail('用户不存在');
}
// 生成新的邀请码
const invite_code = `INV${user.id}_${Date.now().toString(36).toUpperCase()}`;
const invite_link = `https://work.light120.com/invite?code=${invite_code}`;
return ctx.success({
invite_code,
invite_link
});
} catch (error) {
console.error('生成邀请码失败:', error);
return ctx.fail('生成邀请码失败: ' + error.message);
}
},
/**
* @swagger
* /api/invite/records:
* post:
* summary: 获取邀请记录列表
* description: 根据设备SN码获取邀请记录列表
* tags: [前端-推广邀请]
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - sn_code
* properties:
* sn_code:
* type: string
* description: 设备SN码
* page:
* type: integer
* description: 页码可选默认1
* pageSize:
* type: integer
* description: 每页数量可选默认20
* responses:
* 200:
* description: 获取成功
*/
'POST /invite/records': async (ctx) => {
try {
const body = ctx.getBody() || {};
const { sn_code, page = 1, pageSize = 20 } = body;
if (!sn_code) {
return ctx.fail('请提供设备SN码');
}
const { pla_account, invite_record } = await Framework.getModels();
// 根据sn_code查找用户
const user = await pla_account.findOne({
where: { sn_code }
});
if (!user) {
return ctx.fail('用户不存在');
}
// 查询邀请记录列表
const offset = (page - 1) * pageSize;
const records = await invite_record.findAll({
where: {
inviter_id: user.id,
is_delete: 0
},
order: [['register_time', 'DESC']],
limit: pageSize,
offset: offset
});
const total = await invite_record.count({
where: {
inviter_id: user.id,
is_delete: 0
}
});
// 格式化记录数据
const formattedRecords = records.map(record => {
const recordData = record.toJSON();
return {
id: recordData.id,
invitee_phone: recordData.invitee_phone,
invite_code: recordData.invite_code,
register_time: recordData.register_time,
reward_status: recordData.reward_status,
reward_value: recordData.reward_value,
reward_type: recordData.reward_type
};
});
return ctx.success({
list: formattedRecords,
total: total,
page: page,
pageSize: pageSize
});
} catch (error) {
console.error('获取邀请记录列表失败:', error);
return ctx.fail('获取邀请记录列表失败: ' + error.message);
}
}
};