Guide1Screen.js 12 KB

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