Commit 939e928a by wangshuo

公共方法提取

parent 533c5f55
...@@ -173,12 +173,8 @@ class Order extends Component { ...@@ -173,12 +173,8 @@ class Order extends Component {
info: !prevState.info info: !prevState.info
})); }));
}; };
componentDidMount() { // 公共方法 存储数据
let _this = this; publicGetData = (res) => {
console.log(this.props.location.state);
if(this.props.location.state && this.props.location.state.type === 1) {
http.get(`${API['base-api']}/m/order/preorder?type=1`).then((res) => {
if(res.data.errno === 200) {
const { course, total, user_account, user_info, discount } = res.data.data; const { course, total, user_account, user_info, discount } = res.data.data;
this.cacheObj = { this.cacheObj = {
perfect: user_info, perfect: user_info,
...@@ -194,6 +190,9 @@ class Order extends Component { ...@@ -194,6 +190,9 @@ class Order extends Component {
total, total,
discount, discount,
}); });
};
// 公共方法 本地存储
publicLocalStorage = () => {
if(this.props.history.action === 'PUSH') { if(this.props.history.action === 'PUSH') {
sessionStorage.removeItem('orderUseCacheObj'); sessionStorage.removeItem('orderUseCacheObj');
} else{ } else{
...@@ -205,40 +204,24 @@ class Order extends Component { ...@@ -205,40 +204,24 @@ class Order extends Component {
this.computedMoneyByCache(); this.computedMoneyByCache();
} }
} }
};
componentDidMount() {
let _this = this;
console.log(this.props.location.state);
if(this.props.location.state && this.props.location.state.type === 1) {
http.get(`${API['base-api']}/m/order/preorder?type=1`).then((res) => {
if(res.data.errno === 200) {
this.publicGetData(res);
this.publicLocalStorage();
}else{ }else{
Toast.info(res.data.msg, 2); Toast.info(res.data.msg, 2);
} }
}) })
} else if (this.props.location.state && this.props.location.state.simple === 1) { } else if (this.props.location.state && this.props.location.state.simple === 1) {
http.get(`${API.home}/m/order/preorder`).then(res => { http.get(`${API.home}/m/order/preorder`).then(res => {
console.log(res);
if(res.data.code === 200) { if(res.data.code === 200) {
const { course, total, user_account, user_info, discount } = res.data.data; this.publicGetData(res);
this.cacheObj = { this.publicLocalStorage();
perfect: user_info,
orderList: course,
user_account,
total,
discount,
};
this.setState({
perfect: user_info,
orderList: course,
user_account,
total,
discount,
});
if(this.props.history.action === 'PUSH') {
sessionStorage.removeItem('orderUseCacheObj');
} else{
const cacheObj = sessionStorage.getItem('orderUseCacheObj');
if(cacheObj !== null) {
this.setState({
useBalance: true
});
this.computedMoneyByCache();
}
}
}else{ }else{
Toast.info(res.data.msg, 2); Toast.info(res.data.msg, 2);
} }
...@@ -254,54 +237,17 @@ class Order extends Component { ...@@ -254,54 +237,17 @@ class Order extends Component {
return; return;
} }
const { user_info, user_account, discount } = preorder.data.data; this.publicGetData(preorder);
this.cacheObj = {
perfect: user_info,
orderList: orderList,
user_account,
total: preorder.data.data.total,
discount,
};
_this.setState({
perfect: user_info,
orderList: orderList,
user_account,
total: preorder.data.data.total,
discount,
});
}) })
}else{ }else{
http.get(`${API['base-api']}/m/order/preorder`).then((res) => { http.get(`${API['base-api']}/m/order/preorder`).then((res) => {
console.log(res.data);
if (res.data.errno !== 200) { if (res.data.errno !== 200) {
Toast.info(res.data.msg, 2);
return; return;
} }
const { course, total, user_account, user_info, discount } = res.data.data;
this.cacheObj = { this.publicGetData(res);
perfect: user_info, this.publicLocalStorage();
orderList: course,
user_account,
total,
discount,
};
this.setState({
perfect: user_info,
orderList: course,
user_account,
total,
discount,
});
if(this.props.history.action === 'PUSH') {
sessionStorage.removeItem('orderUseCacheObj');
} else{
const cacheObj = sessionStorage.getItem('orderUseCacheObj');
if(cacheObj !== null) {
this.setState({
useBalance: true
});
this.computedMoneyByCache();
}
}
}) })
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment