Guide1Screen.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. console.log({
  244. id,
  245. name,
  246. amount,
  247. discountAmount,
  248. inventory,
  249. week,
  250. startTime,
  251. endTime,
  252. introduction,
  253. img,
  254. merchantId: mid,
  255. });
  256. httpPost(
  257. "/goods/save",
  258. {
  259. id,
  260. name,
  261. amount,
  262. discountAmount,
  263. inventory,
  264. week,
  265. startTime,
  266. endTime,
  267. introduction,
  268. img,
  269. merchantId: mid,
  270. },
  271. { body: "json" },
  272. true
  273. ).then((res) => {
  274. success(id ? "修改" : "添加" + "成功");
  275. if (id) {
  276. getInfo(res);
  277. } else if (routeName != "AddGoods") {
  278. changeGuideStep(1, "Guide2");
  279. } else {
  280. changeId(res.id);
  281. navigation.dispatch(
  282. CommonActions.setParams({
  283. id: res.id,
  284. })
  285. );
  286. }
  287. });
  288. }}
  289. >
  290. {routeName != "AddGoods" ? next : confirm}
  291. </Button>
  292. {routeName == "AddGoods" && id != 0 && (
  293. <Button
  294. appearance='ghost'
  295. status='info'
  296. style={{ marginTop: 10 }}
  297. onPress={() => {
  298. showDialog({
  299. bodyText: removeTips,
  300. cancelable: true,
  301. confirmCallback: () => {
  302. success("删除成功");
  303. navigation.goBack();
  304. },
  305. });
  306. }}
  307. >
  308. {delText}
  309. </Button>
  310. )}
  311. {routeName != "AddGoods" && (
  312. <Button
  313. style={{ marginTop: 10 }}
  314. appearance='ghost'
  315. status='primary'
  316. onPress={() => {
  317. showDialog({
  318. bodyText: passTips,
  319. cancelable: true,
  320. confirmCallback: () => {
  321. changeGuideStep(1, "Guide2");
  322. },
  323. });
  324. }}
  325. >
  326. {pass}
  327. </Button>
  328. )}
  329. </Layout>
  330. </Card>
  331. {routeName != "AddGoods" && <ConnectButton />}
  332. </Layout>
  333. </ScrollPage>
  334. </>
  335. );
  336. }
  337. const styles = StyleSheet.create({
  338. container: {
  339. flex: 1,
  340. paddingBottom: 33,
  341. },
  342. tabContent: {
  343. backgroundColor: "#fff",
  344. marginTop: 20,
  345. },
  346. img: {
  347. width: 100,
  348. height: 100,
  349. alignSelf: "center",
  350. },
  351. img2: {
  352. width: 97,
  353. height: 21,
  354. alignSelf: "center",
  355. marginTop: 2,
  356. },
  357. text: {
  358. marginTop: 16,
  359. },
  360. layoutLeft: {
  361. // flexDirection: "row",
  362. paddingVertical: 10,
  363. justifyContent: "center",
  364. alignItems: "center",
  365. },
  366. form: {
  367. paddingHorizontal: 26,
  368. paddingVertical: 20,
  369. },
  370. textareaContainer: {
  371. backgroundColor: "#F0F0F0",
  372. height: 100,
  373. borderRadius: 4,
  374. },
  375. textarea: {
  376. textAlignVertical: "top", // hack android
  377. fontSize: 13,
  378. color: "#333",
  379. paddingHorizontal: 14,
  380. paddingVertical: 10,
  381. height: 100,
  382. },
  383. });