fix: 优化一些工具函数
This commit is contained in:
parent
30e0d9606d
commit
b4cd064c67
@ -65,9 +65,16 @@ export async function GetCarInfo(): Promise<CarInfoType> {
|
|||||||
export async function UseAuth(context: common.UIAbilityContext): Promise<boolean> {
|
export async function UseAuth(context: common.UIAbilityContext): Promise<boolean> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const permissions: Array<Permissions> =
|
const permissions: Array<Permissions> =
|
||||||
["ohos.permission.SET_TIME", "ohos.permission.READ_IMAGEVIDEO", "ohos.permission.DISTRIBUTED_DATASYNC",
|
[
|
||||||
'ohos.permission.CONNECTIVITY_INTERNAL', 'ohos.permission.CAMERA', 'ohos.permission.READ_MEDIA',
|
"ohos.permission.SET_TIME",
|
||||||
'ohos.permission.WRITE_MEDIA', 'ohos.permission.FILE_ACCESS_MANAGER'];
|
"ohos.permission.READ_IMAGEVIDEO",
|
||||||
|
"ohos.permission.DISTRIBUTED_DATASYNC",
|
||||||
|
"ohos.permission.CONNECTIVITY_INTERNAL",
|
||||||
|
"ohos.permission.CAMERA",
|
||||||
|
"ohos.permission.READ_MEDIA",
|
||||||
|
"ohos.permission.WRITE_MEDIA",
|
||||||
|
"ohos.permission.FILE_ACCESS_MANAGER"
|
||||||
|
];
|
||||||
abilityAccessCtrl.createAtManager().requestPermissionsFromUser(context, permissions).then(res => {
|
abilityAccessCtrl.createAtManager().requestPermissionsFromUser(context, permissions).then(res => {
|
||||||
let grantStatus: Array<number> = res.authResults;
|
let grantStatus: Array<number> = res.authResults;
|
||||||
let length: number = grantStatus.length;
|
let length: number = grantStatus.length;
|
||||||
@ -79,7 +86,7 @@ export async function UseAuth(context: common.UIAbilityContext): Promise<boolean
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).catch((err: BusinessError) => {
|
}).catch((err: BusinessError) => {
|
||||||
dConsole.log("获取权限失败", JSON.stringify(err))
|
dConsole.error("获取权限失败", JSON.stringify(err))
|
||||||
reject(false)
|
reject(false)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@ -11,16 +11,16 @@ interface StuInfo {
|
|||||||
|
|
||||||
export default class FileLog {
|
export default class FileLog {
|
||||||
public folderPath?: string
|
public folderPath?: string
|
||||||
public progressDataFd: number|undefined =undefined
|
public progressDataFd: number | undefined = undefined
|
||||||
public examJudgeWuxiDataFd: number|undefined = undefined
|
public examJudgeWuxiDataFd: number | undefined = undefined
|
||||||
public examJudgeWuxiProgressDataFd: number|undefined = undefined
|
public examJudgeWuxiProgressDataFd: number | undefined = undefined
|
||||||
public plcDataFd: number|undefined = undefined
|
public plcDataFd: number | undefined = undefined
|
||||||
public examJudgeDataFd: number|undefined = undefined
|
public examJudgeDataFd: number | undefined = undefined
|
||||||
public examJudgeCallbackDataFd: number|undefined = undefined
|
public examJudgeCallbackDataFd: number | undefined = undefined
|
||||||
public examJudgeLogDataFd: number|undefined = undefined
|
public examJudgeLogDataFd: number | undefined = undefined
|
||||||
public fourAndOneLogDataFd: number|undefined = undefined
|
public fourAndOneLogDataFd: number | undefined = undefined
|
||||||
public fourAndOneLogDataBytesFd: number|undefined = undefined
|
public fourAndOneLogDataBytesFd: number | undefined = undefined
|
||||||
public examLineDataFd: number|undefined = undefined
|
public examLineDataFd: number | undefined = undefined
|
||||||
private fileUtil: FileUtils
|
private fileUtil: FileUtils
|
||||||
// 过程文件数据
|
// 过程文件数据
|
||||||
public setExamProgressData = async (str: Object) => {
|
public setExamProgressData = async (str: Object) => {
|
||||||
@ -115,7 +115,6 @@ export default class FileLog {
|
|||||||
constructor(context: common.UIAbilityContext) {
|
constructor(context: common.UIAbilityContext) {
|
||||||
const fileUtil = new FileUtils(context)
|
const fileUtil = new FileUtils(context)
|
||||||
this.fileUtil = fileUtil
|
this.fileUtil = fileUtil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//关闭所有文件写入
|
//关闭所有文件写入
|
||||||
|
|||||||
@ -2,6 +2,8 @@ import dayTs from './Date';
|
|||||||
import fs from '@ohos.file.fs';
|
import fs from '@ohos.file.fs';
|
||||||
import { BusinessError } from '@ohos.base';
|
import { BusinessError } from '@ohos.base';
|
||||||
import { CommonFileTag } from '../config';
|
import { CommonFileTag } from '../config';
|
||||||
|
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl';
|
||||||
|
import Prompt from '@system.prompt';
|
||||||
|
|
||||||
enum timeType {
|
enum timeType {
|
||||||
fulltime = 1
|
fulltime = 1
|
||||||
@ -150,7 +152,11 @@ export function ConvertDdmmToDecimalDegrees(ddmm: number): number {
|
|||||||
return degrees + minutes / 60;
|
return degrees + minutes / 60;
|
||||||
}
|
}
|
||||||
|
|
||||||
//对象深拷贝
|
/**
|
||||||
|
* 深度克隆对象
|
||||||
|
* @param target
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
export function DeepClone<T extends Object>(target: T): T {
|
export function DeepClone<T extends Object>(target: T): T {
|
||||||
// 如果是对象,且不是原始值null
|
// 如果是对象,且不是原始值null
|
||||||
if (typeof target === 'object' && target !== null) {
|
if (typeof target === 'object' && target !== null) {
|
||||||
@ -231,3 +237,20 @@ export function FormatTimeFlexible(seconds: number): string {
|
|||||||
|
|
||||||
return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
|
return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 申请授权
|
||||||
|
* @param context 上下文
|
||||||
|
* @param permissionList 需要申请的权限列表
|
||||||
|
*/
|
||||||
|
export function ApplyForAuthorization(context: Context, permissionList: Array<Permissions>) {
|
||||||
|
const atManager = abilityAccessCtrl.createAtManager()
|
||||||
|
atManager.requestPermissionsFromUser(context, permissionList, (code, result) => {
|
||||||
|
if (result.authResults[0] === -1) {
|
||||||
|
Prompt.showToast({
|
||||||
|
message: '请先授权',
|
||||||
|
duration: 3000,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -1,9 +1,11 @@
|
|||||||
import common from '@ohos.app.ability.common'
|
import common from '@ohos.app.ability.common'
|
||||||
import fs from '@ohos.file.fs'
|
import fs from '@ohos.file.fs'
|
||||||
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl'
|
import { Permissions } from '@ohos.abilityAccessCtrl'
|
||||||
import promptAction from '@ohos.promptAction'
|
|
||||||
import { FileTag } from '../config'
|
import { FileTag } from '../config'
|
||||||
import { BusinessError } from '@ohos.base'
|
import { BusinessError } from '@ohos.base'
|
||||||
|
import { ApplyForAuthorization } from './Common'
|
||||||
|
import { dConsole } from './LogWorker'
|
||||||
|
import Prompt from '@system.prompt'
|
||||||
|
|
||||||
|
|
||||||
export default class FileUtils {
|
export default class FileUtils {
|
||||||
@ -127,20 +129,12 @@ export default class FileUtils {
|
|||||||
'ohos.permission.READ_MEDIA',
|
'ohos.permission.READ_MEDIA',
|
||||||
'ohos.permission.WRITE_MEDIA'
|
'ohos.permission.WRITE_MEDIA'
|
||||||
]
|
]
|
||||||
const atManager = abilityAccessCtrl.createAtManager()
|
ApplyForAuthorization(this.context, permissions)
|
||||||
atManager.requestPermissionsFromUser(this.context, permissions, (code, result) => {
|
|
||||||
if (result.authResults[0] === -1) {
|
|
||||||
promptAction.showToast({
|
|
||||||
message: '请先授权',
|
|
||||||
duration: 3000,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleError(message: string, error: BusinessError, filePath: string): void {
|
private handleError(message: string, error: BusinessError, filePath: string): void {
|
||||||
console.error(FileTag, `${message} ${filePath} ${JSON.stringify(error)}`)
|
dConsole.error(FileTag, `${message} ${filePath} ${JSON.stringify(error)}`)
|
||||||
promptAction.showToast({
|
Prompt.showToast({
|
||||||
message: `${message} ${filePath} ${JSON.stringify(error)}`,
|
message: `${message} ${filePath} ${JSON.stringify(error)}`,
|
||||||
duration: 4000,
|
duration: 4000,
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user