HomeScreen.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import * as WebBrowser from "expo-web-browser";
  2. import * as React from "react";
  3. import {
  4. Image,
  5. Platform,
  6. StyleSheet,
  7. Button,
  8. View,
  9. Picker,
  10. Alert,
  11. Text,
  12. } from "react-native";
  13. import { ScrollView } from "react-native-gesture-handler";
  14. import { MonoText } from "../components/StyledText";
  15. import { useFocusEffect } from "@react-navigation/native";
  16. import axios from "axios";
  17. export default function HomeScreen() {
  18. const [token, setToken] = React.useState("");
  19. const [goods, setGoods] = React.useState([]);
  20. const [cartId, setCartId] = React.useState(0);
  21. const [selectId, changeSelect] = React.useState(45);
  22. const [selectGoodsId, changeGoods] = React.useState(0);
  23. const [error, setError] = React.useState("");
  24. const [check, changeCheck] = React.useState(true);
  25. const [orderId, setOrderId] = React.useState(0);
  26. const axiosInstance = React.useMemo(() => {
  27. let axiosInstance = axios.create({
  28. baseURL: "http://dingdong.izouma.com",
  29. });
  30. axiosInstance.interceptors.request.use(
  31. config => {
  32. config.headers = config.headers || {};
  33. if (token) {
  34. config.headers["Authorization"] = "Bearer " + token;
  35. }
  36. return config;
  37. },
  38. error => {
  39. return Promise.reject(error);
  40. }
  41. );
  42. axiosInstance.interceptors.response.use(
  43. response => {
  44. setError("");
  45. return response.data;
  46. },
  47. error => {
  48. setError(error.response.data.error);
  49. return Promise.reject(error.response.data);
  50. }
  51. );
  52. return axiosInstance;
  53. }, [token]);
  54. const chooseGoods = React.useMemo(() => {
  55. if (selectGoodsId) {
  56. return goods.find(item => {
  57. return item.id == selectGoodsId;
  58. });
  59. } else {
  60. return {};
  61. }
  62. }, [selectGoodsId, goods]);
  63. useFocusEffect(
  64. React.useCallback(() => {
  65. var data = new FormData();
  66. data.append("username", "123");
  67. data.append("password", "123");
  68. axiosInstance
  69. .post("/auth/login", data, {
  70. withCredentials: true,
  71. })
  72. .then(res => {
  73. setToken(res);
  74. });
  75. }, [])
  76. );
  77. const GoodsInfos = () => {
  78. return goods.map(item => (
  79. <Picker.Item
  80. label={item.name + "-" + item.merchantId}
  81. value={item.id}
  82. key={item.id}
  83. />
  84. ));
  85. };
  86.  
  87. React.useEffect(() => {
  88. if (token) {
  89. console.log(token);
  90. axiosInstance
  91. .get("/goods/all?size=999&sort=id,desc")
  92. .then(res => {
  93. console.log(res.content);
  94. setGoods(res.content);
  95. });
  96. }
  97. }, [token, check]);
  98. return (
  99. <View style={styles.container}>
  100. {error != "" && <Text>错误提示:{error}</Text>}
  101. {goods.length > 0 && (
  102. <Picker
  103. selectedValue={selectGoodsId}
  104. style={{ height: 50, width: 150 }}
  105. onValueChange={(itemValue, itemIndex) =>
  106. changeGoods(itemValue)
  107. }
  108. >
  109. {GoodsInfos()}
  110. </Picker>
  111. )}
  112. {chooseGoods.status != "PASS" && selectGoodsId != 0 && (
  113. <Button
  114. title='审核通过'
  115. onPress={() => {
  116. axiosInstance
  117. .get(
  118. "/goods/audit?id=" +
  119. selectGoodsId +
  120. "&pass=true"
  121. )
  122. .then(res => {
  123. changeCheck(!check);
  124. });
  125. }}
  126. style={styles.btn}
  127. />
  128. )}
  129. {chooseGoods.id && (
  130. <Button
  131. title='加购'
  132. onPress={() => {
  133. var data = new FormData();
  134. data.append("goodsId", selectGoodsId);
  135. data.append("goodsSpecificationIds", "");
  136. data.append("num", 1);
  137. axiosInstance
  138. .post("/shoppingCart/cart", data, {
  139. withCredentials: true,
  140. })
  141. .then(res => {
  142. setCartId(res.id);
  143. });
  144. }}
  145. style={styles.btn}
  146. />
  147. )}
  148. {cartId != 0 && (
  149. <Button
  150. title='下单'
  151. onPress={() => {
  152. var data = new FormData();
  153. data.append("shoppingCartId", cartId);
  154. data.append("addressId", 134);
  155. axiosInstance
  156. .post("/orderInfo/order", data, {
  157. withCredentials: true,
  158. })
  159. .then(res => {
  160. setOrderId(res.id);
  161. });
  162. }}
  163. style={styles.btn}
  164. />
  165. )}
  166. {orderId != 0 && <Text>生成订单成功,订单id为:{orderId}</Text>}
  167. </View>
  168. );
  169. }
  170. HomeScreen.navigationOptions = {
  171. header: null,
  172. };
  173. const styles = StyleSheet.create({
  174. container: {
  175. flex: 1,
  176. backgroundColor: "#fafafa",
  177. alignItems: "center",
  178. justifyContent: "space-around",
  179. },
  180. btn: {
  181. marginHorizontal: 10,
  182. marginVertical: 10,
  183. },
  184. });