diff --git a/entry/src/main/ets/Model/DateTimeUtil.ts b/entry/src/main/ets/Model/DateTimeUtil.ts new file mode 100644 index 0000000000000000000000000000000000000000..9e051be2d4b1e3be43cd933cf5c84e67ae3ee48f --- /dev/null +++ b/entry/src/main/ets/Model/DateTimeUtil.ts @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file 日期工具 + */ +export default class DateTimeUtil { + + /** + * 时分秒 + */ + getTime() { + const DATETIME = new Date() + return this.concatTime(DATETIME.getHours(), DATETIME.getMinutes(), DATETIME.getSeconds()) + } + + /** + * 年月日 + */ + getDate() { + const DATETIME = new Date() + return this.concatDate(DATETIME.getFullYear(), DATETIME.getMonth() + 1, DATETIME.getDate()) + } + + /** + * 日期不足两位补充0 + * @param value-数据值 + */ + fill(value: number) { + return (value > 9 ? '' : '0') + value + } + + /** + * 年月日格式修饰 + * @param year + * @param month + * @param date + */ + concatDate(year: number, month: number, date: number) { + return `${year}${this.fill(month)}${this.fill(date)}` + } + + /** + * 时分秒格式修饰 + * @param hours + * @param minutes + * @param seconds + */ + concatTime(hours: number, minutes: number, seconds: number) { + return `${this.fill(hours)}${this.fill(minutes)}${this.fill(seconds)}` + } +} \ No newline at end of file diff --git a/entry/src/main/ets/Model/Logger.ts b/entry/src/main/ets/Model/Logger.ts new file mode 100644 index 0000000000000000000000000000000000000000..78950d0a9c3bf24b1f2e1c2e6774a1ef25c8ae51 --- /dev/null +++ b/entry/src/main/ets/Model/Logger.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import hilog from '@ohos.hilog' + +class Logger { + private domain: number + private prefix: string + private format: string = "%{public}s, %{public}s" + + constructor(prefix: string) { + this.prefix = prefix + this.domain = 0xFF00 + } + + debug(...args: any[]) { + hilog.debug(this.domain, this.prefix, this.format, args) + } + + info(...args: any[]) { + hilog.info(this.domain, this.prefix, this.format, args) + } + + warn(...args: any[]) { + hilog.warn(this.domain, this.prefix, this.format, args) + } + + error(...args: any[]) { + hilog.error(this.domain, this.prefix, this.format, args) + } +} + +export default new Logger('[MultiMedia]') \ No newline at end of file diff --git a/entry/src/main/ets/Model/TimeUtils.ts b/entry/src/main/ets/Model/TimeUtils.ts new file mode 100644 index 0000000000000000000000000000000000000000..7a55812b2bfab1d2e82630dcf92b90a6509bd731 --- /dev/null +++ b/entry/src/main/ets/Model/TimeUtils.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +export function tempNum(num) { + if (num < 10) { + return '0' + num + } + return num.toString() +} + +export function getDurationString(duration) { + let hour = Math.floor(duration / (1000 * 60 * 60)) + let minute = Math.floor(duration / (1000 * 60)) + let second = Math.floor(duration / 1000) + if (hour > 0) { + return `${tempNum(hour)}:${tempNum(minute)}:${tempNum(second)}` + } + return `${tempNum(minute)}:${tempNum(second)}` +} diff --git a/entry/src/main/ets/util/DateTimeUtils.ts b/entry/src/main/ets/util/DateTimeUtils.ts new file mode 100644 index 0000000000000000000000000000000000000000..b506cbb157252a465605a06689664c8644e9912e --- /dev/null +++ b/entry/src/main/ets/util/DateTimeUtils.ts @@ -0,0 +1,91 @@ + + +/** + * @file: 日期工具 + */ + +export default class DateTimeUtil { + + /** + * 时分秒 + * + * @return {string} - 返回时分秒 + */ + static getTime() { + const DATETIME = new Date(); + const HOURS = DATETIME.getHours(); + const MINUTES = DATETIME.getMinutes(); + const SECONDS = DATETIME.getSeconds(); + return this.concatTime(HOURS, MINUTES, SECONDS); + } + + /** + * 年月日 + * + * @return {string} - 返回年月日 + */ + static getDate() { + const DATETIME = new Date(); + const YEAR = DATETIME.getFullYear(); + const MONTH = DATETIME.getMonth() + 1; + const DAY = DATETIME.getDate(); + return this.concatDate(YEAR, MONTH, DAY); + } + + /** + * 日期不足两位补 0 + * + * @param {string} value - 数据值 + * @return {string} - 日期不足两位补 0 + */ + static fill(value: number) { + return (value < 10 ? `0${value}` : `${value}`); + } + + /** + * 年月日格式修饰 + * + * @param {string} year - 年 + * @param {string} month - 月 + * @param {string} date - 日 + * @return {string} - 年月日格式修饰 + */ + static concatDate(year, month, date) { + return `${year}${month}${date}`; + } + + /** + * 时分秒格式修饰 + * + * @param {string} hours - 时 + * @param {string} minutes - 分 + * @param {string} seconds - 秒 + * @return {string} - 时分秒格式修饰 + */ + static concatTime(hours, minutes, seconds) { + return `${this.fill(hours)}${this.fill(minutes)}${this.fill(seconds)}`; + } + + static dateFormat(timestamp?) { + const t = new Date(timestamp) + let year = t.getFullYear() + let month = t.getMonth() + 1 + let day = t.getDate() + let hours = t.getHours() + let minutes = t.getMinutes() + let seconds = t.getSeconds() + return year + "-" + this.fill(month) + "-" + this.fill(day) + " " + this.fill(hours) + ":" + this.fill(minutes) + ":" + this.fill(seconds); + } + + static ms2CountdownTime(ms) { + if (!ms)return '00:00' + const days = Math.floor(ms / (1000 * 60 * 60 * 24)) + const hours = Math.floor((ms % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)) + const minutes = Math.floor((ms % (1000 * 60 * 60)) / (1000 * 60)); + const seconds = Math.floor((ms % (1000 * 60)) / 1000); + return (days ? this.fill(days) + ':' : '') + + (hours ? this.fill(hours) + ':' : '') + + this.fill(minutes) + ':' + + this.fill(seconds) + } +} diff --git a/entry/src/main/ets/util/LogUtils.ts b/entry/src/main/ets/util/LogUtils.ts new file mode 100644 index 0000000000000000000000000000000000000000..db8a1bf67dac2edd55e363bb1229f97364094312 --- /dev/null +++ b/entry/src/main/ets/util/LogUtils.ts @@ -0,0 +1,37 @@ + + +import hiLog from '@ohos.hilog'; + +export default class LogUtils { + private static domain: number = 0xFF00 + + static info(tag, msg: string) { + if (globalThis.isShowLog) { + hiLog.info(this.domain, globalThis.LogTag, "%{public}s " + msg, tag); + } + } + + static debug(tag, msg: string) { + if (globalThis.isShowLog) { + hiLog.debug(this.domain, globalThis.LogTag, "%{public}s " + msg, tag); + } + } + + static error(tag, msg: string) { + if (globalThis.isShowLog) { + hiLog.error(this.domain, globalThis.LogTag, "%{public}s " + msg, tag); + } + } + + static fatal(tag, msg: string) { + if (globalThis.isShowLog) { + hiLog.fatal(this.domain, globalThis.LogTag, "%{public}s " + msg, tag); + } + } + + static warn(tag, msg: string) { + if (globalThis.isShowLog) { + hiLog.warn(this.domain, globalThis.LogTag, "%{public}s " + msg, tag); + } + } +} \ No newline at end of file diff --git a/entry/src/main/ets/util/SysPermissionUtils.ts b/entry/src/main/ets/util/SysPermissionUtils.ts new file mode 100644 index 0000000000000000000000000000000000000000..24573b345b91717672165b3856b770928e7941b7 --- /dev/null +++ b/entry/src/main/ets/util/SysPermissionUtils.ts @@ -0,0 +1,68 @@ + + +import bundle from '@ohos.bundle' +import abilityAccessCtrl from '@ohos.abilityAccessCtrl' +import LogUtils from './LogUtils'; + +export default class SysPermissionUtils { + static async hasAuth(permission) { + let bundleFlag = 0 + let userId = 100 + let appInfo = await bundle.getApplicationInfo(globalThis.bundleName, bundleFlag, userId) + let tokenId = appInfo.accessTokenId + let atManager = abilityAccessCtrl.createAtManager() + let result = await atManager.verifyAccessToken(tokenId, permission) + return result == abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED + } + + static async request(targetPermissionArray) { + let needPermissionArray: Array = new Array() + for (let targetPermission of targetPermissionArray) { + let isAuth = await this.hasAuth(targetPermission) + if (!isAuth) { + needPermissionArray.push(targetPermission) + } + } + if (needPermissionArray.length > 0) { + globalThis.context.requestPermissionsFromUser(needPermissionArray) + } + } + + static async requestWithinNecessary(targetPermissionArray: string[], necessaryPermissionArray?: string[]) { + let isNecessaryPermissionAuth: boolean = true + let needPermissionArray: Array = new Array() + if (targetPermissionArray != null) { + for (let targetPermission of targetPermissionArray) { + let isTargetAuth = await this.hasAuth(targetPermission) + if (!isTargetAuth) { + needPermissionArray.push(targetPermission) + } + } + } + if (necessaryPermissionArray != null) { + for (let necessaryPermission of necessaryPermissionArray) { + let isNecessaryAuth = await this.hasAuth(necessaryPermission) + if (!isNecessaryAuth) { + needPermissionArray.push(necessaryPermission) + isNecessaryPermissionAuth = false + } + } + } + if (needPermissionArray.length > 0) { + let result = await globalThis.context.requestPermissionsFromUser(needPermissionArray) + LogUtils.info('SysPermissionUtils', 'request is called,result is ' + JSON.stringify(result)) + if (!isNecessaryPermissionAuth) { + isNecessaryPermissionAuth = true + out: for (let i = 0; i < needPermissionArray.length; i++) { + for (let j = 0; j < necessaryPermissionArray.length; j++) { + if (needPermissionArray[i] == necessaryPermissionArray[j] && (result.authResults[i] == abilityAccessCtrl.GrantStatus.PERMISSION_DENIED)) { + isNecessaryPermissionAuth = false + break out + } + } + } + } + } + return isNecessaryPermissionAuth + } +} \ No newline at end of file