import React from "react";
import { Button, Input } from "@alifd/next";
export default class App extends React.Component {
constructor(props: any) {
super(props);
this.getGlobalData = this.getGlobalData.bind(this);
this.handleLink = this.handleLink.bind(this);
}
componentDidMount(){
my.on('saveData',()=>{
console.log('外部保存按钮点击时')
this.saveData()
})
this.getModuleData((value?: any|string[])=>{
const { link, shopItem, shopItem2, coupon,miniappSelect } = value.webapp;
this.setState({ link, shopItem, shopItem2, coupon,miniappSelect });
})
}
state = {
globalData: "",
moduleData: {},
schemaData:"",
link: "",
shopItem:"",
shopItem2:"",
coupon:"",
image: "",
orderId:"",
miniappSelect: ''
};
getSchemaData = async () => {
my.getSchemaData({
success: (value: string[]) => {
if (value) {
console.log("schemaData---------", value);
this.setState({
schemaData: value,
showMsg: value,
});
}
},
});
};
getModuleData = async (callBlack?: { (): void; (arg0: string[]): any; }) => {
my.getModuleData({
success: (value: any) => {
if (value) {
console.log("moduleData---------", value);
callBlack && callBlack(value)
this.setState({
moduleData: value,
orderId:value.webapp.orderId,
showMsg: value,
});
}
},
});
};
getGlobalData = async () => {
my.getGlobalData({
success: (value: string[]) => {
if (value) {
console.log("globalData---------", value);
this.setState({
globalData: value,
showMsg: value,
});
}
},
});
};
handleLink = async () => {
my.handleLink({
success: (value: string) => {
if (value) {
console.log("link---------", value);
this.setState({
link: value,
});
}
},
});
};
handleItem = async () => {
my.handleItem({
success: (value: any[]) => {
console.log('shopItem',value)
if (Array.isArray(value) && value.length) {
this.setState({
shopItem: value[0].itemId,
});
}
},
});
};
handleItem2 = async () => {
my.handleItem({
success: (value: any[]) => {
console.log('shopItem2',value)
if (Array.isArray(value) && value.length) {
this.setState({
shopItem2: value[0].itemId,
});
}
},
});
};
handleCoupon = () => {
my.handleCoupon({
success: (value: any[]) => {
if (Array.isArray(value) && value.length) {
console.log("优惠卷信息---------", value);
this.setState({
coupon: value[0].activityId,
});
}
},
});
};
handlePicture = () => {
my.handlePicture({
success: (value: any[]) => {
if (Array.isArray(value) && value.length) {
console.log("图片信息---------", value);
this.setState({
image: value[0].url,
});
}
},
});
};
handleSelectMiniApp = () =>{
my.handleSelectMiniApp({
selectedMiniapp: this.state.miniappSelect,
success: (value:any) => {
console.log('选择小程序成功后', value)
this.setState({
miniappSelect: value.miniappSelect
})
},
});
};
saveData = ()=>{
const { link,coupon,shopItem,shopItem2,orderId,miniappSelect } = this.state
my.saveData({
data: {
webapp: { link,coupon,shopItem,shopItem2,orderId,miniappSelect }
}
})
}
render() {
const { link,shopItem,coupon,image,shopItem2,moduleData,globalData,schemaData, miniappSelect } = this.state;
console.log(JSON.stringify(this.state))
return (
<div className="container">
<div className="container-item">
<Button type="primary" className="ed-btn" onClick={this.handleLink}>
链接选择器
</Button>
<Input value={link} />
</div>
<div className="container-item">
<Button type="primary" className="ed-btn" onClick={this.handleItem}>
商品选择器
</Button>
<Input value={shopItem} />
</div>
<div className="container-item">
<Button type="primary" className="ed-btn" onClick={this.handleItem2}>
商品选择器2
</Button>
<Input value={shopItem2} />
</div>
<div className="container-item">
<Button type="primary" className="ed-btn" onClick={this.handleCoupon}>
优惠卷选择器
</Button>
<Input value={coupon} />
</div>
<div className="container-item">
<Button type="primary" className="ed-btn" onClick={this.handlePicture}>
图片选择器
</Button>
<Input value={image} />
</div>
<div className="container-item">
<Button type="primary" className="ed-btn" onClick={this.handleSelectMiniApp}>
选择小程序
</Button>
<Input value={miniappSelect} />
</div>
<div className="container-item">
<Button
type="primary"
className="ed-btn"
onClick={this.getGlobalData}
>
globalData
</Button>
{JSON.stringify(globalData)}
</div>
<div className="container-item">
<Button
type="primary"
className="ed-btn"
onClick={() => this.getModuleData()}
>
moduleData
</Button>
{JSON.stringify(moduleData)}
</div>
<div className="container-item">
<Button
type="primary"
className="ed-btn"
onClick={this.getSchemaData}
>
SchemaData
</Button>
{JSON.stringify(schemaData)}
</div>
<Button type="primary" onClick={this.saveData}>保存数据</Button>
</div>
);
}
}