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")) {
|
||||
// 在此处实现终端设备从HarmonyOS 4.0升级到HarmonyOS NEXT后,应用数据的转换和迁移
|
||||
// 涉及异步操作请进行同步等待
|
||||
this.restoreData()
|
||||
await this.restoreData()
|
||||
console.log(TAG, `HarmonyOS to HarmonyOS NEXT scenario`);
|
||||
} else {
|
||||
// 在此处实现从HarmonyOS NEXT设备迁移到HarmonyOS NEXT设备后,应用数据的处理。无特殊要求,可以空实现
|
||||
|
@ -35,11 +35,17 @@ export default class BackupExtension extends BackupExtensionAbility {
|
|||
const mSpTransferManager = new SpTransferManager()
|
||||
const mMmkvTransferManager = new MmkvTransferManager()
|
||||
|
||||
const mUserDataTransfer = new UserDataTransfer(context, mMmkvTransferManager, mSpTransferManager)
|
||||
const mSettingDataTransfer = new SettingDataTransfer(context, mMmkvTransferManager, mSpTransferManager)
|
||||
new UserDataTransfer({
|
||||
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'
|
||||
// console.log(TAG, '1' + fs.statSync(databasePath).size)
|
||||
|
@ -128,4 +134,4 @@ export default class BackupExtension extends BackupExtensionAbility {
|
|||
// const databasePath = backupDir + '/ce/databases/qingsdk2.db'
|
||||
// fs.copyFileSync(databasePath, DatabaseDir + '/qingsdk2.db')
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -73,7 +73,7 @@ export class MmkvTransferManager extends BaseTransferManager {
|
|||
// 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设置的int long float的类型暂不支持,待后续更新
|
||||
|
@ -98,7 +98,7 @@ export class MmkvTransferManager extends BaseTransferManager {
|
|||
/**
|
||||
* 解析双框架 单个mmkv文件
|
||||
*/
|
||||
parseSingleSourceMmkvInfo(context: common.Context, mmapID: string): MMKV {
|
||||
getSourceMmkv(context: common.Context, mmapID: string): MMKV {
|
||||
let mmkv: MMKV;
|
||||
const rootPath = `/data/storage/el2/backup/restore/${this.BundleName}/ce/files/mmkv`;
|
||||
// const rootPath = context.filesDir + '/mmkv'
|
||||
|
@ -124,7 +124,7 @@ export class MmkvTransferManager extends BaseTransferManager {
|
|||
// mmkv文件名
|
||||
let mmapID: string = 'target_mmkv';
|
||||
// 秘钥,根据实际情况指定秘钥, 当前未设置秘钥
|
||||
let crpKey: string = '44456a255e333467';
|
||||
let crpKey: string = this.crpKey
|
||||
mmkv = MMKV.getMMKVWithMMapID(mmapID, MMKV.SINGLE_PROCESS_MODE, crpKey, '');
|
||||
|
||||
// 设置请求参数中的值
|
||||
|
@ -161,7 +161,7 @@ export class MmkvTransferManager extends BaseTransferManager {
|
|||
// 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中实际存在的key来取值
|
||||
|
|
|
@ -2,22 +2,26 @@ import { common } from '@kit.AbilityKit'
|
|||
import { MmkvTransferManager } from '../backupTransferManagers/MmkvTransferManager'
|
||||
import { SpTransferManager } from '../backupTransferManagers/SpTransferManager'
|
||||
|
||||
export interface ISettingDataTransferProps {
|
||||
context: common.Context,
|
||||
mMmkvTransferManager: MmkvTransferManager,
|
||||
mSpTransferManager: SpTransferManager,
|
||||
}
|
||||
|
||||
export class SettingDataTransfer {
|
||||
private TAG = 'SettingDataTransfer'
|
||||
private mMmkvTransferManager: MmkvTransferManager
|
||||
private mSpTransferManager: SpTransferManager
|
||||
private context: common.Context
|
||||
|
||||
constructor(context: common.Context, mMmkvTransferManager: MmkvTransferManager,
|
||||
mSpTransferManager: SpTransferManager) {
|
||||
this.context = context
|
||||
|
||||
this.mSpTransferManager = mSpTransferManager
|
||||
this.mMmkvTransferManager = mMmkvTransferManager
|
||||
constructor(props: ISettingDataTransferProps) {
|
||||
this.context = props.context
|
||||
this.mMmkvTransferManager = props.mMmkvTransferManager
|
||||
this.mSpTransferManager = props.mSpTransferManager
|
||||
}
|
||||
|
||||
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 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()) {
|
||||
const userId = key
|
||||
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)
|
||||
}
|
||||
|
||||
|
|
|
@ -4,29 +4,34 @@ import { buffer } from '@kit.ArkTS'
|
|||
import { MmkvTransferManager } from '../backupTransferManagers/MmkvTransferManager'
|
||||
import { SpTransferManager } from '../backupTransferManagers/SpTransferManager'
|
||||
|
||||
export interface IUserDataTransferProps {
|
||||
context: common.Context,
|
||||
mMmkvTransferManager: MmkvTransferManager,
|
||||
mSpTransferManager: SpTransferManager,
|
||||
}
|
||||
|
||||
export class UserDataTransfer {
|
||||
private TAG = 'UserDataTransfer'
|
||||
private mMmkvTransferManager: MmkvTransferManager
|
||||
private mSpTransferManager: SpTransferManager
|
||||
private context: common.Context
|
||||
|
||||
constructor(context: common.Context, mMmkvTransferManager: MmkvTransferManager,
|
||||
mSpTransferManager: SpTransferManager) {
|
||||
this.context = context
|
||||
this.mMmkvTransferManager = mMmkvTransferManager
|
||||
this.mSpTransferManager = mSpTransferManager
|
||||
constructor(props: IUserDataTransferProps) {
|
||||
this.context = props.context
|
||||
this.mMmkvTransferManager = props.mMmkvTransferManager
|
||||
this.mSpTransferManager = props.mSpTransferManager
|
||||
}
|
||||
|
||||
execute() {
|
||||
// 读取mmkv
|
||||
const mmkv_login_config = this.mMmkvTransferManager.parseSingleSourceMmkvInfo(this.context, 'login_config')
|
||||
// 1. read mmkv
|
||||
const mmkv_login_config = this.mMmkvTransferManager.getSourceMmkv(this.context, 'login_config')
|
||||
|
||||
// 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_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_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_WPS_USERINFO = mmkv_login_config.decodeString('CLOUD_QING_WPS_USERINFO', '')
|
||||
const CLOUD_LOGIN_USERS = mmkv_login_config.decodeString('CLOUD_LOGIN_USERS', '')
|
||||
|
@ -46,50 +51,23 @@ export class UserDataTransfer {
|
|||
|
||||
mmkv_login_config.close()
|
||||
|
||||
// process data
|
||||
// get 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)
|
||||
// 2. process data
|
||||
const sessionStr = this.getSession(CLOUD_QING_SESSION)
|
||||
// console.log(this.TAG, 'sessionStr ' + JSON.stringify(sessionStr))
|
||||
// get userInfo
|
||||
const loginUsers: Record<string, string | number | boolean>[] = JSON.parse(CLOUD_LOGIN_USERS)
|
||||
const currentUser = loginUsers.find(user => user['is_current']) ?? {}
|
||||
const currentUserInfoStr = this.getCurrentUserInfo(CLOUD_LOGIN_USERS)
|
||||
// console.log(this.TAG, 'currentUser ' + JSON.stringify(currentUser))
|
||||
const userInfo: Record<string, preferences.ValueType> = {
|
||||
"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,
|
||||
const multiAccount: Record<string, preferences.ValueType> = {
|
||||
'login_limit_account': login_limit_account,
|
||||
"result": "ok",
|
||||
"role": ["user"],
|
||||
"sex": "male",
|
||||
"status": "active",
|
||||
"userid": user_id,
|
||||
"uzone": "CN-HN"
|
||||
'users': JSON.parse(CLOUD_LOGIN_USERS)
|
||||
}
|
||||
|
||||
// write into preferences
|
||||
// 3. write into preferences
|
||||
const accountSpData: Map<string, preferences.ValueType> = new Map()
|
||||
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>()
|
||||
authSpData.set('auth_policy', true)
|
||||
|
||||
|
@ -97,6 +75,14 @@ export class UserDataTransfer {
|
|||
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>) {
|
||||
const authkeypair: Record<string, string> = session['authkeypair'] as Record<string, string>
|
||||
session['accessid'] = authkeypair['access_id']
|
||||
|
@ -110,4 +96,20 @@ export class UserDataTransfer {
|
|||
|
||||
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