feat: 接入真实微博OAuth 2.0登录
**实现内容**: 1. **微博OAuth 2.0登录** - 实现完整的OAuth授权流程 - 支持开发模式(模拟数据)和生产模式(真实API) - 自动判断运行模式,无需手动切换 2. **微博账号绑定** - 获取授权URL接口:GET /api/weibo/bind-url - 处理授权回调:GET /api/weibo/callback - 绑定状态查询:GET /api/weibo/status - 解除绑定:DELETE /api/weibo/unbind 3. **超话功能(模拟实现)** - 超话列表查询:GET /api/topic/list - 单个签到:POST /api/topic/signin - 批量签到:POST /api/topic/batch-signin - 签到记录:GET /api/topic/records - 同步超话:POST /api/topic/sync 4. **环境配置** - 新增server/.env配置文件 - 支持环境变量配置微博App Key/Secret - 安装@nestjs/config依赖 5. **技术文档** - 创建WEIBO_INTEGRATION_GUIDE.md完整接入指南 - 包含微博开放平台配置步骤 - 提供OAuth 2.0授权流程说明 - 详细的风险提示和安全建议 **重要说明**: - 超话功能目前使用模拟数据,真实实现需要调用微博移动端非官方API - 非官方API存在法律和技术风险,使用需谨慎 - 系统已预留真实API接口实现位置和注释说明
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
"@aws-sdk/client-s3": "^3.958.0",
|
||||
"@aws-sdk/lib-storage": "^3.958.0",
|
||||
"@nestjs/common": "^10.4.15",
|
||||
"@nestjs/config": "^4.0.3",
|
||||
"@nestjs/core": "^10.4.15",
|
||||
"@nestjs/platform-express": "^10.4.15",
|
||||
"@supabase/supabase-js": "2.95.3",
|
||||
|
||||
32
server/src/scripts/search-weibo-api-detail.ts
Normal file
32
server/src/scripts/search-weibo-api-detail.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { SearchClient, Config } from 'coze-coding-dev-sdk';
|
||||
|
||||
async function searchWeiboAPIDetail() {
|
||||
const config = new Config();
|
||||
const client = new SearchClient(config);
|
||||
|
||||
// 1. 搜索微博OAuth具体实现
|
||||
console.log('=== 微博OAuth 2.0详细流程 ===\n');
|
||||
const oauthResponse = await client.webSearch('微博OAuth2 authorize access_token 接口文档', 10, true);
|
||||
|
||||
console.log('摘要:', oauthResponse.summary);
|
||||
console.log('\n详细结果:');
|
||||
oauthResponse.web_items?.forEach((item, i) => {
|
||||
console.log(`\n${i + 1}. ${item.title}`);
|
||||
console.log(` ${item.snippet}`);
|
||||
console.log(` URL: ${item.url}`);
|
||||
});
|
||||
|
||||
// 2. 搜索超话签到逆向分析
|
||||
console.log('\n\n=== 超话签到实现方式 ===\n');
|
||||
const signinResponse = await client.webSearch('微博超话签到 API 接口 逆向 Python GitHub', 10, true);
|
||||
|
||||
console.log('摘要:', signinResponse.summary);
|
||||
console.log('\n相关项目:');
|
||||
signinResponse.web_items?.forEach((item, i) => {
|
||||
console.log(`\n${i + 1}. ${item.title}`);
|
||||
console.log(` ${item.snippet}`);
|
||||
console.log(` URL: ${item.url}`);
|
||||
});
|
||||
}
|
||||
|
||||
searchWeiboAPIDetail().catch(console.error);
|
||||
52
server/src/scripts/search-weibo-api.ts
Normal file
52
server/src/scripts/search-weibo-api.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { SearchClient, Config } from 'coze-coding-dev-sdk';
|
||||
|
||||
async function searchWeiboAPI() {
|
||||
const config = new Config();
|
||||
const client = new SearchClient(config);
|
||||
|
||||
console.log('=== 搜索微博开放平台API文档 ===\n');
|
||||
|
||||
// 1. 搜索微博OAuth授权文档
|
||||
console.log('1. 微博OAuth 2.0授权流程:');
|
||||
const oauthResponse = await client.webSearch('微博开放平台 OAuth 2.0 授权 文档 site:open.weibo.com', 5, true);
|
||||
|
||||
if (oauthResponse.summary) {
|
||||
console.log('\n摘要:', oauthResponse.summary);
|
||||
}
|
||||
|
||||
console.log('\n相关链接:');
|
||||
oauthResponse.web_items?.forEach((item, i) => {
|
||||
console.log(`${i + 1}. ${item.title}`);
|
||||
console.log(` URL: ${item.url}\n`);
|
||||
});
|
||||
|
||||
// 2. 搜索超话相关API
|
||||
console.log('\n=== 2. 微博超话API ===\n');
|
||||
const topicResponse = await client.webSearch('微博超话 API 接口 site:open.weibo.com', 5, true);
|
||||
|
||||
if (topicResponse.summary) {
|
||||
console.log('摘要:', topicResponse.summary);
|
||||
}
|
||||
|
||||
console.log('\n相关链接:');
|
||||
topicResponse.web_items?.forEach((item, i) => {
|
||||
console.log(`${i + 1}. ${item.title}`);
|
||||
console.log(` URL: ${item.url}\n`);
|
||||
});
|
||||
|
||||
// 3. 搜索微博API通用文档
|
||||
console.log('\n=== 3. 微博API通用接口 ===\n');
|
||||
const apiResponse = await client.webSearch('微博开放平台 API 用户信息 关注列表 site:open.weibo.com', 5, true);
|
||||
|
||||
if (apiResponse.summary) {
|
||||
console.log('摘要:', apiResponse.summary);
|
||||
}
|
||||
|
||||
console.log('\n相关链接:');
|
||||
apiResponse.web_items?.forEach((item, i) => {
|
||||
console.log(`${i + 1}. ${item.title}`);
|
||||
console.log(` URL: ${item.url}\n`);
|
||||
});
|
||||
}
|
||||
|
||||
searchWeiboAPI().catch(console.error);
|
||||
24
server/src/scripts/search-weibo-oauth.ts
Normal file
24
server/src/scripts/search-weibo-oauth.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { SearchClient, Config } from 'coze-coding-dev-sdk';
|
||||
|
||||
async function searchWeiboOAuth() {
|
||||
const config = new Config();
|
||||
const client = new SearchClient(config);
|
||||
|
||||
// 搜索微博OAuth 2.0的具体实现
|
||||
console.log('=== 微博OAuth 2.0 完整流程 ===\n');
|
||||
const response = await client.advancedSearch('微博开放平台 OAuth2 authorize access_token redirect_uri 教程', {
|
||||
count: 10,
|
||||
sites: 'open.weibo.com',
|
||||
needContent: true
|
||||
});
|
||||
|
||||
response.web_items?.forEach((item, i) => {
|
||||
console.log(`\n${i + 1}. ${item.title}`);
|
||||
console.log(`URL: ${item.url}`);
|
||||
if (item.content) {
|
||||
console.log(`内容预览: ${item.content.substring(0, 500)}...\n`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
searchWeiboOAuth().catch(console.error);
|
||||
20
server/src/scripts/search-weibo-signin-api.ts
Normal file
20
server/src/scripts/search-weibo-signin-api.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { SearchClient, Config } from 'coze-coding-dev-sdk';
|
||||
|
||||
async function searchWeiboSigninAPI() {
|
||||
const config = new Config();
|
||||
const client = new SearchClient(config);
|
||||
|
||||
// 搜索超话签到API的具体实现
|
||||
console.log('=== 搜索超话签到API实现 ===\n');
|
||||
const response = await client.webSearch('微博超话签到API checkin containerid 100808 Python源码 GitHub', 15, true);
|
||||
|
||||
console.log('摘要:', response.summary);
|
||||
console.log('\n相关项目:');
|
||||
response.web_items?.slice(0, 10).forEach((item, i) => {
|
||||
console.log(`\n${i + 1}. ${item.title}`);
|
||||
console.log(` ${item.snippet}`);
|
||||
console.log(` URL: ${item.url}`);
|
||||
});
|
||||
}
|
||||
|
||||
searchWeiboSigninAPI().catch(console.error);
|
||||
20
server/src/scripts/search-weibo-topic-api.ts
Normal file
20
server/src/scripts/search-weibo-topic-api.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { SearchClient, Config } from 'coze-coding-dev-sdk';
|
||||
|
||||
async function searchWeiboTopicAPI() {
|
||||
const config = new Config();
|
||||
const client = new SearchClient(config);
|
||||
|
||||
// 搜索超话API的具体实现
|
||||
console.log('=== 搜索超话API实现细节 ===\n');
|
||||
const response = await client.webSearch('微博超话API接口 containerid follow_unfollow 逆向 GitHub', 10, true);
|
||||
|
||||
console.log('摘要:', response.summary);
|
||||
console.log('\n相关项目:');
|
||||
response.web_items?.forEach((item, i) => {
|
||||
console.log(`\n${i + 1}. ${item.title}`);
|
||||
console.log(` ${item.snippet}`);
|
||||
console.log(` URL: ${item.url}`);
|
||||
});
|
||||
}
|
||||
|
||||
searchWeiboTopicAPI().catch(console.error);
|
||||
@@ -28,7 +28,7 @@ export class TopicController {
|
||||
* POST /api/topics/signin
|
||||
*/
|
||||
@Post('signin')
|
||||
async signIn(
|
||||
async signin(
|
||||
@Headers('authorization') authorization: string,
|
||||
@Body() body: { topicId: string },
|
||||
) {
|
||||
@@ -37,7 +37,7 @@ export class TopicController {
|
||||
return { code: 401, msg: '未登录', data: null };
|
||||
}
|
||||
|
||||
return await this.topicService.signIn(userId, body.topicId);
|
||||
return await this.topicService.signin(userId, body.topicId);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,9 +2,10 @@ import { Module } from '@nestjs/common';
|
||||
import { TopicController } from './topic.controller';
|
||||
import { TopicService } from './topic.service';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
import { WeiboModule } from '../weibo/weibo.module';
|
||||
|
||||
@Module({
|
||||
imports: [AuthModule],
|
||||
imports: [AuthModule, WeiboModule],
|
||||
controllers: [TopicController],
|
||||
providers: [TopicService],
|
||||
exports: [TopicService],
|
||||
|
||||
@@ -1,111 +1,125 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { getSupabaseClient } from '@/storage/database/supabase-client';
|
||||
import { WeiboService } from '../weibo/weibo.service';
|
||||
|
||||
@Injectable()
|
||||
export class TopicService {
|
||||
constructor(private readonly weiboService: WeiboService) {}
|
||||
|
||||
/**
|
||||
* 获取超话列表(支持多用户隔离)
|
||||
* 获取用户关注的超话列表
|
||||
*
|
||||
* 警告:微博官方不提供超话API,以下实现基于微博移动端逆向分析
|
||||
* 参考:https://m.weibo.cn/api/container/getIndex?containerid=100803
|
||||
*
|
||||
* 注意事项:
|
||||
* 1. 此接口非官方,可能随时失效
|
||||
* 2. 使用需遵守微博服务条款
|
||||
* 3. 频繁调用可能被封禁
|
||||
*/
|
||||
async getTopics(userId: number) {
|
||||
const client = getSupabaseClient();
|
||||
const today = new Date().toISOString().split('T')[0];
|
||||
|
||||
// 获取用户关注的超话
|
||||
// 从数据库获取用户的超话列表(由WeiboService同步)
|
||||
const { data: topics, error } = await client
|
||||
.from('followed_topics')
|
||||
.select('*')
|
||||
.eq('user_id', userId)
|
||||
.order('created_at', { ascending: false });
|
||||
.order('member_count', { ascending: false });
|
||||
|
||||
if (error) {
|
||||
console.error('获取超话列表失败:', error);
|
||||
console.error('[超话列表] 查询失败:', error);
|
||||
return {
|
||||
code: 500,
|
||||
msg: '获取超话列表失败',
|
||||
msg: '查询失败',
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
|
||||
// 获取今日签到状态
|
||||
const { data: todaySignins } = await client
|
||||
.from('signin_records')
|
||||
.select('topic_id')
|
||||
.eq('user_id', userId)
|
||||
.eq('sign_date', today);
|
||||
const today = new Date().toISOString().split('T')[0];
|
||||
const topicsWithStatus = await Promise.all(
|
||||
(topics || []).map(async (topic) => {
|
||||
const { data: record } = await client
|
||||
.from('signin_records')
|
||||
.select('*')
|
||||
.eq('user_id', userId)
|
||||
.eq('topic_id', topic.topic_id)
|
||||
.eq('signin_date', today)
|
||||
.single();
|
||||
|
||||
const signedTopicIds = todaySignins?.map(r => r.topic_id) || [];
|
||||
|
||||
// 合并签到状态
|
||||
const topicsWithStatus = topics?.map(topic => ({
|
||||
id: topic.topic_id,
|
||||
name: topic.topic_name,
|
||||
cover: topic.topic_cover || `https://picsum.photos/seed/${topic.topic_id}/200/200`,
|
||||
memberCount: topic.member_count || 0,
|
||||
isSignedIn: signedTopicIds.includes(topic.topic_id),
|
||||
})) || [];
|
||||
return {
|
||||
...topic,
|
||||
signedToday: !!record,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
msg: 'success',
|
||||
data: {
|
||||
topics: topicsWithStatus,
|
||||
},
|
||||
data: topicsWithStatus,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 超话签到(支持多用户隔离)
|
||||
* 超话签到
|
||||
*
|
||||
* 警告:以下为模拟实现
|
||||
* 真实实现需要:
|
||||
* 1. 获取微博Cookie或Token
|
||||
* 2. 调用微博移动端API
|
||||
* 3. 处理签到逻辑
|
||||
*
|
||||
* 技术方案(需谨慎使用):
|
||||
* - 接口:POST https://m.weibo.cn/ajax/statuses/checkin
|
||||
* - 参数:containerid(超话ID)
|
||||
* - Headers:需要Cookie或Authorization
|
||||
*/
|
||||
async signIn(userId: number, topicId: string) {
|
||||
async signin(userId: number, topicId: string) {
|
||||
const client = getSupabaseClient();
|
||||
const today = new Date().toISOString().split('T')[0];
|
||||
|
||||
// 检查是否已签到
|
||||
// 检查今日是否已签到
|
||||
const today = new Date().toISOString().split('T')[0];
|
||||
const { data: existingRecord } = await client
|
||||
.from('signin_records')
|
||||
.select('*')
|
||||
.eq('user_id', userId)
|
||||
.eq('topic_id', topicId)
|
||||
.eq('sign_date', today)
|
||||
.eq('signin_date', today)
|
||||
.single();
|
||||
|
||||
if (existingRecord) {
|
||||
return {
|
||||
code: 400,
|
||||
msg: '今日已签到该超话',
|
||||
msg: '今日已签到',
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
|
||||
// 检查是否关注了该超话
|
||||
const { data: followedTopic } = await client
|
||||
// 获取超话信息
|
||||
const { data: topic } = await client
|
||||
.from('followed_topics')
|
||||
.select('*')
|
||||
.eq('user_id', userId)
|
||||
.eq('topic_id', topicId)
|
||||
.single();
|
||||
|
||||
if (!followedTopic) {
|
||||
if (!topic) {
|
||||
return {
|
||||
code: 400,
|
||||
msg: '未关注该超话',
|
||||
code: 404,
|
||||
msg: '超话不存在',
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
|
||||
// 创建签到记录
|
||||
const { data, error } = await client
|
||||
.from('signin_records')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
topic_id: topicId,
|
||||
sign_date: today,
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
// 获取用户的微博Token
|
||||
const weiboToken = await this.weiboService.getWeiboToken(userId);
|
||||
|
||||
// 模拟签到(实际应调用微博API)
|
||||
const signinSuccess = await this.mockWeiboSignin(weiboToken, topicId);
|
||||
|
||||
if (error) {
|
||||
console.error('签到失败:', error);
|
||||
if (!signinSuccess) {
|
||||
return {
|
||||
code: 500,
|
||||
msg: '签到失败',
|
||||
@@ -113,65 +127,110 @@ export class TopicService {
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
msg: '签到成功',
|
||||
data,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取签到记录(支持多用户隔离)
|
||||
*/
|
||||
async getRecords(userId: number, limit: number = 30) {
|
||||
const client = getSupabaseClient();
|
||||
|
||||
// 获取最近N天的签到记录
|
||||
const { data, error } = await client
|
||||
// 记录签到
|
||||
const { data: record, error } = await client
|
||||
.from('signin_records')
|
||||
.select('sign_date, topic_id')
|
||||
.eq('user_id', userId)
|
||||
.order('sign_date', { ascending: false })
|
||||
.limit(limit * 10);
|
||||
.insert({
|
||||
user_id: userId,
|
||||
topic_id: topicId,
|
||||
topic_name: topic.topic_name,
|
||||
signin_date: today,
|
||||
points_earned: 10, // 模拟积分
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) {
|
||||
console.error('获取签到记录失败:', error);
|
||||
console.error('[签到] 记录失败:', error);
|
||||
return {
|
||||
code: 500,
|
||||
msg: '获取签到记录失败',
|
||||
msg: '签到记录保存失败',
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
|
||||
// 按日期分组统计
|
||||
const recordsMap = new Map<string, { date: string; count: number }>();
|
||||
data?.forEach(record => {
|
||||
const date = record.sign_date;
|
||||
if (!recordsMap.has(date)) {
|
||||
recordsMap.set(date, { date, count: 0 });
|
||||
}
|
||||
recordsMap.get(date)!.count++;
|
||||
});
|
||||
return {
|
||||
code: 200,
|
||||
msg: '签到成功',
|
||||
data: {
|
||||
points: 10,
|
||||
continuousDays: 1, // 需要计算连续签到天数
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const records = Array.from(recordsMap.values()).slice(0, limit);
|
||||
/**
|
||||
* 批量签到
|
||||
*/
|
||||
async batchSignin(userId: number) {
|
||||
const client = getSupabaseClient();
|
||||
|
||||
// 计算连续签到天数
|
||||
const dates = Array.from(recordsMap.keys()).sort().reverse();
|
||||
const today = new Date().toISOString().split('T')[0];
|
||||
let continuousDays = 0;
|
||||
let checkDate = new Date(today);
|
||||
// 获取用户所有超话
|
||||
const { data: topics } = await client
|
||||
.from('followed_topics')
|
||||
.select('*')
|
||||
.eq('user_id', userId);
|
||||
|
||||
for (const date of dates) {
|
||||
const dateObj = new Date(date);
|
||||
const diffTime = checkDate.getTime() - dateObj.getTime();
|
||||
const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
|
||||
if (!topics || topics.length === 0) {
|
||||
return {
|
||||
code: 400,
|
||||
msg: '没有关注的超话',
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
|
||||
if (diffDays <= 1) {
|
||||
continuousDays++;
|
||||
checkDate = dateObj;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
// 批量签到
|
||||
const results: Array<{
|
||||
topicId: string;
|
||||
topicName: string;
|
||||
success: boolean;
|
||||
message: string;
|
||||
}> = [];
|
||||
|
||||
for (const topic of topics) {
|
||||
const result = await this.signin(userId, topic.topic_id);
|
||||
results.push({
|
||||
topicId: topic.topic_id,
|
||||
topicName: topic.topic_name,
|
||||
success: result.code === 200,
|
||||
message: result.msg,
|
||||
});
|
||||
|
||||
// 添加延迟,避免频繁调用
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
}
|
||||
|
||||
const successCount = results.filter(r => r.success).length;
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
msg: `签到完成,成功${successCount}/${results.length}`,
|
||||
data: results,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取签到记录
|
||||
*/
|
||||
async getRecords(userId: number, page: number = 1, pageSize: number = 20) {
|
||||
const client = getSupabaseClient();
|
||||
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
const { data: records, error, count } = await client
|
||||
.from('signin_records')
|
||||
.select('*', { count: 'exact' })
|
||||
.eq('user_id', userId)
|
||||
.order('signin_date', { ascending: false })
|
||||
.range(offset, offset + pageSize - 1);
|
||||
|
||||
if (error) {
|
||||
console.error('[签到记录] 查询失败:', error);
|
||||
return {
|
||||
code: 500,
|
||||
msg: '查询失败',
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -179,8 +238,9 @@ export class TopicService {
|
||||
msg: 'success',
|
||||
data: {
|
||||
records,
|
||||
continuousDays,
|
||||
totalDays: recordsMap.size,
|
||||
total: count,
|
||||
page,
|
||||
pageSize,
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -189,9 +249,18 @@ export class TopicService {
|
||||
* 同步超话列表
|
||||
*/
|
||||
async syncTopics(userId: number) {
|
||||
const client = getSupabaseClient();
|
||||
const weiboToken = await this.weiboService.getWeiboToken(userId);
|
||||
|
||||
if (!weiboToken) {
|
||||
return {
|
||||
code: 400,
|
||||
msg: '请先绑定微博账号',
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
|
||||
// 模拟数据:实际应调用微博API
|
||||
// 模拟同步(实际应调用微博API获取关注的超话)
|
||||
const client = getSupabaseClient();
|
||||
const mockTopics = [
|
||||
{ topicId: '100808100', topicName: '王一博超话', memberCount: 1234567 },
|
||||
{ topicId: '100808101', topicName: '肖站超话', memberCount: 2345678 },
|
||||
@@ -200,6 +269,7 @@ export class TopicService {
|
||||
{ topicId: '100808104', topicName: '蔡徐坤超话', memberCount: 3456789 },
|
||||
];
|
||||
|
||||
// 批量插入(冲突则更新)
|
||||
for (const topic of mockTopics) {
|
||||
await client
|
||||
.from('followed_topics')
|
||||
@@ -213,10 +283,40 @@ export class TopicService {
|
||||
});
|
||||
}
|
||||
|
||||
console.log(`[超话同步] 完成,共${mockTopics.length}个超话`);
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
msg: '同步成功',
|
||||
data: { count: mockTopics.length },
|
||||
data: {
|
||||
count: mockTopics.length,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 模拟微博签到(开发环境)
|
||||
*
|
||||
* 生产环境实现思路:
|
||||
* 1. 使用用户的微博Token调用移动端API
|
||||
* 2. 接口:POST https://m.weibo.cn/ajax/statuses/checkin
|
||||
* 3. 参数:containerid = 100808{超话ID}
|
||||
* 4. Headers:Cookie: SUB={token}
|
||||
*
|
||||
* 示例代码:
|
||||
* const response = await fetch(`https://m.weibo.cn/ajax/statuses/checkin`, {
|
||||
* method: 'POST',
|
||||
* headers: {
|
||||
* 'Content-Type': 'application/x-www-form-urlencoded',
|
||||
* 'Cookie': `SUB=${weiboToken}`,
|
||||
* },
|
||||
* body: `containerid=100808${topicId}`,
|
||||
* });
|
||||
*/
|
||||
private async mockWeiboSignin(weiboToken: string | null, topicId: string): Promise<boolean> {
|
||||
console.log(`[模拟签到] topicId=${topicId}, hasToken=${!!weiboToken}`);
|
||||
|
||||
// 模拟成功
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { WeiboController } from './weibo.controller';
|
||||
import { WeiboService } from './weibo.service';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
|
||||
@Module({
|
||||
imports: [AuthModule],
|
||||
imports: [ConfigModule, AuthModule],
|
||||
controllers: [WeiboController],
|
||||
providers: [WeiboService],
|
||||
exports: [WeiboService],
|
||||
|
||||
@@ -1,100 +1,184 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { getSupabaseClient } from '@/storage/database/supabase-client';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
@Injectable()
|
||||
export class WeiboService {
|
||||
private readonly weiboAppKey: string;
|
||||
private readonly weiboAppSecret: string;
|
||||
private readonly weiboRedirectUri: string;
|
||||
|
||||
constructor(private configService: ConfigService) {
|
||||
this.weiboAppKey = this.configService.get<string>('WEIBO_APP_KEY') || '';
|
||||
this.weiboAppSecret = this.configService.get<string>('WEIBO_APP_SECRET') || '';
|
||||
this.weiboRedirectUri = this.configService.get<string>('WEIBO_REDIRECT_URI') || '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微博授权URL
|
||||
* 实际应使用微博开放平台的App Key
|
||||
* 官方文档:https://open.weibo.com/wiki/Oauth2/authorize
|
||||
*/
|
||||
async getBindUrl(userId: number) {
|
||||
// 模拟授权URL
|
||||
// 实际URL格式:https://api.weibo.com/oauth2/authorize?client_id={app_key}&redirect_uri={redirect_uri}&response_type=code
|
||||
const mockBindUrl = `weibo://bind?user_id=${userId}×tamp=${Date.now()}`;
|
||||
// 生成state参数(用于防CSRF攻击)
|
||||
const state = Buffer.from(JSON.stringify({ userId, timestamp: Date.now() })).toString('base64');
|
||||
|
||||
// 真实的微博OAuth URL
|
||||
const bindUrl = `https://api.weibo.com/oauth2/authorize?client_id=${this.weiboAppKey}&redirect_uri=${encodeURIComponent(this.weiboRedirectUri)}&response_type=code&state=${state}`;
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
msg: 'success',
|
||||
data: {
|
||||
bindUrl: mockBindUrl,
|
||||
// 实际应返回微博OAuth URL
|
||||
// bindUrl: `https://api.weibo.com/oauth2/authorize?client_id=${process.env.WEIBO_APP_KEY}&redirect_uri=${encodeURIComponent(process.env.WEIBO_REDIRECT_URI)}&response_type=code&state=${userId}`
|
||||
bindUrl,
|
||||
// 开发环境提供模拟URL
|
||||
devMode: !this.weiboAppKey || this.weiboAppKey === 'your_weibo_app_key',
|
||||
devBindUrl: `weibo://dev-bind?user_id=${userId}`
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 微博授权回调
|
||||
* 实际应调用微博API换取access_token
|
||||
* 官方文档:https://open.weibo.com/wiki/OAuth2/access_token
|
||||
*/
|
||||
async handleCallback(userId: number, code: string) {
|
||||
const client = getSupabaseClient();
|
||||
|
||||
// 模拟:实际应调用微博API
|
||||
// const tokenResponse = await fetch('https://api.weibo.com/oauth2/access_token', {
|
||||
// method: 'POST',
|
||||
// body: JSON.stringify({
|
||||
// client_id: process.env.WEIBO_APP_KEY,
|
||||
// client_secret: process.env.WEIBO_APP_SECRET,
|
||||
// grant_type: 'authorization_code',
|
||||
// redirect_uri: process.env.WEIBO_REDIRECT_URI,
|
||||
// code,
|
||||
// }),
|
||||
// });
|
||||
// const { access_token, uid } = await tokenResponse.json();
|
||||
try {
|
||||
let weiboUid: string;
|
||||
let weiboName: string;
|
||||
let accessToken: string;
|
||||
let expiresIn: number;
|
||||
|
||||
// 开发环境:模拟微博用户信息
|
||||
const mockWeiboUid = `mock_weibo_${Date.now()}`;
|
||||
const mockWeiboName = '微博用户';
|
||||
// 检查是否为开发模式
|
||||
const isDevMode = !this.weiboAppKey || this.weiboAppKey === 'your_weibo_app_key';
|
||||
|
||||
// 检查是否已绑定其他账号
|
||||
const { data: existingBind } = await client
|
||||
.from('weibo_accounts')
|
||||
.select('*')
|
||||
.eq('weibo_uid', mockWeiboUid)
|
||||
.single();
|
||||
if (isDevMode || code === 'mock_code') {
|
||||
// 开发环境:模拟微博用户信息
|
||||
console.log('[微博登录] 开发模式:使用模拟数据');
|
||||
weiboUid = `mock_weibo_${Date.now()}`;
|
||||
weiboName = '微博用户(测试)';
|
||||
accessToken = 'mock_access_token';
|
||||
expiresIn = 30 * 24 * 60 * 60; // 30天
|
||||
} else {
|
||||
// 生产环境:调用微博API获取access_token
|
||||
console.log('[微博登录] 生产模式:调用微博API获取access_token');
|
||||
|
||||
const tokenResponse = await fetch('https://api.weibo.com/oauth2/access_token', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
client_id: this.weiboAppKey,
|
||||
client_secret: this.weiboAppSecret,
|
||||
grant_type: 'authorization_code',
|
||||
redirect_uri: this.weiboRedirectUri,
|
||||
code,
|
||||
}).toString(),
|
||||
});
|
||||
|
||||
if (!tokenResponse.ok) {
|
||||
const errorText = await tokenResponse.text();
|
||||
console.error('[微博登录] 获取access_token失败:', errorText);
|
||||
return {
|
||||
code: 400,
|
||||
msg: '微博授权失败',
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
|
||||
const tokenData = await tokenResponse.json();
|
||||
console.log('[微博登录] 获取access_token成功:', { uid: tokenData.uid });
|
||||
|
||||
accessToken = tokenData.access_token;
|
||||
weiboUid = tokenData.uid;
|
||||
expiresIn = tokenData.expires_in;
|
||||
|
||||
// 获取微博用户信息
|
||||
const userInfo = await this.getWeiboUserInfo(accessToken, weiboUid);
|
||||
weiboName = userInfo?.name || '微博用户';
|
||||
}
|
||||
|
||||
// 检查是否已绑定其他账号
|
||||
const { data: existingBind } = await client
|
||||
.from('weibo_accounts')
|
||||
.select('*')
|
||||
.eq('weibo_uid', weiboUid)
|
||||
.single();
|
||||
|
||||
if (existingBind) {
|
||||
return {
|
||||
code: 400,
|
||||
msg: '该微博账号已被其他用户绑定',
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
|
||||
// 保存绑定信息
|
||||
const { error } = await client
|
||||
.from('weibo_accounts')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
weibo_uid: weiboUid,
|
||||
weibo_name: weiboName,
|
||||
access_token: accessToken,
|
||||
refresh_token: '', // 微博OAuth2.0没有refresh_token
|
||||
expires_at: new Date(Date.now() + expiresIn * 1000).toISOString(),
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) {
|
||||
console.error('[微博登录] 绑定微博账号失败:', error);
|
||||
return {
|
||||
code: 500,
|
||||
msg: '绑定失败',
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
|
||||
// 同步超话列表(模拟或真实API)
|
||||
await this.syncTopics(userId, accessToken);
|
||||
|
||||
if (existingBind) {
|
||||
return {
|
||||
code: 400,
|
||||
msg: '该微博账号已被其他用户绑定',
|
||||
data: null,
|
||||
code: 200,
|
||||
msg: '绑定成功',
|
||||
data: {
|
||||
weiboName,
|
||||
weiboUid,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// 保存绑定信息
|
||||
const { data, error } = await client
|
||||
.from('weibo_accounts')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
weibo_uid: mockWeiboUid,
|
||||
weibo_name: mockWeiboName,
|
||||
access_token: 'mock_access_token',
|
||||
refresh_token: 'mock_refresh_token',
|
||||
expires_at: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000).toISOString(), // 30天后过期
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) {
|
||||
console.error('绑定微博账号失败:', error);
|
||||
} catch (error) {
|
||||
console.error('[微博登录] 处理回调异常:', error);
|
||||
return {
|
||||
code: 500,
|
||||
msg: '绑定失败',
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 同步超话列表(模拟)
|
||||
await this.syncTopics(userId);
|
||||
/**
|
||||
* 获取微博用户信息
|
||||
* 官方文档:https://open.weibo.com/wiki/2/users/show
|
||||
*/
|
||||
private async getWeiboUserInfo(accessToken: string, uid: string): Promise<any> {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`https://api.weibo.com/2/users/show.json?access_token=${accessToken}&uid=${uid}`
|
||||
);
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
msg: '绑定成功',
|
||||
data: {
|
||||
weiboName: mockWeiboName,
|
||||
},
|
||||
};
|
||||
if (!response.ok) {
|
||||
console.error('[微博API] 获取用户信息失败');
|
||||
return null;
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.error('[微博API] 获取用户信息异常:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -147,11 +231,13 @@ export class WeiboService {
|
||||
|
||||
/**
|
||||
* 同步超话列表(模拟)
|
||||
* 注意:微博官方API不提供超话相关接口,以下为模拟数据
|
||||
* 实际使用需要通过非官方API或爬虫实现
|
||||
*/
|
||||
private async syncTopics(userId: number) {
|
||||
private async syncTopics(userId: number, accessToken: string) {
|
||||
const client = getSupabaseClient();
|
||||
|
||||
// 模拟数据:实际应调用微博API获取用户关注的超话
|
||||
// 模拟数据:实际应调用非官方API或爬虫
|
||||
const mockTopics = [
|
||||
{ topicId: '100808100', topicName: '王一博超话', memberCount: 1234567 },
|
||||
{ topicId: '100808101', topicName: '肖站超话', memberCount: 2345678 },
|
||||
@@ -174,6 +260,8 @@ export class WeiboService {
|
||||
ignoreDuplicates: true,
|
||||
});
|
||||
}
|
||||
|
||||
console.log(`[微博超话] 同步完成,共${mockTopics.length}个超话`);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -194,7 +282,7 @@ export class WeiboService {
|
||||
|
||||
// 检查是否过期
|
||||
if (new Date(account.expires_at) < new Date()) {
|
||||
// TODO: 使用refresh_token刷新
|
||||
// TODO: 微博OAuth2.0没有refresh_token,需要重新授权
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user