1# 全局配置项功能场景 2<!--Kit: ArkTS--> 3<!--Subsystem: CommonLibrary--> 4<!--Owner: @lijiamin2025--> 5<!--Designer: @weng-changcheng--> 6<!--Tester: @kirl75; @zsw_zhushiwei--> 7<!--Adviser: @ge-yafang--> 8 9对于需要使用进程单例的场景,例如不同并发实例间需要数据保持一致的全局配置项功能,可以采用[共享模块](arkts-sendable-module.md)来实现。 10 11以下示例展示了只有在Wi-Fi打开且用户登录的情况下才能进行下载的功能,具体步骤如下。 12 131. 编写全局配置文件。 14 15 ```ts 16 // Config.ets 17 18 import { ArkTSUtils } from '@kit.ArkTS'; 19 20 "use shared" 21 22 @Sendable 23 class Config { 24 lock: ArkTSUtils.locks.AsyncLock = new ArkTSUtils.locks.AsyncLock(); 25 isLogin: boolean = false; 26 loginUser?: string; 27 wifiOn: boolean = false; 28 29 async login(user: string) { 30 return this.lock.lockAsync(() => { 31 this.isLogin = true; 32 this.loginUser = user; 33 }, ArkTSUtils.locks.AsyncLockMode.EXCLUSIVE) 34 } 35 36 async logout(user?: string) { 37 return this.lock.lockAsync(() => { 38 this.isLogin = false; 39 this.loginUser = ""; 40 }, ArkTSUtils.locks.AsyncLockMode.EXCLUSIVE) 41 } 42 43 async getIsLogin(): Promise<boolean> { 44 return this.lock.lockAsync(() => { 45 return this.isLogin; 46 }, ArkTSUtils.locks.AsyncLockMode.SHARED) 47 } 48 49 async getUser(): Promise<string> { 50 return this.lock.lockAsync(() => { 51 return this.loginUser!; 52 }, ArkTSUtils.locks.AsyncLockMode.SHARED) 53 } 54 55 async setWifiState(state: boolean) { 56 return this.lock.lockAsync(() => { 57 this.wifiOn = state; 58 }, ArkTSUtils.locks.AsyncLockMode.EXCLUSIVE) 59 } 60 61 async isWifiOn() { 62 return this.lock.lockAsync(() => { 63 return this.wifiOn; 64 }, ArkTSUtils.locks.AsyncLockMode.SHARED) 65 } 66 } 67 68 export let config = new Config(); 69 ``` 70 <!-- @[global_config](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ApplicationMultithreadingDevelopment/PracticalCases/entry/src/main/ets/managers/Config.ets) --> 71 722. UI主线程及子线程访问全局配置项。 73 74 ```ts 75 // Index.ets 76 import { config } from './Config'; 77 import { taskpool } from '@kit.ArkTS'; 78 79 @Concurrent 80 async function download() { 81 if (!await config.isWifiOn()) { 82 console.info("wifi is off"); 83 return false; 84 } 85 if (!await config.getIsLogin()) { 86 console.info("not login"); 87 return false; 88 } 89 console.info(`User[${await config.getUser()}] start download ...`); 90 return true; 91 } 92 93 @Entry 94 @Component 95 struct Index { 96 @State message: string = 'not login'; 97 @State wifiState: string = "wifi off"; 98 @State downloadResult: string = ""; 99 input: string = "" 100 101 build() { 102 Row() { 103 Column() { 104 Text(this.message) 105 .fontSize(50) 106 .fontWeight(FontWeight.Bold) 107 .alignRules({ 108 center: { anchor: '__container__', align: VerticalAlign.Center }, 109 middle: { anchor: '__container__', align: HorizontalAlign.Center } 110 }) 111 TextInput({ placeholder: "请输入用户名" }) 112 .fontSize(20) 113 .fontWeight(FontWeight.Bold) 114 .alignRules({ 115 center: { anchor: '__container__', align: VerticalAlign.Center }, 116 middle: { anchor: '__container__', align: HorizontalAlign.Center } 117 }) 118 .onChange((value) => { 119 this.input = value; 120 }) 121 Text("login") 122 .fontSize(50) 123 .fontWeight(FontWeight.Bold) 124 .alignRules({ 125 center: { anchor: '__container__', align: VerticalAlign.Center }, 126 middle: { anchor: '__container__', align: HorizontalAlign.Center } 127 }) 128 .onClick(async () => { 129 if (!await config.getIsLogin() && this.input) { 130 this.message = "login: " + this.input; 131 config.login(this.input); 132 } 133 }) 134 .backgroundColor(0xcccccc) 135 Text("logout") 136 .fontSize(50) 137 .fontWeight(FontWeight.Bold) 138 .alignRules({ 139 center: { anchor: '__container__', align: VerticalAlign.Center }, 140 middle: { anchor: '__container__', align: HorizontalAlign.Center } 141 }) 142 .onClick(async () => { 143 if (await config.getIsLogin()) { 144 this.message = "not login"; 145 config.logout(); 146 } 147 }) 148 .backgroundColor(0xcccccc) 149 Text(this.wifiState) 150 .fontSize(50) 151 .fontWeight(FontWeight.Bold) 152 .alignRules({ 153 center: { anchor: '__container__', align: VerticalAlign.Center }, 154 middle: { anchor: '__container__', align: HorizontalAlign.Center } 155 }) 156 Toggle({ type: ToggleType.Switch }) 157 .onChange(async (isOn: boolean) => { 158 await config.setWifiState(isOn); 159 this.wifiState = isOn ? "wifi on" : "wifi off"; 160 }) 161 Text("download") 162 .fontSize(50) 163 .fontWeight(FontWeight.Bold) 164 .alignRules({ 165 center: { anchor: '__container__', align: VerticalAlign.Center }, 166 middle: { anchor: '__container__', align: HorizontalAlign.Center } 167 }) 168 .onClick(async () => { 169 let ret = await taskpool.execute(download); 170 this.downloadResult = ret ? "download success" : "download fail"; 171 }) 172 Text(this.downloadResult) 173 .fontSize(20) 174 .fontWeight(FontWeight.Bold) 175 .alignRules({ 176 center: { anchor: '__container__', align: VerticalAlign.Center }, 177 middle: { anchor: '__container__', align: HorizontalAlign.Center } 178 }) 179 } 180 .width('100%') 181 } 182 .height('100%') 183 } 184 } 185 ``` 186 <!-- @[access_global_config](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ApplicationMultithreadingDevelopment/PracticalCases/entry/src/main/ets/managers/GlobalConfigurationGuide.ets) --> 187