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> { | ||||
|   return new Promise((resolve, reject) => { | ||||
|     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.WRITE_MEDIA', 'ohos.permission.FILE_ACCESS_MANAGER']; | ||||
|       [ | ||||
|         "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.WRITE_MEDIA", | ||||
|         "ohos.permission.FILE_ACCESS_MANAGER" | ||||
|       ]; | ||||
|     abilityAccessCtrl.createAtManager().requestPermissionsFromUser(context, permissions).then(res => { | ||||
|       let grantStatus: Array<number> = res.authResults; | ||||
|       let length: number = grantStatus.length; | ||||
| @ -79,7 +86,7 @@ export async function UseAuth(context: common.UIAbilityContext): Promise<boolean | ||||
|         } | ||||
|       } | ||||
|     }).catch((err: BusinessError) => { | ||||
|       dConsole.log("获取权限失败", JSON.stringify(err)) | ||||
|       dConsole.error("获取权限失败", JSON.stringify(err)) | ||||
|       reject(false) | ||||
|     }) | ||||
|   }) | ||||
|  | ||||
| @ -115,7 +115,6 @@ export default class FileLog { | ||||
|   constructor(context: common.UIAbilityContext) { | ||||
|     const fileUtil = new FileUtils(context) | ||||
|     this.fileUtil = fileUtil | ||||
| 
 | ||||
|   } | ||||
| 
 | ||||
|   //关闭所有文件写入 | ||||
|  | ||||
| @ -2,6 +2,8 @@ import dayTs from './Date'; | ||||
| import fs from '@ohos.file.fs'; | ||||
| import { BusinessError } from '@ohos.base'; | ||||
| import { CommonFileTag } from '../config'; | ||||
| import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl'; | ||||
| import Prompt from '@system.prompt'; | ||||
| 
 | ||||
| enum timeType { | ||||
|   fulltime = 1 | ||||
| @ -150,7 +152,11 @@ export function ConvertDdmmToDecimalDegrees(ddmm: number): number { | ||||
|   return degrees + minutes / 60; | ||||
| } | ||||
| 
 | ||||
| //对象深拷贝 | ||||
| /** | ||||
|  * 深度克隆对象 | ||||
|  * @param target | ||||
|  * @returns | ||||
|  */ | ||||
| export function DeepClone<T extends Object>(target: T): T { | ||||
|   // 如果是对象,且不是原始值null | ||||
|   if (typeof target === 'object' && target !== null) { | ||||
| @ -231,3 +237,20 @@ export function FormatTimeFlexible(seconds: number): string { | ||||
| 
 | ||||
|   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 fs from '@ohos.file.fs' | ||||
| import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl' | ||||
| import promptAction from '@ohos.promptAction' | ||||
| import { Permissions } from '@ohos.abilityAccessCtrl' | ||||
| import { FileTag } from '../config' | ||||
| import { BusinessError } from '@ohos.base' | ||||
| import { ApplyForAuthorization } from './Common' | ||||
| import { dConsole } from './LogWorker' | ||||
| import Prompt from '@system.prompt' | ||||
| 
 | ||||
| 
 | ||||
| export default class FileUtils { | ||||
| @ -127,20 +129,12 @@ export default class FileUtils { | ||||
|       'ohos.permission.READ_MEDIA', | ||||
|       'ohos.permission.WRITE_MEDIA' | ||||
|     ] | ||||
|     const atManager = abilityAccessCtrl.createAtManager() | ||||
|     atManager.requestPermissionsFromUser(this.context, permissions, (code, result) => { | ||||
|       if (result.authResults[0] === -1) { | ||||
|         promptAction.showToast({ | ||||
|           message: '请先授权', | ||||
|           duration: 3000, | ||||
|         }) | ||||
|       } | ||||
|     }) | ||||
|     ApplyForAuthorization(this.context, permissions) | ||||
|   } | ||||
| 
 | ||||
|   private handleError(message: string, error: BusinessError, filePath: string): void { | ||||
|     console.error(FileTag, `${message} ${filePath} ${JSON.stringify(error)}`) | ||||
|     promptAction.showToast({ | ||||
|     dConsole.error(FileTag, `${message} ${filePath} ${JSON.stringify(error)}`) | ||||
|     Prompt.showToast({ | ||||
|       message: `${message} ${filePath} ${JSON.stringify(error)}`, | ||||
|       duration: 4000, | ||||
|     }) | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user