family-userInfo.page.js
7.9 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import 'home/family-userInfo.page.css';
import 'home/family/city-swiper.css';
import CitySwiper from 'plugin/city-swiper';
import $ from 'yoho-jquery';
import Page from 'yoho-page';
import tip from 'plugin/tip';
import cityRender from 'home/chosen-city.hbs';
import yoho from 'yoho-app';
class UserInfo extends Page {
constructor() {
super();
this.selector = {
$chosenCity: $('#chosen-city'),
$citySwiper: $('.city-swiper'),
$modifyInp: $('.inp.modify'),
$nickName: $('.inp.nick-name'),
$gender: $('.inp.gender'),
$birthday: $('.inp.birthday'),
$height: $('.inp.height'),
$weight: $('.inp.weight'),
$userAvatar: $('.user-avatar'),
$province: $('#province'),
$city: $('#city'),
$cancelBtn: $('.swiper-header .cancel'),
$okBtn: $('.swiper-header .ok'),
$provinceText: $('.province-text'),
$cityText: $('.city-text'),
$userPic: $('.user-pic'),
$noDate: $('.inp.modify[type!=date]'),
$bottomOut: $('.bottom-out'),
$phone: $('.phone')
};
this.view = {
cityRender
};
this.CityId = 0;
this.province = this.selector.$provinceText.text();
this.city = this.selector.$cityText.text();
this.init();
}
init() {
this.bindEvents();
// city-swiper的回调函数
this.on('citySwiperCb', (event, data) => { // data是返回选中的slide
let $data = $(data);
let $parent = $data.parents('.swiper-wrapper');
if (($parent.attr('id') === 'province')) { // 当滑动省份列表时出发城市列表
this.CityId = $data.attr('id');
this.getCity();
}
});
this.defaultPic();
this.getCity();
this.refreshUserInfo();
if (yoho && yoho.isMarsApp) {
yoho.ready(() => {
yoho.invokeMethod('set.removeTopRightButton');
});
}
}
refreshUserInfo() {
// 提供给app更改完个人信息之后刷新
yoho.ready(function() {
yoho.addNativeMethod('refreshUserInfomation', function() {
location.href = location.href;
});
});
}
bindEvents() {
this.selector.$chosenCity.on('click', this.chosenCity.bind(this));
this.selector.$cancelBtn.on('click', this.cancelFun.bind(this));
this.selector.$okBtn.on('click', this.okFun.bind(this));
this.selector.$chosenCity.on('DOMNodeInserted', this.modifyInp.bind(this));
this.selector.$userPic.on('click', this.userPic.bind(this));
this.selector.$modifyInp.on('click', this.closeCitySwiper.bind(this));
this.selector.$bottomOut.on('focus', this.keepOut.bind(this));
this.selector.$bottomOut.on('blur', this.restore.bind(this));
this.selector.$bottomOut.on('click', this.modPhone.bind(this));
if (yoho.isApp) {
this.selector.$modifyInp.on('change', this.modifyInp.bind(this));
} else {
this.selector.$noDate.on('change', this.modifyInp.bind(this));
this.selector.$birthday.on('blur', this.modifyInp.bind(this));
}
}
closeCitySwiper(e) {
let $this = $(e.currentTarget);
if ($this.attr('id') !== 'chosen-city') {
this.selector.$citySwiper.hide();
}
}
keepOut(e) {
let $this = $(e.currentTarget);
$('body').css({
height: $(window).height() + $this.offset().top * 2 / 3
});
$(document).scrollTop($this.offset().top * 2 / 3);
}
restore() {
$('body').css({
height: $(window).height()
});
$(document).scrollTop(0);
}
userPic() {
if (yoho.isApp) {
yoho.ready(() => {
if (yoho.isYohoApp) {
yoho.invokeMethod('go.setAvatar', {}, (data) => {
if (data) {
let reg = /(\w*){mode}(.*){width}(.*){height}(.*)/g;
data = data.replace(reg, '$12$2100$3100$4');
this.selector.$userAvatar.css('background-image', `url(${data})`);
}
});
} else if (yoho.isMarsApp || yoho.isNowApp) {
yoho.invokeMethod('go.modify_user', {}, (data) => {
if (data) {
this.selector.$userAvatar.css('background-image', `url(${data})`);
}
});
}
});
}
}
cancelFun() {
this.selector.$citySwiper.hide();
}
okFun() {
let $provinceActive = $('#province .swiper-slide-active');
let $cityActive = $('#city .swiper-slide-active');
let $chosenCityText = '';
this.province = $provinceActive.text();
this.city = $cityActive.text();
$chosenCityText = `<span class="province-text">
${this.province}</span> <span class="city-text">${this.city}</span>`;
this.selector.$chosenCity.find('.inp').html($chosenCityText);
this.selector.$citySwiper.hide();
}
defaultPic() {
let myImage = new Image(),
avatar;
avatar = this.selector.$userAvatar.data('avatar');
myImage.src = avatar;
myImage.onload = () => {
this.selector.$userAvatar.css('background-image', 'url(' + avatar + ')');
};
}
modifyInp() {
this.ajax({
url: '/home/family/modify',
data: {
nickName: this.selector.$nickName.val(),
gender: this.selector.$gender.val(),
birthday: this.selector.$birthday.val(),
height: this.selector.$height.val(),
weight: this.selector.$weight.val(),
province: this.province,
city: this.city,
},
}).then(result => {
if (result && result.code === 200) {
if (yoho && yoho.isApp) {
yoho.ready(() => {
yoho.invokeMethod('go.refreshUserInfomation');
});
}
location.href = location.href;
} else {
tip.show(result.message);
}
});
}
chosenCity() {
this.getCity();
this.selector.$citySwiper.show();
}
getCity() {
this.ajax({
url: '/home/family/userInfo/getCity',
data: {
id: this.CityId
}
}).then(result => {
if (this.CityId === 0) {
this.selector.$province.append(this.view.cityRender(result));
new CitySwiper({
lineNum: 5, // 每列行数
centeredSlides: true, // 剧中显示
hidePartingLine: false // 隐藏两条分割线
});
} else {
this.selector.$city.empty();
this.selector.$city.append(this.view.cityRender(result));
}
}).catch(error => {
console.error(error);
});
}
modPhone(e) {
let $this = $(e.currentTarget);
if (yoho && yoho.isApp) {
yoho.ready(() => {
if ($this.hasClass('canMod')) {
if (yoho.isYohoApp) {
yoho.invokeMethod('go.changebindphone');
}
} else {
let href = location.protocol + '//m.yohobuy.com/';
if (yoho.isYohoApp) {
yoho.goH5(href, JSON.stringify({
action: 'go.associatephone'
}));
}
}
});
}
}
}
$(() => {
new UserInfo();
});