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 sms from '@ohos.telephony.sms';
17import HiLog from '../utils/HiLog';
18import common from '../data/commonData';
19import http from '@ohos.net.http';
20
21const TAG = 'SendMsgService'
22
23export default {
24
25    /**
26     * Sending sms messages
27     *
28     * @param params Including the card slot, phone number, and SMS message content
29     * @callback callback Return the message sending status.
30     */
31    sendMessage(params, callback) {
32        try {
33            sms.sendMessage({
34                slotId: Number(params.slotId),
35                destinationHost: params.destinationHost,
36                content: params.content,
37                sendCallback: (err, value) => {
38                    let sendStatus: number = common.int.SEND_MESSAGE_FAILED;
39                    if (err) {
40                        HiLog.w(TAG, 'sendMessage, sendCallback failed err: ' + JSON.stringify(err.message));
41                        sendStatus = common.int.SEND_MESSAGE_FAILED;
42                    } else {
43                        sendStatus = this.dealSendResult(value);
44                        HiLog.i(TAG, `sendMessage, sendCallback success result=${value.result}, sendStatus=${sendStatus}`);
45                    }
46                    callback(sendStatus);
47                },
48                deliveryCallback: (err, value) => {
49                    if (err) {
50                        HiLog.w(TAG, 'sendMessage, deliveryCallback failed err: ' + JSON.stringify(err.message));
51                        return;
52                    }
53                }
54            });
55        } catch (error) {
56            HiLog.e(TAG, 'sendMessage, sendMessage failed error: ' + JSON.stringify(error));
57            callback(common.int.SEND_MESSAGE_FAILED);
58        }
59    },
60
61    dealSendResult(value) {
62        let sendStatus = common.int.SEND_MESSAGE_SENDING;
63        if (value.result == sms.SendSmsResult.SEND_SMS_SUCCESS) {
64            sendStatus = common.int.SEND_MESSAGE_SUCCESS;
65        } else {
66            sendStatus = common.int.SEND_MESSAGE_FAILED;
67        }
68        return sendStatus;
69    },
70
71    sendMmsMessage(params, callback) {
72        let httpRequest = http.createHttp();
73        httpRequest.request(common.string.MMS_URL,
74            {
75                method: http.RequestMethod.POST,
76                header: {
77                    'Content-Type': 'application/vnd.wap.mms-message',
78                    'Accept': 'Accept',
79                    'Accept-Language': 'Accept-Language'
80                },
81                extraData: JSON.stringify(params),
82                readTimeout: 60000,
83                connectTimeout: 60000
84            }, (err, data) => {
85                let sendStatus;
86                if (err) {
87                    HiLog.i(TAG, 'sendMmsMessage, error: ' + JSON.stringify(err.message));
88                    sendStatus = common.int.SEND_MESSAGE_FAILED;
89                } else {
90                    sendStatus = common.int.SEND_MESSAGE_SUCCESS;
91                }
92                callback(sendStatus);
93            }
94        );
95    }
96};