文档中心 > 店铺动态卡片-开发指引

配置应用index样例

更新时间:2023/03/06 访问次数:3776

案例


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: ''
  };
  /**
   * 获得表单schema信息
   */
  getSchemaData = async () => {
    my.getSchemaData({
      success: (value: string[]) => {
        if (value) {
          console.log("schemaData---------", value);
          this.setState({
            schemaData: value,
            showMsg: value,
          });
        }
      },
    });
  };
  /**
   * 获得表单组件内的ModuleData
   */
  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,
          });
        }
      },
    });
  };
  /**
   * 获得表单组件内的globalData
   */
  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>
    );
  }
}


FAQ

关于此文档暂时还没有FAQ
返回
顶部