index.js 2.46 KB
import React, { Component } from "react";
import {
  StyleSheet,
  WebView,
  View,
  Image,
  Dimensions,
  ScrollView,
  Text,
  Clipboard,
  TouchableOpacity
} from "react-native";
import { StackNavigator, TabNavigator } from "react-navigation";

export default class Discover extends Component {
  constructor(props) {
    super(props);
    this.state = {
      image: "",
      word: "",
    };
  }
  handleCopyPress(string) {
    Clipboard.setString(string);
  }
  componentWillMount() {
    this.getImages();
  }
  getImages() {
    let that = this,
      id = this.state.id,
      model = this.state.model;
    return fetch(`https://devpay.brae.co/test/insurance/wallpaper`, {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json"
      }
    })
      .then(resp => {
        console.log("大爷", resp);
        if (resp.status === 200) {
          return resp.json();
        } else {
          console.error("something went wrong!");
        }
      })
      .then(respJson => {
        if (respJson.enmsg != "ok") {
          alert(respJson.cnmsg);
        } else {
          that.setState({
            image: respJson.data.image,
            word: respJson.data.word
          });
          console.log("图片文章", respJson.data);
        }
      });
  }
  render() {
    const { image,word } = this.state;
    return (
      <View style={styles.container}>
        <Image
          style={styles.image}
          source={{ uri: image }}
          resizeMode="contain"
        >
          <Text selectable={true}  style={styles.word}>
            {word}
          </Text>
          <TouchableOpacity style={styles.button} onPress={() => {this.handleCopyPress(word)}}>
            <Text style={styles.buttonText}>复制文案</Text>
          </TouchableOpacity>
        </Image>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "white"
  },
  image: {
    width: Dimensions.get("window").width,
    height: Dimensions.get("window").height,
    justifyContent: "center",
    alignItems: "center"
  },
  word: {
    marginLeft: 15,
    marginRight: 15,
    fontSize: 15,
    lineHeight: 25,
    color: "white",
    backgroundColor: "rgba(0,0,0,0)"
  },
  button: {
    width: 80,
    height: 40,
    marginTop: 20,
    backgroundColor: "rgba(0,0,0,0.2)",
    justifyContent: "center",
    alignItems: "center",
    borderRadius: 5,
  },
  buttonText: {
    color: "white",
  }
});