EditInfoViewModel.ets
5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import {
editItem,
EditListInfo, editModel, editModelParams, WDEditDataModelType } from '../model/EditInfoModel';
import HashMap from '@ohos.util.HashMap';
import { HttpUrlUtils, ResponseDTO, WDHttp } from 'wdNetwork';
import { Logger, ResourcesUtils } from 'wdKit';
import { AreaListManageModel, AreaListModel } from '../model/AreaListModel';
import promptAction from '@ohos.promptAction';
const TAG = "EditInfoViewModel"
class EditInfoViewModel {
private static instance: EditInfoViewModel;
editListData:EditListInfo[] = []
params: editModelParams = {}
/**
* 单例模式
* @returns
*/
public static getInstance(): EditInfoViewModel {
if (!EditInfoViewModel.instance) {
EditInfoViewModel.instance = new EditInfoViewModel();
}
return EditInfoViewModel.instance;
}
BaseGetRequest(requestUrl:string){
let headers: HashMap<string, string> = HttpUrlUtils.getCommonHeaders()
requestUrl = HttpUrlUtils.getHost() + requestUrl
return WDHttp.get<editItem>(requestUrl,headers)
}
BasePostRequest(requestUrl:string,params: object){
let headers: HashMap<string, string> = HttpUrlUtils.getCommonHeaders()
requestUrl = HttpUrlUtils.getHost() + requestUrl
return WDHttp.post<ResponseDTO>(requestUrl,params,headers)
}
getEditListInfo(item?:editModel):EditListInfo[]{
this.editListData = [
new EditListInfo('昵称',item&&item.userName.length > 0?item.userName:'待完善'),
new EditListInfo('简介',item&&item.userExtend.introduction?item.userExtend.introduction:'待完善'),
new EditListInfo('地区',item&&item.userExtend.address?item.userExtend.address:'待完善'),
new EditListInfo('生日',item&&item.userExtend.birthday?item.userExtend.birthday:'待完善'),
new EditListInfo('性别',item?(item.userExtend.sex === 1?'男':'女'):'待完善'),]
return this.editListData
}
///1普通用户
queryAccountOwnerInfo(userType:number, context: Context):PromiseLike<editModel>{
return new Promise(((success, error) => {
this.BaseGetRequest(userType == 1?HttpUrlUtils.APPOINTMENT_QueryUserDetail_PATH:HttpUrlUtils.APPOINTMENT_AccountOwner_PATH).then((editDTO:editItem) =>{
if (editDTO.code == 0) {
success(editDTO.data)
}else {
success(this.GetqueryAccountOwnerLocal(context))
}
}).catch((error: Error) => {
Logger.info(TAG,'queryAccountOwnerInfo','EditInfoViewModel')
success(this.GetqueryAccountOwnerLocal(context))
})
}))
}
async GetqueryAccountOwnerLocal(context: Context): Promise<editModel> {
Logger.info(TAG, `getBottomNavDataMock start`);
let compRes: ResponseDTO<editModel> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<editModel>>(context,'userInfo.json');
if (!compRes || !compRes.data) {
Logger.info(TAG, `getAppointmentListDataLocal compRes is empty`);
return new editModel()
}
Logger.info(TAG, `getAppointmentListDataLocal getResourcesJsonSync compRes : ${JSON.stringify(compRes)}`);
return compRes.data
}
getAreaList(context: Context):PromiseLike<AreaListModel[]>{
return new Promise((success, error) => {
success(this.getAreaListLocal(context))
// HttpRequest.post(HttpUrlUtils.APPOINTMENT_AccountOwner_PATH).then((navResDTO:ResponseDTO) =>{
// if (navResDTO.code == 200) {
// // let editM = navResDTO.data as EditInfoModel
// // success(JSON.parse(navResDTO.data)
// }
// }).catch((error: Error) => {
// Logger.info(TAG,'getAreaList','EditInfoViewModel')
// success(this.getAreaListLocal(context))
//
// })
})
}
async getAreaListLocal(context: Context): Promise<AreaListModel[]> {
Logger.info(TAG, `getBottomNavDataMock start`);
let compRes: ResponseDTO<AreaListModel[]> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<AreaListModel[]>>(context,'areaList_data.json');
if (!compRes || !compRes.data) {
Logger.info(TAG, `getAppointmentListDataLocal compRes is empty`);
return [] as AreaListModel[]
}
Logger.info(TAG, `getAppointmentListDataLocal getResourcesJsonSync compRes : ${JSON.stringify(compRes)}`);
return compRes.data
}
getAreaListManageModel(item:AreaListModel):AreaListManageModel{
return new AreaListManageModel(item.code,item.id,item.label,item.children)
}
updateUserInfo(item:editModel):Promise<ResponseDTO> {
if (item.editDataType == WDEditDataModelType.WDEditDataModelType_head) {
} else if (item.editDataType == WDEditDataModelType.WDEditDataModelType_nickname) {
this.params = { userName: item.userName }
}
else if (item.editDataType == WDEditDataModelType.WDEditDataModelType_intro) {
this.params = { introduction: item.userExtend.introduction }
} else if (item.editDataType == WDEditDataModelType.WDEditDataModelType_sex) {
this.params = { sex: item.userExtend.sex.toString() }
} else if (item.editDataType == WDEditDataModelType.WDEditDataModelType_birthday) {
this.params = { birthday: item.userExtend.birthday }
} else if (item.editDataType == WDEditDataModelType.WDEditDataModelType_region) {
this.params = {province:item.userExtend.province,city:item.userExtend.city, county: item.userExtend.county ,address:item.userExtend.address}
}
return new Promise((success, error) => {
this.BasePostRequest(item.editDataType == WDEditDataModelType.WDEditDataModelType_nickname?HttpUrlUtils.APPOINTMENT_editUserDetail1_PATH:HttpUrlUtils.APPOINTMENT_editUserDetail_PATH,this.params)
.then((navResDTO: ResponseDTO) => {
if (navResDTO.code == 0) {
promptAction.showToast({ message: '修改成功' })
}
})
.catch((error: Error) => {
Logger.info(TAG,'updateUserInfo','EditInfoViewModel')
})
})
}
}
const editInfoViewModel = EditInfoViewModel.getInstance();
export default editInfoViewModel as EditInfoViewModel