refactor
This commit is contained in:
parent
42ae085631
commit
c31f542613
|
@ -21,7 +21,7 @@ export default class BackupExtension extends BackupExtensionAbility {
|
||||||
if (bundleVersion.name.startsWith("0.0.0.0")) {
|
if (bundleVersion.name.startsWith("0.0.0.0")) {
|
||||||
// 在此处实现终端设备从HarmonyOS 4.0升级到HarmonyOS NEXT后,应用数据的转换和迁移
|
// 在此处实现终端设备从HarmonyOS 4.0升级到HarmonyOS NEXT后,应用数据的转换和迁移
|
||||||
// 涉及异步操作请进行同步等待
|
// 涉及异步操作请进行同步等待
|
||||||
this.restoreData()
|
await this.restoreData()
|
||||||
console.log(TAG, `HarmonyOS to HarmonyOS NEXT scenario`);
|
console.log(TAG, `HarmonyOS to HarmonyOS NEXT scenario`);
|
||||||
} else {
|
} else {
|
||||||
// 在此处实现从HarmonyOS NEXT设备迁移到HarmonyOS NEXT设备后,应用数据的处理。无特殊要求,可以空实现
|
// 在此处实现从HarmonyOS NEXT设备迁移到HarmonyOS NEXT设备后,应用数据的处理。无特殊要求,可以空实现
|
||||||
|
@ -35,11 +35,17 @@ export default class BackupExtension extends BackupExtensionAbility {
|
||||||
const mSpTransferManager = new SpTransferManager()
|
const mSpTransferManager = new SpTransferManager()
|
||||||
const mMmkvTransferManager = new MmkvTransferManager()
|
const mMmkvTransferManager = new MmkvTransferManager()
|
||||||
|
|
||||||
const mUserDataTransfer = new UserDataTransfer(context, mMmkvTransferManager, mSpTransferManager)
|
new UserDataTransfer({
|
||||||
const mSettingDataTransfer = new SettingDataTransfer(context, mMmkvTransferManager, mSpTransferManager)
|
context: context,
|
||||||
|
mMmkvTransferManager: mMmkvTransferManager,
|
||||||
|
mSpTransferManager: mSpTransferManager,
|
||||||
|
}).execute()
|
||||||
|
new SettingDataTransfer({
|
||||||
|
context: context,
|
||||||
|
mMmkvTransferManager: mMmkvTransferManager,
|
||||||
|
mSpTransferManager: mSpTransferManager,
|
||||||
|
}).execute()
|
||||||
|
|
||||||
mUserDataTransfer.execute()
|
|
||||||
mSettingDataTransfer.execute()
|
|
||||||
|
|
||||||
// const databasePath = `/data/storage/el2/backup/restore/${BundleName}` + '/ce/databases/qingsdk2.db'
|
// const databasePath = `/data/storage/el2/backup/restore/${BundleName}` + '/ce/databases/qingsdk2.db'
|
||||||
// console.log(TAG, '1' + fs.statSync(databasePath).size)
|
// console.log(TAG, '1' + fs.statSync(databasePath).size)
|
||||||
|
|
|
@ -73,7 +73,7 @@ export class MmkvTransferManager extends BaseTransferManager {
|
||||||
// mmkv文件名
|
// mmkv文件名
|
||||||
let mmapID: string = 'source_mmkv';
|
let mmapID: string = 'source_mmkv';
|
||||||
// 秘钥,根据实际情况指定秘钥, 当前未设置秘钥
|
// 秘钥,根据实际情况指定秘钥, 当前未设置秘钥
|
||||||
let crpKey: string = '44456a255e333467';
|
let crpKey: string = this.crpKey;
|
||||||
mmkv = MMKV.getBackedUpMMKVWithID(mmapID, MMKV.SINGLE_PROCESS_MODE, crpKey, '');
|
mmkv = MMKV.getBackedUpMMKVWithID(mmapID, MMKV.SINGLE_PROCESS_MODE, crpKey, '');
|
||||||
|
|
||||||
// 注:当前mmkv对于双框架mmkv设置的int long float的类型暂不支持,待后续更新
|
// 注:当前mmkv对于双框架mmkv设置的int long float的类型暂不支持,待后续更新
|
||||||
|
@ -98,7 +98,7 @@ export class MmkvTransferManager extends BaseTransferManager {
|
||||||
/**
|
/**
|
||||||
* 解析双框架 单个mmkv文件
|
* 解析双框架 单个mmkv文件
|
||||||
*/
|
*/
|
||||||
parseSingleSourceMmkvInfo(context: common.Context, mmapID: string): MMKV {
|
getSourceMmkv(context: common.Context, mmapID: string): MMKV {
|
||||||
let mmkv: MMKV;
|
let mmkv: MMKV;
|
||||||
const rootPath = `/data/storage/el2/backup/restore/${this.BundleName}/ce/files/mmkv`;
|
const rootPath = `/data/storage/el2/backup/restore/${this.BundleName}/ce/files/mmkv`;
|
||||||
// const rootPath = context.filesDir + '/mmkv'
|
// const rootPath = context.filesDir + '/mmkv'
|
||||||
|
@ -124,7 +124,7 @@ export class MmkvTransferManager extends BaseTransferManager {
|
||||||
// mmkv文件名
|
// mmkv文件名
|
||||||
let mmapID: string = 'target_mmkv';
|
let mmapID: string = 'target_mmkv';
|
||||||
// 秘钥,根据实际情况指定秘钥, 当前未设置秘钥
|
// 秘钥,根据实际情况指定秘钥, 当前未设置秘钥
|
||||||
let crpKey: string = '44456a255e333467';
|
let crpKey: string = this.crpKey
|
||||||
mmkv = MMKV.getMMKVWithMMapID(mmapID, MMKV.SINGLE_PROCESS_MODE, crpKey, '');
|
mmkv = MMKV.getMMKVWithMMapID(mmapID, MMKV.SINGLE_PROCESS_MODE, crpKey, '');
|
||||||
|
|
||||||
// 设置请求参数中的值
|
// 设置请求参数中的值
|
||||||
|
@ -161,7 +161,7 @@ export class MmkvTransferManager extends BaseTransferManager {
|
||||||
// mmkv文件名
|
// mmkv文件名
|
||||||
let mmapID: string = 'target_mmkv';
|
let mmapID: string = 'target_mmkv';
|
||||||
// 秘钥,根据实际情况指定秘钥, 当前未设置秘钥
|
// 秘钥,根据实际情况指定秘钥, 当前未设置秘钥
|
||||||
let crpKey: string = '44456a255e333467';
|
let crpKey: string = this.crpKey
|
||||||
mmkv = MMKV.getMMKVWithMMapID(mmapID, MMKV.SINGLE_PROCESS_MODE, crpKey, '');
|
mmkv = MMKV.getMMKVWithMMapID(mmapID, MMKV.SINGLE_PROCESS_MODE, crpKey, '');
|
||||||
|
|
||||||
// 取值,根据mmkv中实际存在的key来取值
|
// 取值,根据mmkv中实际存在的key来取值
|
||||||
|
|
|
@ -2,22 +2,26 @@ import { common } from '@kit.AbilityKit'
|
||||||
import { MmkvTransferManager } from '../backupTransferManagers/MmkvTransferManager'
|
import { MmkvTransferManager } from '../backupTransferManagers/MmkvTransferManager'
|
||||||
import { SpTransferManager } from '../backupTransferManagers/SpTransferManager'
|
import { SpTransferManager } from '../backupTransferManagers/SpTransferManager'
|
||||||
|
|
||||||
|
export interface ISettingDataTransferProps {
|
||||||
|
context: common.Context,
|
||||||
|
mMmkvTransferManager: MmkvTransferManager,
|
||||||
|
mSpTransferManager: SpTransferManager,
|
||||||
|
}
|
||||||
|
|
||||||
export class SettingDataTransfer {
|
export class SettingDataTransfer {
|
||||||
private TAG = 'SettingDataTransfer'
|
private TAG = 'SettingDataTransfer'
|
||||||
private mMmkvTransferManager: MmkvTransferManager
|
private mMmkvTransferManager: MmkvTransferManager
|
||||||
private mSpTransferManager: SpTransferManager
|
private mSpTransferManager: SpTransferManager
|
||||||
private context: common.Context
|
private context: common.Context
|
||||||
|
|
||||||
constructor(context: common.Context, mMmkvTransferManager: MmkvTransferManager,
|
constructor(props: ISettingDataTransferProps) {
|
||||||
mSpTransferManager: SpTransferManager) {
|
this.context = props.context
|
||||||
this.context = context
|
this.mMmkvTransferManager = props.mMmkvTransferManager
|
||||||
|
this.mSpTransferManager = props.mSpTransferManager
|
||||||
this.mSpTransferManager = mSpTransferManager
|
|
||||||
this.mMmkvTransferManager = mMmkvTransferManager
|
|
||||||
}
|
}
|
||||||
|
|
||||||
execute() {
|
execute() {
|
||||||
const mmkv_public_default = this.mMmkvTransferManager.parseSingleSourceMmkvInfo(this.context, 'public_default')
|
const mmkv_public_default = this.mMmkvTransferManager.getSourceMmkv(this.context, 'public_default')
|
||||||
const fileradar_auto_upload_only_wifi_USERID = mmkv_public_default?.decodeBool('fileradar_auto_upload_only_wifi_USERID', false)
|
const fileradar_auto_upload_only_wifi_USERID = mmkv_public_default?.decodeBool('fileradar_auto_upload_only_wifi_USERID', false)
|
||||||
|
|
||||||
const sp_auto_upload_switch = this.mSpTransferManager.getDataFromSourceSp(this.context, 'auto_upload_switch.xml')
|
const sp_auto_upload_switch = this.mSpTransferManager.getDataFromSourceSp(this.context, 'auto_upload_switch.xml')
|
||||||
|
@ -27,7 +31,7 @@ export class SettingDataTransfer {
|
||||||
for (const key of sp_auto_upload_switch.keys()) {
|
for (const key of sp_auto_upload_switch.keys()) {
|
||||||
const userId = key
|
const userId = key
|
||||||
const auto_upload_switch: boolean = sp_auto_upload_switch.get(userId) as boolean
|
const auto_upload_switch: boolean = sp_auto_upload_switch.get(userId) as boolean
|
||||||
spMap.set(`${userId}/docSync`, auto_upload_switch)
|
spMap.set(`${userId}/docSync`, !!auto_upload_switch)
|
||||||
spMap.set(`${userId}/uploadOnlyWlan`, fileradar_auto_upload_only_wifi_USERID)
|
spMap.set(`${userId}/uploadOnlyWlan`, fileradar_auto_upload_only_wifi_USERID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,29 +4,34 @@ import { buffer } from '@kit.ArkTS'
|
||||||
import { MmkvTransferManager } from '../backupTransferManagers/MmkvTransferManager'
|
import { MmkvTransferManager } from '../backupTransferManagers/MmkvTransferManager'
|
||||||
import { SpTransferManager } from '../backupTransferManagers/SpTransferManager'
|
import { SpTransferManager } from '../backupTransferManagers/SpTransferManager'
|
||||||
|
|
||||||
|
export interface IUserDataTransferProps {
|
||||||
|
context: common.Context,
|
||||||
|
mMmkvTransferManager: MmkvTransferManager,
|
||||||
|
mSpTransferManager: SpTransferManager,
|
||||||
|
}
|
||||||
|
|
||||||
export class UserDataTransfer {
|
export class UserDataTransfer {
|
||||||
private TAG = 'UserDataTransfer'
|
private TAG = 'UserDataTransfer'
|
||||||
private mMmkvTransferManager: MmkvTransferManager
|
private mMmkvTransferManager: MmkvTransferManager
|
||||||
private mSpTransferManager: SpTransferManager
|
private mSpTransferManager: SpTransferManager
|
||||||
private context: common.Context
|
private context: common.Context
|
||||||
|
|
||||||
constructor(context: common.Context, mMmkvTransferManager: MmkvTransferManager,
|
constructor(props: IUserDataTransferProps) {
|
||||||
mSpTransferManager: SpTransferManager) {
|
this.context = props.context
|
||||||
this.context = context
|
this.mMmkvTransferManager = props.mMmkvTransferManager
|
||||||
this.mMmkvTransferManager = mMmkvTransferManager
|
this.mSpTransferManager = props.mSpTransferManager
|
||||||
this.mSpTransferManager = mSpTransferManager
|
|
||||||
}
|
}
|
||||||
|
|
||||||
execute() {
|
execute() {
|
||||||
// 读取mmkv
|
// 1. read mmkv
|
||||||
const mmkv_login_config = this.mMmkvTransferManager.parseSingleSourceMmkvInfo(this.context, 'login_config')
|
const mmkv_login_config = this.mMmkvTransferManager.getSourceMmkv(this.context, 'login_config')
|
||||||
|
|
||||||
// console.log(this.TAG, 'keys ' + mmkv_login_config.getAllKeys())
|
// console.log(this.TAG, 'keys ' + mmkv_login_config.getAllKeys())
|
||||||
|
|
||||||
const CLOUD_QING_USER_WORKSPACE = mmkv_login_config.decodeString('CLOUD_QING_USER_WORKSPACE', '')
|
const CLOUD_QING_USER_WORKSPACE = mmkv_login_config.decodeString('CLOUD_QING_USER_WORKSPACE', '')
|
||||||
const CLOUD_QING_USER_ID = mmkv_login_config.decodeString('CLOUD_QING_USER_ID', '')
|
const CLOUD_QING_USER_ID = mmkv_login_config.decodeString('CLOUD_QING_USER_ID', '')
|
||||||
const CLOUD_QING_USER_WORKSPACE_INFO_xxxx = mmkv_login_config.decodeString('CLOUD_QING_USER_WORKSPACE_INFO_41000207', '') // company id
|
const CLOUD_QING_USER_WORKSPACE_INFO_xxxx = mmkv_login_config.decodeString('CLOUD_QING_USER_WORKSPACE_INFO_41000207', '') // company id
|
||||||
const CLOUD_QING_SESSION = mmkv_login_config.decodeString('CLOUD_QING_SESSION', '')
|
const CLOUD_QING_SESSION = mmkv_login_config.decodeString('CLOUD_QING_SESSION', '') // user session
|
||||||
const CLOUD_QING_ACCOUNT_TYPE = mmkv_login_config.decodeString('CLOUD_QING_ACCOUNT_TYPE', '')
|
const CLOUD_QING_ACCOUNT_TYPE = mmkv_login_config.decodeString('CLOUD_QING_ACCOUNT_TYPE', '')
|
||||||
const CLOUD_QING_WPS_USERINFO = mmkv_login_config.decodeString('CLOUD_QING_WPS_USERINFO', '')
|
const CLOUD_QING_WPS_USERINFO = mmkv_login_config.decodeString('CLOUD_QING_WPS_USERINFO', '')
|
||||||
const CLOUD_LOGIN_USERS = mmkv_login_config.decodeString('CLOUD_LOGIN_USERS', '')
|
const CLOUD_LOGIN_USERS = mmkv_login_config.decodeString('CLOUD_LOGIN_USERS', '')
|
||||||
|
@ -46,50 +51,23 @@ export class UserDataTransfer {
|
||||||
|
|
||||||
mmkv_login_config.close()
|
mmkv_login_config.close()
|
||||||
|
|
||||||
// process data
|
// 2. process data
|
||||||
// get session
|
const sessionStr = this.getSession(CLOUD_QING_SESSION)
|
||||||
const sessionRow = CLOUD_QING_SESSION
|
|
||||||
const sessionJsonStr = buffer.from(sessionRow, "base64").toString("utf-8");
|
|
||||||
const sessionJsonObj: Record<string, string | number | boolean | object> = JSON.parse(sessionJsonStr)
|
|
||||||
const sessionStr = this.session2String(sessionJsonObj)
|
|
||||||
// console.log(this.TAG, 'sessionStr ' + JSON.stringify(sessionStr))
|
// console.log(this.TAG, 'sessionStr ' + JSON.stringify(sessionStr))
|
||||||
// get userInfo
|
const currentUserInfoStr = this.getCurrentUserInfo(CLOUD_LOGIN_USERS)
|
||||||
const loginUsers: Record<string, string | number | boolean>[] = JSON.parse(CLOUD_LOGIN_USERS)
|
|
||||||
const currentUser = loginUsers.find(user => user['is_current']) ?? {}
|
|
||||||
// console.log(this.TAG, 'currentUser ' + JSON.stringify(currentUser))
|
// console.log(this.TAG, 'currentUser ' + JSON.stringify(currentUser))
|
||||||
const userInfo: Record<string, preferences.ValueType> = {
|
const multiAccount: Record<string, preferences.ValueType> = {
|
||||||
"account": "",
|
'login_limit_account': login_limit_account,
|
||||||
"account_num": 1,
|
|
||||||
"address": "",
|
|
||||||
"city": "",
|
|
||||||
"companyid": currentUser['company_id'],
|
|
||||||
"country": "",
|
|
||||||
"current_companyid": 0,
|
|
||||||
"departmentid": "",
|
|
||||||
"email": "",
|
|
||||||
"firstname": "",
|
|
||||||
"is_company_account": currentUser['is_company_account'],
|
|
||||||
"is_plus": true,
|
|
||||||
"lastname": "",
|
|
||||||
"loginmode": "phone_sms:+86***862",
|
|
||||||
"nickname": user_name,
|
|
||||||
"phonenumber": "130***862",
|
|
||||||
"pic": user_avatar_url,
|
|
||||||
"postal": "",
|
|
||||||
"province": "",
|
|
||||||
"regtime": 1566368398,
|
|
||||||
"result": "ok",
|
"result": "ok",
|
||||||
"role": ["user"],
|
'users': JSON.parse(CLOUD_LOGIN_USERS)
|
||||||
"sex": "male",
|
|
||||||
"status": "active",
|
|
||||||
"userid": user_id,
|
|
||||||
"uzone": "CN-HN"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// write into preferences
|
// 3. write into preferences
|
||||||
const accountSpData: Map<string, preferences.ValueType> = new Map()
|
const accountSpData: Map<string, preferences.ValueType> = new Map()
|
||||||
accountSpData.set('session', sessionStr)
|
accountSpData.set('session', sessionStr)
|
||||||
accountSpData.set('userInfo', JSON.stringify(userInfo))
|
accountSpData.set('userInfo', currentUserInfoStr)
|
||||||
|
accountSpData.set('multiAccount', JSON.stringify(multiAccount))
|
||||||
|
|
||||||
const authSpData = new Map<string, preferences.ValueType>()
|
const authSpData = new Map<string, preferences.ValueType>()
|
||||||
authSpData.set('auth_policy', true)
|
authSpData.set('auth_policy', true)
|
||||||
|
|
||||||
|
@ -97,6 +75,14 @@ export class UserDataTransfer {
|
||||||
this.mSpTransferManager.insertDataToTargetSp(this.context, 'auth', authSpData)
|
this.mSpTransferManager.insertDataToTargetSp(this.context, 'auth', authSpData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getSession(sessionRow: string) {
|
||||||
|
const sessionJsonStr = buffer.from(sessionRow, "base64").toString("utf-8");
|
||||||
|
const sessionJsonObj: Record<string, string | number | boolean | object> = JSON.parse(sessionJsonStr)
|
||||||
|
const sessionStr = this.session2String(sessionJsonObj)
|
||||||
|
|
||||||
|
return sessionStr
|
||||||
|
}
|
||||||
|
|
||||||
private session2String(session: Record<string, string | number | boolean | object>) {
|
private session2String(session: Record<string, string | number | boolean | object>) {
|
||||||
const authkeypair: Record<string, string> = session['authkeypair'] as Record<string, string>
|
const authkeypair: Record<string, string> = session['authkeypair'] as Record<string, string>
|
||||||
session['accessid'] = authkeypair['access_id']
|
session['accessid'] = authkeypair['access_id']
|
||||||
|
@ -110,4 +96,20 @@ export class UserDataTransfer {
|
||||||
|
|
||||||
return strList.join('; ')
|
return strList.join('; ')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getCurrentUserInfo(loginUsersStr: string) {
|
||||||
|
const loginUsers: Record<string, string | number | boolean>[] = JSON.parse(loginUsersStr)
|
||||||
|
const currentUser = loginUsers.find(user => user['is_current']) ?? {}
|
||||||
|
|
||||||
|
const currentUserInfo = this.precessUserInfo(currentUser)
|
||||||
|
|
||||||
|
return JSON.stringify(currentUserInfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
private precessUserInfo(userInfo: Record<string, preferences.ValueType>) {
|
||||||
|
userInfo['pic'] = userInfo['avatar_url']
|
||||||
|
userInfo['companyid'] = userInfo['company_id']
|
||||||
|
|
||||||
|
return userInfo
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user