zhangbo1_wd

公共header梳理,user相关、定位相关修改

@@ -34,6 +34,8 @@ export class SpConstants{ @@ -34,6 +34,8 @@ export class SpConstants{
34 //定位相关 34 //定位相关
35 static LOCATION_CITY_NAME = "location_city_name" //定位 35 static LOCATION_CITY_NAME = "location_city_name" //定位
36 static LOCATION_CITY_CODE = "location_city_code" //定位 36 static LOCATION_CITY_CODE = "location_city_code" //定位
  37 + static LOCATION_PROVINCE_CODE = "location_province_code" //定位,省份code
  38 + static LOCATION_DISTRICT_CODE = "location_district_code" //定位,区县,返回9位,如:合肥-瑶海区-310115114
37 static LOCATION_PERMISSION_REFUSE = "location_permission_refuse" //定位 39 static LOCATION_PERMISSION_REFUSE = "location_permission_refuse" //定位
38 40
39 //启动页数据存储key 41 //启动页数据存储key
1 import data_preferences from '@ohos.data.preferences'; 1 import data_preferences from '@ohos.data.preferences';
  2 +import { Logger } from './Logger';
  3 +
  4 +const TAG = 'SPHelper'
2 5
3 /** 6 /**
4 * sp存储 7 * sp存储
5 - *  
6 - * 单例模式  
7 - * TODO 新增内存存储,避免频繁sp获取  
8 */ 8 */
9 -// SPHelper.default.get("key1", "defValue1").then((value1) => {  
10 -// this.message = value1.toString();  
11 -// })  
12 -  
13 -// let value2: string = await SPHelper.default.get("key2", "defValue2");  
14 -// this.message = result;  
15 export class SPHelper { 9 export class SPHelper {
16 private static context: Context; 10 private static context: Context;
17 private static spFilename: string = '__SPHelper'; 11 private static spFilename: string = '__SPHelper';
@@ -51,7 +45,11 @@ export class SPHelper { @@ -51,7 +45,11 @@ export class SPHelper {
51 saveSync(key: string, value: data_preferences.ValueType) { 45 saveSync(key: string, value: data_preferences.ValueType) {
52 const preferences: data_preferences.Preferences = this.getVideoPreferencesSync(); 46 const preferences: data_preferences.Preferences = this.getVideoPreferencesSync();
53 preferences.putSync(key, value) 47 preferences.putSync(key, value)
54 - preferences.flush() // todo:Asynchronously 48 + preferences.flush().then(() => {
  49 + Logger.debug(TAG, 'saveSync flush success')
  50 + }).catch((error: object) => {
  51 + Logger.debug(TAG, 'saveSync flush failed: ' + JSON.stringify(error))
  52 + });
55 } 53 }
56 54
57 async get(key: string, defValue: data_preferences.ValueType): Promise<data_preferences.ValueType> { 55 async get(key: string, defValue: data_preferences.ValueType): Promise<data_preferences.ValueType> {
@@ -86,7 +84,11 @@ export class SPHelper { @@ -86,7 +84,11 @@ export class SPHelper {
86 deleteSync(key: string) { 84 deleteSync(key: string) {
87 const preferences: data_preferences.Preferences = this.getVideoPreferencesSync(); 85 const preferences: data_preferences.Preferences = this.getVideoPreferencesSync();
88 preferences.deleteSync(key) 86 preferences.deleteSync(key)
89 - preferences.flush(); // todo:Asynchronously 87 + preferences.flush().then(() => {
  88 + Logger.debug(TAG, 'deleteSync flush success')
  89 + }).catch((error: object) => {
  90 + Logger.debug(TAG, 'deleteSync flush failed: ' + JSON.stringify(error))
  91 + });
90 } 92 }
91 93
92 async clearSync() { 94 async clearSync() {
@@ -98,12 +100,6 @@ export class SPHelper { @@ -98,12 +100,6 @@ export class SPHelper {
98 }); 100 });
99 } 101 }
100 102
101 - // clearSync() {  
102 - // let preferences: data_preferences.Preferences = this.getVideoPreferencesSync()  
103 - // preferences.clearSync()  
104 - // preferences.flush()  
105 - // }  
106 -  
107 public getPreferences() { 103 public getPreferences() {
108 let preferences = data_preferences.getPreferences(SPHelper.context, SPHelper.spFilename); 104 let preferences = data_preferences.getPreferences(SPHelper.context, SPHelper.spFilename);
109 return preferences; 105 return preferences;
@@ -25,15 +25,14 @@ export class HttpParams { @@ -25,15 +25,14 @@ export class HttpParams {
25 headers['imei'] = DeviceUtil.clientId() 25 headers['imei'] = DeviceUtil.clientId()
26 headers['Accept-Language'] = 'zh' 26 headers['Accept-Language'] = 'zh'
27 headers['timestamp'] = DateTimeUtils.getTimeStamp() + '' 27 headers['timestamp'] = DateTimeUtils.getTimeStamp() + ''
  28 + headers['mpassid'] = 'ZbHTMeTsfaYDAHqt8ZHIzcPs'
28 HttpParams.setLocationHeader(headers) 29 HttpParams.setLocationHeader(headers)
29 - // // TODO 判断是否登录  
30 - headers['RMRB-X-TOKEN'] = HttpUtils.getXToken()  
31 - if (HttpUtils.getXToken() != '') {  
32 - headers['cookie'] = 'RMRB-X-TOKEN=' + HttpUtils.getXToken() 30 + if (HttpUtils.isLogin()) {
  31 + headers['RMRB-X-TOKEN'] = HttpUtils.getUserToken()
  32 + headers['cookie'] = 'RMRB-X-TOKEN=' + HttpUtils.getUserToken()
  33 + headers['userId'] = HttpUtils.getUserId()
  34 + headers['userType'] = HttpUtils.getUserType()
33 } 35 }
34 - headers['userId'] = HttpUtils.getUserId()  
35 - headers['userType'] = HttpUtils.getUserType()  
36 - headers['mpassid'] = 'ZbHTMeTsfaYDAHqt8ZHIzcPs'  
37 HttpParams.addSpecialHeaders(headers); 36 HttpParams.addSpecialHeaders(headers);
38 return headers; 37 return headers;
39 } 38 }
1 import { SpConstants } from 'wdConstant/Index'; 1 import { SpConstants } from 'wdConstant/Index';
2 import { DeviceUtil, SPHelper, StringUtils } from 'wdKit/Index'; 2 import { DeviceUtil, SPHelper, StringUtils } from 'wdKit/Index';
3 -import { HttpParams } from '../http/HttpCommonParams';  
4 import { HttpRequest } from '../http/HttpRequest'; 3 import { HttpRequest } from '../http/HttpRequest';
5 4
6 const TAG: string = '[HttpUtils]' 5 const TAG: string = '[HttpUtils]'
@@ -9,10 +8,6 @@ const TAG: string = '[HttpUtils]' @@ -9,10 +8,6 @@ const TAG: string = '[HttpUtils]'
9 * http相关工具类,对外暴露 8 * http相关工具类,对外暴露
10 */ 9 */
11 export class HttpUtils { 10 export class HttpUtils {
12 - private static userId = ''  
13 - private static userType = ''  
14 - private static token = ''  
15 -  
16 /** 11 /**
17 * 添加公共参数,如登录后,添加登录信息 12 * 添加公共参数,如登录后,添加登录信息
18 */ 13 */
@@ -49,25 +44,27 @@ export class HttpUtils { @@ -49,25 +44,27 @@ export class HttpUtils {
49 * 定位,城市code 44 * 定位,城市code
50 */ 45 */
51 public static getCityCode(): string { 46 public static getCityCode(): string {
52 - // TODO  
53 // 城市编码 47 // 城市编码
54 - return '340100'; 48 + return SPHelper.default.getSync(SpConstants.LOCATION_CITY_CODE, '') as string
55 } 49 }
56 50
57 /** 51 /**
58 * 定位,省份code 52 * 定位,省份code
59 */ 53 */
60 public static getProvinceCode(): string { 54 public static getProvinceCode(): string {
61 - // TODO  
62 - return '340000'; 55 + return SPHelper.default.getSync(SpConstants.LOCATION_PROVINCE_CODE, '') as string
63 } 56 }
64 57
65 /** 58 /**
66 * 定位,地区code 59 * 定位,地区code
67 */ 60 */
68 public static getDistrictCode(): string { 61 public static getDistrictCode(): string {
69 - // TODO  
70 - return '340103'; 62 + let districtCode = SPHelper.default.getSync(SpConstants.LOCATION_DISTRICT_CODE, '') as string
  63 + if (districtCode === '') {
  64 + return districtCode
  65 + }
  66 + // 截取前6位,如返回310115114,需要310115 (上海浦东)
  67 + return districtCode.substring(0, 6);
71 } 68 }
72 69
73 public static getImei(): string { 70 public static getImei(): string {
@@ -75,43 +72,22 @@ export class HttpUtils { @@ -75,43 +72,22 @@ export class HttpUtils {
75 } 72 }
76 73
77 public static getUserId(): string { 74 public static getUserId(): string {
78 - // TODO 对接登录  
79 - if (StringUtils.isNotEmpty(HttpUtils.userId)) {  
80 - return HttpUtils.userId  
81 - }  
82 - HttpUtils.userId = SPHelper.default.getSync(SpConstants.USER_ID, "") as string  
83 - return HttpUtils.userId; 75 + return SPHelper.default.getSync(SpConstants.USER_ID, "") as string
84 } 76 }
85 77
86 public static getUserType(): string { 78 public static getUserType(): string {
87 - if (StringUtils.isNotEmpty(HttpUtils.userType)) {  
88 - return HttpUtils.userType  
89 - }  
90 - HttpUtils.userType = SPHelper.default.getSync(SpConstants.USER_Type, "") as string  
91 - return HttpUtils.userType; 79 + return SPHelper.default.getSync(SpConstants.USER_Type, "") as string
92 } 80 }
93 81
94 - static getXToken(): string {  
95 - if (StringUtils.isNotEmpty(HttpUtils.token)) {  
96 - return HttpUtils.token  
97 - }  
98 - HttpUtils.token = SPHelper.default.getSync(SpConstants.USER_JWT_TOKEN, "") as string  
99 - if (StringUtils.isNotEmpty(HttpUtils.token)) {  
100 - return HttpUtils.token  
101 - }  
102 - return '' 82 + static getUserToken(): string {
  83 + return SPHelper.default.getSync(SpConstants.USER_JWT_TOKEN, "") as string
103 } 84 }
104 85
105 - public static setUserId(userId: string) {  
106 - // TODO 优化赋值  
107 - HttpUtils.userId = userId;  
108 - }  
109 -  
110 - public static setUserType(userType: string) {  
111 - HttpUtils.userType = userType;  
112 - }  
113 -  
114 - public static setUserToken(token: string) {  
115 - HttpUtils.token = token; 86 + public static isLogin(): boolean {
  87 + let token = HttpUtils.getUserToken()
  88 + if (token == null || token == undefined || token.length <= 0) {
  89 + return false
  90 + }
  91 + return true
116 } 92 }
117 } 93 }
@@ -29,9 +29,6 @@ export class LogoutViewModel{ @@ -29,9 +29,6 @@ export class LogoutViewModel{
29 SPHelper.default.saveSync(SpConstants.USER_Type, '') 29 SPHelper.default.saveSync(SpConstants.USER_Type, '')
30 SPHelper.default.saveSync(SpConstants.USER_NAME, '') 30 SPHelper.default.saveSync(SpConstants.USER_NAME, '')
31 SPHelper.default.saveSync(SpConstants.USER_PHONE, '') 31 SPHelper.default.saveSync(SpConstants.USER_PHONE, '')
32 - HttpUtils.setUserId("")  
33 - HttpUtils.setUserType("")  
34 - HttpUtils.setUserToken('')  
35 UserDataLocal.clearUserData() 32 UserDataLocal.clearUserData()
36 } 33 }
37 } 34 }
@@ -9,8 +9,6 @@ import { ResponseDTO } from 'wdNetwork/Index'; @@ -9,8 +9,6 @@ import { ResponseDTO } from 'wdNetwork/Index';
9 * 系统定位服务实现 9 * 系统定位服务实现
10 * */ 10 * */
11 export class HWLocationUtils { 11 export class HWLocationUtils {
12 -  
13 -  
14 static LOCATION: Permissions = 'ohos.permission.LOCATION' 12 static LOCATION: Permissions = 'ohos.permission.LOCATION'
15 static APPROXIMATELY_LOCATION: Permissions = 'ohos.permission.APPROXIMATELY_LOCATION' 13 static APPROXIMATELY_LOCATION: Permissions = 'ohos.permission.APPROXIMATELY_LOCATION'
16 14
@@ -115,17 +113,23 @@ export class HWLocationUtils { @@ -115,17 +113,23 @@ export class HWLocationUtils {
115 if (cityName == name) { 113 if (cityName == name) {
116 return 114 return
117 } 115 }
118 - let code = await HWLocationUtils.getCityCode(data[0].administrativeArea, data[0].subAdministrativeArea)  
119 - if (code) { 116 + // code=[省份code,城市code]
  117 + let code: string[] = await HWLocationUtils.getCityCode(data[0].administrativeArea, data[0].subAdministrativeArea)
  118 + if (code && code.length >= 2) {
120 SPHelper.default.save(SpConstants.LOCATION_CITY_NAME, cityName) 119 SPHelper.default.save(SpConstants.LOCATION_CITY_NAME, cityName)
121 - SPHelper.default.save(SpConstants.LOCATION_CITY_CODE, code) 120 + SPHelper.default.save(SpConstants.LOCATION_PROVINCE_CODE, code[0])
  121 + SPHelper.default.save(SpConstants.LOCATION_CITY_CODE, code[1])
  122 + }
  123 + if (data[0].descriptions && data[0].descriptions.length > 1) {
  124 + // 保存区县code,9位数字
  125 + let districtCode = data[0].descriptions[1] || ''
  126 + SPHelper.default.save(SpConstants.LOCATION_DISTRICT_CODE, districtCode)
122 } 127 }
123 } 128 }
124 } 129 }
125 }) 130 })
126 } 131 }
127 132
128 -  
129 //取消定位 133 //取消定位
130 static cancelLocation() { 134 static cancelLocation() {
131 // geoLocationManager.off('locationChange') 135 // geoLocationManager.off('locationChange')
@@ -143,10 +147,14 @@ export class HWLocationUtils { @@ -143,10 +147,14 @@ export class HWLocationUtils {
143 if (bean.code == 0 && bean.data) { 147 if (bean.code == 0 && bean.data) {
144 for (let i = 0; i < bean.data.length; i++) { 148 for (let i = 0; i < bean.data.length; i++) {
145 if (bean.data[i].label == administrativeArea) { 149 if (bean.data[i].label == administrativeArea) {
  150 + let str:string[] = []
  151 + let provinceCode = bean.data[i].code
  152 + str[0] = provinceCode
146 for (let j = 0; j < bean.data[i].children.length; j++) { 153 for (let j = 0; j < bean.data[i].children.length; j++) {
147 if (bean.data[i].children[j].label == cityName) { 154 if (bean.data[i].children[j].label == cityName) {
148 Logger.debug("huaw" + bean.data[i].children[j].code) 155 Logger.debug("huaw" + bean.data[i].children[j].code)
149 - return bean.data[i].children[j].code 156 + str[1] = bean.data[i].children[j].code
  157 + return str
150 } 158 }
151 } 159 }
152 } 160 }
@@ -155,7 +163,7 @@ export class HWLocationUtils { @@ -155,7 +163,7 @@ export class HWLocationUtils {
155 163
156 } 164 }
157 } 165 }
158 - return '' 166 + return []
159 } 167 }
160 168
161 // 通过省份code获取省份名称 169 // 通过省份code获取省份名称
@@ -55,9 +55,6 @@ export class LoginViewModel { @@ -55,9 +55,6 @@ export class LoginViewModel {
55 SPHelper.default.saveSync(SpConstants.USER_STATUS, data.status) 55 SPHelper.default.saveSync(SpConstants.USER_STATUS, data.status)
56 SPHelper.default.saveSync(SpConstants.USER_Type, data.userType) 56 SPHelper.default.saveSync(SpConstants.USER_Type, data.userType)
57 SPHelper.default.saveSync(SpConstants.USER_NAME, data.userName) 57 SPHelper.default.saveSync(SpConstants.USER_NAME, data.userName)
58 - HttpUtils.setUserId(data.id+"")  
59 - HttpUtils.setUserType(data.userType+"")  
60 - HttpUtils.setUserToken(data.jwtToken)  
61 success(data) 58 success(data)
62 }).catch((error:string) => { 59 }).catch((error:string) => {
63 fail(error) 60 fail(error)
@@ -85,9 +82,6 @@ export class LoginViewModel { @@ -85,9 +82,6 @@ export class LoginViewModel {
85 SPHelper.default.saveSync(SpConstants.USER_STATUS, data.status) 82 SPHelper.default.saveSync(SpConstants.USER_STATUS, data.status)
86 SPHelper.default.saveSync(SpConstants.USER_Type, data.userType) 83 SPHelper.default.saveSync(SpConstants.USER_Type, data.userType)
87 SPHelper.default.saveSync(SpConstants.USER_NAME, data.userName) 84 SPHelper.default.saveSync(SpConstants.USER_NAME, data.userName)
88 - HttpUtils.setUserId(data.id+"")  
89 - HttpUtils.setUserType(data.userType+"")  
90 - HttpUtils.setUserToken(data.jwtToken)  
91 success(data) 85 success(data)
92 }).catch((value: string) => { 86 }).catch((value: string) => {
93 fail(value) 87 fail(value)
@@ -163,9 +157,6 @@ export class LoginViewModel { @@ -163,9 +157,6 @@ export class LoginViewModel {
163 SPHelper.default.saveSync(SpConstants.USER_STATUS, '') 157 SPHelper.default.saveSync(SpConstants.USER_STATUS, '')
164 SPHelper.default.saveSync(SpConstants.USER_Type, '') 158 SPHelper.default.saveSync(SpConstants.USER_Type, '')
165 SPHelper.default.saveSync(SpConstants.USER_NAME, '') 159 SPHelper.default.saveSync(SpConstants.USER_NAME, '')
166 - HttpUtils.setUserId("")  
167 - HttpUtils.setUserType("")  
168 - HttpUtils.setUserToken('')  
169 success(data) 160 success(data)
170 }).catch((message: string) => { 161 }).catch((message: string) => {
171 fail(message) 162 fail(message)