xugenyuan

ref |> 解决未登录状态无法保存设置中的开关状态问题

1 import data_preferences from '@ohos.data.preferences'; 1 import data_preferences from '@ohos.data.preferences';
2 import { Logger } from './Logger'; 2 import { Logger } from './Logger';
  3 +import { JSON } from '@kit.ArkTS';
3 4
4 const TAG = 'SPHelper' 5 const TAG = 'SPHelper'
5 6
  7 +const logEnable = false
  8 +
6 /** 9 /**
7 * sp存储,单条数据,value<1k;业务数据超过1k的,建议使用KVStoreHelper 10 * sp存储,单条数据,value<1k;业务数据超过1k的,建议使用KVStoreHelper
8 */ 11 */
@@ -12,10 +15,14 @@ export class SPHelper { @@ -12,10 +15,14 @@ export class SPHelper {
12 15
13 static init(context: Context) { 16 static init(context: Context) {
14 SPHelper.context = context; 17 SPHelper.context = context;
  18 + if (context) {
  19 + Logger.debug(TAG, '初始化context')
  20 + }
15 } 21 }
16 22
17 static setSpFilename(spFilename: string) { 23 static setSpFilename(spFilename: string) {
18 SPHelper.spFilename = spFilename; 24 SPHelper.spFilename = spFilename;
  25 + Logger.debug(TAG, '设置文件名: ' + spFilename)
19 } 26 }
20 27
21 // 静态属性 28 // 静态属性
@@ -37,47 +44,75 @@ export class SPHelper { @@ -37,47 +44,75 @@ export class SPHelper {
37 } 44 }
38 45
39 async save(key: string, value: data_preferences.ValueType) { 46 async save(key: string, value: data_preferences.ValueType) {
40 - const preferences: data_preferences.Preferences = await this.getVideoPreferences();  
41 - await preferences.put(key, value)  
42 - await preferences.flush() 47 + try {
  48 + const preferences: data_preferences.Preferences = await this.getVideoPreferences();
  49 + await preferences.put(key, value)
  50 + await preferences.flush()
  51 + if (logEnable) {
  52 + Logger.debug(TAG, '保存 key: ' + key + " value => " + value)
  53 + }
  54 + } catch (e) {
  55 + Logger.error(TAG, '保存 key: ' + key + " value => " + value + " 报错:" + JSON.stringify(e))
  56 + }
43 } 57 }
44 58
45 saveSync(key: string, value: data_preferences.ValueType) { 59 saveSync(key: string, value: data_preferences.ValueType) {
46 const preferences: data_preferences.Preferences = this.getVideoPreferencesSync(); 60 const preferences: data_preferences.Preferences = this.getVideoPreferencesSync();
47 preferences.putSync(key, value) 61 preferences.putSync(key, value)
48 preferences.flush().then(() => { 62 preferences.flush().then(() => {
49 - Logger.debug(TAG, 'saveSync flush success') 63 + if (logEnable) {
  64 + Logger.debug(TAG, 'sync保存 key: ' + key + " value => " + value)
  65 + }
50 }).catch((error: object) => { 66 }).catch((error: object) => {
51 - Logger.debug(TAG, 'saveSync flush failed: ' + JSON.stringify(error)) 67 + Logger.error(TAG, 'sync保存 key: ' + key + " value => " + value + " 报错:" + JSON.stringify(error))
52 }); 68 });
53 } 69 }
54 70
55 async get(key: string, defValue: data_preferences.ValueType): Promise<data_preferences.ValueType> { 71 async get(key: string, defValue: data_preferences.ValueType): Promise<data_preferences.ValueType> {
56 const preferences: data_preferences.Preferences = await this.getVideoPreferences(); 72 const preferences: data_preferences.Preferences = await this.getVideoPreferences();
57 - return await preferences.get(key, defValue); 73 + const data = await preferences.get(key, defValue);
  74 + if (logEnable) {
  75 + Logger.debug(TAG, '获取 key: ' + key + " value => " + data + "| 默认值:" + defValue)
  76 + }
  77 + return data
58 } 78 }
59 79
60 getSync(key: string, defValue: data_preferences.ValueType): data_preferences.ValueType { 80 getSync(key: string, defValue: data_preferences.ValueType): data_preferences.ValueType {
61 const preferences: data_preferences.Preferences = this.getVideoPreferencesSync(); 81 const preferences: data_preferences.Preferences = this.getVideoPreferencesSync();
62 - return preferences.getSync(key, defValue); 82 + const data = preferences.getSync(key, defValue);
  83 + if (logEnable) {
  84 + Logger.debug(TAG, 'sync获取 key: ' + key + " value => " + data + "| 默认值:" + defValue)
  85 + }
  86 + return data
63 } 87 }
64 88
65 async has(key: string): Promise<boolean> { 89 async has(key: string): Promise<boolean> {
66 const preferences: data_preferences.Preferences = await this.getVideoPreferences(); 90 const preferences: data_preferences.Preferences = await this.getVideoPreferences();
67 - return await preferences.has(key); 91 + const data = await preferences.has(key);
  92 + if (logEnable) {
  93 + Logger.debug(TAG, 'has key: ' + key + ' => ' + data)
  94 + }
  95 + return data
68 } 96 }
69 97
70 hasSync(key: string): boolean { 98 hasSync(key: string): boolean {
71 const preferences: data_preferences.Preferences = this.getVideoPreferencesSync(); 99 const preferences: data_preferences.Preferences = this.getVideoPreferencesSync();
72 - return preferences.hasSync(key); 100 + const data = preferences.hasSync(key);
  101 + if (logEnable) {
  102 + Logger.debug(TAG, 'synchas key: ' + key + ' => ' + data)
  103 + }
  104 + return data
73 } 105 }
74 106
75 async delete(key: string) { 107 async delete(key: string) {
76 const preferences: data_preferences.Preferences = await this.getVideoPreferences(); 108 const preferences: data_preferences.Preferences = await this.getVideoPreferences();
77 preferences.delete(key).then(async () => { 109 preferences.delete(key).then(async () => {
78 await preferences.flush(); 110 await preferences.flush();
  111 + if (logEnable) {
  112 + Logger.debug(TAG, '删除 key: ' + key)
  113 + }
79 }).catch((err: Error) => { 114 }).catch((err: Error) => {
80 - // Logger.error(TAG, 'Failed to delete the key. Cause: ' + err); 115 + Logger.error(TAG, '删除 key失败:' + JSON.stringify(err));
81 }); 116 });
82 } 117 }
83 118
@@ -85,9 +120,11 @@ export class SPHelper { @@ -85,9 +120,11 @@ export class SPHelper {
85 const preferences: data_preferences.Preferences = this.getVideoPreferencesSync(); 120 const preferences: data_preferences.Preferences = this.getVideoPreferencesSync();
86 preferences.deleteSync(key) 121 preferences.deleteSync(key)
87 preferences.flush().then(() => { 122 preferences.flush().then(() => {
88 - Logger.debug(TAG, 'deleteSync flush success') 123 + if (logEnable) {
  124 + Logger.debug(TAG, 'sync删除 key: ' + key)
  125 + }
89 }).catch((error: object) => { 126 }).catch((error: object) => {
90 - Logger.debug(TAG, 'deleteSync flush failed: ' + JSON.stringify(error)) 127 + Logger.error(TAG, 'sync删除 key失败:' + JSON.stringify(error));
91 }); 128 });
92 } 129 }
93 130
@@ -95,8 +132,9 @@ export class SPHelper { @@ -95,8 +132,9 @@ export class SPHelper {
95 this.getVideoPreferences().then(async (preferences: data_preferences.Preferences) => { 132 this.getVideoPreferences().then(async (preferences: data_preferences.Preferences) => {
96 preferences.clearSync() 133 preferences.clearSync()
97 await preferences.flush() 134 await preferences.flush()
  135 + Logger.debug(TAG, 'sync清除所有数据');
98 }).catch((err: Error) => { 136 }).catch((err: Error) => {
99 - // Logger.error(TAG, 'get the preferences failed, Cause: ' + err); 137 + Logger.error(TAG, 'sync清除所有数据,失败:' + JSON.stringify(err));
100 }); 138 });
101 } 139 }
102 140
1 import { SpConstants } from 'wdConstant'; 1 import { SpConstants } from 'wdConstant';
2 import { Logger, SPHelper, ToastUtils, EmitterEventId, EmitterUtils, DateTimeUtils, CustomToast } from 'wdKit'; 2 import { Logger, SPHelper, ToastUtils, EmitterEventId, EmitterUtils, DateTimeUtils, CustomToast } from 'wdKit';
3 import {MineMainSettingFunctionItem} from '../../viewmodel/MineMainSettingFunctionItem'; 3 import {MineMainSettingFunctionItem} from '../../viewmodel/MineMainSettingFunctionItem';
4 -import MineSettingDatasModel from '../../model/MineSettingDatasModel'; 4 +import { MineSettingDatasModel } from '../../model/MineSettingDatasModel';
5 import router from '@ohos.router'; 5 import router from '@ohos.router';
6 import { WDRouterPage, WDRouterRule } from 'wdRouter'; 6 import { WDRouterPage, WDRouterRule } from 'wdRouter';
7 import { Params } from 'wdBean'; 7 import { Params } from 'wdBean';
@@ -9,13 +9,15 @@ import { Params } from 'wdBean'; @@ -9,13 +9,15 @@ import { Params } from 'wdBean';
9 // import { common } from '@kit.AbilityKit'; 9 // import { common } from '@kit.AbilityKit';
10 import fs from '@ohos.file.fs'; 10 import fs from '@ohos.file.fs';
11 import { CustomCacheDialog } from './CustomCacheDialog'; 11 import { CustomCacheDialog } from './CustomCacheDialog';
12 -import MineSettingDatasModel from '../../model/MineSettingDatasModel'; 12 +import { MineSettingDatasModel } from '../../model/MineSettingDatasModel';
13 import { MineMainSettingFunctionItem } from '../../viewmodel/MineMainSettingFunctionItem'; 13 import { MineMainSettingFunctionItem } from '../../viewmodel/MineMainSettingFunctionItem';
14 import common from '@ohos.app.ability.common'; 14 import common from '@ohos.app.ability.common';
15 import dataPreferences from '@ohos.data.preferences'; 15 import dataPreferences from '@ohos.data.preferences';
16 import { TitleBackComponent } from './TitleBackComponent'; 16 import { TitleBackComponent } from './TitleBackComponent';
17 import { MyCustomDialog } from '../reusable/MyCustomDialog'; 17 import { MyCustomDialog } from '../reusable/MyCustomDialog';
18 import { TrackingButton, TrackConstants } from 'wdTracking/Index'; 18 import { TrackingButton, TrackConstants } from 'wdTracking/Index';
  19 +import { JSON } from '@kit.ArkTS';
  20 +import { HttpUtils } from 'wdNetwork/Index';
19 21
20 @Component 22 @Component
21 export struct MineSettingComponent { 23 export struct MineSettingComponent {
@@ -82,10 +84,9 @@ export struct MineSettingComponent { @@ -82,10 +84,9 @@ export struct MineSettingComponent {
82 84
83 async getSettingPageData() { 85 async getSettingPageData() {
84 let oldList = MineSettingDatasModel.getMineMainSettingFunctionItemData(); 86 let oldList = MineSettingDatasModel.getMineMainSettingFunctionItemData();
85 - let userId=await SPHelper.default.get(SpConstants.USER_ID,'') as string  
86 - if(userId==''){  
87 - this.listData=oldList.slice(1,oldList.length)  
88 - }else { 87 + if (!HttpUtils.isLogin()) {
  88 + this.listData = oldList.slice(1,oldList.length)
  89 + } else {
89 this.listData = oldList; 90 this.listData = oldList;
90 } 91 }
91 92
@@ -167,6 +168,7 @@ export struct MineSettingComponent { @@ -167,6 +168,7 @@ export struct MineSettingComponent {
167 .margin({ left: `${this.calcHeight(81)}lpx`, right: `${this.calcHeight(29)}lpx` }) 168 .margin({ left: `${this.calcHeight(81)}lpx`, right: `${this.calcHeight(29)}lpx` })
168 .selectedColor("#ED2800") 169 .selectedColor("#ED2800")
169 .onChange((isOn: boolean) => { 170 .onChange((isOn: boolean) => {
  171 + Logger.debug("SPHelper", "数据 : " + JSON.stringify(item))
170 if(item.itemType=='push_switch'){ 172 if(item.itemType=='push_switch'){
171 trackButtonClick("settingPagePushSwitch") 173 trackButtonClick("settingPagePushSwitch")
172 //推送 174 //推送
@@ -12,74 +12,49 @@ const TAG = "MineSettingDatasModel" @@ -12,74 +12,49 @@ const TAG = "MineSettingDatasModel"
12 /** 12 /**
13 * 我的设置页面 所有数据 获取封装类 13 * 我的设置页面 所有数据 获取封装类
14 */ 14 */
15 -class MineSettingDatasModel{  
16 - private static instance: MineSettingDatasModel;  
17 - mainSettingData:Array<MineMainSettingFunctionItem> = []  
18 - accountAndSecurityData:Array<MineMainSettingFunctionItem> = []  
19 -  
20 - private constructor() { }  
21 -  
22 - /**  
23 - * 单例模式  
24 - * @returns  
25 - */  
26 - public static getInstance(): MineSettingDatasModel {  
27 - if (!MineSettingDatasModel.instance) {  
28 - MineSettingDatasModel.instance = new MineSettingDatasModel();  
29 - }  
30 - return MineSettingDatasModel.instance;  
31 - }  
32 -  
33 -  
34 - 15 +export class MineSettingDatasModel {
35 16
36 /** 17 /**
37 * 评论 关注 收藏 等7个数据 18 * 评论 关注 收藏 等7个数据
38 * 包含名字和图标 19 * 包含名字和图标
39 */ 20 */
40 - getMineMainSettingFunctionItemData():MineMainSettingFunctionItem[]{  
41 - if(this.mainSettingData.length === 7){  
42 - return this.mainSettingData  
43 - }  
44 - this.mainSettingData = []  
45 - this.mainSettingData.push(new MineMainSettingFunctionItem(null, '账户与安全', '18888888888', 0, false,"account")) 21 + static getMineMainSettingFunctionItemData() {
  22 + let mainSettingData: MineMainSettingFunctionItem[] = []
  23 + mainSettingData.push(new MineMainSettingFunctionItem(null, '账户与安全', '18888888888', 0, false,"account"))
46 let pushState=SPHelper.default.getSync(SpConstants.SETTING_PUSH_SWITCH,false) as boolean 24 let pushState=SPHelper.default.getSync(SpConstants.SETTING_PUSH_SWITCH,false) as boolean
47 - this.mainSettingData.push(new MineMainSettingFunctionItem(null, '接收推送', null, 1, pushState,"push_switch"))  
48 - this.mainSettingData.push(new MineMainSettingFunctionItem(null, '隐私设罝', null, 0, false,"private_setting")) 25 + mainSettingData.push(new MineMainSettingFunctionItem(null, '接收推送', null, 1, pushState,"push_switch"))
  26 + mainSettingData.push(new MineMainSettingFunctionItem(null, '隐私设罝', null, 0, false,"private_setting"))
49 let wifiState=SPHelper.default.getSync(SpConstants.SETTING_WIFI_IMAGE_SWITCH,false) as boolean 27 let wifiState=SPHelper.default.getSync(SpConstants.SETTING_WIFI_IMAGE_SWITCH,false) as boolean
50 - this.mainSettingData.push(new MineMainSettingFunctionItem(null, '仅wifi网络加载图片', null, 1, wifiState,"wifi_switch")) 28 + mainSettingData.push(new MineMainSettingFunctionItem(null, '仅wifi网络加载图片', null, 1, wifiState,"wifi_switch"))
51 let videoState=SPHelper.default.getSync(SpConstants.SETTING_WIFI_VIDEO_SWITCH,false) as boolean 29 let videoState=SPHelper.default.getSync(SpConstants.SETTING_WIFI_VIDEO_SWITCH,false) as boolean
52 - this.mainSettingData.push(new MineMainSettingFunctionItem(null, 'wifi网络情况下自动播放视频', null, 1, videoState,"video_switch")) 30 + mainSettingData.push(new MineMainSettingFunctionItem(null, 'wifi网络情况下自动播放视频', null, 1, videoState,"video_switch"))
53 let suspensionState=SPHelper.default.getSync(SpConstants.SETTING_SUSPENSION_SWITCH,false) as boolean 31 let suspensionState=SPHelper.default.getSync(SpConstants.SETTING_SUSPENSION_SWITCH,false) as boolean
54 - this.mainSettingData.push(new MineMainSettingFunctionItem(null, '开启播放器悬浮窗', null, 1, suspensionState,"suspensionState_switch")) 32 + mainSettingData.push(new MineMainSettingFunctionItem(null, '开启播放器悬浮窗', null, 1, suspensionState,"suspensionState_switch"))
55 // this.mainSettingData.push(new MineMainSettingFunctionItem(null, null, null, 2, null,"")) 33 // this.mainSettingData.push(new MineMainSettingFunctionItem(null, null, null, 2, null,""))
56 - this.mainSettingData.push(new MineMainSettingFunctionItem(null, '清理缓存', '32MB', 0, false,"clear_cache")) 34 + mainSettingData.push(new MineMainSettingFunctionItem(null, '清理缓存', '32MB', 0, false,"clear_cache"))
57 // this.mainSettingData.push(new MineMainSettingFunctionItem(null, '评价我们', null, 0, false,"")) 35 // this.mainSettingData.push(new MineMainSettingFunctionItem(null, '评价我们', null, 0, false,""))
58 36
59 - return this.mainSettingData 37 + return mainSettingData
60 } 38 }
61 39
62 /** 40 /**
63 * 评论 关注 收藏 等7个数据 41 * 评论 关注 收藏 等7个数据
64 * 包含名字和图标 42 * 包含名字和图标
65 */ 43 */
66 - getAccountAndSecuritySettingData():MineMainSettingFunctionItem[]{  
67 - if(this.accountAndSecurityData.length === 7){  
68 - return this.accountAndSecurityData  
69 - }  
70 - this.accountAndSecurityData = []  
71 - this.accountAndSecurityData.push(new MineMainSettingFunctionItem(null, '更换手机号', '18888888888', 0, false,""))  
72 - this.accountAndSecurityData.push(new MineMainSettingFunctionItem(null, '设置密码', null, 0, false,""))  
73 - this.accountAndSecurityData.push(new MineMainSettingFunctionItem(null, null, null, 2, null,"")) 44 + static getAccountAndSecuritySettingData():MineMainSettingFunctionItem[]{
  45 + let accountAndSecurityData: MineMainSettingFunctionItem[] = []
  46 + accountAndSecurityData.push(new MineMainSettingFunctionItem(null, '更换手机号', '18888888888', 0, false,""))
  47 + accountAndSecurityData.push(new MineMainSettingFunctionItem(null, '设置密码', null, 0, false,""))
  48 + accountAndSecurityData.push(new MineMainSettingFunctionItem(null, null, null, 2, null,""))
74 // this.accountAndSecurityData.push(new MineMainSettingFunctionItem($r('app.media.account_qqicon'), '绑定QQ', '立即绑定', 0, false,"")) 49 // this.accountAndSecurityData.push(new MineMainSettingFunctionItem($r('app.media.account_qqicon'), '绑定QQ', '立即绑定', 0, false,""))
75 // this.accountAndSecurityData.push(new MineMainSettingFunctionItem($r('app.media.account_wechaticon'), '绑定微信', '立即绑定', 0, false,"")) 50 // this.accountAndSecurityData.push(new MineMainSettingFunctionItem($r('app.media.account_wechaticon'), '绑定微信', '立即绑定', 0, false,""))
76 // this.accountAndSecurityData.push(new MineMainSettingFunctionItem($r('app.media.account_weiboicon'), '绑定新浪微博', '立即绑定', 0, false,"")) 51 // this.accountAndSecurityData.push(new MineMainSettingFunctionItem($r('app.media.account_weiboicon'), '绑定新浪微博', '立即绑定', 0, false,""))
77 // this.accountAndSecurityData.push(new MineMainSettingFunctionItem($r('app.media.account_appleicon'), 'Apple ID', null, 0, false,"")) 52 // this.accountAndSecurityData.push(new MineMainSettingFunctionItem($r('app.media.account_appleicon'), 'Apple ID', null, 0, false,""))
78 // this.accountAndSecurityData.push(new MineMainSettingFunctionItem(null, null, null, 2, null,"")) 53 // this.accountAndSecurityData.push(new MineMainSettingFunctionItem(null, null, null, 2, null,""))
79 54
80 - this.accountAndSecurityData.push(new MineMainSettingFunctionItem(null, '注销账号', null, 0, false,"")) 55 + accountAndSecurityData.push(new MineMainSettingFunctionItem(null, '注销账号', null, 0, false,""))
81 56
82 - return this.accountAndSecurityData 57 + return accountAndSecurityData
83 } 58 }
84 59
85 60
@@ -113,10 +88,10 @@ class MineSettingDatasModel{ @@ -113,10 +88,10 @@ class MineSettingDatasModel{
113 /** 88 /**
114 * 判断是否设置过密码 89 * 判断是否设置过密码
115 */ 90 */
116 - checkSetPassword(): Promise<CheckSetPasswordItem> { 91 + static checkSetPassword(): Promise<CheckSetPasswordItem> {
117 return new Promise<CheckSetPasswordItem>((success, error) => { 92 return new Promise<CheckSetPasswordItem>((success, error) => {
118 Logger.info(TAG, `checkSetPassword start`); 93 Logger.info(TAG, `checkSetPassword start`);
119 - this.fetchCheckSetPassword().then((navResDTO: ResponseDTO<CheckSetPasswordItem>) => { 94 + MineSettingDatasModel.fetchCheckSetPassword().then((navResDTO: ResponseDTO<CheckSetPasswordItem>) => {
120 if (!navResDTO || navResDTO.code != 0) { 95 if (!navResDTO || navResDTO.code != 0) {
121 error(null) 96 error(null)
122 return 97 return
@@ -131,13 +106,9 @@ class MineSettingDatasModel{ @@ -131,13 +106,9 @@ class MineSettingDatasModel{
131 }) 106 })
132 } 107 }
133 108
134 - fetchCheckSetPassword() { 109 + static fetchCheckSetPassword() {
135 let url = HttpUrlUtils.checkSetPassword() 110 let url = HttpUrlUtils.checkSetPassword()
136 return WDHttp.get<ResponseDTO<CheckSetPasswordItem>>(url) 111 return WDHttp.get<ResponseDTO<CheckSetPasswordItem>>(url)
137 }; 112 };
138 113
139 } 114 }
140 -  
141 -const mineSettingDatasModel = MineSettingDatasModel.getInstance()  
142 -export default mineSettingDatasModel as MineSettingDatasModel  
143 -// export default MineMainSettingFunctionItem as MineMainSettingFunctionItem