riku
13 小时以前 cf4787bc8188cd0acc8a42793730b076742f29c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { fetchLedgerType } from '../../services/enterprise/fetchLedger';
import { fetchLedgerDetail } from '../../services/enterprise/fetchLedger';
import { toLabel } from '../../common/dataSceneTypes';
 
/**
 * 台账专题管理
 */
export const useTopic = Behavior({
  data: {
    // 专题选择弹出框
    popupVisible: false,
    // 按场景类型分类的台账选择情况
    selectedTopics: {},
    // 专题对应的台账信息加载状态
    topicLoading: false,
  },
  methods: {
    /**
     * 初始化当前场景类型下的可选台账选项,
     * 初始化默认选择的专题(台账类型),
     * @param {Function} callback 可选的回调函数
     */
    initselectedTopics(callback) {
      const { selectedTopics } = this.data;
      const { sceneType } = this.data.searchOptions;
      if (selectedTopics[sceneType] == undefined || selectedTopics[sceneType].length == 0) {
        fetchLedgerType({ sceneType: parseInt(sceneType) }).then(res => {
          // 选择前三个台账为默认专题
          let count = 3;
          for (let i = 0; i < 3; i++) {
            if (i >= res.length) break;
            res[i].selected = {
              color: 'green',
              count: '✓',
              shape: 'square',
            };
          }
          selectedTopics[sceneType] = {
            sceneType,
            sceneTypeText: toLabel(sceneType),
            topicText: '请选择专题',
            allLedgerTypes: res,
            selectedLedgers: [],
            checkAll: false,
            selectedCount: 0,
          };
          this.setData({ selectedTopics });
          this.refreshSelectedLedger();
          if (typeof callback === 'function') {
            callback();
          }
        });
      } else {
        if (typeof callback === 'function') {
          callback();
        }
      }
    },
    // 显示弹出框
    showPopUp() {
      this.initselectedTopics(() => this.setData({ popupVisible: true }));
    },
    // 监听弹出框状态变化,例如通过点击外部遮罩层关闭弹出框时
    onPopupVisibleChange(e) {
      const { visible } = e.detail;
      this.setData({ popupVisible: visible });
      if (!visible) {
        this.closePatrolList();
      }
    },
    // 取消并关闭台账类别选择弹出框
    closePatrolList() {
      const { selectedTopics } = this.data;
      const { sceneType } = this.data.searchOptions;
      const { allLedgerTypes, selectedLedgers } = selectedTopics[sceneType];
      allLedgerTypes.forEach(p => {
        if (selectedLedgers.indexOf(p.ledgerSubTypeId) == -1) {
          p.selected = false;
        } else {
          p.selected = {
            color: 'green',
            count: '✓',
            shape: 'square',
          };
        }
      });
      this.setData({
        [`selectedTopics.${sceneType}.allLedgerTypes`]: allLedgerTypes,
        popupVisible: false,
      });
      this._isCheckAll();
    },
    // 选择台账类别
    choseLedgerType(e) {
      const { index } = e.currentTarget.dataset;
      const { selectedTopics } = this.data;
      const { sceneType } = this.data.searchOptions;
      const { allLedgerTypes, selectedLedgers } = selectedTopics[sceneType];
      let { selected, ledgerSubTypeId } = allLedgerTypes[index];
      if (selected) {
        selected = null;
      } else {
        selected = {
          color: 'green',
          count: '✓',
          shape: 'square',
        };
      }
      this.setData({
        [`selectedTopics.${sceneType}.allLedgerTypes[${index}].selected`]: selected,
      });
      this._isCheckAll();
    },
    // 全选
    onCheckAll(e) {
      const { checked } = e.detail;
      const { selectedTopics } = this.data;
      const { sceneType } = this.data.searchOptions;
      const { allLedgerTypes } = selectedTopics[sceneType];
      allLedgerTypes.forEach(p => {
        if (checked) {
          p.selected = {
            color: 'green',
            count: '✓',
            shape: 'square',
          };
        } else {
          p.selected = null;
        }
      });
      this.setData({ [`selectedTopics.${sceneType}.allLedgerTypes`]: allLedgerTypes });
      this._isCheckAll();
    },
    /**
     * 全选判断
     * 根据用户选择的情况,判断是否已经全选,更新全选变量状态
     */
    _isCheckAll() {
      const { selectedTopics } = this.data;
      const { sceneType } = this.data.searchOptions;
      const { allLedgerTypes } = selectedTopics[sceneType];
      // 判断是否全选中
      const p = allLedgerTypes.filter(v => {
        return v.selected == undefined || v.selected == null || v.selected == false;
      });
      this.setData({
        [`selectedTopics.${sceneType}.selectedCount`]: allLedgerTypes.length - p.length,
        [`selectedTopics.${sceneType}.checkAll`]: p.length == 0,
      });
    },
    // 刷新选择结果
    refreshSelectedLedger() {
      const { selectedTopics } = this.data;
      const { sceneType } = this.data.searchOptions;
      const { allLedgerTypes, selectedLedgers } = selectedTopics[sceneType];
      let topicText = '';
      allLedgerTypes.forEach(p => {
        const i = selectedLedgers.indexOf(p.ledgerSubTypeId);
        if (p.selected) {
          if (i == -1) {
            selectedLedgers.push(p.ledgerSubTypeId);
          }
          if (topicText != '') {
            topicText += '、';
          }
          topicText += p.ledgerName;
        } else {
          if (i != -1) {
            selectedLedgers.splice(i, 1);
          }
        }
      });
      if (topicText == '') {
        topicText = '点击选择';
      } else if (selectedLedgers.length == allLedgerTypes.length) {
        topicText = '已选全部类别';
      } else if (topicText.length > 9) {
        topicText = topicText.substring(0, 10);
        topicText += `...等${selectedLedgers.length}个类别`;
      }
      this.setData({
        [`selectedTopics.${sceneType}.selectedLedgers`]: selectedLedgers,
        [`selectedTopics.${sceneType}.topicText`]: topicText,
      });
    },
    /**
     * 台账类别选择事件
     * 在用户点击弹出框的“确定”按钮后,将[allLedgerTypes]选中的台账更新到[selectedLedgers]列表中,
     * 关闭弹出框,并且触发获取台账信息的函数
     */
    onLedgerPickerChange() {
      this.refreshSelectedLedger();
      this.setData({ popupVisible: false });
      this.fetchTopicLedgers();
    },
    /**
     * 获取专题相关的台账信息
     * @param {Int} startIndex 需要获取台账信息的起始索引
     */
    fetchTopicLedgers(startIndex) {
      this.setData({ topicLoading: true });
      // 确定所选的台账类型
      const { selectedTopics } = this.data;
      const { sceneType, time } = this.data.searchOptions;
      const { allLedgerTypes, selectedLedgers } = selectedTopics[sceneType];
      const _selectedLedgers = allLedgerTypes.filter(v => {
        return selectedLedgers.indexOf(v.ledgerSubTypeId) != -1;
      });
 
      if (_selectedLedgers.length == 0) {
        searchResult.forEach(e => {
          e.topics = [];
        });
      } else {
        const { searchResult } = this.data;
        const alltask = [];
        for (let i = startIndex ? startIndex : 0; i < searchResult.length; i++) {
          const e = searchResult[i];
          this.setData({ [`searchResult[${i}].topicLoading`]: true });
          const t = fetchLedgerDetail({ userId: e.userId, sceneType, time }).then(res => {
            const topics = [];
            _selectedLedgers.forEach(l => {
              const ledger = res.data.find(r => r.ledgerSubTypeId == l.ledgerSubTypeId);
              const detail = ledger ? ledger : { ledgerName: l.ledgerName };
              topics.push({ ...l, detail });
            });
            this.setData({
              [`searchResult[${i}].topicLoading`]: false,
              [`searchResult[${i}].topics`]: topics,
            });
          });
          alltask.push(t);
        }
        Promise.all(alltask).finally(() => {
          this.setData({ topicLoading: false });
          console.log('searchResult', this.data.searchResult);
        });
      }
    },
  },
});