project-detail.component.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. import { Router, ActivatedRoute, RouterModule } from '@angular/router';
  4. import { IonicModule } from '@ionic/angular';
  5. import { WxworkSDK, WxworkCorp, WxworkAuth } from 'fmode-ng/core';
  6. import { FmodeParse, FmodeObject } from 'fmode-ng/parse';
  7. import { ProfileService } from '../../../../app/services/profile.service';
  8. import { ProjectBottomCardComponent } from '../../components/project-bottom-card/project-bottom-card.component';
  9. import { ProjectFilesModalComponent } from '../../components/project-files-modal/project-files-modal.component';
  10. import { ProjectMembersModalComponent } from '../../components/project-members-modal/project-members-modal.component';
  11. import { ProjectIssuesModalComponent } from '../../components/project-issues-modal/project-issues-modal.component';
  12. import { ProjectIssueService } from '../../services/project-issue.service';
  13. import { CustomerProfileComponent } from '../contact/contact.component';
  14. import { FormsModule } from '@angular/forms';
  15. import { CustomerSelectorComponent } from '../../components/contact-selector/contact-selector.component';
  16. const Parse = FmodeParse.with('nova');
  17. /**
  18. * 项目详情核心组件
  19. *
  20. * 功能:
  21. * 1. 展示四阶段导航(订单分配、确认需求、交付执行、售后归档)
  22. * 2. 根据角色控制权限
  23. * 3. 子路由切换阶段内容
  24. * 4. 支持@Input和路由参数两种数据加载方式
  25. *
  26. * 路由:/wxwork/:cid/project/:projectId
  27. */
  28. @Component({
  29. selector: 'app-project-detail',
  30. standalone: true,
  31. imports: [
  32. CommonModule,
  33. IonicModule,
  34. RouterModule,
  35. ProjectBottomCardComponent,
  36. ProjectFilesModalComponent,
  37. ProjectMembersModalComponent,
  38. ProjectIssuesModalComponent,
  39. CustomerProfileComponent,
  40. CustomerSelectorComponent
  41. ],
  42. templateUrl: './project-detail.component.html',
  43. styleUrls: ['./project-detail.component.scss']
  44. })
  45. export class ProjectDetailComponent implements OnInit {
  46. // 输入参数(支持组件复用)
  47. @Input() project: FmodeObject | null = null;
  48. @Input() groupChat: FmodeObject | null = null;
  49. @Input() currentUser: FmodeObject | null = null;
  50. // 问题统计
  51. issueCount: number = 0;
  52. // 路由参数
  53. cid: string = '';
  54. projectId: string = '';
  55. groupId: string = '';
  56. profileId: string = '';
  57. chatId: string = ''; // 从企微进入时的 chat_id
  58. // 企微SDK
  59. wxwork: WxworkSDK | null = null;
  60. wecorp: WxworkCorp | null = null;
  61. wxAuth: WxworkAuth | null = null; // WxworkAuth 实例
  62. // 加载状态
  63. loading: boolean = true;
  64. error: string | null = null;
  65. // 项目数据
  66. contact: FmodeObject | null = null;
  67. assignee: FmodeObject | null = null;
  68. // 当前阶段
  69. currentStage: string = 'order'; // order | requirements | delivery | aftercare
  70. stages = [
  71. { id: 'order', name: '订单分配', icon: 'document-text-outline', number: 1 },
  72. { id: 'requirements', name: '确认需求', icon: 'checkmark-circle-outline', number: 2 },
  73. { id: 'delivery', name: '交付执行', icon: 'rocket-outline', number: 3 },
  74. { id: 'aftercare', name: '售后归档', icon: 'archive-outline', number: 4 }
  75. ];
  76. // 权限
  77. canEdit: boolean = false;
  78. canViewCustomerPhone: boolean = false;
  79. role: string = '';
  80. // 模态框状态
  81. showFilesModal: boolean = false;
  82. showMembersModal: boolean = false;
  83. showIssuesModal: boolean = false;
  84. // 新增:客户详情侧栏面板状态
  85. showContactPanel: boolean = false;
  86. // 问卷状态
  87. surveyStatus: {
  88. filled: boolean;
  89. text: string;
  90. icon: string;
  91. surveyLog?: FmodeObject;
  92. } = {
  93. filled: false,
  94. text: '发送问卷',
  95. icon: 'document-text-outline'
  96. };
  97. constructor(
  98. private router: Router,
  99. private route: ActivatedRoute,
  100. private profileService: ProfileService,
  101. private issueService: ProjectIssueService
  102. ) {}
  103. async ngOnInit() {
  104. // 获取路由参数
  105. this.cid = this.route.snapshot.paramMap.get('cid') || '';
  106. this.projectId = this.route.snapshot.paramMap.get('projectId') || '';
  107. this.groupId = this.route.snapshot.queryParamMap.get('groupId') || '';
  108. this.profileId = this.route.snapshot.queryParamMap.get('profileId') || '';
  109. this.chatId = this.route.snapshot.queryParamMap.get('chatId') || '';
  110. // 监听路由变化
  111. this.route.firstChild?.url.subscribe((segments) => {
  112. if (segments.length > 0) {
  113. this.currentStage = segments[0].path;
  114. }
  115. });
  116. // 初始化企微授权(不阻塞页面加载)
  117. await this.initWxworkAuth();
  118. await this.loadData();
  119. }
  120. /**
  121. * 初始化企微授权(不阻塞页面)
  122. */
  123. async initWxworkAuth() {
  124. try {
  125. let cid = this.cid || localStorage.getItem("company") || "";
  126. // 如果没有cid,记录警告但不抛出错误
  127. if (!cid) {
  128. console.warn('⚠️ 未找到company ID (cid),企微功能将不可用');
  129. return;
  130. }
  131. this.wxAuth = new WxworkAuth({ cid: cid });
  132. this.wxwork = new WxworkSDK({ cid: cid, appId: 'crm' });
  133. this.wecorp = new WxworkCorp(cid);
  134. console.log('✅ 企微SDK初始化成功,cid:', cid);
  135. } catch (error) {
  136. console.error('❌ 企微SDK初始化失败:', error);
  137. // 不阻塞页面加载
  138. }
  139. }
  140. /**
  141. * 加载数据
  142. */
  143. async loadData() {
  144. try {
  145. this.loading = true;
  146. // 2. 获取当前用户(优先从全局服务获取)
  147. if (!this.currentUser?.id && this.wxAuth) {
  148. try {
  149. this.currentUser = await this.wxAuth.currentProfile();
  150. } catch (error) {
  151. console.warn('⚠️ 获取当前用户Profile失败:', error);
  152. }
  153. }
  154. // 设置权限
  155. this.role = this.currentUser?.get('roleName') || '';
  156. this.canEdit = ['客服', '组员', '组长', '管理员'].includes(this.role);
  157. this.canViewCustomerPhone = ['客服', '组长', '管理员'].includes(this.role);
  158. const companyId = this.currentUser?.get('company')?.id || localStorage?.getItem("company");
  159. // 3. 加载项目
  160. if (!this.project) {
  161. if (this.projectId) {
  162. // 通过 projectId 加载(从后台进入)
  163. const query = new Parse.Query('Project');
  164. query.include('contact', 'assignee','department','department.leader');
  165. this.project = await query.get(this.projectId);
  166. } else if (this.chatId) {
  167. // 通过 chat_id 查找项目(从企微群聊进入)
  168. if (companyId) {
  169. // 先查找 GroupChat
  170. const gcQuery = new Parse.Query('GroupChat');
  171. gcQuery.equalTo('chat_id', this.chatId);
  172. gcQuery.equalTo('company', companyId);
  173. let groupChat = await gcQuery.first();
  174. if (groupChat) {
  175. this.groupChat = groupChat;
  176. const projectPointer = groupChat.get('project');
  177. if (projectPointer) {
  178. const pQuery = new Parse.Query('Project');
  179. pQuery.include('contact', 'assignee','department','department.leader');
  180. this.project = await pQuery.get(projectPointer.id);
  181. }
  182. }
  183. if (!this.project) {
  184. throw new Error('该群聊尚未关联项目,请先在后台创建项目');
  185. }
  186. }
  187. }
  188. }
  189. if(!this.groupChat?.id){
  190. const gcQuery2 = new Parse.Query('GroupChat');
  191. gcQuery2.equalTo('project', this.projectId);
  192. gcQuery2.equalTo('company', companyId);
  193. this.groupChat = await gcQuery2.first();
  194. }
  195. this.wxwork?.syncGroupChat(this.groupChat?.toJSON())
  196. if (!this.project) {
  197. throw new Error('无法加载项目信息');
  198. }
  199. this.contact = this.project.get('contact');
  200. this.assignee = this.project.get('assignee');
  201. // 加载问卷状态
  202. await this.loadSurveyStatus();
  203. // 更新问题计数
  204. try {
  205. if (this.project?.id) {
  206. this.issueService.seed(this.project.id!);
  207. const counts = this.issueService.getCounts(this.project.id!);
  208. this.issueCount = counts.total;
  209. }
  210. } catch (e) {
  211. console.warn('统计问题数量失败:', e);
  212. }
  213. // 4. 加载群聊(如果没有传入且有groupId)
  214. if (!this.groupChat && this.groupId) {
  215. try {
  216. const gcQuery = new Parse.Query('GroupChat');
  217. this.groupChat = await gcQuery.get(this.groupId);
  218. } catch (err) {
  219. console.warn('加载群聊失败:', err);
  220. }
  221. }
  222. // 5. 根据项目当前阶段设置默认路由
  223. const projectStage = this.project.get('currentStage');
  224. const stageMap: any = {
  225. '订单分配': 'order',
  226. '确认需求': 'requirements',
  227. '方案确认': 'requirements',
  228. '方案深化': 'requirements',
  229. '交付执行': 'delivery',
  230. '建模': 'delivery',
  231. '软装': 'delivery',
  232. '渲染': 'delivery',
  233. '后期': 'delivery',
  234. '尾款结算': 'aftercare',
  235. '客户评价': 'aftercare',
  236. '投诉处理': 'aftercare'
  237. };
  238. const targetStage = stageMap[projectStage] || 'order';
  239. // 如果当前没有子路由,跳转到对应阶段
  240. if (!this.route.firstChild) {
  241. this.router.navigate([targetStage], { relativeTo: this.route, replaceUrl: true });
  242. }
  243. } catch (err: any) {
  244. console.error('加载失败:', err);
  245. this.error = err.message || '加载失败';
  246. } finally {
  247. this.loading = false;
  248. }
  249. }
  250. /**
  251. * 切换阶段
  252. */
  253. switchStage(stageId: string) {
  254. this.currentStage = stageId;
  255. this.router.navigate([stageId], { relativeTo: this.route });
  256. }
  257. /**
  258. * 获取阶段状态
  259. */
  260. getStageStatus(stageId: string): 'completed' | 'active' | 'pending' {
  261. const projectStage = this.project?.get('currentStage') || '';
  262. const stageOrder = ['订单分配', '确认需求', '建模', '软装', '渲染', '后期', '尾款结算', '客户评价'];
  263. const currentIndex = stageOrder.indexOf(projectStage);
  264. const stageIndexMap: any = {
  265. 'order': 0,
  266. 'requirements': 1,
  267. 'delivery': 3,
  268. 'aftercare': 6
  269. };
  270. const targetIndex = stageIndexMap[stageId];
  271. if (currentIndex > targetIndex) {
  272. return 'completed';
  273. } else if (this.currentStage === stageId) {
  274. return 'active';
  275. } else {
  276. return 'pending';
  277. }
  278. }
  279. /**
  280. * 返回
  281. */
  282. goBack() {
  283. let ua = navigator.userAgent.toLowerCase();
  284. let isWeixin = ua.indexOf("micromessenger") != -1;
  285. if(isWeixin){
  286. this.router.navigate(['/wxwork', this.cid, 'project-loader']);
  287. }else{
  288. history.back();
  289. }
  290. }
  291. /**
  292. * 更新项目阶段
  293. */
  294. async updateProjectStage(stage: string) {
  295. if (!this.project || !this.canEdit) return;
  296. try {
  297. this.project.set('currentStage', stage);
  298. await this.project.save();
  299. // 添加阶段历史
  300. const data = this.project.get('data') || {};
  301. const stageHistory = data.stageHistory || [];
  302. stageHistory.push({
  303. stage,
  304. startTime: new Date(),
  305. status: 'current',
  306. operator: {
  307. id: this.currentUser!.id,
  308. name: this.currentUser!.get('name'),
  309. role: this.role
  310. }
  311. });
  312. this.project.set('data', { ...data, stageHistory });
  313. await this.project.save();
  314. } catch (err) {
  315. console.error('更新阶段失败:', err);
  316. alert('更新失败');
  317. }
  318. }
  319. /**
  320. * 发送企微消息
  321. */
  322. async sendWxMessage(message: string) {
  323. if (!this.groupChat || !this.wecorp) return;
  324. try {
  325. const chatId = this.groupChat.get('chat_id');
  326. await this.wecorp.appchat.sendText(chatId, message);
  327. } catch (err) {
  328. console.error('发送消息失败:', err);
  329. }
  330. }
  331. /**
  332. * 选择客户(从群聊成员中选择外部联系人)
  333. */
  334. async selectCustomer() {
  335. console.log(this.canEdit, this.groupChat)
  336. if (!this.groupChat) return;
  337. try {
  338. const memberList = this.groupChat.get('member_list') || [];
  339. const externalMembers = memberList.filter((m: any) => m.type === 2);
  340. if (externalMembers.length === 0) {
  341. alert('当前群聊中没有外部联系人');
  342. return;
  343. }
  344. console.log(externalMembers)
  345. // 简单实现:选择第一个外部联系人
  346. // TODO: 实现选择器UI
  347. const selectedMember = externalMembers[0];
  348. await this.setCustomerFromMember(selectedMember);
  349. } catch (err) {
  350. console.error('选择客户失败:', err);
  351. alert('选择客户失败');
  352. }
  353. }
  354. /**
  355. * 从群成员设置客户
  356. */
  357. async setCustomerFromMember(member: any) {
  358. if (!this.wecorp) return;
  359. try {
  360. const companyId = this.currentUser?.get('company')?.id || localStorage.getItem("company");
  361. if (!companyId) throw new Error('无法获取企业信息');
  362. // 1. 查询是否已存在 ContactInfo
  363. const query = new Parse.Query('ContactInfo');
  364. query.equalTo('external_userid', member.userid);
  365. query.equalTo('company', companyId);
  366. let contactInfo = await query.first();
  367. // 2. 如果不存在,通过企微API获取并创建
  368. if (!contactInfo) {
  369. contactInfo = new Parse.Object("ContactInfo");
  370. }
  371. const externalContactData = await this.wecorp.externalContact.get(member.userid);
  372. console.log("externalContactData",externalContactData)
  373. const ContactInfo = Parse.Object.extend('ContactInfo');
  374. contactInfo.set('name', externalContactData.name);
  375. contactInfo.set('external_userid', member.userid);
  376. const company = new Parse.Object('Company');
  377. company.id = companyId;
  378. const companyPointer = company.toPointer();
  379. contactInfo.set('company', companyPointer);
  380. contactInfo.set('data', externalContactData);
  381. await contactInfo.save();
  382. // 3. 设置为项目客户
  383. if (this.project) {
  384. this.project.set('contact', contactInfo.toPointer());
  385. await this.project.save();
  386. this.contact = contactInfo;
  387. alert('客户设置成功');
  388. }
  389. } catch (err) {
  390. console.error('设置客户失败:', err);
  391. throw err;
  392. }
  393. }
  394. /**
  395. * 显示文件模态框
  396. */
  397. showFiles() {
  398. this.showFilesModal = true;
  399. }
  400. /**
  401. * 显示成员模态框
  402. */
  403. showMembers() {
  404. this.showMembersModal = true;
  405. }
  406. /** 显示问题模态框 */
  407. showIssues() {
  408. this.showIssuesModal = true;
  409. }
  410. /**
  411. * 关闭文件模态框
  412. */
  413. closeFilesModal() {
  414. this.showFilesModal = false;
  415. }
  416. /**
  417. * 关闭成员模态框
  418. */
  419. closeMembersModal() {
  420. this.showMembersModal = false;
  421. }
  422. /** 显示客户详情面板 */
  423. openContactPanel() {
  424. if (this.contact) {
  425. this.showContactPanel = true;
  426. }
  427. }
  428. /** 关闭客户详情面板 */
  429. closeContactPanel() {
  430. this.showContactPanel = false;
  431. }
  432. /** 关闭问题模态框 */
  433. closeIssuesModal() {
  434. this.showIssuesModal = false;
  435. if (this.project?.id) {
  436. const counts = this.issueService.getCounts(this.project.id!);
  437. this.issueCount = counts.total;
  438. }
  439. }
  440. /** 客户选择事件回调(接收子组件输出) */
  441. onContactSelected(evt: { contact: FmodeObject; isNewCustomer: boolean; action: 'selected' | 'created' | 'updated' }) {
  442. this.contact = evt.contact;
  443. // 重新加载问卷状态
  444. this.loadSurveyStatus();
  445. }
  446. /**
  447. * 加载问卷状态
  448. */
  449. async loadSurveyStatus() {
  450. if (!this.project?.id || !this.contact?.id) return;
  451. try {
  452. const query = new Parse.Query('SurveyLog');
  453. query.equalTo('project', this.project.toPointer());
  454. query.equalTo('contact', this.contact.toPointer());
  455. query.equalTo('type', 'survey-project');
  456. query.equalTo('isCompleted', true);
  457. const surveyLog = await query.first();
  458. if (surveyLog) {
  459. this.surveyStatus = {
  460. filled: true,
  461. text: '查看问卷',
  462. icon: 'checkmark-circle',
  463. surveyLog
  464. };
  465. console.log('✅ 问卷已填写');
  466. } else {
  467. this.surveyStatus = {
  468. filled: false,
  469. text: '发送问卷',
  470. icon: 'document-text-outline'
  471. };
  472. console.log('✅ 问卷未填写');
  473. }
  474. } catch (err) {
  475. console.error('❌ 查询问卷状态失败:', err);
  476. }
  477. }
  478. /**
  479. * 发送问卷
  480. */
  481. async sendSurvey() {
  482. if (!this.groupChat || !this.wxwork) {
  483. alert('无法发送问卷:未找到群聊或企微SDK未初始化');
  484. return;
  485. }
  486. try {
  487. const chatId = this.groupChat.get('chat_id');
  488. const surveyUrl = `${document.baseURI}/wxwork/${this.cid}/survey/project/${this.project?.id}`;
  489. await this.wxwork.ww.openExistedChatWithMsg({
  490. chatId: chatId,
  491. msg: {
  492. msgtype: 'link',
  493. link: {
  494. title: '《家装效果图服务需求调查表》',
  495. desc: '为让本次服务更贴合您的需求,请花3-5分钟填写简短问卷,感谢支持!',
  496. url: surveyUrl,
  497. imgUrl: `${document.baseURI}/assets/logo.jpg`
  498. }
  499. }
  500. });
  501. alert('问卷已发送到群聊!');
  502. } catch (err) {
  503. console.error('❌ 发送问卷失败:', err);
  504. alert('发送失败,请重试');
  505. }
  506. }
  507. /**
  508. * 查看问卷结果
  509. */
  510. async viewSurvey() {
  511. if (!this.surveyStatus.surveyLog) return;
  512. // 跳转到问卷页面查看结果
  513. this.router.navigate(['/wxwork', this.cid, 'survey', 'project', this.project?.id]);
  514. }
  515. /**
  516. * 处理问卷点击
  517. */
  518. async handleSurveyClick(event: Event) {
  519. event.stopPropagation();
  520. if (this.surveyStatus.filled) {
  521. // 已填写,查看结果
  522. await this.viewSurvey();
  523. } else {
  524. // 未填写,发送问卷
  525. await this.sendSurvey();
  526. }
  527. }
  528. }
  529. // duplicate inline CustomerSelectorComponent removed (we keep single declaration above)