EditInfoViewModel.ets
8.74 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import {
editItem,
EditListInfo, editModel, editModelParams,
peopleItem,
peopleItemModel,
WDEditDataModelType } from '../model/EditInfoModel';
import { HttpUrlUtils, ResponseDTO, WDHttp } from 'wdNetwork';
import { Logger, ResourcesUtils } from 'wdKit';
import { AreaListManageModel, AreaListModel,AreaListData } 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){
requestUrl = HttpUrlUtils.getHost() + requestUrl
return WDHttp.get<editItem>(requestUrl)
}
BasePostRequest(requestUrl:string,params: object){
requestUrl = HttpUrlUtils.getHost() + requestUrl
return WDHttp.post<ResponseDTO>(requestUrl,params)
}
getEditListInfo(item?:editModel):EditListInfo[]{
if (item?.userType === 1) {
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 === 0?'待完善':(item.userExtend.sex === 1?'男':'女')):'待完善'),]
}else {
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:'待完善')]
}
return this.editListData
}
///1普通用户
queryAccountOwnerInfo(context: Context):PromiseLike<editModel>{
return new Promise(((success, error) => {
this.BaseGetRequest(HttpUrlUtils.APPOINTMENT_QueryUserDetail_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))
})
}))
}
BasePeopleGetRequest(requestUrl:string){
requestUrl = HttpUrlUtils.getHost() + requestUrl
return WDHttp.get<peopleItem>(requestUrl)
}
queryPeopleAccountOwnerInfo(context: Context):PromiseLike<peopleItemModel>{
return new Promise(((success, error) => {
this.BasePeopleGetRequest(HttpUrlUtils.APPOINTMENT_AccountOwner_PATH).then((editDTO:peopleItem) =>{
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) => {
WDHttp.get<AreaListData>(HttpUrlUtils.getHost() + HttpUrlUtils.APPOINTMENT_userArea_PATH).then((navResDTO: AreaListData) => {
if (navResDTO.code == 0) {
success(navResDTO.data)
}else {
success(this.getAreaListLocal(context))
}
}).catch((error: Error) => {
Logger.info(TAG,'executeCollcet','ResponseDTO')
success(this.getAreaListLocal(context))
})
})
}
getLocalAreaList(context: Context):PromiseLike<AreaListModel[]>{
return new Promise((success, error) => {
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.userType === 1){
if (item.editDataType == WDEditDataModelType.WDEditDataModelType_head) {
this.params = { headPhotoUrl: item.headPhotoUrl }
} 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: '您的资料已提交' })
success(navResDTO)
}else {
promptAction.showToast({ message: navResDTO.message })
}
})
.catch((error: Error) => {
Logger.info(TAG,'updateUserInfo','EditInfoViewModel')
promptAction.showToast({ message: error.message })
})
})
}else {
let url = ''
if (item.editDataType == WDEditDataModelType.WDEditDataModelType_head) {
this.params = { headPhotoUrl: item.headPhotoUrl}
url = '/api/rmrb-creator-user/c/rmrb-creator-user/creator/updateIcon'
} else if (item.editDataType == WDEditDataModelType.WDEditDataModelType_nickname) {
this.params = { name: item.userName}
url = '/api/rmrb-creator-user/c/rmrb-creator-user/creator/updateName'
} else if (item.editDataType == WDEditDataModelType.WDEditDataModelType_intro) {
this.params = { introduction: item.userExtend.introduction }
url = '/api/rmrb-creator-user/c/rmrb-creator-user/creator/updateIntroduction'
} else if (item.editDataType == WDEditDataModelType.WDEditDataModelType_region) {
this.params = {provinceCode:item.userExtend.provinceCode,cityCode:item.userExtend.cityCode, districtCode: item.userExtend.districtCode ,countryCode:'86'}
url = '/api/rmrb-creator-user/c/rmrb-creator-user/creator/updateArea'
}
this.params.creatorId = item.userExtend.creatorId
return new Promise((success, error) => {
this.BasePostRequest(url,this.params)
.then((navResDTO: ResponseDTO) => {
if (navResDTO.code == 0) {
promptAction.showToast({ message: '您的资料已提交' })
success(navResDTO)
}else {
promptAction.showToast({ message: navResDTO.message })
}
})
.catch((error: Error) => {
Logger.info(TAG,'updateUserInfo','EditInfoViewModel')
promptAction.showToast({ message: error.message })
})
})
}
}
}
const editInfoViewModel = EditInfoViewModel.getInstance();
export default editInfoViewModel as EditInfoViewModel