Guide1Screen.js 13 KB

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