home.js
3.69 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
import wx from '../../utils/wx';
import event from '../../common/event';
import homeModel from '../../models/home/index';
import accountModel from '../../models/account/index';
import {tapToLogin, getPhoneNumber} from '../../common/login';
//获取应用实例
let app = getApp();
Page({
data: {
productList: [],
userInfo: {},
isLogin: false
},
onLoad: function () {
event.on('user-login-success', this.showUserInfo);
event.on('wx-union-id-update', () => {
this.setData({
hasUnionID: !!app.globalData.unionID
});
});
if (app.getUid()) {
this.showUserInfo();
}
this.chooseForYouList();
},
backToTop: function () {
wx.pageScrollTo({
scrollTop: 0
})
},
chooseForYouList: function () {
homeModel.productList({
page: 1,
limit: 20
}).then(res => {
if (res.code === 200) {
const keyAdapter = {
skn: 'product_skn',
salePriceStr: 'sales_price',
productName: 'product_name',
defaultImages: 'default_images'
};
let list = [];
(res.data.product_list || []).forEach(product => {
let item = {};
Object.keys(keyAdapter).forEach(key => {
item[key] = product[keyAdapter[key]]
});
list.push(item);
});
this.setData({
productList: list
});
}
});
},
loginRegTap: function() {
tapToLogin()
.then(res => {
if (res.code === 10003) {
event.emit('user-login-success');
}
});
},
toOrdersTap: function (e) {
if (!this.data.isLogin) {
return wx.showToast({
title: '请先完成登录/注册,再查看!',
icon: 'none',
duration: 2000
});
}
let type = parseInt(e.currentTarget.dataset.type);
wx.navigateTo({
url: '/pages/home/order/order?type=' + type + '&page_name=' + 'userCenter' + '&page_param=' + ''
})
},
toAddress:function(){
if (!this.data.isLogin) {
return wx.showToast({
title: '请先完成登录/注册,再查看!',
icon: 'none',
duration: 2000
});
}
wx.navigateTo({
url: '/pages/home/address/list?currentMode=modeEdit' + '&page_name=' + 'userCenter' + '&page_param=' + ''
});
},
toService:function (){
wx.navigateTo({
url: '/pages/home/service/service',
});
},
showUserInfo: function() {
const defaultAvatar = '../../static/icons/default-avatar.png';
accountModel.getProfile().then(res => {
this.data.userInfo = app.globalData.userInfo;
this.data.userInfo.nickName = res.data.nickname;
this.data.userInfo.avatarUrl = res.data.head_ico || defaultAvatar;
wx.hideLoading();
this.setData({
isLogin: true,
userInfo: {
phoneNum: res.data.mobile,
nickName: res.data.nickname,
avatarUrl: res.data.head_ico || defaultAvatar,
defaultAvatar: this.data.userInfo.avatarUrl === defaultAvatar
}
});
}).catch(() => {
wx.hideLoading();
});
this.chooseForYouList();
}
});