index.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { defineStore } from 'pinia';
  2. import { safeJsonParse } from '../utils/common';
  3. import {
  4. analysisKeyword,
  5. analysisSuggestions,
  6. analysisRival,
  7. analysisQualitative
  8. } from '../utils/api';
  9. import { incrementCount } from '@/utils/limit';
  10. import type { FormDataInfo, KeywordInfo, RelatedInfoBOItem, CompetitorBOSItem } from '@/types';
  11. export const useMainStore = defineStore('main', {
  12. state: () => ({
  13. currentStep: 1,
  14. clearCache: false,
  15. formData: {} as FormDataInfo,
  16. keywordInfo: {
  17. loading: true,
  18. data: {} as KeywordInfo
  19. },
  20. suggestionsInfo: {
  21. loading: true,
  22. autoFinish: false,
  23. data: [] as RelatedInfoBOItem[],
  24. fail: false
  25. },
  26. expanded: false,
  27. isLoadOver: false
  28. }),
  29. actions: {
  30. setClearCache(val: boolean) {
  31. this.clearCache = val;
  32. },
  33. setIsLoadOver(val: boolean) {
  34. this.isLoadOver = val;
  35. },
  36. setExpanded(val: boolean) {
  37. this.expanded = val;
  38. },
  39. setCurrentStep(val: number) {
  40. this.currentStep = val;
  41. },
  42. setFormData(data: FormDataInfo) {
  43. sessionStorage.setItem('formData', JSON.stringify(data));
  44. this.formData = data;
  45. },
  46. // 获取定性分析
  47. getQualitative(): Promise<any> {
  48. const { productName, description = '' } = this.getFormData;
  49. return analysisQualitative(encodeURIComponent(productName + description));
  50. },
  51. // 获取竞品
  52. getRival(website: string): Promise<any> {
  53. const { locationName } = this.getFormData;
  54. return analysisRival({ competitorWebsite: website, locationName });
  55. },
  56. // 获取推荐
  57. async getSuggestions() {
  58. this.suggestionsInfo.loading = true;
  59. const { productName, locationName } = this.getFormData;
  60. return analysisSuggestions({ productName, locationName })
  61. .then((res) => {
  62. this.suggestionsInfo.data = res.data.relatedInfoBOList;
  63. this.suggestionsInfo.autoFinish = true;
  64. setTimeout(() => {
  65. this.suggestionsInfo.loading = false;
  66. this.suggestionsInfo.autoFinish = false;
  67. }, 300);
  68. })
  69. .catch(() => (this.suggestionsInfo.fail = true));
  70. },
  71. // 获取关键词
  72. async getKeywordData() {
  73. this.keywordInfo.loading = true;
  74. const { productName, locationName } = this.getFormData;
  75. return analysisKeyword({ productName, locationName })
  76. .then((res) => {
  77. this.keywordInfo.data = res.data;
  78. })
  79. .finally(async () => {
  80. this.keywordInfo.loading = false;
  81. await incrementCount();
  82. });
  83. },
  84. initData() {
  85. this.getKeywordData();
  86. this.getSuggestions();
  87. // this.getQualitative();
  88. }
  89. },
  90. getters: {
  91. getClearCache(): boolean {
  92. return this.clearCache;
  93. },
  94. getExpanded(): boolean {
  95. return this.expanded;
  96. },
  97. getCurrentStep(): number {
  98. return this.currentStep;
  99. },
  100. getFormData(): FormDataInfo {
  101. if (Object.keys(this.formData).length === 0) {
  102. const data = sessionStorage.getItem('formData') ?? '';
  103. return safeJsonParse(data);
  104. }
  105. return this.formData;
  106. },
  107. getKeywordInfo(): { data: KeywordInfo } & { loading: boolean } {
  108. return this.keywordInfo;
  109. },
  110. getSuggestionsInfo(): { data: RelatedInfoBOItem[] } & {
  111. autoFinish: boolean;
  112. loading: boolean;
  113. } {
  114. return this.suggestionsInfo;
  115. },
  116. getIsLoadOver(): boolean {
  117. return this.isLoadOver;
  118. }
  119. }
  120. });