user-service.js
25.4 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/**
* 个人中心 编辑资料models
* @author: gaohongwei<hongwei.gao@yoho.cn>
* @date: 2016/8/16
*/
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const api = global.yoho.API;
const _ = require('lodash');
const UserApi = require('./user-api');
const helpers = global.yoho.helpers;
const Images = require('../../../utils/images');
const configData = {
gender: [{
key: 'gender',
value: 1,
text: '男'
}, {
key: 'gender',
value: 2,
text: '女'
}],
income: [{
key: 'income',
value: 0,
text: '请选择'
}, {
key: 'income',
value: 1,
text: '1000以下'
}, {
key: 'income',
value: 2,
text: '1000-2999'
}, {
key: 'income',
value: 3,
text: '3000-5999'
}, {
key: 'income',
value: 4,
text: '6000-7999'
}, {
key: 'income',
value: 5,
text: '8000-10000'
}, {
key: 'income',
value: 6,
text: '10000以上'
}],
profession: [{
key: 'profession',
value: 1,
text: '已工作'
}, {
key: 'profession',
value: 2,
text: '大学生'
}, {
key: 'profession',
value: 3,
text: '中学生'
}, {
key: 'profession',
value: 4,
text: '其他'
}],
shoppingHabits: [{
type: 'radio',
key: 'shopping',
value: 1,
text: '目的型购物'
}, {
type: 'radio',
key: 'shopping',
value: 2,
text: '冲动型购物'
}, {
type: 'radio',
key: 'shopping',
value: 3,
text: '保守型购物'
}],
// 着装习惯
dressHabits: [{
type: 'checkbox',
key: 'dress',
index: 'dress-1',
value: 1,
text: '正装'
}, {
type: 'checkbox',
key: 'dress',
index: 'dress-2',
value: 2,
text: '商务'
}, {
type: 'checkbox',
key: 'dress',
index: 'dress-3',
value: 3,
text: '街头流行'
}, {
type: 'checkbox',
key: 'dress',
index: 'dress-4',
value: 4,
text: '运动休闲'
}, {
type: 'checkbox',
key: 'dress',
index: 'dress-5',
value: 5,
text: '文艺气质'
}, {
type: 'checkbox',
key: 'dress',
index: 'dress-6',
value: 6,
text: '甜美可爱'
}, {
type: 'checkbox',
key: 'dress',
index: 'dress-7',
value: 7,
text: '另类'
}],
headDefaultImgIcon: '//img11.static.yhbimg.com/yhb-img01/' +
'2016/07/05/13/017ec560b82c132ab2fdb22f7cf6f42b83.png?imageView/2/w/100/h/100'
};
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
getDays(year, month) {
let m = (month - 1) % 7 % 2 ? 30 : 31,
y400 = year % 400 ? 28 : 29,
y100 = year % 100 ? 29 : y400,
y4 = year % 4 ? 28 : y100;
return month === 2 ? y4 : m;
}
getBirthday(birthday) {
let that = this;
let year = parseInt(birthday.split('-')[0], 0),
month = parseInt(birthday.split('-')[1], 0),
day = parseInt(birthday.split('-')[2], 0),
nowYear = new Date().getFullYear(),
yearArr = [{value: 0, text: '年'}],
monthArr = [{value: 0, text: '月'}],
dayArr = [{value: 0, text: '日'}],
days = that.getDays(year, month);
for (let i = 1950; i <= nowYear; i++) {
let yearJson = {value: i, text: i};
if (i === year) {
yearJson.isChecked = true;
}
yearArr.push(yearJson);
}
for (let j = 1; j <= 12; j++) {
let monthJson = {value: j, text: j};
if (j === month) {
monthJson.isChecked = true;
}
monthArr.push(monthJson);
}
for (let k = 1; k <= days; k++) {
let dayJson = {value: k, text: k};
if (k === day) {
dayJson.isChecked = true;
}
dayArr.push(dayJson);
}
return {
yearArr: yearArr,
monthArr: monthArr,
dayArr: dayArr
};
}
formatDate(y, m, d) {
m = m < 10 ? ('0' + m) : m;
d = d < 10 ? ('0' + d) : d;
return y + '-' + m + '-' + d;
}
getHotBrands(brandList) {
let hotBrands = [],
i = 20;
if (brandList) {
for (let j = 0; j < brandList.length; j++) {
if (brandList[j].is_hot === 'Y') {
hotBrands.push({
id: 'pp-' + brandList[j].id,
name: 'pp[]',
value: brandList[j].id,
text: brandList[j].brand_name
});
i--;
}
if (i <= 0) {
break;
}
}
}
return hotBrands;
}
/**
* 联动取地区信息
*/
getProviceCityInfo(parentId, checkValue) {
let that = this;
return co(function*() {
let userDataModel = new UserApi(that.ctx);
let addressInfo = yield userDataModel.getProviceCityInfo(parentId),
res = [{value: 0}],
defaultText;
if (parentId.length === 2) {
defaultText = '请选择省份';
} else if (parentId.length === 4) {
defaultText = '请选择市';
} else {
defaultText = '请选择区县';
}
res[0].text = defaultText;
if (addressInfo.code === 200) {
for (let i = addressInfo.data.length - 1; i >= 0; i--) {
let addressId = String(addressInfo.data[i].id);
let addressJson = {
value: addressId,
text: addressInfo.data[i].caption
};
if (checkValue && addressId === checkValue) {
addressJson.isChecked = true;
}
res.push(addressJson);
}
}
return res;
})();
}
getUserInfo(uid) {
let that = this;
return co(function*() {
let userDataModel = new UserApi(that.ctx);
let result = yield api.all([
userDataModel.getUserInfo(uid),
userDataModel.getTipConfig(uid),
userDataModel.getUserContactInfo(uid),
userDataModel.getUserHabitsInfo(uid),
userDataModel.getUserLikeBrand(uid),
userDataModel.getFavorBrand(),
userDataModel.getCanRemoveBindOpenIdTag(uid)
// searchApi.get('/brand/list.json', {}, {cache: true})
]);
let finalResult = {},
userInfo = {},
canRemoveBind;
if (result[0].code === 200) {
let gender,
genderArr,
birthday,
birthdayJson,
yearArr,
monthArr,
dayArr,
profession,
professionArr,
income,
incomeArr;
userInfo = result[0].data || {};
userInfo.head_ico = userInfo.head_ico === '' ?
configData.headDefaultImgIcon : Images.getImageUrl(userInfo.head_ico, 100, 100);
if (userInfo) {
gender = userInfo.gender || 3;
birthday = userInfo.birthday || '';
profession = userInfo.profession || 0;
income = userInfo.income || 0;
}
genderArr = _.cloneDeep(configData.gender);
for (let value of genderArr) {
if (value.value === parseInt(gender, 0)) {
value.isChecked = true;
}
}
professionArr = _.cloneDeep(configData.profession);
for (let value of professionArr) {
if (value.value === parseInt(profession, 0)) {
value.isChecked = true;
}
}
incomeArr = _.cloneDeep(configData.income);
for (let value of incomeArr) {
if (value.value === parseInt(income, 0)) {
value.isChecked = true;
}
}
birthdayJson = that.getBirthday(birthday);
yearArr = birthdayJson.yearArr;
monthArr = birthdayJson.monthArr;
dayArr = birthdayJson.dayArr;
finalResult.userPersonalInfo = {
subTitle: '会员信息',
certified: parseInt(userInfo.vip_info.is_student, 10) === 1,
tipsUrl: parseInt(userInfo.vip_info.is_student, 10) === 1 ?
helpers.urlFormat('/list') : helpers.urlFormat('/product/students'),
firstBox: true,
submitId: 'base-info',
profileSrc: userInfo.head_ico,
email: {
labelText: '登录邮箱:',
value: userInfo.email || ''
},
name: [
{
labelText: '昵称:',
value: userInfo.nickname,
key: 'nickname',
tips: '与Yoho!业务或商家品牌冲突的昵称,Yoho!将有可能收回'
},
{
labelText: '真实姓名:',
value: userInfo.username,
key: 'username',
tips: '' // 4.8去掉该提示
}
],
gender: {
labelText: '性别:',
key: 'gender',
data: genderArr
},
birthday: {
labelText: '生日:',
key: 'birthday',
tips: '',
selects: [
{
key: 'year',
options: yearArr,
unit: '年'
},
{
key: 'month',
options: monthArr,
unit: '月'
},
{
key: 'day',
options: dayArr,
unit: '日'
}
]
},
profession: {
labelText: '职业:',
key: 'profession',
data: professionArr
},
income: {
labelText: '收入状况:',
key: 'income',
data: incomeArr
}
};
}
if (result[1].code === 200) {
finalResult.isShowTip = parseInt(result[1].data.config.sn, 10);
}
if (result[2].code === 200) {
let contactInfo = result[2].data || '',
areaCode = contactInfo.area_code || '',
areaCodeStr = String(areaCode);
let proviceOpts = [{value: 0, text: '请选择省份'}],
cityOpts = [{value: 0, text: '请选择市'}],
areaOpts = [{value: 0, text: '请选择区县'}];
if (areaCodeStr.length < 2) {
proviceOpts = yield that.getProviceCityInfo(0);
}
if (areaCodeStr.length >= 2) {
proviceOpts = yield that.getProviceCityInfo(0, areaCodeStr.substr(0, 2));
}
if (areaCodeStr.length >= 4) {
cityOpts = yield that.getProviceCityInfo(areaCodeStr.substr(0, 2), areaCodeStr.substr(0, 4));
}
if (areaCodeStr.length >= 6) {
areaOpts = yield that.getProviceCityInfo(areaCodeStr.substr(0, 4), areaCodeStr);
}
finalResult.contactInfo = {
subTitle: '联系信息',
submitId: 'contact-info',
region: {
labelText: '来自:',
selects: [
{
key: 'province',
options: proviceOpts
},
{
key: 'city',
options: cityOpts
},
{
key: 'areaCode',
options: areaOpts
}
]
},
details: [{
labelText: '固定电话:',
key: 'phone',
value: contactInfo.phone || '',
tips: '如: 010-82831245'
},
{
labelText: '手机号码:',
key: 'mobile',
value: contactInfo.mobile || '',
tips: '填写手机号便于接收发货和收货通知'
},
{
labelText: 'QQ:',
key: 'qq',
value: contactInfo.qq || '',
tips: '填写QQ方便您的好友联系你'
},
{
longInput: true,
labelText: '联系地址:',
key: 'fullAddress',
value: contactInfo.full_address || '',
tips: '请填写详细地址'
},
{
labelText: '邮编:',
key: 'zipCode',
value: contactInfo.zip_code || '',
tips: '请输入收货人所在地邮编号'
}]
};
}
if (result[3].code === 200) {
let habitsInfo = result[3].data || '',
dressHabitArr,
shoppingHabitArr,
dressArr = [];
shoppingHabitArr = _.cloneDeep(configData.shoppingHabits);
if (habitsInfo.shopping) {
for (let value of shoppingHabitArr) {
if (value.value === parseInt(habitsInfo.shopping, 0)) {
value.isChecked = true;
}
}
}
dressHabitArr = _.cloneDeep(configData.dressHabits);
if (habitsInfo.dress) {
dressArr = habitsInfo.dress.split(',');
for (let value of dressHabitArr) {
for (let i = 0; i < dressArr.length; i++) {
if (value.value === parseInt(dressArr[i], 0)) {
value.isChecked = true;
}
}
}
}
finalResult.habbit = {
subTitle: '购物&着装习惯',
submitId: 'shopping-info',
details: [
{
labelText: '购物习惯:',
data: shoppingHabitArr
},
{
labelText: '着装习惯:',
data: dressHabitArr
}
]
};
}
if (result[4].code === 200 && result[5].code === 200) {
let likeBrandStr = result[4].data.brand || '',
brandList = result[5].data,
favBrands = [];
let hotBrands = that.getHotBrands(brandList);
if (likeBrandStr !== '') {
let brandArr = likeBrandStr.split(',');
for (let i = 0; i < brandArr.length; i++) {
for (let j = 0; j < brandList.length; j++) {
if (String(brandList[j].id) === brandArr[i]) {
favBrands.push({
name: brandList[j].brand_name,
id: 'brand-' + brandList[j].id
});
}
}
}
}
finalResult.favorite = {
subTitle: '喜爱品牌',
submitId: 'favorite-brand',
likebrand: (likeBrandStr.substr(0, 1) === ',') ? likeBrandStr : ',' + likeBrandStr,
favoriteBrands: favBrands,
hotBrands: hotBrands
};
}
if (result[6] && result[6].data) {
canRemoveBind = result[6].data.isCanRemoveBindOpenId || false;
}
finalResult.bindInfo = {
canRemoveBind: canRemoveBind,
wechatBind: userInfo.wechat_bind === 'Y',
wechatNickname: userInfo.wechat_nickname,
wechatBindUrl: helpers.urlFormat('/home/bind/wechat'),
wechatCancelBindUrl: helpers.urlFormat('/home/cancelbind/wechat'),
qqBind: userInfo.qq_bind === 'Y',
qqNickname: userInfo.qq_nickname,
qqBindUrl: helpers.urlFormat('/home/bind/qq'),
qqCancelBindUrl: helpers.urlFormat('/home/cancelbind/qq'),
sinaBind: userInfo.sina_bind === 'Y',
sinaNickname: userInfo.sina_nickname,
sinaBindUrl: helpers.urlFormat('/home/bind/sina'),
sinaCancelBindUrl: helpers.urlFormat('/home/cancelbind/sina'),
alipayBind: userInfo.alipay_bind === 'Y',
alipayNickname: userInfo.alipay_nickname,
alipayBindUrl: helpers.urlFormat('/home/bind/alipay'),
alipayCancelBindUrl: helpers.urlFormat('/home/cancelbind/alipay'),
doubanBind: userInfo.douban_bind === 'Y',
doubanNickname: userInfo.douban_nickname,
doubanBindUrl: helpers.urlFormat('/home/bind/douban'),
doubanCancelBindUrl: helpers.urlFormat('/home/cancelbind/douban'),
renrenBind: userInfo.renren_bind === 'Y',
renrenNickname: userInfo.renren_nickname,
renrenBindUrl: helpers.urlFormat('/home/bind/renren'),
renrenCancelBindUrl: helpers.urlFormat('/home/cancelbind/renren')
};
finalResult.meEditPage = true;
return finalResult;
})();
}
editUserInfo(req, uid) {
let that = this;
let userDataModel = new UserApi(that.ctx);
let respData,
userInfo = {
uid: uid,
nickname: _.trim(req.body.nickname),
username: _.trim(req.body.username),
gender: req.body.gender || 3,
birthday: req.body.year === '' ? '' : that.formatDate(req.body.year, req.body.month, req.body.day),
profession: req.body.profession || 0,
income: req.body.income || 0
};
if (!userInfo.nickname || !userInfo.username || userInfo.birthday === '') {
respData = Promise.resolve({
code: 400,
message: '缺失必填项',
data: ''
});
} else {
respData = userDataModel.editUserInfo(userInfo);
}
return respData;
}
editUserContactInfo(req, uid) {
let that = this;
return co(function*() {
let userDataModel = new UserApi(that.ctx);
let contactInfo = {},
respData;
contactInfo.uid = uid;
contactInfo.areaCode = _.trim(req.body.areaCode);
contactInfo.phone = _.trim(req.body.phone);
contactInfo.mobile = _.trim(req.body.mobile);
contactInfo.qq = _.trim(req.body.qq);
contactInfo.fullAddress = _.trim(req.body.fullAddress);
contactInfo.zipCode = _.trim(req.body.zipCode);
if (!contactInfo.areaCode || !contactInfo.fullAddress ||
!contactInfo.zipCode || !contactInfo.phone || !contactInfo.mobile) {
respData = {
code: 400,
message: '缺失必填项',
data: ''
};
return respData;
}
respData = yield userDataModel.editUserContactInfo(contactInfo);
return respData;
})();
}
editUserHabitsInfo(req, uid) {
let that = this;
return co(function*() {
let habitsInfo = {},
dressArr = [],
respData;
let userDataModel = new UserApi(that.ctx);
habitsInfo.uid = uid;
habitsInfo.shopping = req.body.shopping;
if (req.body.dress) {
dressArr = req.body.dress;
habitsInfo.dress = dressArr instanceof Array ? dressArr.join(',') : dressArr;
} else {
habitsInfo.dress = '';
}
if (!habitsInfo.shopping) {
respData = {
code: 400,
message: '缺失必填项',
data: ''
};
return respData;
}
respData = yield userDataModel.editUserHabitsInfo(habitsInfo);
return respData;
})();
}
editUserLikeBrand(req, uid) {
let that = this;
return co(function*() {
let brand,
respData;
let userDataModel = new UserApi(that.ctx);
brand = req.body.brand;
if (!brand) {
respData = {
code: 400,
message: '缺失必填项',
data: ''
};
return respData;
}
respData = yield userDataModel.editUserLikeBrand(uid, brand);
return respData;
})();
}
isBrandName(req) {
let that = this;
return co(function*() {
let userDataModel = new UserApi(that.ctx);
let result = yield userDataModel.getFavorBrand();
let brandList = result.data,
brandName = _.trim(req.body.brandName),
brandInfo = {},
flag = false;
if (!brandName) {
return {
code: 400,
message: '缺失必填项!',
data: ''
};
}
if (brandList) {
for (let i = 0; i < brandList.length; i++) {
if (brandList[i].brand_name === brandName) {
flag = true;
brandInfo.brand_name = brandName;
brandInfo.id = brandList[i].id;
break;
}
}
}
if (flag) {
return {
code: 200,
message: '',
data: brandInfo
};
} else {
return {
code: 400,
message: '很抱歉,您要添加的品牌不在我们的品牌库中。',
data: ''
};
}
})();
}
getProviceList(pid) {
let that = this;
return co(function*() {
let userDataModel = new UserApi(that.ctx);
let reqData = {},
proList = yield userDataModel.getProviceCityInfo(pid),
opts = [];
reqData.code = proList.code;
if (proList.data) {
for (let i = 0; i < proList.data.length; i++) {
opts.push({
value: proList.data[i].id,
text: proList.data[i].caption,
is_support: proList.data[i].is_support_express
});
}
reqData.options = opts;
}
return reqData;
})();
}
bind3partyAccount(uid, param) {
let userDataModel = new UserApi(this.ctx);
return userDataModel.bind3partyAccount(uid, param);
}
cancelBind3partyAccount(uid, type) {
let userDataModel = new UserApi(this.ctx);
return userDataModel.cancelBind3partyAccount(uid, type);
}
};