| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- import { Component, OnInit } from '@angular/core';
- import { CommonModule } from '@angular/common';
- import { Router, ActivatedRoute } from '@angular/router';
- import { FormsModule } from '@angular/forms';
- import { IonicModule } from '@ionic/angular';
- import { WxworkSDK, WxworkCorp, WxworkCurrentChat } from 'fmode-ng/core';
- import { FmodeParse, FmodeObject } from 'fmode-ng/parse';
- const Parse = FmodeParse.with('nova');
- /**
- * 项目预加载页面
- *
- * 功能:
- * 1. 从企微会话获取上下文(群聊或联系人)
- * 2. 获取当前登录用户(Profile)
- * 3. 根据场景跳转到对应页面
- * - 群聊 → 项目详情 或 创建项目引导
- * - 联系人 → 客户画像
- *
- * 路由:/wxwork/:cid/project-loader
- */
- @Component({
- selector: 'app-project-loader',
- standalone: true,
- imports: [CommonModule, FormsModule, IonicModule],
- templateUrl: './project-loader.component.html',
- styleUrls: ['./project-loader.component.scss']
- })
- export class ProjectLoaderComponent implements OnInit {
- // 基础数据
- cid: string = '';
- appId: string = 'crm';
- // 加载状态
- loading: boolean = true;
- loadingMessage: string = '正在加载...';
- error: string | null = null;
- // 企微SDK
- wxwork: WxworkSDK | null = null;
- wecorp: WxworkCorp | null = null;
- // 上下文数据
- currentUser: FmodeObject | null = null; // Profile
- currentChat: WxworkCurrentChat | null = null;
- groupChat: FmodeObject | null = null; // GroupChat
- contact: FmodeObject | null = null; // ContactInfo
- project: FmodeObject | null = null; // Project
- // 创建项目引导
- showCreateGuide: boolean = false;
- defaultProjectName: string = '';
- projectName: string = '';
- creating: boolean = false;
- // 历史项目(当前群聊无项目时展示)
- historyProjects: FmodeObject[] = [];
- constructor(
- private router: Router,
- private route: ActivatedRoute
- ) {}
- async ngOnInit() {
- // 获取路由参数
- this.cid = this.route.snapshot.paramMap.get('cid') || '';
- this.appId = this.route.snapshot.queryParamMap.get('appId') || 'crm';
- if (!this.cid) {
- this.error = '缺少企业ID参数';
- this.loading = false;
- return;
- }
- await this.loadData();
- }
- /**
- * 加载数据主流程
- */
- async loadData() {
- try {
- this.loading = true;
- this.loadingMessage = '初始化企微SDK...';
- // 1. 初始化SDK
- this.wxwork = new WxworkSDK({ cid: this.cid, appId: this.appId });
- this.wecorp = new WxworkCorp(this.cid);
- // 2. 获取企微上下文(群聊或联系人)
- this.loadingMessage = '获取会话信息...';
- const chatObject = await this.wxwork.getCurrentChatObject();
- this.currentChat = chatObject.currentChat;
- this.groupChat = chatObject.GroupChat || null;
- this.contact = chatObject.Contact || null;
- // 3. 获取当前登录用户
- this.loadingMessage = '获取用户信息...';
- this.currentUser = await this.wxwork.getCurrentUser();
- console.log('当前用户:', this.currentUser?.get('name'), '角色:', this.currentUser?.get('role'));
- console.log('会话类型:', this.currentChat?.type);
- // 4. 根据场景处理
- if (this.groupChat) {
- await this.handleGroupChatScene();
- } else if (this.contact) {
- await this.handleContactScene();
- } else {
- this.error = '无法识别当前会话类型';
- }
- } catch (err: any) {
- console.error('加载失败:', err);
- this.error = err.message || '加载失败,请重试';
- } finally {
- this.loading = false;
- }
- }
- /**
- * 处理群聊场景
- */
- async handleGroupChatScene() {
- this.loadingMessage = '查询项目信息...';
- // 查询群聊关联的项目
- const projectPointer = this.groupChat!.get('project');
- if (projectPointer) {
- // 有项目,加载项目详情
- try {
- const query = new Parse.Query('Project');
- query.include('customer', 'assignee');
- this.project = await query.get(projectPointer.id);
- console.log('找到项目:', this.project.get('title'));
- // 跳转项目详情
- await this.navigateToProjectDetail();
- } catch (err) {
- console.error('加载项目失败:', err);
- this.error = '项目已删除或无权访问';
- }
- } else {
- // 无项目,查询历史项目并显示创建引导
- await this.loadHistoryProjects();
- this.showCreateProjectGuide();
- }
- }
- /**
- * 处理联系人场景
- */
- async handleContactScene() {
- console.log('联系人场景,跳转客户画像');
- // 跳转客户画像页面
- await this.router.navigate(['/wxwork', this.cid, 'customer', this.contact!.id], {
- queryParams: {
- profileId: this.currentUser!.id
- }
- });
- }
- /**
- * 加载历史项目(当前群聊相关的其他项目)
- */
- async loadHistoryProjects() {
- try {
- // 通过 ProjectGroup 查询该群聊的所有项目
- const pgQuery = new Parse.Query('ProjectGroup');
- pgQuery.equalTo('groupChat', this.groupChat!.toPointer());
- pgQuery.include('project');
- const projectGroups = await pgQuery.find();
- this.historyProjects = projectGroups
- .map(pg => pg.get('project'))
- .filter(p => p && !p.get('isDeleted'));
- console.log('找到历史项目:', this.historyProjects.length, '个');
- } catch (err) {
- console.error('加载历史项目失败:', err);
- }
- }
- /**
- * 显示创建项目引导
- */
- showCreateProjectGuide() {
- this.showCreateGuide = true;
- this.defaultProjectName = this.groupChat!.get('name') || '新项目';
- this.projectName = this.defaultProjectName;
- }
- /**
- * 创建项目
- */
- async createProject() {
- if (!this.projectName.trim()) {
- alert('请输入项目名称');
- return;
- }
- // 权限检查
- const role = this.currentUser!.get('role');
- if (!['客服', '组长', '管理员'].includes(role)) {
- alert('您没有权限创建项目');
- return;
- }
- try {
- this.creating = true;
- // 1. 创建项目
- const Project = Parse.Object.extend('Project');
- const project = new Project();
- project.set('title', this.projectName.trim());
- project.set('company', this.currentUser!.get('company'));
- project.set('status', '待分配');
- project.set('currentStage', '订单分配');
- project.set('data', {
- createdBy: this.currentUser!.id,
- createdFrom: 'wxwork_groupchat'
- });
- await project.save();
- console.log('项目创建成功:', project.id);
- // 2. 关联群聊
- this.groupChat!.set('project', project.toPointer());
- await this.groupChat!.save();
- // 3. 创建 ProjectGroup 关联(支持多项目多群)
- const ProjectGroup = Parse.Object.extend('ProjectGroup');
- const pg = new ProjectGroup();
- pg.set('project', project.toPointer());
- pg.set('groupChat', this.groupChat!.toPointer());
- pg.set('isPrimary', true);
- await pg.save();
- // 4. 跳转项目详情
- this.project = project;
- await this.navigateToProjectDetail();
- } catch (err: any) {
- console.error('创建项目失败:', err);
- alert('创建失败: ' + (err.message || '未知错误'));
- } finally {
- this.creating = false;
- }
- }
- /**
- * 选择历史项目
- */
- async selectHistoryProject(project: FmodeObject) {
- // 更新群聊的当前项目
- this.groupChat!.set('project', project.toPointer());
- await this.groupChat!.save();
- // 跳转项目详情
- this.project = project;
- await this.navigateToProjectDetail();
- }
- /**
- * 跳转项目详情
- */
- async navigateToProjectDetail() {
- await this.router.navigate(['/wxwork', this.cid, 'project', this.project!.id], {
- queryParams: {
- groupId: this.groupChat?.id,
- profileId: this.currentUser!.id
- }
- });
- }
- /**
- * 重新加载
- */
- async reload() {
- this.error = null;
- this.showCreateGuide = false;
- await this.loadData();
- }
- /**
- * 获取项目状态的显示样式类
- */
- getProjectStatusClass(status: string): string {
- const classMap: any = {
- '待分配': 'status-pending',
- '进行中': 'status-active',
- '已完成': 'status-completed',
- '已暂停': 'status-paused',
- '已取消': 'status-cancelled'
- };
- return classMap[status] || 'status-default';
- }
- /**
- * 格式化日期
- */
- formatDate(date: Date): string {
- if (!date) return '';
- const d = new Date(date);
- return `${d.getMonth() + 1}/${d.getDate()}`;
- }
- }
|