1048147e0Sopenharmony_ci/**
2048147e0Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3048147e0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4048147e0Sopenharmony_ci * you may not use this file except in compliance with the License.
5048147e0Sopenharmony_ci * You may obtain a copy of the License at
6048147e0Sopenharmony_ci *
7048147e0Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8048147e0Sopenharmony_ci *
9048147e0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10048147e0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11048147e0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12048147e0Sopenharmony_ci * See the License for the specific language governing permissions and
13048147e0Sopenharmony_ci * limitations under the License.
14048147e0Sopenharmony_ci */
15048147e0Sopenharmony_ci
16048147e0Sopenharmony_ciimport sms from '@ohos.telephony.sms';
17048147e0Sopenharmony_ciimport HiLog from '../utils/HiLog';
18048147e0Sopenharmony_ciimport common from '../data/commonData';
19048147e0Sopenharmony_ciimport http from '@ohos.net.http';
20048147e0Sopenharmony_ci
21048147e0Sopenharmony_ciconst TAG = 'SendMsgService'
22048147e0Sopenharmony_ci
23048147e0Sopenharmony_ciexport default {
24048147e0Sopenharmony_ci
25048147e0Sopenharmony_ci    /**
26048147e0Sopenharmony_ci     * Sending sms messages
27048147e0Sopenharmony_ci     *
28048147e0Sopenharmony_ci     * @param params Including the card slot, phone number, and SMS message content
29048147e0Sopenharmony_ci     * @callback callback Return the message sending status.
30048147e0Sopenharmony_ci     */
31048147e0Sopenharmony_ci    sendMessage(params, callback) {
32048147e0Sopenharmony_ci        try {
33048147e0Sopenharmony_ci            sms.sendMessage({
34048147e0Sopenharmony_ci                slotId: Number(params.slotId),
35048147e0Sopenharmony_ci                destinationHost: params.destinationHost,
36048147e0Sopenharmony_ci                content: params.content,
37048147e0Sopenharmony_ci                sendCallback: (err, value) => {
38048147e0Sopenharmony_ci                    let sendStatus: number = common.int.SEND_MESSAGE_FAILED;
39048147e0Sopenharmony_ci                    if (err) {
40048147e0Sopenharmony_ci                        HiLog.w(TAG, 'sendMessage, sendCallback failed err: ' + JSON.stringify(err.message));
41048147e0Sopenharmony_ci                        sendStatus = common.int.SEND_MESSAGE_FAILED;
42048147e0Sopenharmony_ci                    } else {
43048147e0Sopenharmony_ci                        sendStatus = this.dealSendResult(value);
44048147e0Sopenharmony_ci                        HiLog.i(TAG, `sendMessage, sendCallback success result=${value.result}, sendStatus=${sendStatus}`);
45048147e0Sopenharmony_ci                    }
46048147e0Sopenharmony_ci                    callback(sendStatus);
47048147e0Sopenharmony_ci                },
48048147e0Sopenharmony_ci                deliveryCallback: (err, value) => {
49048147e0Sopenharmony_ci                    if (err) {
50048147e0Sopenharmony_ci                        HiLog.w(TAG, 'sendMessage, deliveryCallback failed err: ' + JSON.stringify(err.message));
51048147e0Sopenharmony_ci                        return;
52048147e0Sopenharmony_ci                    }
53048147e0Sopenharmony_ci                }
54048147e0Sopenharmony_ci            });
55048147e0Sopenharmony_ci        } catch (error) {
56048147e0Sopenharmony_ci            HiLog.e(TAG, 'sendMessage, sendMessage failed error: ' + JSON.stringify(error));
57048147e0Sopenharmony_ci            callback(common.int.SEND_MESSAGE_FAILED);
58048147e0Sopenharmony_ci        }
59048147e0Sopenharmony_ci    },
60048147e0Sopenharmony_ci
61048147e0Sopenharmony_ci    dealSendResult(value) {
62048147e0Sopenharmony_ci        let sendStatus = common.int.SEND_MESSAGE_SENDING;
63048147e0Sopenharmony_ci        if (value.result == sms.SendSmsResult.SEND_SMS_SUCCESS) {
64048147e0Sopenharmony_ci            sendStatus = common.int.SEND_MESSAGE_SUCCESS;
65048147e0Sopenharmony_ci        } else {
66048147e0Sopenharmony_ci            sendStatus = common.int.SEND_MESSAGE_FAILED;
67048147e0Sopenharmony_ci        }
68048147e0Sopenharmony_ci        return sendStatus;
69048147e0Sopenharmony_ci    },
70048147e0Sopenharmony_ci
71048147e0Sopenharmony_ci    sendMmsMessage(params, callback) {
72048147e0Sopenharmony_ci        let httpRequest = http.createHttp();
73048147e0Sopenharmony_ci        httpRequest.request(common.string.MMS_URL,
74048147e0Sopenharmony_ci            {
75048147e0Sopenharmony_ci                method: http.RequestMethod.POST,
76048147e0Sopenharmony_ci                header: {
77048147e0Sopenharmony_ci                    'Content-Type': 'application/vnd.wap.mms-message',
78048147e0Sopenharmony_ci                    'Accept': 'Accept',
79048147e0Sopenharmony_ci                    'Accept-Language': 'Accept-Language'
80048147e0Sopenharmony_ci                },
81048147e0Sopenharmony_ci                extraData: JSON.stringify(params),
82048147e0Sopenharmony_ci                readTimeout: 60000,
83048147e0Sopenharmony_ci                connectTimeout: 60000
84048147e0Sopenharmony_ci            }, (err, data) => {
85048147e0Sopenharmony_ci                let sendStatus;
86048147e0Sopenharmony_ci                if (err) {
87048147e0Sopenharmony_ci                    HiLog.i(TAG, 'sendMmsMessage, error: ' + JSON.stringify(err.message));
88048147e0Sopenharmony_ci                    sendStatus = common.int.SEND_MESSAGE_FAILED;
89048147e0Sopenharmony_ci                } else {
90048147e0Sopenharmony_ci                    sendStatus = common.int.SEND_MESSAGE_SUCCESS;
91048147e0Sopenharmony_ci                }
92048147e0Sopenharmony_ci                callback(sendStatus);
93048147e0Sopenharmony_ci            }
94048147e0Sopenharmony_ci        );
95048147e0Sopenharmony_ci    }
96048147e0Sopenharmony_ci};