Guide1Screen.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. import * as WebBrowser from "expo-web-browser";
  2. import * as React from "react";
  3. import {
  4. Image,
  5. Platform,
  6. StyleSheet,
  7. TouchableOpacity,
  8. View,
  9. ImageBackground,
  10. } from "react-native";
  11. import scrollPage from "../decorator/scrollPage";
  12. import { useModel } from "flooks";
  13. import {
  14. Layout,
  15. Tab,
  16. TabView,
  17. Text,
  18. useTheme,
  19. Button,
  20. Card,
  21. } from "@ui-kitten/components";
  22. import FormInput from "../components/FormInput";
  23. import { useFocusEffect } from "@react-navigation/native";
  24. import ScrollPage from "../components/ScrollPage";
  25. import ConnectButton from "../components/ConnectButton";
  26. import GuideHeaderBar from "../components/GuideHeaderBar";
  27. import Textarea from "react-native-textarea";
  28. import NavHeaderBar from "../components/NavHeaderBar";
  29. import moment from "moment";
  30. import { CommonActions } from "@react-navigation/native";
  31. export default function Guide1Screen({ navigation, route }) {
  32. const theme = useTheme();
  33. const { changeBackground } = useModel("barModel");
  34. const { httpGet, httpPost } = useModel("httpModel", true);
  35. const { success } = useModel("loadingModel", true);
  36. const { mid, changeGuideStep } = useModel("userModel", true);
  37. const {
  38. guide1_title1,
  39. guideHome_title2,
  40. guide1_form_1,
  41. guide1_pla_1,
  42. guide1_form_2,
  43. guide1_pla_2,
  44. guide1_form_3,
  45. guide1_pla_3,
  46. guide1_form_4,
  47. guide1_pla_4,
  48. guide1_form_5,
  49. guide1_pla_5,
  50. next,
  51. pass,
  52. passTips,
  53. confirm,
  54. addGoods,
  55. delText,
  56. removeTips,
  57. } = useModel("wordsModel");
  58. const { showDialog } = useModel("dialogModel");
  59. const routeName = route.name;
  60. const [id, changeId] = React.useState("");
  61. const [name, changeName] = React.useState("");
  62. const [amount, changeAmount] = React.useState("");
  63. const [discountAmount, changeDiscountAmount] = React.useState("");
  64. const [inventory, changeInventory] = React.useState(0);
  65. const [week, changeWeek] = React.useState("");
  66. const [startTime, changeStartTime] = React.useState("08:00");
  67. const [endTime, changeEndTime] = React.useState("22:00");
  68. const [introduction, changeIntroduction] = React.useState();
  69. const [loadingSuccess, ChangeLoadingSuccess] = React.useState(false);
  70. const [defaultValue, ChangeDefaultValue] = React.useState("");
  71. const [img, changeImg] = React.useState();
  72. const timeValue = React.useMemo(() => {
  73. if (week && startTime && endTime) {
  74. return week + " " + startTime + "~" + endTime;
  75. } else {
  76. return " ";
  77. }
  78. }, [week, startTime, endTime]);
  79. useFocusEffect(
  80. React.useCallback(() => {
  81. changeBackground(theme["color-primary-500"]);
  82. console.log(route.params);
  83. if (route.params) {
  84. if (route.params.id) {
  85. changeId(route.params.id);
  86. }
  87. } else {
  88. ChangeLoadingSuccess(true);
  89. }
  90. }, [])
  91. );
  92. React.useEffect(() => {
  93. if (id) {
  94. httpGet("/goods/get/" + id).then((res) => {
  95. getInfo(res);
  96. ChangeLoadingSuccess(true);
  97. });
  98. } else {
  99. getInfo({});
  100. }
  101. }, [id]);
  102. function getInfo(res) {
  103. changeName(res.name || "");
  104. if (res.discountAmount) {
  105. changeDiscountAmount(res.discountAmount.toString());
  106. }
  107. if (res.amount) {
  108. changeAmount(res.amount.toString());
  109. }
  110. if (res.inventory) {
  111. changeInventory(res.inventory.toString());
  112. }
  113. changeWeek(res.week || "");
  114. changeStartTime(res.startTime || "");
  115. changeEndTime(res.endTime || "");
  116. changeIntroduction(res.introduction || "");
  117. console.log(res);
  118. // if (res.img && res.img.length) {
  119. changeImg(res.img);
  120. // }
  121. if (res.week) {
  122. ChangeDefaultValue([
  123. moment(res.startTime, "HH:mm:ss").format("HH:mm") +
  124. "~" +
  125. moment(res.endTime, "HH:mm:ss").format("HH:mm"),
  126. res.week,
  127. ]);
  128. }
  129. }
  130. const canNext = React.useMemo(() => {
  131. if (
  132. name &&
  133. amount &&
  134. discountAmount &&
  135. inventory &&
  136. week &&
  137. startTime &&
  138. endTime &&
  139. introduction &&
  140. img
  141. ) {
  142. return true;
  143. } else {
  144. return false;
  145. }
  146. }, [
  147. name,
  148. amount,
  149. discountAmount,
  150. inventory,
  151. week,
  152. startTime,
  153. endTime,
  154. introduction,
  155. img,
  156. ]);
  157. return (
  158. <>
  159. {routeName != "AddGoods" ? (
  160. <GuideHeaderBar />
  161. ) : (
  162. <NavHeaderBar title={addGoods} />
  163. )}
  164. <ScrollPage>
  165. <Layout style={styles.container}>
  166. {routeName != "AddGoods" && (
  167. <Card appearance='headFilled'>
  168. <Text category='s1'>{guide1_title1}</Text>
  169. <Text category='s1'>{guideHome_title2}</Text>
  170. </Card>
  171. )}
  172. <Card appearance='formFilled'>
  173. {/* 商品名称 */}
  174. <FormInput
  175. label={guide1_form_1}
  176. placeholder={guide1_pla_1}
  177. value={name}
  178. onChange={changeName}
  179. style={{ paddingVertical: 3 }}
  180. />
  181. {/* 商品价格 */}
  182. <FormInput
  183. label={guide1_form_2}
  184. placeholder={guide1_pla_2}
  185. value={amount}
  186. type='money'
  187. onChange={changeAmount}
  188. textAlign='right'
  189. />
  190. {/* 优惠价格 */}
  191. <FormInput
  192. label={guide1_form_3}
  193. placeholder={guide1_pla_3}
  194. value={discountAmount}
  195. type='money'
  196. onChange={changeDiscountAmount}
  197. textAlign='right'
  198. />
  199. {/* 每日供应数量 */}
  200. <FormInput
  201. label={guide1_form_4}
  202. placeholder={guide1_pla_4}
  203. value={inventory}
  204. type='amount'
  205. onChange={changeInventory}
  206. />
  207. {/* 供应时间 */}
  208. <FormInput
  209. label={guide1_form_5}
  210. type='openTime'
  211. defaultValue={defaultValue}
  212. onChange={(week, startTime, endTime) => {
  213. changeWeek(week);
  214. changeStartTime(startTime);
  215. changeEndTime(endTime);
  216. }}
  217. />
  218. <Layout>
  219. {loadingSuccess && (
  220. <Textarea
  221. containerStyle={styles.textareaContainer}
  222. style={styles.textarea}
  223. onChangeText={changeIntroduction}
  224. defaultValue={introduction}
  225. maxLength={50}
  226. placeholder={guide1_pla_5}
  227. placeholderTextColor={"#B4B4B4"}
  228. underlineColorAndroid={"transparent"}
  229. />
  230. )}
  231. </Layout>
  232. <FormInput
  233. type='img'
  234. value={img}
  235. textAlign='right'
  236. onChange={changeImg}
  237. />
  238. <Layout style={styles.layoutLeft} level='1'>
  239. <Button
  240. status='primary'
  241. disabled={!canNext}
  242. onPress={() => {
  243. httpPost(
  244. "/goods/save",
  245. {
  246. id,
  247. name,
  248. amount,
  249. discountAmount,
  250. inventory,
  251. week,
  252. startTime,
  253. endTime,
  254. introduction,
  255. img,
  256. merchantId: mid,
  257. },
  258. { body: "json" }
  259. ).then((res) => {
  260. success(id ? "修改" : "添加" + "成功");
  261. if (id) {
  262. getInfo(res);
  263. } else {
  264. changeId(res.id);
  265. navigation.dispatch(
  266. CommonActions.setParams({
  267. id: res.id,
  268. })
  269. );
  270. }
  271. });
  272. }}
  273. >
  274. {routeName != "AddGoods" ? next : confirm}
  275. </Button>
  276. {routeName == "AddGoods" && id != 0 && (
  277. <Button
  278. appearance='ghost'
  279. status='info'
  280. style={{ marginTop: 10 }}
  281. onPress={() => {
  282. showDialog({
  283. bodyText: removeTips,
  284. cancelable: true,
  285. confirmCallback: () => {
  286. success("删除成功");
  287. navigation.goBack();
  288. },
  289. });
  290. }}
  291. >
  292. {delText}
  293. </Button>
  294. )}
  295. {routeName != "AddGoods" && (
  296. <Button
  297. style={{ marginTop: 10 }}
  298. appearance='ghost'
  299. status='primary'
  300. onPress={() => {
  301. showDialog({
  302. bodyText: passTips,
  303. cancelable: true,
  304. confirmCallback: () => {
  305. changeGuideStep(1, "Guide2");
  306. },
  307. });
  308. }}
  309. >
  310. {pass}
  311. </Button>
  312. )}
  313. </Layout>
  314. </Card>
  315. {routeName != "AddGoods" && <ConnectButton />}
  316. </Layout>
  317. </ScrollPage>
  318. </>
  319. );
  320. }
  321. const styles = StyleSheet.create({
  322. container: {
  323. flex: 1,
  324. paddingBottom: 33,
  325. },
  326. tabContent: {
  327. backgroundColor: "#fff",
  328. marginTop: 20,
  329. },
  330. img: {
  331. width: 100,
  332. height: 100,
  333. alignSelf: "center",
  334. },
  335. img2: {
  336. width: 97,
  337. height: 21,
  338. alignSelf: "center",
  339. marginTop: 2,
  340. },
  341. text: {
  342. marginTop: 16,
  343. },
  344. layoutLeft: {
  345. // flexDirection: "row",
  346. paddingVertical: 10,
  347. justifyContent: "center",
  348. alignItems: "center",
  349. },
  350. form: {
  351. paddingHorizontal: 26,
  352. paddingVertical: 20,
  353. },
  354. textareaContainer: {
  355. backgroundColor: "#F0F0F0",
  356. height: 100,
  357. borderRadius: 4,
  358. },
  359. textarea: {
  360. textAlignVertical: "top", // hack android
  361. fontSize: 13,
  362. color: "#333",
  363. paddingHorizontal: 14,
  364. paddingVertical: 10,
  365. height: 100,
  366. },
  367. });