fix: 优化一些公共方法

This commit is contained in:
wangzhongjie 2025-06-18 15:24:10 +08:00
parent 163793b5b1
commit 3999e1d32b
4 changed files with 60 additions and 51 deletions

View File

@ -43,8 +43,7 @@ import {
} from '../model'; } from '../model';
import { GetSyncData } from '../utils/table/Operation'; import { GetSyncData } from '../utils/table/Operation';
import dayTs from '../utils/Date'; import dayTs from '../utils/Date';
import { GetCurrentTime } from '../utils/Common'; import { CutArray, GetCurrentTime } from '../utils/Common';
import { chunkArr } from './judgeSDK/utils/Common';
import FileUtils from '../utils/FileUtils'; import FileUtils from '../utils/FileUtils';
@ -812,7 +811,7 @@ struct Index {
if (this.examSubject == "2") { if (this.examSubject == "2") {
Flex({ wrap: FlexWrap.Wrap, direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceBetween }) { Flex({ wrap: FlexWrap.Wrap, direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceBetween }) {
List({}) { List({}) {
ForEach(chunkArr(this.projects, 2), (item: [ProjectInfo, ProjectInfo]) => { ForEach(CutArray(this.projects, 2), (item: [ProjectInfo, ProjectInfo]) => {
ListItem() { ListItem() {
Row() { Row() {
Row() { Row() {

View File

@ -9,7 +9,7 @@ import FileModel from './utils/fileModel';
import FilePhoto from './utils/filePhoto'; import FilePhoto from './utils/filePhoto';
import FileLog from './utils/fileLog'; import FileLog from './utils/fileLog';
import JudgeTask from './utils/judgeTask'; import JudgeTask from './utils/judgeTask';
import { JudgeConfig } from "../../config"; import { JudgeConfig } from '../../config';
import { LANE } from '../judgeSDK/api/judgeSDK.d'; import { LANE } from '../judgeSDK/api/judgeSDK.d';
import { GetSyncData, SqlInsertTable } from '../../utils/table/Operation'; import { GetSyncData, SqlInsertTable } from '../../utils/table/Operation';
@ -33,8 +33,8 @@ import {
import { uploadExamProgressData, writeObjectOut } from '../../api/judge'; import { uploadExamProgressData, writeObjectOut } from '../../api/judge';
import { endRecordVideo, saveStartRecordVideo } from '../../utils/Video'; import { endRecordVideo, saveStartRecordVideo } from '../../utils/Video';
import common from '@ohos.app.ability.common'; import common from '@ohos.app.ability.common';
import { convertGpsCoord2, deepClone } from './utils/Common';
import { GetCurrentTime } from '../../utils/Common'; import { ConvertDdmmToDecimalDegrees, GetCurrentTime,DeepClone } from '../../utils/Common';
import UsbService from '../../utils/USB'; import UsbService from '../../utils/USB';
import FileUtils from '../../utils/FileUtils'; import FileUtils from '../../utils/FileUtils';
import { import {
@ -49,7 +49,6 @@ import {
JudgeCallBackData, JudgeCallBackData,
JudgeConfigObj, JudgeConfigObj,
JudgeConfigObjKmItems, JudgeConfigObjKmItems,
JudgeEventKf,
JudgeInitObj, JudgeInitObj,
JudgeKFXM, JudgeKFXM,
JudgeKSJS, JudgeKSJS,
@ -264,8 +263,8 @@ export default class Judge {
handDistance = async () => { handDistance = async () => {
const dwzt = this.tempData?.gps?.dwzt || ""; const dwzt = this.tempData?.gps?.dwzt || "";
const jdzt = this.tempData?.gps?.jdzt || ""; const jdzt = this.tempData?.gps?.jdzt || "";
const tJD = convertGpsCoord2(this.tempData?.gps?.jd || 0) const tJD = ConvertDdmmToDecimalDegrees(this.tempData?.gps?.jd || 0)
const tWD = convertGpsCoord2(this.tempData?.gps?.wd || 0) const tWD = ConvertDdmmToDecimalDegrees(this.tempData?.gps?.wd || 0)
if (this.prevJd && dwzt == 4 && jdzt == 3) { if (this.prevJd && dwzt == 4 && jdzt == 3) {
const distance = await examCalcGpsDistance({ const distance = await examCalcGpsDistance({
jd1: this.prevJd, jd1: this.prevJd,
@ -1052,7 +1051,7 @@ export default class Judge {
//更新UI //更新UI
if (event == 1 || event == 2 || event == 3 || event == 6) { if (event == 1 || event == 2 || event == 3 || event == 6) {
const copyProjectsObj: object = this.judgeUI.projectsObj; const copyProjectsObj: object = this.judgeUI.projectsObj;
judgeUI.projectsObj = deepClone(copyProjectsObj); judgeUI.projectsObj = DeepClone(copyProjectsObj);
} }
} }
// 更改考试状态 // 更改考试状态

View File

@ -1,41 +0,0 @@
// 根据指定个数分割数组
export function chunkArr<T>(arr: T[], size: number): T[][] {
//判断如果不是数组(就没有length)或者size没有传值size小于1就返回空数组
if (!arr.length || !size || size < 1) {
return []
}
let start = 0
let end = 0
let result: T[][] = []
for (let i = 0; i < Math.ceil(arr.length / size); i++) {
start = i * size
end = start + size
result.push(arr.slice(start, end))
}
return result
}
//对象深拷贝
export function deepClone<T extends Object>(target: T): T {
// 如果是对象且不是原始值null
if (typeof target === 'object' && target !== null) {
// 创建容器
const result: ESObject = Array.isArray(target) ? [] : {};
const keys = Object.keys(target) as Array<keyof T>; //注解二
keys.forEach(key => {
Reflect.set(result, key, deepClone(Reflect.get(target, key)));
});
return result;
}
// 如果是原始值,则直接返回
return target;
}
//经纬度转换
export function convertGpsCoord2(num: number): number {
const tempNum = Math.floor(num);
const du = Math.floor(tempNum / 100);
const fen = tempNum % 100 + num - tempNum;
return du + fen / 60
}

View File

@ -96,6 +96,12 @@ export function NumberToByteArray(number: number | string, len: number): number[
return arrBytes; return arrBytes;
} }
/*
* 将字符串填充为指定长度的字符串前面补0
* @param str 要填充的字符串或数字
* @param len 目标长度
* @return 返回填充后的字符串
*/
export function FillZero(str: string | number, len: number): string { export function FillZero(str: string | number, len: number): string {
str = str.toString(); str = str.toString();
if (str.length >= len || len <= 0) { if (str.length >= len || len <= 0) {
@ -103,4 +109,50 @@ export function FillZero(str: string | number, len: number): string {
} }
const zeroStr = '0'.repeat(len - str.length); const zeroStr = '0'.repeat(len - str.length);
return zeroStr + str; return zeroStr + str;
}
/**
* 将数组按指定大小分块
* @param arr 原始数组
* @param size 每块大小(必须 >= 1
* @returns 分块后的二维数组
*/
export function CutArray<T>(arr: readonly T[], size: number): T[][] {
if (!Array.isArray(arr) || size < 1) return [];
const result: T[][] = [];
for (let i = 0; i < arr.length; i += size) {
result.push(arr.slice(i, i + size));
}
return result;
}
/**
* 将GPS坐标从度分格式(DDMM.MMMM)转换为十进制度格式
* @param ddmm - 度分格式坐标(例如 11630.1234 表示116度30.1234分)
* @returns 十进制度格式坐标(例如 116.502056666...)
*/
export function ConvertDdmmToDecimalDegrees(ddmm: number): number {
// 提取度数部分除以100后取整
const degrees = Math.floor(ddmm / 100);
// 提取分数部分包含小数对100取模
const minutes = ddmm % 100;
// 将分数转换为度并相加1度=60分
return degrees + minutes / 60;
}
//对象深拷贝
export function DeepClone<T extends Object>(target: T): T {
// 如果是对象且不是原始值null
if (typeof target === 'object' && target !== null) {
// 创建容器
const result: ESObject = Array.isArray(target) ? [] : {};
const keys = Object.keys(target) as Array<keyof T>; //注解二
keys.forEach(key => {
Reflect.set(result, key, DeepClone(Reflect.get(target, key)));
});
return result;
}
// 如果是原始值,则直接返回
return target;
} }