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 sms.sendMessage({ 33 slotId: params.slotId, 34 destinationHost: params.destinationHost, 35 content: params.content, 36 sendCallback: (err, value) => { 37 let sendStatus; 38 if (err) { 39 HiLog.w(TAG, "sendMessage, sendCallback failed err: " + JSON.stringify(err.message)); 40 sendStatus = common.int.SEND_MESSAGE_FAILED; 41 } else { 42 sendStatus = this.dealSendResult(value); 43 HiLog.i(TAG, `sendMessage, sendCallback success result=${value.result}, sendStatus=${sendStatus}`); 44 } 45 callback(sendStatus); 46 }, 47 deliveryCallback: (err, value) => { 48 if (err) { 49 HiLog.w(TAG, "sendMessage, deliveryCallback failed err: " + JSON.stringify(err.message)); 50 return; 51 } 52 } 53 }); 54 }, 55 56 dealSendResult(value) { 57 let sendStatus = common.int.SEND_MESSAGE_SENDING; 58 if (value.result == sms.SendSmsResult.SEND_SMS_SUCCESS) { 59 sendStatus = common.int.SEND_MESSAGE_SUCCESS; 60 } else { 61 sendStatus = common.int.SEND_MESSAGE_FAILED; 62 } 63 return sendStatus; 64 }, 65 66 sendMmsMessage(params, callback) { 67 let httpRequest = http.createHttp(); 68 httpRequest.request(common.string.MMS_URL, 69 { 70 method: http.RequestMethod.POST, 71 header: { 72 "Content-Type": "application/vnd.wap.mms-message", 73 "Accept": "Accept", 74 "Accept-Language": "Accept-Language" 75 }, 76 extraData: JSON.stringify(params), 77 readTimeout: 60000, 78 connectTimeout: 60000 79 }, (err, data) => { 80 let sendStatus; 81 if (err) { 82 HiLog.i(TAG, "sendMmsMessage, error: " + JSON.stringify(err.message)); 83 sendStatus = common.int.SEND_MESSAGE_FAILED; 84 } else { 85 sendStatus = common.int.SEND_MESSAGE_SUCCESS; 86 } 87 callback(sendStatus); 88 } 89 ); 90 } 91};