1/**
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import router from '@system.router';
17
18import ContactService from '../service/ContactsService';
19import ConversationListService from '../service/ConversationListService';
20import common from '../data/commonData';
21import HiLog from './HiLog';
22import LooseObject from '../data/LooseObject';
23
24const TAG = 'WantUtil';
25
26export default {
27  getWant() {
28    HiLog.i(TAG, 'getWant start');
29    if (globalThis.abilityWant == null) {
30      return;
31    }
32    let abilityWant = globalThis.abilityWant;
33    globalThis.abilityWant = null;
34
35    if (abilityWant.hasOwnProperty('parameters')) {
36      let parameters = abilityWant.parameters;
37      let pageFlag = '';
38      if (parameters.hasOwnProperty('pageFlag')) {
39        pageFlag = parameters.pageFlag;
40        HiLog.i(TAG, 'pageFlag = ' + pageFlag);
41      }
42      let contactParams = {}
43      if (parameters.hasOwnProperty('contactObjects')) {
44        contactParams = ContactService.getInstance().dealContactParams(parameters.contactObjects);
45      }
46      this.jump(pageFlag, contactParams);
47    }
48  },
49
50  jump(pageFlag, contactParams) {
51    let result = {
52      uri: '',
53      params: {}
54    };
55    HiLog.i(TAG, 'want.pageFlag: ' + pageFlag);
56    switch (pageFlag) {
57      case 'conversation':
58        result.uri = 'pages/conversation/conversation';
59        if (contactParams) {
60          result.params = contactParams;
61          this.jumpIsNewPage(result, contactParams);
62        } else {
63          router.push(result);
64        }
65        break;
66      default:
67        break;
68    }
69  },
70
71  async jumpIsNewPage(result, contactParams) {
72    // Check whether a session has been created for the current phone number in the SMS message.
73    ConversationListService.getInstance().querySessionByTelephone(contactParams.strContactsNumber, res => {
74      if (res.code == common.int.SUCCESS && res.response.id > 0) {
75        result.params.threadId = res.response.id;
76        let actionData: LooseObject = {};
77        actionData.threadId = result.params.threadId;
78        ConversationListService.getInstance().markAllToRead(actionData);
79        if (res.response.hasDraft && res.response.messageCount == 0) {
80          result.params.isNewMsg = true;
81        } else {
82          result.params.isNewMsg = false;
83        }
84      } else {
85        result.params.isNewMsg = true;
86      }
87      router.push(result);
88    }, null);
89  },
90}