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