index.js
7.72 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
import Taro, { Component } from '@tarojs/taro';
import { View, Image } from '@tarojs/components';
import arrowRightIcon from '../../static/images/arrow-right-2.png';
import avatar from '../../static/images/avatar.png';
import yinVip from '../../static/images/yin-vip@2x.png';
import jinVip from '../../static/images/jin-vip@2x.png';
import baiJinVip from '../../static/images/baijin-vip@2x.png';
import { connect } from '@tarojs/redux';
import { bindActionCreators } from 'redux';
import * as globalData from '../../actions/globalData';
import user from '../../utils/user';
import { login as loginModel } from '../../models';
import './index.scss';
import event from '../../utils/event.js';
import { getImgUrl } from '../../utils/index.js'
@connect(({ globalData }) => ({
globalData
}), (dispatch) => {
return bindActionCreators({ ...globalData }, dispatch);
})
export default class UserCenter extends Component {
constructor(props) {
super(...arguments);
this.state = {
list: [
{
text: '购买'
},
{
text: '出售'
},
{
text: '收藏'
},
{
text: '我的资产'
},
{
text: '客服与帮助',
mt: true
}
],
hasUnionID: '',
isLogin: false,
userInfo: {},
uid: '',
infoNum: {}
};
}
config = {
navigationBarTitleText: '我的'
}
componentDidShow() {
let { globalData } = this.props;
console.log('进入个人中心页面')
console.log(globalData)
this.setState({
hasUnionID: globalData.wxUnionId !== null && globalData.wxUnionId !== '' && globalData.wxUnionId !== undefined ? true : false,
isLogin: globalData.userInfo.uid > 0 ? true : false
});
this.showUserInfo();
}
//测试接口,将获取到的unionID等参数,上报给服务端以便于调试
// checkUnionIDExist(openID, unionID, srdSession, method) {
// let param = {
// ops: method,
// openId: openID,
// unionId: unionID,
// srdSession: srdSession
// }
// GET(API_HOST + "/wechat/miniapp/collect/", param)
// .then(data => {
// callbackFunc({
// succeed: true,
// message: ''
// });
// })
// .catch(error => {
// callbackFunc({
// succeed: false,
// message: error.message
// });
// });
// }
getWechatThirdSession() {
let value = this.props.globalData.WXThird_session;
return !value ? user.getYHStorageSync('WXThird_session', 'app') : value;
}
/**
* 新的授权方式
*/
async getUserInfo(e) {
console.log('getUserInfo')
console.log(e)
console.log(this.props.globalData.WXThirdSession)
if (e.detail.errMsg === 'getUserInfo:ok') {
let response = await this.props.decodeUnionId(this.props.globalData.WXThirdSession, e);
console.log('getUserInfo decodeUnionId')
console.log(response)
if (response.isHaveUnionID) {
this.setState({
hasUnionID: true
});
}
} else {
let response = await this.props.loginAndRegisterTapped();
console.log('getUserInfo loginAndRegisterTapped')
console.log(response)
if (response && response.isHaveUnionID) {
this.setState({
hasUnionID: true
})
}
}
}
async getPhoneNumber(e) {
console.log('getPhoneNumber')
console.log(e)
if (e.detail.errMsg === 'getPhoneNumber:ok') {
let result = await this.props.decodePhoneNumber(e.detail.iv, e.detail.encryptedData, '');
console.log('====getPhoneNumber result', result)
result = result || {};
if (result.code != 200) {
Taro.showToast({
title: result.message,
duration: 1000,
success: async () => {
let ret = await this.props.loginAndRegisterTapped();
console.l0g('getPhoneNumber ret', ret)
if (ret && ret.succeed === true) {
this.showUserInfo();
this.getInfoNum();
}
}
});
} else {
if (result.is_register) {
cosnole.log('getPhoneNumber is_register')
console.log(this.props)
Taro.showToast({
title: '欢迎加入Yoho!Family!新人礼包已发放到个人中心-优惠券,请注意查收',
duration: 1500,
success() {
let uid = this.props.globalData.userInfo.uid > 0 ? this.props.globalData.userInfo.uid : 0;
this.props.setUserInfo({ ...this.props.globalData.userInfo, uid: uid });
this.setState({ uid: uid });
this.showUserInfo();
}
});
} else {
let uid = this.props.globalData.userInfo.uid > 0 ? this.props.globalData.userInfo.uid : 0;
this.props.setUserInfo({ ...this.props.globalData.userInfo, uid: uid });
this.setState({ uid: uid });
this.showUserInfo();
}
}
} else {
Taro.navigateTo({
url: '/pages/login/index',
});
}
}
showUserInfo() {
this.props.reloadSessionkey();
this.props.reloadUserInfo();
let uid = this.props.globalData.userInfo.uid;
if (uid) {
loginModel.profile({
uid: uid
}).then((ret) => {
console.log('showUserInfo ret', ret)
if (ret && ret.code === 200) {
let img = '';
let curLevel = ret.data.vip_info.cur_level;
if (curLevel === 1) {
img = yinVip;
} else if (curLevel === 2) {
img = jinVip;
} else if (curLevel === 3) {
img = baiJinVip;
}
this.setState({
userInfo: Object.assign(...this.props.globalData.userInfo, {
nickName: ret.data.nickname,
avatarUrl: !ret.data.head_ico ? avatar : ret.data.head_ico,
vipImg: img
}),
isLogin: true
});
} else {
this.setState({
isLogin: false
});
}
});
} else {
this.setState({
userInfo: this.props.globalData.userInfo,
isLogin: false
});
}
}
login() {
event.emit('user-is-login');
}
getInfoNum() {
// let myUid = this.props.globalData.userInfo.uid;
loginModel.getInfoNum().then(ret => {
if (ret && ret.code === 200) {
this.setData({
infoNum: ret.data
});
}
});
}
render() {
let { list, hasUnionID, isLogin, userInfo, vipImg } = this.state;
// console.log('用户信息')
// console.log(this.state.userInfo);
// console.log('islogin')
// console.log(this.state.isLogin)
// console.log('hasUnionID')
// console.log(this.state.userInfo)
return (
<View className= "user-center-page" >
<View className="header" >
<Image src={ isLogin ? getImgUrl(userInfo.avatarUrl) : avatar } className = "avatar-img" > </Image>
{
!isLogin &&
<Button
className="user-name"
onClick = { this.login } > 点击登录 < /Button>
}
{
isLogin &&
<View className="user-name" > { userInfo.nickName } < /View>
}
{
userInfo.vipImg &&
<Image className='vip-icon-image' src = { userInfo.vipImg } mode = "aspectFill" > </Image>
}
</View>
{
list.map(item => {
return (
<View className= { item.mt ? 'entry-item mt' : 'entry-item' } key = { item.text } >
<View className="label" > { item.text } < /View>
< View className = "num" > <Image src={ arrowRightIcon } className = "arrow-right-icon" > </Image></View >
</View>
)
})
}
</View>
)
}
}