2026-07-25 01:24:08 +08:00

33 lines
1012 B
JavaScript
Raw Permalink 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 BaseService = require('./base');
const crypto = require('crypto');
class BindService extends BaseService {
async generateCode() {
const { app, ctx } = this;
const uniqueId = ctx.uniqueId;
try {
// 生成随机码
const randomCode = crypto.randomBytes(3).toString('hex'); // 6位随机码
const key = `bind:${randomCode}`;
const expirationTime = 300; // 有效期 300 秒
// 存储对象到 Redis
const value = {
uniqueId,
randomCode,
createdAt: Date.now(),
expiresIn: expirationTime, // 失效时间
};
// 写入 Redis,设置有效期
await app.redis.set(key, JSON.stringify(value), 'EX', expirationTime);
return this.successResponse('bind.generateCode', value, '随机码生成成功');
} catch (error) {
console.error('生成随机码时发生错误:', error);
return this.errorResponse('bind.generateCode', '随机码生成失败');
}
}
}
module.exports = BindService;